Changed commited changes load block to avoid changing the state to loading when is not necessary
This commit is contained in:
parent
46cb205739
commit
543545d93d
26
src/main/kotlin/app/extensions/StateManagementUtils.kt
Normal file
26
src/main/kotlin/app/extensions/StateManagementUtils.kt
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package app.viewmodels
|
package app.viewmodels
|
||||||
|
|
||||||
|
import app.extensions.delayedStateChange
|
||||||
import app.git.DiffManager
|
import app.git.DiffManager
|
||||||
import app.git.RefreshType
|
import app.git.RefreshType
|
||||||
import app.git.TabState
|
import app.git.TabState
|
||||||
@ -9,6 +10,8 @@ import org.eclipse.jgit.diff.DiffEntry
|
|||||||
import org.eclipse.jgit.revwalk.RevCommit
|
import org.eclipse.jgit.revwalk.RevCommit
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
private const val MIN_TIME_IN_MS_TO_SHOW_LOAD = 300L
|
||||||
|
|
||||||
class CommitChangesViewModel @Inject constructor(
|
class CommitChangesViewModel @Inject constructor(
|
||||||
private val tabState: TabState,
|
private val tabState: TabState,
|
||||||
private val diffManager: DiffManager,
|
private val diffManager: DiffManager,
|
||||||
@ -19,14 +22,16 @@ class CommitChangesViewModel @Inject constructor(
|
|||||||
fun loadChanges(commit: RevCommit) = tabState.runOperation(
|
fun loadChanges(commit: RevCommit) = tabState.runOperation(
|
||||||
refreshType = RefreshType.NONE,
|
refreshType = RefreshType.NONE,
|
||||||
) { git ->
|
) { 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 {
|
sealed class CommitChangesStatus {
|
||||||
object Loading : CommitChangesStatus()
|
object Loading : CommitChangesStatus()
|
||||||
|
Loading…
Reference in New Issue
Block a user