Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Optimize & Remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Mar 6, 2024
1 parent e10e4b6 commit af792fc
Show file tree
Hide file tree
Showing 27 changed files with 421 additions and 356 deletions.
146 changes: 146 additions & 0 deletions app/schemas/com.sanmer.geomag.database.AppDatabase/5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{
"formatVersion": 1,
"database": {
"version": 5,
"identityHash": "3b345455dad5236626f153423a11993d",
"entities": [
{
"tableName": "records",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`model` TEXT NOT NULL, `time` TEXT NOT NULL, `altitude` REAL NOT NULL, `latitude` REAL NOT NULL, `longitude` REAL NOT NULL, `x` REAL NOT NULL, `xDot` REAL NOT NULL, `y` REAL NOT NULL, `yDot` REAL NOT NULL, `z` REAL NOT NULL, `zDot` REAL NOT NULL, `h` REAL NOT NULL, `hDot` REAL NOT NULL, `f` REAL NOT NULL, `fDot` REAL NOT NULL, `d` REAL NOT NULL, `dDot` REAL NOT NULL, `i` REAL NOT NULL, `iDot` REAL NOT NULL, PRIMARY KEY(`model`, `time`, `altitude`, `latitude`, `longitude`))",
"fields": [
{
"fieldPath": "key.model",
"columnName": "model",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "key.time",
"columnName": "time",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "key.altitude",
"columnName": "altitude",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "key.latitude",
"columnName": "latitude",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "key.longitude",
"columnName": "longitude",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.x",
"columnName": "x",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.xDot",
"columnName": "xDot",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.y",
"columnName": "y",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.yDot",
"columnName": "yDot",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.z",
"columnName": "z",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.zDot",
"columnName": "zDot",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.h",
"columnName": "h",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.hDot",
"columnName": "hDot",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.f",
"columnName": "f",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.fDot",
"columnName": "fDot",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.d",
"columnName": "d",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.dDot",
"columnName": "dDot",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.i",
"columnName": "i",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.iDot",
"columnName": "iDot",
"affinity": "REAL",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"model",
"time",
"altitude",
"latitude",
"longitude"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '3b345455dad5236626f153423a11993d')"
]
}
}
47 changes: 18 additions & 29 deletions app/src/main/kotlin/com/sanmer/geomag/GeomagExt.kt
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
package com.sanmer.geomag

import com.sanmer.geomag.model.data.MagneticFieldExt
import com.sanmer.geomag.model.data.Position
import android.location.Location
import dev.sanmer.geomag.Geomag
import kotlinx.datetime.LocalDateTime

object GeomagExt {
fun toDecimalYears(date: LocalDateTime) = Geomag.toDecimalYears(date)

fun igrf(
position: Position,
decimalYears: Double
) = Geomag.igrf(
latitude = position.latitude,
longitude = position.longitude,
altitude = position.altitude,
decimalYears = decimalYears
).let { MagneticFieldExt(it) }

fun wmm(
position: Position,
decimalYears: Double
) = Geomag.wmm(
latitude = position.latitude,
longitude = position.longitude,
altitude = position.altitude,
decimalYears = decimalYears
).let { MagneticFieldExt(it) }

fun single(
model: Models,
position: Position,
location: Location,
decimalYears: Double
): MagneticFieldExt {
val func = when (model) {
Models.IGRF -> GeomagExt::igrf
Models.WMM -> GeomagExt::wmm
) = when (model) {
Models.IGRF -> {
Geomag.igrf(
longitude = location.longitude,
latitude = location.latitude,
altitude = location.altitude,
decimalYears = decimalYears
)
}
Models.WMM -> {
Geomag.wmm(
longitude = location.longitude,
latitude = location.latitude,
altitude = location.altitude,
decimalYears = decimalYears
)
}

return func(position, decimalYears)
}

enum class Models {
Expand Down
70 changes: 32 additions & 38 deletions app/src/main/kotlin/com/sanmer/geomag/database/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.room.migration.Migration
import com.sanmer.geomag.database.dao.RecordDao
import com.sanmer.geomag.database.entity.RecordEntity

@Database(entities = [RecordEntity::class], version = 4)
@Database(entities = [RecordEntity::class], version = 5)
abstract class AppDatabase : RoomDatabase() {
abstract fun recordDao(): RecordDao

Expand All @@ -19,13 +19,15 @@ abstract class AppDatabase : RoomDatabase() {
.addMigrations(
MIGRATION_1_2,
MIGRATION_2_3,
MIGRATION_3_4
MIGRATION_3_4,
MIGRATION_4_5
)
.build()
}

private val MIGRATION_1_2 = Migration(1, 2) {
it.execSQL("CREATE TABLE IF NOT EXISTS records_new (" +
it.execSQL("DROP TABLE records")
it.execSQL("CREATE TABLE IF NOT EXISTS records (" +
"id REAL NOT NULL, " +
"model TEXT NOT NULL, " +
"time TEXT NOT NULL, " +
Expand All @@ -47,27 +49,11 @@ abstract class AppDatabase : RoomDatabase() {
"totalIntensity REAL NOT NULL, " +
"totalSV REAL NOT NULL, " +
"PRIMARY KEY(id))")

it.execSQL("INSERT INTO records_new (" +
"id, model, time, altitude, latitude, longitude, " +
"declination, declinationSV, inclination, inclinationSV, " +
"horizontalIntensity, horizontalSV, northComponent, northSV, " +
"eastComponent, eastSV, verticalComponent, verticalSV, " +
"totalIntensity, totalSV) " +
"SELECT " +
"id, model, time, altitude, latitude, longitude, " +
"declination, declination_sv, inclination, inclination_sv, " +
"horizontal_intensity, horizontal_sv, north_component, north_sv, " +
"east_component, east_sv, vertical_component, vertical_sv, " +
"total_intensity, total_sv " +
"FROM records")

it.execSQL("DROP TABLE records")
it.execSQL("ALTER TABLE records_new RENAME TO records")
}

private val MIGRATION_2_3 = Migration(2, 3) {
it.execSQL("CREATE TABLE IF NOT EXISTS records_new (" +
it.execSQL("DROP TABLE records")
it.execSQL("CREATE TABLE IF NOT EXISTS records (" +
"model TEXT NOT NULL, " +
"time TEXT NOT NULL, " +
"altitude REAL NOT NULL, " +
Expand All @@ -88,23 +74,6 @@ abstract class AppDatabase : RoomDatabase() {
"totalIntensity REAL NOT NULL, " +
"totalSV REAL NOT NULL, " +
"PRIMARY KEY(model, time, altitude, latitude, longitude))")

it.execSQL("INSERT INTO records_new (" +
"model, time, altitude, latitude, longitude, " +
"declination, declinationSV, inclination, inclinationSV, " +
"horizontalIntensity, horizontalSV, northComponent, northSV, " +
"eastComponent, eastSV, verticalComponent, verticalSV, " +
"totalIntensity, totalSV) " +
"SELECT " +
"model, time, altitude, latitude, longitude, " +
"declination, declinationSV, inclination, inclinationSV, " +
"horizontalIntensity, horizontalSV, northComponent, northSV, " +
"eastComponent, eastSV, verticalComponent, verticalSV, " +
"totalIntensity, totalSV " +
"FROM records")

it.execSQL("DROP TABLE records")
it.execSQL("ALTER TABLE records_new RENAME TO records")
}

private val MIGRATION_3_4 = Migration(3, 4) {
Expand All @@ -131,5 +100,30 @@ abstract class AppDatabase : RoomDatabase() {
"totalSV REAL NOT NULL, " +
"PRIMARY KEY(model, time, altitude, latitude, longitude))")
}

private val MIGRATION_4_5 = Migration(4, 5) {
it.execSQL("DROP TABLE records")
it.execSQL("CREATE TABLE IF NOT EXISTS records (" +
"model TEXT NOT NULL, " +
"time TEXT NOT NULL, " +
"altitude REAL NOT NULL, " +
"latitude REAL NOT NULL, " +
"longitude REAL NOT NULL, " +
"x REAL NOT NULL, " +
"xDot REAL NOT NULL, " +
"y REAL NOT NULL, " +
"yDot REAL NOT NULL, " +
"z REAL NOT NULL, " +
"zDot REAL NOT NULL, " +
"h REAL NOT NULL, " +
"hDot REAL NOT NULL, " +
"f REAL NOT NULL, " +
"fDot REAL NOT NULL, " +
"d REAL NOT NULL, " +
"dDot REAL NOT NULL, " +
"i REAL NOT NULL, " +
"iDot REAL NOT NULL, " +
"PRIMARY KEY(model, time, altitude, latitude, longitude))")
}
}
}

This file was deleted.

Loading

0 comments on commit af792fc

Please sign in to comment.