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