mirror of
https://gitlab.com/ultrasonic/ultrasonic.git
synced 2025-04-24 21:00:55 +03:00
Don't manage ID manually, but use autoGenerate to ensure uniqueness
(across the lifetime of the db)
This commit is contained in:
parent
e77b5abd3e
commit
bfc11f9924
@ -10,9 +10,9 @@ import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
* This could be settings or data that are not specific to any remote music database
|
||||
*/
|
||||
@Database(
|
||||
entities = [ServerSetting::class],
|
||||
version = 4,
|
||||
exportSchema = true
|
||||
entities = [ServerSetting::class],
|
||||
version = 5,
|
||||
exportSchema = true
|
||||
)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
|
||||
|
@ -19,7 +19,8 @@ import androidx.room.PrimaryKey
|
||||
*/
|
||||
@Entity
|
||||
data class ServerSetting(
|
||||
@PrimaryKey var id: Int,
|
||||
// Default ID is 0, which will trigger SQLite to generate a unique ID.
|
||||
@PrimaryKey(autoGenerate = true) var id: Int = 0,
|
||||
@ColumnInfo(name = "index") var index: Int,
|
||||
@ColumnInfo(name = "name") var name: String,
|
||||
@ColumnInfo(name = "url") var url: String,
|
||||
@ -37,6 +38,6 @@ data class ServerSetting(
|
||||
@ColumnInfo(name = "podcastSupport") var podcastSupport: Boolean? = null
|
||||
) {
|
||||
constructor() : this (
|
||||
-1, 0, "", "", null, "", "", false, false, false, null, null
|
||||
0, 0, "", "", null, "", "", false, false, false, null, null
|
||||
)
|
||||
}
|
||||
|
@ -69,12 +69,6 @@ interface ServerSettingDao {
|
||||
@Query("SELECT COUNT(*) FROM serverSetting")
|
||||
fun liveServerCount(): LiveData<Int?>
|
||||
|
||||
/**
|
||||
* Retrieves the greatest value of the Id column in the table
|
||||
*/
|
||||
@Query("SELECT MAX([id]) FROM serverSetting")
|
||||
suspend fun getMaxId(): Int?
|
||||
|
||||
/**
|
||||
* Retrieves the greatest value of the Index column in the table
|
||||
*/
|
||||
|
@ -127,7 +127,6 @@ class ServerSettingsModel(
|
||||
|
||||
appScope.launch {
|
||||
serverSetting.index = (repository.count() ?: 0) + 1
|
||||
serverSetting.id = (repository.getMaxId() ?: 0) + 1
|
||||
repository.insert(serverSetting)
|
||||
Timber.d("saveNewItem saved server setting: $serverSetting")
|
||||
}
|
||||
@ -142,12 +141,11 @@ class ServerSettingsModel(
|
||||
|
||||
runBlocking {
|
||||
demo.index = (repository.count() ?: 0) + 1
|
||||
demo.id = (repository.getMaxId() ?: 0) + 1
|
||||
repository.insert(demo)
|
||||
Timber.d("Added demo server")
|
||||
}
|
||||
|
||||
return demo.id
|
||||
return demo.index
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user