diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0e52b75..fdc33c7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + currentState.copy(timerRunning = false) } @@ -183,6 +198,8 @@ class TimerViewModel( _timerState.update { it.copy(timerRunning = true) } if (pauseTime != 0L) pauseDuration += SystemClock.elapsedRealtime() - pauseTime + var iterations = -1 + timerJob = viewModelScope.launch { while (true) { if (!timerState.value.timerRunning) break @@ -201,6 +218,10 @@ class TimerViewModel( } } + iterations = (iterations + 1) % 50 + + if (iterations == 0) showTimerNotification(time.value.toInt()) + if (time.value < 0) { skipTimer() @@ -232,6 +253,67 @@ class TimerViewModel( } } + @RequiresPermission(Manifest.permission.POST_NOTIFICATIONS) + fun showTimerNotification( + remainingTime: Int, + paused: Boolean = false, + complete: Boolean = false + ) { + val totalTime = when (timerState.value.timerMode) { + TimerMode.FOCUS -> timerRepository.focusTime.toInt() + TimerMode.SHORT_BREAK -> timerRepository.shortBreakTime.toInt() + else -> timerRepository.longBreakTime.toInt() + } + + val currentTimer = when (timerState.value.timerMode) { + TimerMode.FOCUS -> "Focus" + TimerMode.SHORT_BREAK -> "Short break" + else -> "Long break" + } + + val nextTimer = when (timerState.value.nextTimerMode) { + TimerMode.FOCUS -> "Focus" + TimerMode.SHORT_BREAK -> "Short break" + else -> "Long break" + } + + notificationManager.notify( + 1, + notificationBuilder + .setContentTitle("$currentTimer $middleDot ${ceil(remainingTime.toFloat() / 60000f).toInt()} min remaining") + .setContentText("Up next: $nextTimer (${timerState.value.nextTimeStr})") + .setStyle( + NotificationCompat.ProgressStyle() + .also { + for (i in 0..