mirror of
https://github.com/rhunk/SnapEnhance.git
synced 2025-06-13 13:47:47 +02:00
fix(streaks_reminder): cooldown
This commit is contained in:
@ -16,6 +16,9 @@ import me.rhunk.snapenhance.SharedContextHolder
|
|||||||
import me.rhunk.snapenhance.bridge.ForceStartActivity
|
import me.rhunk.snapenhance.bridge.ForceStartActivity
|
||||||
import me.rhunk.snapenhance.common.util.snap.BitmojiSelfie
|
import me.rhunk.snapenhance.common.util.snap.BitmojiSelfie
|
||||||
import me.rhunk.snapenhance.ui.util.ImageRequestHelper
|
import me.rhunk.snapenhance.ui.util.ImageRequestHelper
|
||||||
|
import kotlin.time.Duration.Companion.hours
|
||||||
|
import kotlin.time.Duration.Companion.milliseconds
|
||||||
|
import kotlin.time.Duration.Companion.minutes
|
||||||
|
|
||||||
class StreaksReminder(
|
class StreaksReminder(
|
||||||
private val remoteSideContext: RemoteSideContext? = null
|
private val remoteSideContext: RemoteSideContext? = null
|
||||||
@ -37,11 +40,21 @@ class StreaksReminder(
|
|||||||
override fun onReceive(ctx: Context, intent: Intent) {
|
override fun onReceive(ctx: Context, intent: Intent) {
|
||||||
val remoteSideContext = this.remoteSideContext ?: SharedContextHolder.remote(ctx)
|
val remoteSideContext = this.remoteSideContext ?: SharedContextHolder.remote(ctx)
|
||||||
val streaksReminderConfig = remoteSideContext.config.root.streaksReminder
|
val streaksReminderConfig = remoteSideContext.config.root.streaksReminder
|
||||||
|
val sharedPreferences = remoteSideContext.sharedPreferences
|
||||||
|
|
||||||
if (streaksReminderConfig.globalState != true) return
|
if (streaksReminderConfig.globalState != true) return
|
||||||
|
|
||||||
|
val interval = streaksReminderConfig.interval.get()
|
||||||
val remainingHours = streaksReminderConfig.remainingHours.get()
|
val remainingHours = streaksReminderConfig.remainingHours.get()
|
||||||
|
|
||||||
|
if (sharedPreferences.getLong("lastStreaksReminder", 0).milliseconds + interval.hours - 10.minutes > System.currentTimeMillis().milliseconds) return
|
||||||
|
sharedPreferences.edit().putLong("lastStreaksReminder", System.currentTimeMillis()).apply()
|
||||||
|
|
||||||
|
remoteSideContext.androidContext.getSystemService(AlarmManager::class.java).setRepeating(
|
||||||
|
AlarmManager.RTC_WAKEUP, 5000, interval.toLong() * 60 * 60 * 1000,
|
||||||
|
PendingIntent.getBroadcast(remoteSideContext.androidContext, 0, Intent(remoteSideContext.androidContext, StreaksReminder::class.java),
|
||||||
|
PendingIntent.FLAG_IMMUTABLE)
|
||||||
|
)
|
||||||
|
|
||||||
val notifyFriendList = remoteSideContext.modDatabase.getFriends()
|
val notifyFriendList = remoteSideContext.modDatabase.getFriends()
|
||||||
.associateBy { remoteSideContext.modDatabase.getFriendStreaks(it.userId) }
|
.associateBy { remoteSideContext.modDatabase.getFriendStreaks(it.userId) }
|
||||||
@ -102,18 +115,9 @@ class StreaksReminder(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: ask for notifications permission for a13+
|
|
||||||
fun init() {
|
fun init() {
|
||||||
if (remoteSideContext == null) throw IllegalStateException("RemoteSideContext is null")
|
if (remoteSideContext == null) throw IllegalStateException("RemoteSideContext is null")
|
||||||
val reminderConfig = remoteSideContext.config.root.streaksReminder.also {
|
if (remoteSideContext.config.root.streaksReminder.globalState != true) return
|
||||||
if (it.globalState != true) return
|
|
||||||
}
|
|
||||||
|
|
||||||
remoteSideContext.androidContext.getSystemService(AlarmManager::class.java).setRepeating(
|
|
||||||
AlarmManager.RTC_WAKEUP, 5000, reminderConfig.interval.get().toLong() * 60 * 60 * 1000,
|
|
||||||
PendingIntent.getBroadcast(remoteSideContext.androidContext, 0, Intent(remoteSideContext.androidContext, StreaksReminder::class.java),
|
|
||||||
PendingIntent.FLAG_IMMUTABLE)
|
|
||||||
)
|
|
||||||
|
|
||||||
onReceive(remoteSideContext.androidContext, Intent())
|
onReceive(remoteSideContext.androidContext, Intent())
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package me.rhunk.snapenhance.common.config.impl
|
|||||||
import me.rhunk.snapenhance.common.config.ConfigContainer
|
import me.rhunk.snapenhance.common.config.ConfigContainer
|
||||||
|
|
||||||
class StreaksReminderConfig : ConfigContainer(hasGlobalState = true) {
|
class StreaksReminderConfig : ConfigContainer(hasGlobalState = true) {
|
||||||
val interval = integer("interval", 2)
|
val interval = integer("interval", 1)
|
||||||
val remainingHours = integer("remaining_hours", 13)
|
val remainingHours = integer("remaining_hours", 13)
|
||||||
val groupNotifications = boolean("group_notifications", true)
|
val groupNotifications = boolean("group_notifications", true)
|
||||||
}
|
}
|
Reference in New Issue
Block a user