From 19c47dd69716a87c7d3768b84b8733dfce240a8e Mon Sep 17 00:00:00 2001 From: Nishant Mishra Date: Wed, 20 Aug 2025 19:52:45 +0530 Subject: [PATCH] feat: Implement a basic Progress-Centric notification Permissions are not checked for now. This will be fixed in further commits --- app/src/main/AndroidManifest.xml | 2 + .../timerScreen/viewModel/TimerViewModel.kt | 103 +++++++++++++++++- 2 files changed, 103 insertions(+), 2 deletions(-) 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..