fix: 字典重命名通过事务删除旧主键并插入新记录
This commit is contained in:
@@ -5,6 +5,7 @@ import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.Transaction
|
||||
import androidx.room.Update
|
||||
import io.legado.app.data.entities.DictRule
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
@@ -45,4 +46,13 @@ interface DictRuleDao {
|
||||
@Query("DELETE FROM dictRules WHERE name IN (:names)")
|
||||
suspend fun deleteByIds(names: Set<String>)
|
||||
|
||||
@Query("DELETE FROM dictRules WHERE name = :name")
|
||||
fun deleteByName(name: String)
|
||||
|
||||
@Transaction
|
||||
fun replacePrimaryKey(oldName: String, rule: DictRule) {
|
||||
deleteByName(oldName)
|
||||
insert(rule)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -44,6 +44,12 @@ class DictRuleRepository {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun replacePrimaryKey(oldName: String, rule: DictRule) {
|
||||
withContext(Dispatchers.IO) {
|
||||
dao.replacePrimaryKey(oldName, rule)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun findById(id: String): DictRule? = withContext(Dispatchers.IO) {
|
||||
dao.getByName(id)
|
||||
}
|
||||
|
||||
@@ -47,7 +47,11 @@ sealed interface DictRuleIntent {
|
||||
data class ExportSelection(val uri: Uri) : DictRuleIntent
|
||||
data class MoveItem(val from: Int, val to: Int) : DictRuleIntent
|
||||
data object SaveSortOrder : DictRuleIntent
|
||||
data class SaveRule(val rule: DictRule, val isNew: Boolean) : DictRuleIntent
|
||||
data class SaveRule(
|
||||
val rule: DictRule,
|
||||
val isNew: Boolean,
|
||||
val originalName: String? = null,
|
||||
) : DictRuleIntent
|
||||
data class DeleteRule(val rule: DictRule) : DictRuleIntent
|
||||
data class SetRuleEnabled(val rule: DictRule, val enabled: Boolean) : DictRuleIntent
|
||||
data class CopyRule(val rule: DictRule) : DictRuleIntent
|
||||
|
||||
@@ -237,7 +237,13 @@ fun DictRuleScreen(
|
||||
editingRule = null
|
||||
},
|
||||
onSave = { updatedRule ->
|
||||
onIntent(DictRuleIntent.SaveRule(updatedRule, isNew = editingRule == null))
|
||||
onIntent(
|
||||
DictRuleIntent.SaveRule(
|
||||
rule = updatedRule,
|
||||
isNew = editingRule == null,
|
||||
originalName = editingRule?.name,
|
||||
)
|
||||
)
|
||||
showEditSheet = false
|
||||
editingRule = null
|
||||
},
|
||||
|
||||
@@ -70,6 +70,10 @@ class DictRuleViewModel(
|
||||
is DictRuleIntent.SaveRule -> {
|
||||
if (intent.isNew) {
|
||||
insert(intent.rule)
|
||||
} else if (intent.originalName != null &&
|
||||
intent.originalName != intent.rule.name
|
||||
) {
|
||||
replacePrimaryKey(intent.originalName, intent.rule)
|
||||
} else {
|
||||
update(intent.rule)
|
||||
}
|
||||
@@ -188,6 +192,8 @@ class DictRuleViewModel(
|
||||
|
||||
fun update(vararg rule: DictRule) = viewModelScope.launch { repository.update(*rule) }
|
||||
fun insert(vararg rule: DictRule) = viewModelScope.launch { repository.insert(*rule) }
|
||||
fun replacePrimaryKey(oldName: String, rule: DictRule) =
|
||||
viewModelScope.launch { repository.replacePrimaryKey(oldName, rule) }
|
||||
fun delete(vararg dictRule: DictRule) = viewModelScope.launch { repository.delete(*dictRule) }
|
||||
|
||||
fun copyRule(dictRule: DictRule) {
|
||||
|
||||
Reference in New Issue
Block a user