[修复] 替换净化界面的闪退问题

This commit is contained in:
HapeLee committed 2025-12-18 01:33:56 +08:00
1 parent 766e3a5e8c
commit 6f3b8275d5
5 files changed
+43 -5

No files matched your search

@@ -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.constant.AppPattern import io.legado.app.constant.AppPattern
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
@@ -159,6 +160,13 @@ interface ReplaceRuleDao {
@Delete @Delete
fun delete(vararg replaceRules: ReplaceRule) fun delete(vararg replaceRules: ReplaceRule)
@Transaction
suspend fun updateAllOrders(rules: List<ReplaceRule>) {
rules.forEachIndexed { index, rule ->
updateOrder(rule.id, index + 1)
}
}
private fun dealGroups(list: List<String>): List<String> { private fun dealGroups(list: List<String>): List<String> {
val groups = linkedSetOf<String>() val groups = linkedSetOf<String>()
list.forEach { list.forEach {
@@ -3,11 +3,13 @@ package io.legado.app.data.repository
import android.text.TextUtils import android.text.TextUtils
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.ui.replace.ReplaceRuleItemUi
import io.legado.app.utils.splitNotBlank import io.legado.app.utils.splitNotBlank
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlin.collections.map
class ReplaceRuleRepository { class ReplaceRuleRepository {
@@ -170,4 +172,14 @@ class ReplaceRuleRepository {
appDb.replaceRuleDao.update(*updated.toTypedArray()) appDb.replaceRuleDao.update(*updated.toTypedArray())
} }
suspend fun moveOrder(currentRules: List<ReplaceRuleItemUi>) {
withContext(Dispatchers.IO) {
val updatedRules = currentRules.mapIndexed { index, itemUi ->
itemUi.rule.copy(order = index + 1)
}
appDb.replaceRuleDao.update(*updatedRules.toTypedArray())
}
}
} }
@@ -17,7 +17,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
@@ -51,7 +50,7 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application
private val repository = ReplaceRuleRepository() private val repository = ReplaceRuleRepository()
private val _sortMode = MutableStateFlow(context.getPrefString(PreferKey.replaceSortMode, "desc") ?: "desc") private val _sortMode = MutableStateFlow(context.getPrefString(PreferKey.replaceSortMode, "desc") ?: "desc")
private val _searchKey = MutableStateFlow<String?>(null) private val _searchKey = MutableStateFlow<String?>(null)
private val _uiRules = MutableStateFlow<List<ReplaceRuleItemUi>>(emptyList())
private val _selectedRuleIds = MutableStateFlow<Set<Long>>(emptySet()) private val _selectedRuleIds = MutableStateFlow<Set<Long>>(emptySet())
val selectedRuleIds: StateFlow<Set<Long>> = _selectedRuleIds val selectedRuleIds: StateFlow<Set<Long>> = _selectedRuleIds
@@ -97,14 +96,14 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application
when (it.order) { when (it.order) {
-1 -> Long.MIN_VALUE -1 -> Long.MIN_VALUE
-2 -> Long.MAX_VALUE -2 -> Long.MAX_VALUE
else -> 0 else -> 0L
} }
}.thenBy { it.name.lowercase() } }.thenBy { it.name.lowercase() }
"name_desc" -> compareBy<ReplaceRule> { "name_desc" -> compareBy<ReplaceRule> {
when (it.order) { when (it.order) {
-1 -> Long.MIN_VALUE -1 -> Long.MIN_VALUE
-2 -> Long.MAX_VALUE -2 -> Long.MAX_VALUE
else -> 0 else -> 0L
} }
}.thenByDescending { it.name.lowercase() } }.thenByDescending { it.name.lowercase() }
else -> null else -> null
@@ -249,4 +248,21 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application
repository.delGroup(group) repository.delGroup(group)
} }
} }
fun moveItemInList(fromIndex: Int, toIndex: Int) {
_uiRules.update { currentList ->
val list = currentList.toMutableList()
val item = list.removeAt(fromIndex)
list.add(toIndex, item)
list
}
}
fun saveSortOrder() {
val currentRules = _uiRules.value
execute {
repository.moveOrder(currentRules)
}
}
} }
@@ -20,6 +20,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:clickable="true" android:clickable="true"
android:focusable="true" android:focusable="true"
app:cardBackgroundColor="@android:color/transparent"
android:background="?attr/selectableItemBackground" android:background="?attr/selectableItemBackground"
app:cardCornerRadius="6dp" app:cardCornerRadius="6dp"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
@@ -34,7 +35,6 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:scaleType="centerCrop" android:scaleType="centerCrop"
android:src="?attr/colorSurfaceVariant"
android:transitionName="img_cover" android:transitionName="img_cover"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
@@ -7,6 +7,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="4dp" android:layout_marginBottom="4dp"
android:foreground="?attr/selectableItemBackground" android:foreground="?attr/selectableItemBackground"
app:cardBackgroundColor="@android:color/transparent"
android:clickable="true" android:clickable="true"
android:focusable="true" android:focusable="true"
app:strokeWidth="0dp" app:strokeWidth="0dp"
@@ -23,6 +24,7 @@
android:layout_height="match_parent" android:layout_height="match_parent"
app:cardCornerRadius="6dp" app:cardCornerRadius="6dp"
android:layout_marginHorizontal="8dp" android:layout_marginHorizontal="8dp"
app:cardBackgroundColor="@android:color/transparent"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:transitionName="img_cover" android:transitionName="img_cover"
app:strokeWidth="0dp"> app:strokeWidth="0dp">