diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index 351168c3e..834130b0e 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -1,6 +1,8 @@ package io.legado.app.data +import android.content.ContentValues import android.content.Context +import android.database.sqlite.SQLiteDatabase import androidx.room.AutoMigration import androidx.room.Database import androidx.room.Room @@ -9,6 +11,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase import io.legado.app.constant.AppConst import io.legado.app.data.dao.* import io.legado.app.data.entities.* +import io.legado.app.help.DefaultData import splitties.init.appCtx import java.util.* @@ -94,6 +97,23 @@ abstract class AppDatabase : RoomDatabase() { db.execSQL("update rssSources set loginUi = null where loginUi = 'null'") db.execSQL("update httpTTS set loginUi = null where loginUi = 'null'") db.execSQL("update httpTTS set concurrentRate = '0' where loginUi is null") + db.query("select * from keyboardAssists order by serialNo").use { + if (it.count == 0) { + DefaultData.keyboardAssists.forEach { keyboardAssist -> + val contentValues = ContentValues().apply { + put("type", keyboardAssist.type) + put("key", keyboardAssist.key) + put("value", keyboardAssist.value) + put("serialNo", keyboardAssist.serialNo) + } + db.insert( + "keyboardAssists", + SQLiteDatabase.CONFLICT_REPLACE, + contentValues + ) + } + } + } } } diff --git a/app/src/main/java/io/legado/app/data/dao/KeyboardAssistsDao.kt b/app/src/main/java/io/legado/app/data/dao/KeyboardAssistsDao.kt index 1292c219b..b25fec1a9 100644 --- a/app/src/main/java/io/legado/app/data/dao/KeyboardAssistsDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/KeyboardAssistsDao.kt @@ -2,25 +2,24 @@ package io.legado.app.data.dao import androidx.room.* import io.legado.app.data.entities.KeyboardAssist -import io.legado.app.help.DefaultData import kotlinx.coroutines.flow.Flow @Dao interface KeyboardAssistsDao { - @get:Query("select * from keyboardAssists") + @get:Query("select * from keyboardAssists order by serialNo") val all: List - @Query("select * from keyboardAssists where type = :type") + @Query("select * from keyboardAssists where type = :type order by serialNo") fun getByType(type: Int): List - @get:Query("select * from keyboardAssists") + @get:Query("select * from keyboardAssists order by serialNo") val flowAll: Flow> - @Query("select * from keyboardAssists where type = :type") + @Query("select * from keyboardAssists where type = :type order by serialNo") fun flowByType(type: Int): Flow> - @get:Query("select max(serialNo) from keyboardAssists") + @get:Query("select max(serialNo) from keyboardAssists order by serialNo") val maxSerialNo: Int @Insert(onConflict = OnConflictStrategy.REPLACE) @@ -32,11 +31,4 @@ interface KeyboardAssistsDao { @Delete fun delete(vararg keyboardAssist: KeyboardAssist) - fun getOrDefault(): List { - return all.ifEmpty { - insert(*DefaultData.keyboardAssists.toTypedArray()) - DefaultData.keyboardAssists - } - } - } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/KeyboardAssist.kt b/app/src/main/java/io/legado/app/data/entities/KeyboardAssist.kt index e3765e641..7cab22111 100644 --- a/app/src/main/java/io/legado/app/data/entities/KeyboardAssist.kt +++ b/app/src/main/java/io/legado/app/data/entities/KeyboardAssist.kt @@ -1,8 +1,12 @@ package io.legado.app.data.entities +import android.os.Parcelable import androidx.room.ColumnInfo import androidx.room.Entity +import kotlinx.parcelize.Parcelize + +@Parcelize @Entity(tableName = "keyboardAssists", primaryKeys = ["type", "key"]) data class KeyboardAssist( @ColumnInfo(defaultValue = "0") @@ -13,4 +17,4 @@ data class KeyboardAssist( var value: String, @ColumnInfo(defaultValue = "0") var serialNo: Int = 0 -) \ No newline at end of file +) : Parcelable \ No newline at end of file