mirror of
https://gitlab.com/ultrasonic/ultrasonic.git
synced 2025-04-26 21:52:16 +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
|
* This could be settings or data that are not specific to any remote music database
|
||||||
*/
|
*/
|
||||||
@Database(
|
@Database(
|
||||||
entities = [ServerSetting::class],
|
entities = [ServerSetting::class],
|
||||||
version = 4,
|
version = 5,
|
||||||
exportSchema = true
|
exportSchema = true
|
||||||
)
|
)
|
||||||
abstract class AppDatabase : RoomDatabase() {
|
abstract class AppDatabase : RoomDatabase() {
|
||||||
|
|
||||||
|
@ -19,7 +19,8 @@ import androidx.room.PrimaryKey
|
|||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
data class ServerSetting(
|
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 = "index") var index: Int,
|
||||||
@ColumnInfo(name = "name") var name: String,
|
@ColumnInfo(name = "name") var name: String,
|
||||||
@ColumnInfo(name = "url") var url: String,
|
@ColumnInfo(name = "url") var url: String,
|
||||||
@ -37,6 +38,6 @@ data class ServerSetting(
|
|||||||
@ColumnInfo(name = "podcastSupport") var podcastSupport: Boolean? = null
|
@ColumnInfo(name = "podcastSupport") var podcastSupport: Boolean? = null
|
||||||
) {
|
) {
|
||||||
constructor() : this (
|
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")
|
@Query("SELECT COUNT(*) FROM serverSetting")
|
||||||
fun liveServerCount(): LiveData<Int?>
|
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
|
* Retrieves the greatest value of the Index column in the table
|
||||||
*/
|
*/
|
||||||
|
@ -127,7 +127,6 @@ class ServerSettingsModel(
|
|||||||
|
|
||||||
appScope.launch {
|
appScope.launch {
|
||||||
serverSetting.index = (repository.count() ?: 0) + 1
|
serverSetting.index = (repository.count() ?: 0) + 1
|
||||||
serverSetting.id = (repository.getMaxId() ?: 0) + 1
|
|
||||||
repository.insert(serverSetting)
|
repository.insert(serverSetting)
|
||||||
Timber.d("saveNewItem saved server setting: $serverSetting")
|
Timber.d("saveNewItem saved server setting: $serverSetting")
|
||||||
}
|
}
|
||||||
@ -142,12 +141,11 @@ class ServerSettingsModel(
|
|||||||
|
|
||||||
runBlocking {
|
runBlocking {
|
||||||
demo.index = (repository.count() ?: 0) + 1
|
demo.index = (repository.count() ?: 0) + 1
|
||||||
demo.id = (repository.getMaxId() ?: 0) + 1
|
|
||||||
repository.insert(demo)
|
repository.insert(demo)
|
||||||
Timber.d("Added demo server")
|
Timber.d("Added demo server")
|
||||||
}
|
}
|
||||||
|
|
||||||
return demo.id
|
return demo.index
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user