From 543545d93dfcaeda47ebaa29bb114c1063d3ace5 Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Mon, 23 May 2022 19:36:06 +0200 Subject: [PATCH] Changed commited changes load block to avoid changing the state to loading when is not necessary --- .../app/extensions/StateManagementUtils.kt | 26 +++++++++++++++++++ .../app/viewmodels/CommitChangesViewModel.kt | 15 +++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/app/extensions/StateManagementUtils.kt diff --git a/src/main/kotlin/app/extensions/StateManagementUtils.kt b/src/main/kotlin/app/extensions/StateManagementUtils.kt new file mode 100644 index 0000000..93f477c --- /dev/null +++ b/src/main/kotlin/app/extensions/StateManagementUtils.kt @@ -0,0 +1,26 @@ +package app.extensions + +import kotlinx.coroutines.* + +/** + * Calls a code [onDelayTriggered] if [block] has not completed before the time specified in [delayMs]. + * Use case: Sometimes is not worth updating the UI with a state to "loading" if the load code executed afterwards is really + * fast. + */ +suspend fun delayedStateChange(delayMs: Long, onDelayTriggered: suspend () -> Unit, block: suspend () -> Unit) { + val scope = CoroutineScope(Dispatchers.IO) + var completed = false + + scope.launch { + delay(delayMs) + if(!completed) { + onDelayTriggered() + } + } + try { + block() + scope.cancel() + } finally { + completed = true + } +} \ No newline at end of file diff --git a/src/main/kotlin/app/viewmodels/CommitChangesViewModel.kt b/src/main/kotlin/app/viewmodels/CommitChangesViewModel.kt index 2e5da4b..d89ab89 100644 --- a/src/main/kotlin/app/viewmodels/CommitChangesViewModel.kt +++ b/src/main/kotlin/app/viewmodels/CommitChangesViewModel.kt @@ -1,5 +1,6 @@ package app.viewmodels +import app.extensions.delayedStateChange import app.git.DiffManager import app.git.RefreshType import app.git.TabState @@ -9,6 +10,8 @@ import org.eclipse.jgit.diff.DiffEntry import org.eclipse.jgit.revwalk.RevCommit import javax.inject.Inject +private const val MIN_TIME_IN_MS_TO_SHOW_LOAD = 300L + class CommitChangesViewModel @Inject constructor( private val tabState: TabState, private val diffManager: DiffManager, @@ -19,15 +22,17 @@ class CommitChangesViewModel @Inject constructor( fun loadChanges(commit: RevCommit) = tabState.runOperation( refreshType = RefreshType.NONE, ) { git -> - _commitChangesStatus.value = CommitChangesStatus.Loading + delayedStateChange( + delayMs = MIN_TIME_IN_MS_TO_SHOW_LOAD, + onDelayTriggered = { _commitChangesStatus.value = CommitChangesStatus.Loading } + ) { + val changes = diffManager.commitDiffEntries(git, commit) - val changes = diffManager.commitDiffEntries(git, commit) - - _commitChangesStatus.value = CommitChangesStatus.Loaded(commit, changes) + _commitChangesStatus.value = CommitChangesStatus.Loaded(commit, changes) + } } } - sealed class CommitChangesStatus { object Loading : CommitChangesStatus() data class Loaded(val commit: RevCommit, val changes: List) : CommitChangesStatus()