fix(timer): significantly reduce recomposition count (by ~90%)

This improves the performance and perceived smoothness of the timer screen by a lot
This commit is contained in:
Nishant Mishra
2025-11-05 08:51:58 +05:30
parent 41e8618d14
commit ee5bf647c6
2 changed files with 10 additions and 4 deletions

View File

@@ -45,7 +45,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
@@ -79,9 +78,7 @@ fun AppScreen(
val context = LocalContext.current
val uiState by timerViewModel.timerState.collectAsStateWithLifecycle()
val remainingTime by timerViewModel.time.collectAsStateWithLifecycle()
val progress by rememberUpdatedState((uiState.totalTime.toFloat() - remainingTime) / uiState.totalTime)
val progress by timerViewModel.progress.collectAsStateWithLifecycle()
val layoutDirection = LocalLayoutDirection.current
val motionScheme = motionScheme

View File

@@ -30,8 +30,11 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import org.nsh07.pomodoro.TomatoApplication
@@ -55,6 +58,12 @@ class TimerViewModel(
val timerState: StateFlow<TimerState> = _timerState.asStateFlow()
val time: StateFlow<Long> = _time.asStateFlow()
val progress = _time.map {
val uiState = timerState.value
(uiState.totalTime.toFloat() - it) / uiState.totalTime
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), 0f)
private var cycles = 0
private var startTime = 0L