fix: Fix a bug that caused timer settings to not save

#48
This commit is contained in:
Nishant Mishra
2025-09-27 22:30:40 +05:30
parent dd33c250b6
commit 748829f794

View File

@@ -22,7 +22,6 @@ import androidx.lifecycle.viewmodel.viewModelFactory
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.debounce
@@ -60,10 +59,9 @@ class SettingsViewModel(
val currentAlarmSound = timerRepository.alarmSoundUri.toString() val currentAlarmSound = timerRepository.alarmSoundUri.toString()
private val flowCollectionJob = SupervisorJob() private var focusFlowCollectionJob: Job? = null
private val focusFlowCollectionJob = Job(flowCollectionJob) private var shortBreakFlowCollectionJob: Job? = null
private val shortBreakFlowCollectionJob = Job(flowCollectionJob) private var longBreakFlowCollectionJob: Job? = null
private val longBreakFlowCollectionJob = Job(flowCollectionJob)
val alarmSound = val alarmSound =
preferenceRepository.getStringPreferenceFlow("alarm_sound").distinctUntilChanged() preferenceRepository.getStringPreferenceFlow("alarm_sound").distinctUntilChanged()
@@ -101,7 +99,7 @@ class SettingsViewModel(
} }
fun runTextFieldFlowCollection() { fun runTextFieldFlowCollection() {
viewModelScope.launch(focusFlowCollectionJob + Dispatchers.IO) { focusFlowCollectionJob = viewModelScope.launch(Dispatchers.IO) {
snapshotFlow { focusTimeTextFieldState.text } snapshotFlow { focusTimeTextFieldState.text }
.debounce(500) .debounce(500)
.collect { .collect {
@@ -114,7 +112,7 @@ class SettingsViewModel(
} }
} }
} }
viewModelScope.launch(shortBreakFlowCollectionJob + Dispatchers.IO) { shortBreakFlowCollectionJob = viewModelScope.launch(Dispatchers.IO) {
snapshotFlow { shortBreakTimeTextFieldState.text } snapshotFlow { shortBreakTimeTextFieldState.text }
.debounce(500) .debounce(500)
.collect { .collect {
@@ -127,7 +125,7 @@ class SettingsViewModel(
} }
} }
} }
viewModelScope.launch(longBreakFlowCollectionJob + Dispatchers.IO) { longBreakFlowCollectionJob = viewModelScope.launch(Dispatchers.IO) {
snapshotFlow { longBreakTimeTextFieldState.text } snapshotFlow { longBreakTimeTextFieldState.text }
.debounce(500) .debounce(500)
.collect { .collect {
@@ -142,7 +140,11 @@ class SettingsViewModel(
} }
} }
fun cancelTextFieldFlowCollection() = flowCollectionJob.cancel() fun cancelTextFieldFlowCollection() {
focusFlowCollectionJob?.cancel()
shortBreakFlowCollectionJob?.cancel()
longBreakFlowCollectionJob?.cancel()
}
fun saveAlarmEnabled(enabled: Boolean) { fun saveAlarmEnabled(enabled: Boolean) {
viewModelScope.launch { viewModelScope.launch {