0
0
mirror of https://github.com/mediathekview/zapp.git synced 2024-09-20 04:12:14 +02:00

Give user chance to extend sleep timer when time's almost up

This commit is contained in:
Christine Coenen 2023-03-25 14:48:35 +01:00
parent 2a0b7f9538
commit 2bfd7fa075
2 changed files with 19 additions and 0 deletions

View File

@ -237,6 +237,10 @@ abstract class AbstractPlayerActivity :
}
}
override fun onTimerAlmostEnded() {
showSleepTimerBottomSheet()
}
override fun onTimerEnded() {
showSleepTimerBottomSheet()
}

View File

@ -6,6 +6,7 @@ import java.time.Instant
import java.util.*
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
class SleepTimer(
private val onTimerEnded: () -> Unit
@ -15,6 +16,10 @@ class SleepTimer(
private var timerEndTime: Instant? = null
private val listeners: WeakHashMap<Listener, Void> = WeakHashMap()
private val timerAlmostEndedRunnable = Runnable {
onTimerAlmostEnded()
}
private val timerEndedRunnable = Runnable {
onTimerEnded()
stop()
@ -40,14 +45,19 @@ class SleepTimer(
fun start(duration: Duration) {
stop()
timerEndTime = Instant.now().plusMillis(duration.inWholeMilliseconds)
onIsRunningChanged()
val almostEndedDuration = duration.minus(10.seconds)
handler.postDelayed(timerAlmostEndedRunnable, almostEndedDuration.inWholeMilliseconds)
handler.postDelayed(timerEndedRunnable, duration.inWholeMilliseconds)
}
fun stop() {
timerEndTime = null
onIsRunningChanged()
handler.removeCallbacks(timerAlmostEndedRunnable)
handler.removeCallbacks(timerEndedRunnable)
}
@ -64,6 +74,10 @@ class SleepTimer(
listeners.keys.forEach { it.onIsRunningChanged(isRunning) }
}
private fun onTimerAlmostEnded() {
listeners.keys.forEach { it.onTimerAlmostEnded() }
}
private fun onTimerEnded() {
onTimerEnded.invoke()
listeners.keys.forEach { it.onTimerEnded() }
@ -71,6 +85,7 @@ class SleepTimer(
interface Listener {
fun onIsRunningChanged(isRunning: Boolean) {}
fun onTimerAlmostEnded() {}
fun onTimerEnded() {}
}
}