Skip to content

Commit

Permalink
trying to fix lag when navigating to statistics detail
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Jul 7, 2024
1 parent 92c9e40 commit d708b3e
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.example.util.simpletimetracker.navigation.params.screen.RecordsFilter
import com.example.util.simpletimetracker.navigation.params.screen.RecordsFilterParams
import com.example.util.simpletimetracker.navigation.params.screen.RecordsFilterResultParams
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand All @@ -29,6 +30,7 @@ class StatisticsDetailFilterViewModelDelegate @Inject constructor(
private var records: List<RecordBase> = emptyList() // all records with selected ids
private var compareRecords: List<RecordBase> = emptyList() // all records with selected ids
private var loadJob: Job? = null
private var firstLoad: Boolean = true

override fun attach(parent: StatisticsDetailViewModelDelegate.Parent) {
this.parent = parent
Expand All @@ -37,6 +39,7 @@ class StatisticsDetailFilterViewModelDelegate @Inject constructor(
fun onVisible() {
loadJob?.cancel()
loadJob = delegateScope.launch {
delayLoadIfNeeded()
loadRecordsCache()
parent?.updateViewData()
}
Expand Down Expand Up @@ -122,6 +125,17 @@ class StatisticsDetailFilterViewModelDelegate @Inject constructor(
)
}

// Delay data load until screen transition finishes
// to avoid lagging while recycler is inflating views.
// Only done when no shared transitions, they delay onResume.
private suspend fun delayLoadIfNeeded() {
val extra = parent?.extra ?: return
if (extra.transitionName.isEmpty() && firstLoad) {
delay(300)
firstLoad = false
}
}

private suspend fun loadRecordsCache() {
// Load all records without date filter for faster date selection.
records = recordFilterInteractor.getByFilter(filter)
Expand Down

0 comments on commit d708b3e

Please sign in to comment.