Advertisement
DroidZed

timer viewmodel

May 5th, 2025
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.52 KB | Software | 0 0
  1. class TimerViewModel(
  2.     private val toaster: ToasterManager,
  3.     private val repo: TimerRepo,
  4.     private val alarmRepo: AlarmRepo,
  5. ) : ViewModel() {
  6.  
  7.     val timerState: StateFlow<TimerGlobalState> = repo.sharedTimerState
  8.         .stateIn(
  9.             viewModelScope,
  10.             SharingStarted.WhileSubscribed(
  11.                 stopTimeoutMillis = 1_000L,
  12.                 replayExpirationMillis = 9_000L,
  13.             ),
  14.             TimerGlobalState()
  15.         )
  16.  
  17.     private val _isPressed = MutableStateFlow(false)
  18.     val isPressed: StateFlow<Boolean> = _isPressed.stateIn(
  19.         viewModelScope,
  20.         SharingStarted.WhileSubscribed(5_000L),
  21.         false,
  22.     )
  23.  
  24.     private val _showBottomSheet = MutableStateFlow<Boolean>(false)
  25.     val showBottomSheet = _showBottomSheet.stateIn(
  26.         viewModelScope,
  27.         SharingStarted.WhileSubscribed(5_000L),
  28.         false,
  29.     )
  30.  
  31.     private var timerJob: Job? = null
  32.  
  33.     fun handleSheetDisplay(shown: Boolean) = _showBottomSheet.update { shown }
  34.  
  35.     fun updatePress(value: Boolean) = _isPressed.update { value }
  36.  
  37.     fun addOneMinute() {
  38.         viewModelScope.launch {
  39.             repo.addOneMinute()
  40.         }
  41.     }
  42.  
  43.     fun restartTimer() {
  44.         viewModelScope.launch {
  45.             repo.restartTimer()
  46.         }
  47.     }
  48.  
  49.     fun activateTimer(useVibrationForAlarm: Boolean) {
  50.         viewModelScope.launch {
  51.             alarmRepo.activateAlarm(timerState.value.time, useVibrationForAlarm)
  52.             repo.activateTimer()
  53.         }
  54.     }
  55.  
  56.     fun stopTimer(state: TimerState) {
  57.         viewModelScope.launch {
  58.             repo.stopTimer(state)
  59.         }
  60.  
  61.         alarmRepo.cancelAlarm()
  62.     }
  63.  
  64.     fun resetTimer() {
  65.         repo.resetTimer()
  66.  
  67.         alarmRepo.cancelAlarm()
  68.     }
  69.  
  70.     // -- START CONFIG PART -- \\
  71.  
  72.     var hours by mutableIntStateOf(0)
  73.         private set
  74.  
  75.     var minutes by mutableIntStateOf(0)
  76.         private set
  77.  
  78.     var alarmSeconds by mutableIntStateOf(0)
  79.         private set
  80.  
  81.     fun zeroValues() = hours == 0 && minutes == 0
  82.  
  83.     fun updateAlarmSeconds(value: Int) {
  84.         if (value <= 59)
  85.             alarmSeconds = value
  86.     }
  87.  
  88.     fun updateHours(value: Int) {
  89.         if (value <= 23)
  90.             hours = value
  91.     }
  92.  
  93.     fun updateMinutes(value: Int) {
  94.         if (value <= 59)
  95.             minutes = value
  96.     }
  97.  
  98.     fun saveTimer() {
  99.         viewModelScope.launch {
  100.             repo.updateState(
  101.                 TimerGlobalState(
  102.                     time = calcTimeLong(hours, minutes),
  103.                     totalTime = calcTimeLong(hours, minutes),
  104.                     state = TimerState.Stale,
  105.                 )
  106.             )
  107.             toaster.showShort("Timer saved")
  108.         }
  109.     }
  110.  
  111.     private fun calcTimeLong(
  112.         hours: Int,
  113.         minutes: Int,
  114.         seconds: Int = 0,
  115.     ) = 3600L * hours + 60L * minutes + 60L * seconds
  116.  
  117.     fun saveAlarm() {
  118.         alarmRepo.saveAlarm(
  119.             alarmSeconds,
  120.             timerState.value.hashCode()
  121.         )
  122.         toaster.showShort("Alarm saved")
  123.     }
  124.  
  125.     fun clearAfterSave() {
  126.         hours = 0
  127.         minutes = 0
  128.         alarmSeconds = 0
  129.         alarmSeconds = 0
  130.     }
  131.  
  132.     fun getFormattedTime(): String = if (hours == 0)
  133.         String.format(
  134.             Locale.getDefault(),
  135.             if (minutes <= 9) "%01d minute(s)" else "%02d minute(s)",
  136.             minutes
  137.         )
  138.     else
  139.         String.format(
  140.             Locale.getDefault(),
  141.             when {
  142.                 hours <= 9 && minutes <= 9 -> {
  143.                     "%01d hour(s) and %01d minute(s)"
  144.                 }
  145.  
  146.                 hours <= 9 && minutes > 9 -> {
  147.                     "%01d hour(s) and %02d minutes"
  148.                 }
  149.  
  150.                 hours > 9 && minutes <= 9 -> {
  151.                     "%02d hours and %01d minute(s)"
  152.                 }
  153.  
  154.                 else -> {
  155.                     "%02d hours and %02d minutes"
  156.                 }
  157.             },
  158.             hours,
  159.             minutes
  160.         )
  161.  
  162.     fun clearInputs() {
  163.         hours = 0
  164.         minutes = 0
  165.         alarmSeconds = 0
  166.         toaster.showShort(resId = R.string.settings_reset)
  167.     }
  168.  
  169.     fun clearAlarm() {
  170.         alarmSeconds = 0
  171.         toaster.showShort(resId = R.string.settings_reset)
  172.     }
  173.  
  174.     // -- END CONFIG PART -- \\
  175.  
  176.     override fun onCleared() {
  177.         super.onCleared()
  178.         timerJob?.cancel()
  179.         KLogger.i(tag = "TimerViewModel", message = {
  180.             "TimerViewModel cleared"
  181.         })
  182.     }
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement