commit 94d064e0a548432049e144af10953172afe87b25
parent 7c5195e83cc981197fe5f0c59cd74c4a636d8022
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date:   Fri,  1 Sep 2023 13:20:25 +0200

improve: streaks_reminder
- add remaining hours
- add group notifications

Diffstat:
Mapp/src/main/kotlin/me/rhunk/snapenhance/messaging/StreaksReminder.kt | 33+++++++++++++++++++++++++++++----
Mapp/src/main/kotlin/me/rhunk/snapenhance/ui/manager/sections/social/SocialSection.kt | 4+++-
Mcore/src/main/assets/lang/en_US.json | 12++++++++++++
Mcore/src/main/kotlin/me/rhunk/snapenhance/core/config/impl/StreaksReminderConfig.kt | 2++
Mcore/src/main/kotlin/me/rhunk/snapenhance/core/messaging/MessagingCoreObjects.kt | 8+-------
5 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/messaging/StreaksReminder.kt b/app/src/main/kotlin/me/rhunk/snapenhance/messaging/StreaksReminder.kt @@ -40,13 +40,29 @@ class StreaksReminder( override fun onReceive(ctx: Context, intent: Intent) { val remoteSideContext = this.remoteSideContext ?: SharedContextHolder.remote(ctx) - if (remoteSideContext.config.root.streaksReminder.globalState != true) return + val streaksReminderConfig = remoteSideContext.config.root.streaksReminder + + if (streaksReminderConfig.globalState != true) return + + val remainingHours = streaksReminderConfig.remainingHours.get() + val notifyFriendList = remoteSideContext.modDatabase.getFriends() .associateBy { remoteSideContext.modDatabase.getFriendStreaks(it.userId) } - .filter { (streaks, _) -> streaks != null && streaks.notify && streaks.isAboutToExpire() } + .filter { (streaks, _) -> streaks != null && streaks.notify && streaks.isAboutToExpire(remainingHours) } val notificationManager = getNotificationManager(ctx) + val streaksReminderTranslation = remoteSideContext.translation.getCategory("streaks_reminder") + + if (streaksReminderConfig.groupNotifications.get() && notifyFriendList.isNotEmpty()) { + notificationManager.notify(0, NotificationCompat.Builder(ctx, NOTIFICATION_CHANNEL_ID) + .setPriority(NotificationCompat.PRIORITY_HIGH) + .setAutoCancel(true) + .setGroup("streaks") + .setGroupSummary(true) + .setSmallIcon(R.drawable.streak_icon) + .build()) + } notifyFriendList.forEach { (streaks, friend) -> coroutineScope.launch { @@ -56,9 +72,14 @@ class StreaksReminder( ) val notificationBuilder = NotificationCompat.Builder(ctx, NOTIFICATION_CHANNEL_ID) - .setContentTitle("Streaks") - .setContentText("You will lose streaks with ${friend.displayName} in ${streaks?.hoursLeft() ?: 0} hours") + .setContentTitle(streaksReminderTranslation["notification_title"]) + .setContentText(streaksReminderTranslation.format("notification_text", + "friend" to (friend.displayName ?: friend.mutableUsername), + "hoursLeft" to (streaks?.hoursLeft() ?: 0).toString() + )) .setPriority(NotificationCompat.PRIORITY_DEFAULT) + .setAutoCancel(true) + .setGroup("streaks") .setContentIntent(PendingIntent.getActivity( ctx, 0, @@ -74,6 +95,10 @@ class StreaksReminder( } } + if (streaksReminderConfig.groupNotifications.get()) { + notificationBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN) + } + notificationManager.notify(friend.userId.hashCode(), notificationBuilder.build().apply { flags = NotificationCompat.FLAG_ONLY_ALERT_ONCE }) diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/ui/manager/sections/social/SocialSection.kt b/app/src/main/kotlin/me/rhunk/snapenhance/ui/manager/sections/social/SocialSection.kt @@ -134,6 +134,8 @@ class SocialSection : Section() { @Composable private fun ScopeList(scope: SocialScope) { + val remainingHours = remember { context.config.root.streaksReminder.remainingHours.get() } + LazyColumn( modifier = Modifier .padding(2.dp) @@ -213,7 +215,7 @@ class SocialSection : Section() { imageVector = ImageVector.vectorResource(id = R.drawable.streak_icon), contentDescription = null, modifier = Modifier.height(40.dp), - tint = if (streaks.isAboutToExpire()) + tint = if (streaks.isAboutToExpire(remainingHours)) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.primary ) diff --git a/core/src/main/assets/lang/en_US.json b/core/src/main/assets/lang/en_US.json @@ -308,6 +308,14 @@ "interval": { "name": "Interval", "description": "The interval between each reminder (in hours)" + }, + "remaining_hours": { + "name": "Remaining Hours", + "description": "The remaining hours before the notification is shown" + }, + "group_notifications": { + "name": "Group Notifications", + "description": "Group notifications into a single one" } } }, @@ -605,5 +613,9 @@ }, "spoof_activity": { "title": "Spoof Settings" + }, + "streaks_reminder": { + "notification_title": "Streaks", + "notification_text": "You will lose streaks with {friend} in {hoursLeft} hours" } } \ No newline at end of file diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/config/impl/StreaksReminderConfig.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/config/impl/StreaksReminderConfig.kt @@ -4,4 +4,6 @@ import me.rhunk.snapenhance.core.config.ConfigContainer class StreaksReminderConfig : ConfigContainer(hasGlobalState = true) { val interval = integer("interval", 2) + val remainingHours = integer("remaining_hours", 13) + val groupNotifications = boolean("group_notifications", true) } \ No newline at end of file diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/messaging/MessagingCoreObjects.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/messaging/MessagingCoreObjects.kt @@ -1,7 +1,6 @@ package me.rhunk.snapenhance.core.messaging import me.rhunk.snapenhance.util.SerializableDataObject -import kotlin.time.Duration.Companion.hours enum class RuleState( @@ -47,14 +46,9 @@ data class FriendStreaks( val expirationTimestamp: Long, val length: Int ) : SerializableDataObject() { - companion object { - //TODO: config - val EXPIRE_THRESHOLD = 12.hours - } - fun hoursLeft() = (expirationTimestamp - System.currentTimeMillis()) / 1000 / 60 / 60 - fun isAboutToExpire() = expirationTimestamp - System.currentTimeMillis() < EXPIRE_THRESHOLD.inWholeMilliseconds + fun isAboutToExpire(expireHours: Int) = expirationTimestamp - System.currentTimeMillis() < expireHours * 60 * 60 * 1000 } data class MessagingGroupInfo(