Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TimerViewModel(
- private val toaster: ToasterManager,
- private val repo: TimerRepo,
- private val alarmRepo: AlarmRepo,
- ) : ViewModel() {
- val timerState: StateFlow<TimerGlobalState> = repo.sharedTimerState
- .stateIn(
- viewModelScope,
- SharingStarted.WhileSubscribed(
- stopTimeoutMillis = 1_000L,
- replayExpirationMillis = 9_000L,
- ),
- TimerGlobalState()
- )
- private val _isPressed = MutableStateFlow(false)
- val isPressed: StateFlow<Boolean> = _isPressed.stateIn(
- viewModelScope,
- SharingStarted.WhileSubscribed(5_000L),
- false,
- )
- private val _showBottomSheet = MutableStateFlow<Boolean>(false)
- val showBottomSheet = _showBottomSheet.stateIn(
- viewModelScope,
- SharingStarted.WhileSubscribed(5_000L),
- false,
- )
- private var timerJob: Job? = null
- fun handleSheetDisplay(shown: Boolean) = _showBottomSheet.update { shown }
- fun updatePress(value: Boolean) = _isPressed.update { value }
- fun addOneMinute() {
- viewModelScope.launch {
- repo.addOneMinute()
- }
- }
- fun restartTimer() {
- viewModelScope.launch {
- repo.restartTimer()
- }
- }
- fun activateTimer(useVibrationForAlarm: Boolean) {
- viewModelScope.launch {
- alarmRepo.activateAlarm(timerState.value.time, useVibrationForAlarm)
- repo.activateTimer()
- }
- }
- fun stopTimer(state: TimerState) {
- viewModelScope.launch {
- repo.stopTimer(state)
- }
- alarmRepo.cancelAlarm()
- }
- fun resetTimer() {
- repo.resetTimer()
- alarmRepo.cancelAlarm()
- }
- // -- START CONFIG PART -- \\
- var hours by mutableIntStateOf(0)
- private set
- var minutes by mutableIntStateOf(0)
- private set
- var alarmSeconds by mutableIntStateOf(0)
- private set
- fun zeroValues() = hours == 0 && minutes == 0
- fun updateAlarmSeconds(value: Int) {
- if (value <= 59)
- alarmSeconds = value
- }
- fun updateHours(value: Int) {
- if (value <= 23)
- hours = value
- }
- fun updateMinutes(value: Int) {
- if (value <= 59)
- minutes = value
- }
- fun saveTimer() {
- viewModelScope.launch {
- repo.updateState(
- TimerGlobalState(
- time = calcTimeLong(hours, minutes),
- totalTime = calcTimeLong(hours, minutes),
- state = TimerState.Stale,
- )
- )
- toaster.showShort("Timer saved")
- }
- }
- private fun calcTimeLong(
- hours: Int,
- minutes: Int,
- seconds: Int = 0,
- ) = 3600L * hours + 60L * minutes + 60L * seconds
- fun saveAlarm() {
- alarmRepo.saveAlarm(
- alarmSeconds,
- timerState.value.hashCode()
- )
- toaster.showShort("Alarm saved")
- }
- fun clearAfterSave() {
- hours = 0
- minutes = 0
- alarmSeconds = 0
- alarmSeconds = 0
- }
- fun getFormattedTime(): String = if (hours == 0)
- String.format(
- Locale.getDefault(),
- if (minutes <= 9) "%01d minute(s)" else "%02d minute(s)",
- minutes
- )
- else
- String.format(
- Locale.getDefault(),
- when {
- hours <= 9 && minutes <= 9 -> {
- "%01d hour(s) and %01d minute(s)"
- }
- hours <= 9 && minutes > 9 -> {
- "%01d hour(s) and %02d minutes"
- }
- hours > 9 && minutes <= 9 -> {
- "%02d hours and %01d minute(s)"
- }
- else -> {
- "%02d hours and %02d minutes"
- }
- },
- hours,
- minutes
- )
- fun clearInputs() {
- hours = 0
- minutes = 0
- alarmSeconds = 0
- toaster.showShort(resId = R.string.settings_reset)
- }
- fun clearAlarm() {
- alarmSeconds = 0
- toaster.showShort(resId = R.string.settings_reset)
- }
- // -- END CONFIG PART -- \\
- override fun onCleared() {
- super.onCleared()
- timerJob?.cancel()
- KLogger.i(tag = "TimerViewModel", message = {
- "TimerViewModel cleared"
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement