Skip to content

Commit

Permalink
Fix issues with rapid button clicks
Browse files Browse the repository at this point in the history
  • Loading branch information
syt0r committed Jan 28, 2024
1 parent 23422b0 commit 3d92351
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ package ua.syt0r.kanji.presentation.screen.main.screen.home.screen.practice_dash

import androidx.compose.runtime.mutableStateOf
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import ua.syt0r.kanji.core.analytics.AnalyticsManager
import ua.syt0r.kanji.core.app_state.AppStateManager
import ua.syt0r.kanji.core.app_state.DailyGoalConfiguration
import ua.syt0r.kanji.core.logger.Logger
import ua.syt0r.kanji.core.user_data.UserPreferencesRepository
import ua.syt0r.kanji.presentation.screen.main.screen.home.screen.practice_dashboard.PracticeDashboardScreenContract.ScreenState
import kotlin.time.Duration.Companion.seconds


@OptIn(FlowPreview::class)
class PracticeDashboardViewModel(
private val viewModelScope: CoroutineScope,
loadDataUseCase: PracticeDashboardScreenContract.LoadDataUseCase,
Expand All @@ -29,9 +36,12 @@ class PracticeDashboardViewModel(
private var sortByTimeEnabled: Boolean = false
private lateinit var listMode: MutableStateFlow<PracticeDashboardListMode>

private val sortRequestsChannel = Channel<PracticeReorderRequestData>()

init {
loadDataUseCase.load()
.onEach {
Logger.d("applying new state")
sortByTimeEnabled = userPreferencesRepository.getDashboardSortByTime()
val sortedItems = applySortUseCase.sort(sortByTimeEnabled, it.items)
listMode = MutableStateFlow(PracticeDashboardListMode.Default(sortedItems))
Expand All @@ -41,6 +51,12 @@ class PracticeDashboardViewModel(
)
}
.launchIn(viewModelScope)

sortRequestsChannel.consumeAsFlow()
// To avoid infinite loading when rapidly clicking on apply sort button
.debounce(1.seconds)
.onEach { updateSortUseCase.update(it) }
.launchIn(viewModelScope)
}

override fun updateDailyGoal(configuration: DailyGoalConfiguration) {
Expand All @@ -66,6 +82,7 @@ class PracticeDashboardViewModel(
}

override fun merge(data: PracticeMergeRequestData) {
Logger.d("data[$data]")
state.value = ScreenState.Loading
viewModelScope.launch { mergePracticeSetsUseCase.merge(data) }
}
Expand All @@ -80,9 +97,10 @@ class PracticeDashboardViewModel(
}

override fun reorder(data: PracticeReorderRequestData) {
Logger.d("data[$data]")
state.value = ScreenState.Loading
sortByTimeEnabled = data.sortByTime
viewModelScope.launch { updateSortUseCase.update(data) }
viewModelScope.launch { sortRequestsChannel.send(data) }
}

override fun enableDefaultMode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ abstract class BaseCharacterReviewManager<HistoryStatus, CharacterDetails, Chara

@OptIn(ExperimentalCoroutinesApi::class)
private fun next() {
val characterData = queue.poll()
// Ignore handling if queue is empty, happens when user rapidly clicks on last button
val characterData = queue.poll() ?: return
addCharacterReviewDuration(characterData.character)

val summaryCharacterData = SummaryCharacterData(
Expand Down

0 comments on commit 3d92351

Please sign in to comment.