From 85012bf2287bc685584120d2c6636d082868e013 Mon Sep 17 00:00:00 2001 From: Tim Kluge Date: Wed, 11 Dec 2024 21:45:03 +0100 Subject: [PATCH] fix #8: Add delay between reminders triggered at the same time --- app/build.gradle.kts | 4 +- app/manifest.json | 4 +- .../karooreminder/KarooReminderExtension.kt | 57 ++++++++++--------- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index c3bd4e9..3e0599b 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -13,8 +13,8 @@ android { applicationId = "de.timklge.karooreminder" minSdk = 26 targetSdk = 34 - versionCode = 6 - versionName = "1.0.5" + versionCode = 7 + versionName = "1.0.6" } buildTypes { diff --git a/app/manifest.json b/app/manifest.json index 3660572..6bab58d 100644 --- a/app/manifest.json +++ b/app/manifest.json @@ -3,8 +3,8 @@ "packageName": "de.timklge.karooreminder", "iconUrl": "https://github.com/timklge/karoo-reminder/releases/latest/download/karoo-reminder.png", "latestApkUrl": "https://github.com/timklge/karoo-reminder/releases/latest/download/app-release.apk", - "latestVersion": "1.0.5", - "latestVersionCode": 6, + "latestVersion": "1.0.6", + "latestVersionCode": 7, "developer": "timklge", "description": "Simple karoo extension that shows in-ride alerts every X minutes", "releaseNotes": "Added display duration setting, bluetooth alert sound" diff --git a/app/src/main/kotlin/de/timklge/karooreminder/KarooReminderExtension.kt b/app/src/main/kotlin/de/timklge/karooreminder/KarooReminderExtension.kt index 703aa3c..cef6791 100644 --- a/app/src/main/kotlin/de/timklge/karooreminder/KarooReminderExtension.kt +++ b/app/src/main/kotlin/de/timklge/karooreminder/KarooReminderExtension.kt @@ -18,6 +18,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.drop @@ -27,7 +28,7 @@ import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.launch import kotlinx.serialization.json.Json -class KarooReminderExtension : KarooExtension("karoo-reminder", "1.0.5") { +class KarooReminderExtension : KarooExtension("karoo-reminder", "1.0.6") { companion object { const val TAG = "karoo-reminder" @@ -69,35 +70,39 @@ class KarooReminderExtension : KarooExtension("karoo-reminder", "1.0.5") { .filterNot { it == 0 } .combine(preferences) { elapsedMinutes, reminders -> elapsedMinutes to reminders} .distinctUntilChanged { old, new -> old.first == new.first } - .collect { (elapsedMinutes, reminders) -> - reminders + .collectLatest { (elapsedMinutes, reminders) -> + val rs = reminders .filter { reminder -> reminder.isActive && elapsedMinutes % reminder.interval == 0 } - .forEach { reminder -> - karooSystem.dispatch(TurnScreenOn) - val intent = Intent("de.timklge.HIDE_POWERBAR").apply { - putExtra("duration", (if(reminder.isAutoDismiss) reminder.autoDismissSeconds * 1000L else 15_000L) + 1000L) - putExtra("location", "top") - } + for (reminder in rs){ + karooSystem.dispatch(TurnScreenOn) - delay(1_000) - applicationContext.sendBroadcast(intent) + val intent = Intent("de.timklge.HIDE_POWERBAR").apply { + putExtra("duration", (if(reminder.isAutoDismiss) reminder.autoDismissSeconds * 1000L else 15_000L) + 1000L) + putExtra("location", "top") + } - if (reminder.tone != ReminderBeepPattern.NO_TONES){ - karooSystem.dispatch(PlayBeepPattern(reminder.tone.tones)) - mediaPlayer.start() - } - karooSystem.dispatch( - InRideAlert( - id = "reminder-${reminder.id}-${elapsedMinutes}", - detail = reminder.text, - title = reminder.name, - autoDismissMs = if(reminder.isAutoDismiss) reminder.autoDismissSeconds * 1000L else null, - icon = R.drawable.ic_launcher, - textColor = R.color.white, - backgroundColor = reminder.getResourceColor(applicationContext) - ), - ) + delay(1_000) + applicationContext.sendBroadcast(intent) + + if (reminder.tone != ReminderBeepPattern.NO_TONES){ + karooSystem.dispatch(PlayBeepPattern(reminder.tone.tones)) + mediaPlayer.start() + } + karooSystem.dispatch( + InRideAlert( + id = "reminder-${reminder.id}-${elapsedMinutes}", + detail = reminder.text, + title = reminder.name, + autoDismissMs = if(reminder.isAutoDismiss) reminder.autoDismissSeconds * 1000L else null, + icon = R.drawable.ic_launcher, + textColor = R.color.white, + backgroundColor = reminder.getResourceColor(applicationContext) + ), + ) + + val delayMs = if(reminder.isAutoDismiss) reminder.autoDismissSeconds * 1000L else 20000L + delay(delayMs) } } }