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:
@@ -45,7 +45,6 @@ import androidx.compose.runtime.Composable
|
|||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.rememberUpdatedState
|
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
@@ -79,9 +78,7 @@ fun AppScreen(
|
|||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
|
||||||
val uiState by timerViewModel.timerState.collectAsStateWithLifecycle()
|
val uiState by timerViewModel.timerState.collectAsStateWithLifecycle()
|
||||||
val remainingTime by timerViewModel.time.collectAsStateWithLifecycle()
|
val progress by timerViewModel.progress.collectAsStateWithLifecycle()
|
||||||
|
|
||||||
val progress by rememberUpdatedState((uiState.totalTime.toFloat() - remainingTime) / uiState.totalTime)
|
|
||||||
|
|
||||||
val layoutDirection = LocalLayoutDirection.current
|
val layoutDirection = LocalLayoutDirection.current
|
||||||
val motionScheme = motionScheme
|
val motionScheme = motionScheme
|
||||||
|
|||||||
@@ -30,8 +30,11 @@ import kotlinx.coroutines.Dispatchers
|
|||||||
import kotlinx.coroutines.FlowPreview
|
import kotlinx.coroutines.FlowPreview
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.nsh07.pomodoro.TomatoApplication
|
import org.nsh07.pomodoro.TomatoApplication
|
||||||
@@ -55,6 +58,12 @@ class TimerViewModel(
|
|||||||
val timerState: StateFlow<TimerState> = _timerState.asStateFlow()
|
val timerState: StateFlow<TimerState> = _timerState.asStateFlow()
|
||||||
|
|
||||||
val time: StateFlow<Long> = _time.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 cycles = 0
|
||||||
|
|
||||||
private var startTime = 0L
|
private var startTime = 0L
|
||||||
|
|||||||
Reference in New Issue
Block a user