fix(stats): perform flow transformations on Dispatchers.IO

This commit is contained in:
Nishant Mishra
2025-12-06 20:44:33 +05:30
parent c0dc45d740
commit b8630ce639
2 changed files with 19 additions and 3 deletions

View File

@@ -39,6 +39,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
@@ -64,7 +65,9 @@ class SettingsViewModel(
val backStack = mutableStateListOf<Screen.Settings>(Screen.Settings.Main) val backStack = mutableStateListOf<Screen.Settings>(Screen.Settings.Main)
val isPlus = billingManager.isPlus val isPlus = billingManager.isPlus
val serviceRunning = stateRepository.timerState.map { it.serviceRunning } val serviceRunning = stateRepository.timerState
.map { it.serviceRunning }
.flowOn(Dispatchers.IO)
.stateIn( .stateIn(
viewModelScope, viewModelScope,
SharingStarted.WhileSubscribed(5000), SharingStarted.WhileSubscribed(5000),

View File

@@ -27,9 +27,10 @@ import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer
import com.patrykandpatrick.vico.core.cartesian.data.columnSeries import com.patrykandpatrick.vico.core.cartesian.data.columnSeries
import com.patrykandpatrick.vico.core.cartesian.data.lineSeries import com.patrykandpatrick.vico.core.cartesian.data.lineSeries
import com.patrykandpatrick.vico.core.common.data.ExtraStore import com.patrykandpatrick.vico.core.common.data.ExtraStore
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -46,7 +47,13 @@ class StatsViewModel(
private val statRepository: StatRepository private val statRepository: StatRepository
) : ViewModel() { ) : ViewModel() {
val todayStat = statRepository.getTodayStat().distinctUntilChanged() val todayStat = statRepository
.getTodayStat()
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000),
initialValue = null
)
private val lastWeekSummary = private val lastWeekSummary =
Pair(CartesianChartModelProducer(), ExtraStore.Key<List<String>>()) Pair(CartesianChartModelProducer(), ExtraStore.Key<List<String>>())
@@ -75,6 +82,7 @@ class StatsViewModel(
} }
lastWeekSummary lastWeekSummary
} }
.flowOn(Dispatchers.IO)
.stateIn( .stateIn(
scope = viewModelScope, scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000), started = SharingStarted.WhileSubscribed(5000),
@@ -91,6 +99,7 @@ class StatsViewModel(
it?.focusTimeQ4?.toInt() ?: 0 it?.focusTimeQ4?.toInt() ?: 0
) )
} }
.flowOn(Dispatchers.IO)
.stateIn( .stateIn(
scope = viewModelScope, scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000), started = SharingStarted.WhileSubscribed(5000),
@@ -109,6 +118,7 @@ class StatsViewModel(
} }
lastMonthSummary lastMonthSummary
} }
.flowOn(Dispatchers.IO)
.stateIn( .stateIn(
scope = viewModelScope, scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000), started = SharingStarted.WhileSubscribed(5000),
@@ -125,6 +135,7 @@ class StatsViewModel(
it?.focusTimeQ4?.toInt() ?: 0 it?.focusTimeQ4?.toInt() ?: 0
) )
} }
.flowOn(Dispatchers.IO)
.stateIn( .stateIn(
scope = viewModelScope, scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000), started = SharingStarted.WhileSubscribed(5000),
@@ -143,6 +154,7 @@ class StatsViewModel(
} }
lastYearSummary lastYearSummary
} }
.flowOn(Dispatchers.IO)
.stateIn( .stateIn(
scope = viewModelScope, scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000), started = SharingStarted.WhileSubscribed(5000),
@@ -159,6 +171,7 @@ class StatsViewModel(
it?.focusTimeQ4?.toInt() ?: 0 it?.focusTimeQ4?.toInt() ?: 0
) )
} }
.flowOn(Dispatchers.IO)
.stateIn( .stateIn(
scope = viewModelScope, scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000), started = SharingStarted.WhileSubscribed(5000),