diff --git a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt index 4c5ea4f08..413db9f4f 100644 --- a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt @@ -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.constant.AppPattern import io.legado.app.data.entities.ReplaceRule @@ -159,6 +160,13 @@ interface ReplaceRuleDao { @Delete fun delete(vararg replaceRules: ReplaceRule) + @Transaction + suspend fun updateAllOrders(rules: List) { + rules.forEachIndexed { index, rule -> + updateOrder(rule.id, index + 1) + } + } + private fun dealGroups(list: List): List { val groups = linkedSetOf() list.forEach { diff --git a/app/src/main/java/io/legado/app/data/repository/ReplaceRuleRepository.kt b/app/src/main/java/io/legado/app/data/repository/ReplaceRuleRepository.kt index c621dd2f3..383e8355f 100644 --- a/app/src/main/java/io/legado/app/data/repository/ReplaceRuleRepository.kt +++ b/app/src/main/java/io/legado/app/data/repository/ReplaceRuleRepository.kt @@ -3,11 +3,13 @@ package io.legado.app.data.repository import android.text.TextUtils import io.legado.app.data.appDb import io.legado.app.data.entities.ReplaceRule +import io.legado.app.ui.replace.ReplaceRuleItemUi import io.legado.app.utils.splitNotBlank import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.withContext +import kotlin.collections.map class ReplaceRuleRepository { @@ -170,4 +172,14 @@ class ReplaceRuleRepository { appDb.replaceRuleDao.update(*updated.toTypedArray()) } + suspend fun moveOrder(currentRules: List) { + withContext(Dispatchers.IO) { + val updatedRules = currentRules.mapIndexed { index, itemUi -> + itemUi.rule.copy(order = index + 1) + } + + appDb.replaceRuleDao.update(*updatedRules.toTypedArray()) + } + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleViewModel.kt index d5e4f80f1..5d374d425 100644 --- a/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replace/ReplaceRuleViewModel.kt @@ -17,7 +17,6 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map @@ -51,7 +50,7 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application private val repository = ReplaceRuleRepository() private val _sortMode = MutableStateFlow(context.getPrefString(PreferKey.replaceSortMode, "desc") ?: "desc") private val _searchKey = MutableStateFlow(null) - + private val _uiRules = MutableStateFlow>(emptyList()) private val _selectedRuleIds = MutableStateFlow>(emptySet()) val selectedRuleIds: StateFlow> = _selectedRuleIds @@ -97,14 +96,14 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application when (it.order) { -1 -> Long.MIN_VALUE -2 -> Long.MAX_VALUE - else -> 0 + else -> 0L } }.thenBy { it.name.lowercase() } "name_desc" -> compareBy { when (it.order) { -1 -> Long.MIN_VALUE -2 -> Long.MAX_VALUE - else -> 0 + else -> 0L } }.thenByDescending { it.name.lowercase() } else -> null @@ -249,4 +248,21 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application 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) + } + } + } \ No newline at end of file diff --git a/app/src/main/res/layout/item_bookshelf_grid_compact_group.xml b/app/src/main/res/layout/item_bookshelf_grid_compact_group.xml index 678d3400d..aaf73da18 100644 --- a/app/src/main/res/layout/item_bookshelf_grid_compact_group.xml +++ b/app/src/main/res/layout/item_bookshelf_grid_compact_group.xml @@ -20,6 +20,7 @@ android:layout_height="wrap_content" android:clickable="true" android:focusable="true" + app:cardBackgroundColor="@android:color/transparent" android:background="?attr/selectableItemBackground" app:cardCornerRadius="6dp" app:layout_constraintTop_toTopOf="parent" @@ -34,7 +35,6 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" - android:src="?attr/colorSurfaceVariant" android:transitionName="img_cover" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" diff --git a/app/src/main/res/layout/item_bookshelf_grid_group.xml b/app/src/main/res/layout/item_bookshelf_grid_group.xml index d26a54877..45d9cb89d 100644 --- a/app/src/main/res/layout/item_bookshelf_grid_group.xml +++ b/app/src/main/res/layout/item_bookshelf_grid_group.xml @@ -7,6 +7,7 @@ android:layout_height="wrap_content" android:layout_marginBottom="4dp" android:foreground="?attr/selectableItemBackground" + app:cardBackgroundColor="@android:color/transparent" android:clickable="true" android:focusable="true" app:strokeWidth="0dp" @@ -23,6 +24,7 @@ android:layout_height="match_parent" app:cardCornerRadius="6dp" android:layout_marginHorizontal="8dp" + app:cardBackgroundColor="@android:color/transparent" android:layout_marginTop="8dp" android:transitionName="img_cover" app:strokeWidth="0dp">