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