fix: synchronous reorder for keywords and sites (v0.3.0)

This commit is contained in:
2026-08-21 00:27:17 +00:00
parent fb1e8b71d3
commit 6f69c39ff8
@@ -109,11 +109,13 @@ class MainViewModel @Inject constructor(
} }
fun reorderSites(fromIndex: Int, toIndex: Int) { fun reorderSites(fromIndex: Int, toIndex: Int) {
viewModelScope.launch { // siteOrder는 appSettings.siteOrder에서 stateIn으로 가져오므로
// appSettings를 즉시 저장하고 Flow가 업데이트되도록 함
val current = siteOrder.value.toMutableList() val current = siteOrder.value.toMutableList()
if (fromIndex in current.indices && toIndex in current.indices) { if (fromIndex in current.indices && toIndex in current.indices) {
val item = current.removeAt(fromIndex) val item = current.removeAt(fromIndex)
current.add(toIndex, item) current.add(toIndex, item)
viewModelScope.launch {
appSettings.setSiteOrder(current) appSettings.setSiteOrder(current)
} }
} }
@@ -128,7 +130,6 @@ class MainViewModel @Inject constructor(
isEnabled = true isEnabled = true
) )
) )
// 키워드 순서 맨 앞에 추가
val currentOrder = appSettings.keywordOrder.first().toMutableList() val currentOrder = appSettings.keywordOrder.first().toMutableList()
currentOrder.add(0, newId) currentOrder.add(0, newId)
appSettings.setKeywordOrder(currentOrder) appSettings.setKeywordOrder(currentOrder)
@@ -151,12 +152,16 @@ class MainViewModel @Inject constructor(
} }
fun reorderKeywords(fromIndex: Int, toIndex: Int) { fun reorderKeywords(fromIndex: Int, toIndex: Int) {
val state = uiState.value as? MainUiState.Success ?: return // uiState를 동기적으로 즉시 업데이트하여 빠른 드래그 시 stale 데이터 방지
viewModelScope.launch { val state = _uiState.value as? MainUiState.Success ?: return
val current = state.keywords.toMutableList() val current = state.keywords.toMutableList()
if (fromIndex in current.indices && toIndex in current.indices) { if (fromIndex in current.indices && toIndex in current.indices) {
val item = current.removeAt(fromIndex) val item = current.removeAt(fromIndex)
current.add(toIndex, item) current.add(toIndex, item)
// UI 즉시 갱신
_uiState.value = state.copy(keywords = current)
// DataStore 비동기 영속화
viewModelScope.launch {
appSettings.setKeywordOrder(current.map { it.id }) appSettings.setKeywordOrder(current.map { it.id })
} }
} }