-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the logic out of the SleepTimerDialogController into it's ow…
…n viewmodel. Ensure that the creation of the bookmark is not cancelled through the lifecycle scope moving the coroutine in a dedicated scope.
- Loading branch information
1 parent
2bb4174
commit cefe290
Showing
2 changed files
with
111 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
sleepTimer/src/main/kotlin/voice/sleepTimer/SleepTimerDialogViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package voice.sleepTimer | ||
|
||
import de.paulwoitaschek.flowpref.Pref | ||
import kotlinx.coroutines.MainScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import voice.common.pref.PrefKeys | ||
import voice.data.Book | ||
import voice.data.repo.BookRepository | ||
import voice.data.repo.BookmarkRepo | ||
import javax.inject.Inject | ||
import javax.inject.Named | ||
|
||
class SleepTimerDialogViewModel | ||
@Inject constructor( | ||
private val bookmarkRepo: BookmarkRepo, | ||
private val sleepTimer: SleepTimer, | ||
private val bookRepo: BookRepository, | ||
@Named(PrefKeys.SLEEP_TIME) | ||
private val sleepTimePref: Pref<Int> | ||
) { | ||
|
||
private val scope = MainScope() | ||
|
||
private val selectedMinutes = MutableStateFlow(sleepTimePref.value) | ||
|
||
fun viewState(): Flow<SleepTimerDialogViewState> { | ||
return selectedMinutes | ||
.map { selectedMinutes -> | ||
SleepTimerDialogViewState( | ||
selectedMinutes = selectedMinutes, | ||
showFab = selectedMinutes > 0 | ||
) | ||
} | ||
} | ||
|
||
fun onNumberClicked(number: Int) { | ||
require(number in 0..9) | ||
selectedMinutes.update { oldValue -> | ||
val newValue = (oldValue * 10 + number) | ||
if (newValue > 999) { | ||
oldValue | ||
} else { | ||
sleepTimePref.value = newValue | ||
newValue | ||
} | ||
} | ||
} | ||
|
||
fun onNumberDeleteClicked() { | ||
selectedMinutes.update { it / 10 } | ||
} | ||
|
||
fun onNumberDeleteLongClicked() { | ||
selectedMinutes.update { 0 } | ||
} | ||
|
||
fun onConfirmButtonClicked(bookId: Book.Id) { | ||
check(selectedMinutes.value > 0) | ||
|
||
scope.launch { | ||
val book = bookRepo.get(bookId) ?: return@launch | ||
bookmarkRepo.addBookmarkAtBookPosition( | ||
book = book, | ||
setBySleepTimer = true, | ||
title = null | ||
) | ||
} | ||
sleepTimer.setActive(true) | ||
} | ||
} | ||
|
||
data class SleepTimerDialogViewState( | ||
val selectedMinutes: Int, | ||
val showFab: Boolean, | ||
) |