Skip to content

Commit 7fffc44

Browse files
D4vRAM369claude
andcommitted
fix(settings): resolve category drag-and-drop snapping back to original position
Three chained bugs caused dragged categories to snap back immediately on release, with no order persisted across screen reopens. Bug 1 — LaunchedEffect fired at the wrong moment (SettingsScreen.kt): isDragging was listed as a LaunchedEffect key, so releasing the drag triggered the effect immediately. At that point categories still held the stale order (DataStore hadn't responded yet), resetting orderedCategories to the old list and producing the visible snap-back on every drag release. Fix: remove isDragging from the key list; keep it only as an internal guard. Bug 2 — reorderFromIds() was a silent no-op (CategoryRepository.kt): The method body was empty — left as a stub. SettingsViewModel wrapped it in a db.withTransaction that wrote nothing, so DataStore was never updated and the custom order was lost on any recomposition. Fix: call preferences.setCategoryOrder(orderedIds) directly in updateCategoryOrder(), removing the pointless db transaction wrapper. Bug 3 — SettingsViewModel.categories ignored DataStore order (SettingsViewModel.kt): The categories StateFlow was built from observeAll() alone (Room ORDER BY nombre ASC), always returning alphabetical order regardless of any saved custom order. VaultViewModel already had the correct pattern but it was never replicated here. Fix: mirror VaultViewModel — combine(observeAll, categoryOrderFlow) and apply applyCategoryOrder() to respect the persisted DataStore order. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 8021fbb commit 7fffc44

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

app/src/main/java/com/d4vram/threadsvault/ui/settings/SettingsScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fun SettingsScreen(
123123
val scope = rememberCoroutineScope()
124124
var showHowToUse by remember { mutableStateOf(false) }
125125

126-
LaunchedEffect(categories, isDragging) {
126+
LaunchedEffect(categories) {
127127
if (!isDragging) {
128128
orderedCategories = categories
129129
}

app/src/main/java/com/d4vram/threadsvault/ui/settings/SettingsViewModel.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import kotlinx.coroutines.flow.MutableSharedFlow
2020
import kotlinx.coroutines.flow.SharingStarted
2121
import kotlinx.coroutines.flow.StateFlow
2222
import kotlinx.coroutines.flow.asSharedFlow
23+
import kotlinx.coroutines.flow.combine
2324
import kotlinx.coroutines.flow.first
2425
import kotlinx.coroutines.flow.stateIn
26+
import com.d4vram.threadsvault.utils.applyCategoryOrder
2527
import kotlinx.coroutines.launch
2628
import kotlinx.coroutines.withContext
2729
import kotlinx.coroutines.sync.Mutex
@@ -51,8 +53,12 @@ class SettingsViewModel(context: Context) : ViewModel() {
5153
val autoBackupIntervalHours: StateFlow<Int> = preferences.autoBackupIntervalHoursFlow
5254
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), 24)
5355

54-
val categories: StateFlow<List<CategoryEntity>> = categoryRepository.observeAll()
55-
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())
56+
val categories: StateFlow<List<CategoryEntity>> = combine(
57+
categoryRepository.observeAll(),
58+
preferences.categoryOrderFlow
59+
) { cats, orderedIds ->
60+
applyCategoryOrder(cats, orderedIds)
61+
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())
5662

5763
private val _saveDocumentEvents = MutableSharedFlow<SaveDocumentRequest>()
5864
val saveDocumentEvents = _saveDocumentEvents.asSharedFlow()
@@ -300,9 +306,7 @@ class SettingsViewModel(context: Context) : ViewModel() {
300306
fun updateCategoryOrder(orderedIds: List<Long>) {
301307
viewModelScope.launch {
302308
reorderMutex.withLock {
303-
db.withTransaction {
304-
categoryRepository.reorderFromIds(orderedIds)
305-
}
309+
preferences.setCategoryOrder(orderedIds)
306310
}
307311
}
308312
}

0 commit comments

Comments
 (0)