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

Commit

Permalink
Tidy up database
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Aug 13, 2023
1 parent b8e98fd commit 1d2a5cb
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 40 deletions.
148 changes: 148 additions & 0 deletions app/schemas/com.sanmer.geomag.database.AppDatabase/2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "52106f15f8adce755d04751dca2c035b",
"entities": [
{
"tableName": "records",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` REAL NOT NULL, `model` TEXT NOT NULL, `time` TEXT NOT NULL, `altitude` REAL NOT NULL, `latitude` REAL NOT NULL, `longitude` REAL NOT NULL, `declination` REAL NOT NULL, `declinationSV` REAL NOT NULL, `inclination` REAL NOT NULL, `inclinationSV` REAL NOT NULL, `horizontalIntensity` REAL NOT NULL, `horizontalSV` REAL NOT NULL, `northComponent` REAL NOT NULL, `northSV` REAL NOT NULL, `eastComponent` REAL NOT NULL, `eastSV` REAL NOT NULL, `verticalComponent` REAL NOT NULL, `verticalSV` REAL NOT NULL, `totalIntensity` REAL NOT NULL, `totalSV` REAL NOT NULL, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "model",
"columnName": "model",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "time",
"columnName": "time",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "altitude",
"columnName": "altitude",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "latitude",
"columnName": "latitude",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "longitude",
"columnName": "longitude",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.declination",
"columnName": "declination",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.declinationSV",
"columnName": "declinationSV",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.inclination",
"columnName": "inclination",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.inclinationSV",
"columnName": "inclinationSV",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.horizontalIntensity",
"columnName": "horizontalIntensity",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.horizontalSV",
"columnName": "horizontalSV",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.northComponent",
"columnName": "northComponent",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.northSV",
"columnName": "northSV",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.eastComponent",
"columnName": "eastComponent",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.eastSV",
"columnName": "eastSV",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.verticalComponent",
"columnName": "verticalComponent",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.verticalSV",
"columnName": "verticalSV",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.totalIntensity",
"columnName": "totalIntensity",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "values.totalSV",
"columnName": "totalSV",
"affinity": "REAL",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"id"
]
},
"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, '52106f15f8adce755d04751dca2c035b')"
]
}
}
58 changes: 57 additions & 1 deletion app/src/main/kotlin/com/sanmer/geomag/database/AppDatabase.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
package com.sanmer.geomag.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import com.sanmer.geomag.database.dao.RecordDao
import com.sanmer.geomag.database.entity.RecordEntity

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

companion object {
fun build(context: Context): AppDatabase {
return Room.databaseBuilder(context,
AppDatabase::class.java, "geomag")
.addMigrations(
MIGRATION_1_2
)
.build()
}

private val MIGRATION_1_2 = Migration(1, 2) {
it.execSQL("CREATE TABLE IF NOT EXISTS records_new (" +
"id REAL NOT NULL, " +
"model TEXT NOT NULL, " +
"time TEXT NOT NULL, " +
"altitude REAL NOT NULL, " +
"latitude REAL NOT NULL, " +
"longitude REAL NOT NULL, " +
"declination REAL NOT NULL, " +
"declinationSV REAL NOT NULL, " +
"inclination REAL NOT NULL, " +
"inclinationSV REAL NOT NULL, " +
"horizontalIntensity REAL NOT NULL, " +
"horizontalSV REAL NOT NULL, " +
"northComponent REAL NOT NULL, " +
"northSV REAL NOT NULL, " +
"eastComponent REAL NOT NULL, " +
"eastSV REAL NOT NULL, " +
"verticalComponent REAL NOT NULL, " +
"verticalSV REAL NOT NULL, " +
"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")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import kotlinx.coroutines.flow.Flow

@Dao
interface RecordDao {
@Query("SELECT * FROM records")
fun getAll(): List<RecordEntity>

@Query("SELECT * FROM records")
fun getAllAsFlow(): Flow<List<RecordEntity>>

@Query("SELECT * FROM records WHERE id LIKE :id LIMIT 1")
@Query("SELECT * FROM records WHERE id = :id LIMIT 1")
fun getById(id: Double): RecordEntity

@Insert(onConflict = OnConflictStrategy.REPLACE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.sanmer.geomag.database.di

import android.content.Context
import androidx.room.Room
import com.sanmer.geomag.database.AppDatabase
import com.sanmer.geomag.database.dao.RecordDao
import dagger.Module
Expand All @@ -20,11 +19,7 @@ object DatabaseModule {
@Singleton
fun providesAppDatabase(
@ApplicationContext context: Context
): AppDatabase {
return Room.databaseBuilder(context,
AppDatabase::class.java, "geomag")
.build()
}
): AppDatabase = AppDatabase.build(context)

@Provides
@Singleton
Expand Down
37 changes: 18 additions & 19 deletions app/src/main/kotlin/com/sanmer/geomag/database/entity/Record.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sanmer.geomag.database.entity

import androidx.room.ColumnInfo
import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.PrimaryKey
Expand All @@ -12,13 +11,13 @@ import kotlinx.datetime.toLocalDateTime

@Entity(tableName = "records")
data class RecordEntity(
@PrimaryKey val id: Double,
val model: String,
val time: String,
val altitude: Double,
val latitude: Double,
val longitude: Double,
@Embedded val values: MagneticFieldEntity,
@PrimaryKey val id: Double
@Embedded val values: MagneticFieldEntity
)

val Record.primaryKey: Double get() {
Expand All @@ -28,13 +27,13 @@ val Record.primaryKey: Double get() {
}

fun Record.toEntity() = RecordEntity(
id = primaryKey,
model = model.name,
time = time.toString(),
altitude = position.altitude,
latitude = position.latitude,
longitude = position.longitude,
values = values.toEntity(),
id = primaryKey
)

fun RecordEntity.toRecord() = Record(
Expand All @@ -48,22 +47,22 @@ fun RecordEntity.toRecord() = Record(
values = values.toMF()
)

@Entity(tableName = "magnetic_field")
@Entity(tableName = "values")
data class MagneticFieldEntity(
@ColumnInfo(name = "declination") val declination: Double,
@ColumnInfo(name = "declination_sv") val declinationSV: Double,
@ColumnInfo(name = "inclination") val inclination: Double,
@ColumnInfo(name = "inclination_sv") val inclinationSV: Double,
@ColumnInfo(name = "horizontal_intensity") val horizontalIntensity: Double,
@ColumnInfo(name = "horizontal_sv") val horizontalSV: Double,
@ColumnInfo(name = "north_component") val northComponent: Double,
@ColumnInfo(name = "north_sv") val northSV: Double,
@ColumnInfo(name = "east_component") val eastComponent: Double,
@ColumnInfo(name = "east_sv") val eastSV: Double,
@ColumnInfo(name = "vertical_component") val verticalComponent: Double,
@ColumnInfo(name = "vertical_sv") val verticalSV: Double,
@ColumnInfo(name = "total_intensity") val totalIntensity: Double,
@ColumnInfo(name = "total_sv") val totalSV: Double
val declination: Double,
val declinationSV: Double,
val inclination: Double,
val inclinationSV: Double,
val horizontalIntensity: Double,
val horizontalSV: Double,
val northComponent: Double,
val northSV: Double,
val eastComponent: Double,
val eastSV: Double,
val verticalComponent: Double,
val verticalSV: Double,
val totalIntensity: Double,
val totalSV: Double
)

fun MagneticFieldExt.toEntity() = MagneticFieldEntity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ class LocalRepository @Inject constructor(
list.map { it.toRecord() }
}

suspend fun getAll() = withContext(Dispatchers.IO) {
recordDao.getAll().map { it.toRecord() }
}

suspend fun getById(id: Double) = withContext(Dispatchers.IO) {
recordDao.getById(id).toRecord()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ class RecordViewModel @Inject constructor(

init {
Timber.d("RecordViewModel init: $primaryKey")
runCatching {
getRecord(primaryKey.toDouble())
}
}

private fun getRecord(id: Double) = viewModelScope.launch {
record = localRepository.getById(id)
viewModelScope.launch {
runCatching {
localRepository.getById(primaryKey.toDouble())
}.onSuccess {
record = it
}
}
}

fun share(context: Context) {
Expand Down

0 comments on commit 1d2a5cb

Please sign in to comment.