fix: Only update notification progress segments on play/pause

This commit is contained in:
Nishant Mishra
2025-09-27 13:57:01 +05:30
parent dc40b6eeff
commit 2286760ba5

View File

@@ -69,6 +69,8 @@ class TimerService : Service() {
private val cs by lazy { timerRepository.colorScheme } private val cs by lazy { timerRepository.colorScheme }
private lateinit var notificationStyle: NotificationCompat.ProgressStyle
override fun onBind(intent: Intent?): IBinder? { override fun onBind(intent: Intent?): IBinder? {
return null return null
} }
@@ -101,6 +103,8 @@ class TimerService : Service() {
} }
private fun toggleTimer() { private fun toggleTimer() {
updateProgressSegments()
if (timerState.value.timerRunning) { if (timerState.value.timerRunning) {
notificationBuilder.clearActions().addTimerActions( notificationBuilder.clearActions().addTimerActions(
this, R.drawable.play, "Start" this, R.drawable.play, "Start"
@@ -194,7 +198,28 @@ class TimerService : Service() {
) )
.setContentText("Up next: $nextTimer (${timerState.value.nextTimeStr})") .setContentText("Up next: $nextTimer (${timerState.value.nextTimeStr})")
.setStyle( .setStyle(
NotificationCompat.ProgressStyle() notificationStyle
.setProgress( // Set the current progress by filling the previous intervals and part of the current interval
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) {
(totalTime - remainingTime) + ((cycles + 1) / 2) * timerRepository.focusTime.toInt() + (cycles / 2) * timerRepository.shortBreakTime.toInt()
} else (totalTime - remainingTime)
)
)
.setWhen(System.currentTimeMillis() + remainingTime) // Sets the Live Activity/Now Bar chip time
.setShortCriticalText(millisecondsToStr(time.coerceAtLeast(0)))
.build()
)
if (complete) {
startAlarm()
_timerState.update { currentState ->
currentState.copy(alarmRinging = true)
}
}
}
private fun updateProgressSegments() {
notificationStyle = NotificationCompat.ProgressStyle()
.also { .also {
// Add all the Focus, Short break and long break intervals in order // Add all the Focus, Short break and long break intervals in order
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) {
@@ -230,23 +255,6 @@ class TimerService : Service() {
) )
} }
} }
.setProgress( // Set the current progress by filling the previous intervals and part of the current interval
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) {
(totalTime - remainingTime) + ((cycles + 1) / 2) * timerRepository.focusTime.toInt() + (cycles / 2) * timerRepository.shortBreakTime.toInt()
} else (totalTime - remainingTime)
)
)
.setWhen(System.currentTimeMillis() + remainingTime) // Sets the Live Activity/Now Bar chip time
.setShortCriticalText(millisecondsToStr(time.coerceAtLeast(0)))
.build()
)
if (complete) {
startAlarm()
_timerState.update { currentState ->
currentState.copy(alarmRinging = true)
}
}
} }
private fun resetTimer() { private fun resetTimer() {