Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface TimerRepo {
- val sharedTimerState: StateFlow<TimerGlobalState>
- suspend fun updateState(state: TimerGlobalState)
- suspend fun addOneMinute()
- suspend fun restartTimer()
- suspend fun activateTimer()
- suspend fun stopTimer(state: TimerState)
- fun resetTimer()
- }
- class TimerRepoImpl(
- private val coroutineScope: CloseableCoroutineScope,
- private val prefs: PreferencesManager,
- private val toaster: ToasterManager
- ) : TimerRepo {
- private var isInitialized = false
- private var timerJob: Job? = null
- private val _timerState = MutableStateFlow(
- TimerGlobalState()
- )
- override val sharedTimerState: StateFlow<TimerGlobalState> =
- _timerState.asStateFlow()
- init {
- coroutineScope.launch {
- setupRepo()
- }
- }
- private suspend fun setupRepo() {
- if (!isInitialized) {
- try {
- _timerState.update {
- Json.decodeFromString<TimerGlobalState>(
- prefs.readData("TIMER_REPO_KEY", "{}")
- ).copy(
- state = TimerState.Stale
- )
- }
- } catch (e: Exception) {
- KLogger.e(
- "Failed to load or parse timer state, using default.",
- e,
- tag = "TimerRepoImpl"
- )
- _timerState.update { TimerGlobalState() }
- try {
- prefs.deleteKey("TIMER_REPO_KEY")
- } catch (delEx: Exception) {
- KLogger.e("Failed to delete corrupted key: TIMER_REPO_KEY", delEx)
- }
- }
- isInitialized = true
- }
- }
- override suspend fun updateState(state: TimerGlobalState) {
- _timerState.update { state }
- persistTimer()
- toaster.showShort("Timer saved")
- }
- override suspend fun addOneMinute() = when (_timerState.value.state) {
- TimerState.Stale ->
- toaster.showShort("Timer is zero.")
- TimerState.Paused -> {
- _timerState.update {
- it.copy(
- time = _timerState.value.time + 60L
- )
- }
- persistTimer()
- }
- else -> Unit
- }
- override suspend fun restartTimer() {
- _timerState.update {
- it.copy(
- state = TimerState.Stale,
- time = _timerState.value.totalTime
- )
- }
- persistTimer()
- toaster.showShort("Timer Replay!")
- }
- override suspend fun activateTimer() {
- if (_timerState.value.time == 0L) {
- toaster.showShort("Add some time before starting!")
- return
- }
- _timerState.update { it.copy(state = TimerState.Running) }
- timerJob = timerRunner()
- }
- override suspend fun stopTimer(state: TimerState) {
- timerJob?.cancel()
- timerJob = null
- _timerState.update { it.copy(state = state) }
- persistTimer()
- }
- override fun resetTimer() {
- timerJob?.cancel()
- timerJob = null
- _timerState.update {
- it.copy(
- state = TimerState.Stale,
- time = 0L
- )
- }
- }
- private suspend fun persistTimer() {
- prefs.set("TIMER_REPO_KEY", Json.encodeToString(_timerState.value))
- }
- private fun timerRunner(): Job = coroutineScope.launch {
- while (isActive) {
- val currentState = _timerState.value
- if (currentState.time == 0L) {
- stopTimer(TimerState.Ended)
- break
- }
- delay(1_000)
- _timerState.update {
- it.copy(
- state = TimerState.Running,
- time = currentState.time - 1,
- )
- }
- persistTimer()
- KLogger.d(
- "RUNNING State value: ${_timerState.value}",
- tag = "TIMER_VM"
- )
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement