Fix categories reordering

pull/488/head
Koitharu 3 years ago
parent c4ff37350c
commit ea34abb1d7
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -3,8 +3,11 @@ package org.koitharu.kotatsu.favourites.ui.categories
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.yield
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.model.FavouriteCategory import org.koitharu.kotatsu.core.model.FavouriteCategory
import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.AppSettings
@ -25,7 +28,7 @@ class FavouritesCategoriesViewModel @Inject constructor(
private val settings: AppSettings, private val settings: AppSettings,
) : BaseViewModel() { ) : BaseViewModel() {
private var reorderJob: Job? = null private var commitJob: Job? = null
val content = MutableStateFlow<List<ListModel>>(listOf(LoadingState)) val content = MutableStateFlow<List<ListModel>>(listOf(LoadingState))
@ -33,7 +36,7 @@ class FavouritesCategoriesViewModel @Inject constructor(
launchJob(Dispatchers.Default) { launchJob(Dispatchers.Default) {
repository.observeCategoriesWithCovers() repository.observeCategoriesWithCovers()
.collectLatest { .collectLatest {
reorderJob?.join() commitJob?.join()
updateContent(it) updateContent(it)
} }
} }
@ -52,17 +55,10 @@ class FavouritesCategoriesViewModel @Inject constructor(
fun isEmpty(): Boolean = content.value.none { it is CategoryListModel } fun isEmpty(): Boolean = content.value.none { it is CategoryListModel }
fun reorderCategories(oldPos: Int, newPos: Int) { fun reorderCategories(oldPos: Int, newPos: Int) {
val prevJob = reorderJob
reorderJob = launchJob(Dispatchers.Default) {
prevJob?.join()
val snapshot = content.requireValue().toMutableList() val snapshot = content.requireValue().toMutableList()
snapshot.move(oldPos, newPos) snapshot.move(oldPos, newPos)
content.value = snapshot content.value = snapshot
val ids = snapshot.mapNotNullTo(ArrayList(snapshot.size)) { commit(snapshot)
(it as? CategoryListModel)?.category?.id
}
repository.reorderCategories(ids)
}
} }
fun setIsVisible(ids: Set<Long>, isVisible: Boolean) { fun setIsVisible(ids: Set<Long>, isVisible: Boolean) {
@ -80,6 +76,19 @@ class FavouritesCategoriesViewModel @Inject constructor(
} }
} }
private fun commit(snapshot: List<ListModel>) {
val prevJob = commitJob
commitJob = launchJob {
prevJob?.cancelAndJoin()
delay(500)
val ids = snapshot.mapNotNullTo(ArrayList(snapshot.size)) {
(it as? CategoryListModel)?.category?.id
}
repository.reorderCategories(ids)
yield()
}
}
private fun updateContent(categories: Map<FavouriteCategory, List<Cover>>) { private fun updateContent(categories: Map<FavouriteCategory, List<Cover>>) {
content.value = categories.map { (category, covers) -> content.value = categories.map { (category, covers) ->
CategoryListModel( CategoryListModel(

@ -141,9 +141,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), AppBarOwner, BottomNav
viewModel.counters.observe(this, ::onCountersChanged) viewModel.counters.observe(this, ::onCountersChanged)
viewModel.appUpdate.observe(this, MenuInvalidator(this)) viewModel.appUpdate.observe(this, MenuInvalidator(this))
viewModel.onFirstStart.observeEvent(this) { viewModel.onFirstStart.observeEvent(this) {
OnboardDialogFragment.showWelcome( OnboardDialogFragment.show(supportFragmentManager)
supportFragmentManager
)
} }
searchSuggestionViewModel.isIncognitoModeEnabled.observe(this, this::onIncognitoModeChanged) searchSuggestionViewModel.isIncognitoModeEnabled.observe(this, this::onIncognitoModeChanged)
} }

Loading…
Cancel
Save