From c8b54fd10a08ed53eb7d21578a4fe990fe14e3bc Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 23 Feb 2020 16:06:45 -0800 Subject: Move prefs to service locator --- .../com/pitchedapps/frost/services/BaseJobService.kt | 2 +- .../pitchedapps/frost/services/FrostNotifications.kt | 18 +++++++++--------- .../frost/services/NotificationService.kt | 20 +++++++++++--------- .../pitchedapps/frost/services/NotificationUtils.kt | 19 +++++++++++-------- .../com/pitchedapps/frost/services/UpdateReceiver.kt | 8 ++++++-- 5 files changed, 38 insertions(+), 29 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/services') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/BaseJobService.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/BaseJobService.kt index 4d1317d5..0db08d0f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/BaseJobService.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/BaseJobService.kt @@ -20,9 +20,9 @@ import android.app.job.JobParameters import android.app.job.JobService import androidx.annotation.CallSuper import ca.allanwang.kau.utils.ContextHelper -import kotlin.coroutines.CoroutineContext import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job +import kotlin.coroutines.CoroutineContext abstract class BaseJobService : JobService(), CoroutineScope { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt index 955d6482..7b20e07c 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt @@ -67,7 +67,7 @@ enum class NotificationType( private val overlayContext: OverlayContext, private val fbItem: FbItem, private val parser: FrostParser, - private val ringtone: () -> String + private val ringtoneProvider: (Prefs) -> String ) { GENERAL( @@ -75,7 +75,7 @@ enum class NotificationType( OverlayContext.NOTIFICATION, FbItem.NOTIFICATIONS, NotifParser, - Prefs::notificationRingtone + { it.notificationRingtone } ), MESSAGE( @@ -83,7 +83,7 @@ enum class NotificationType( OverlayContext.MESSAGE, FbItem.MESSAGES, MessageParser, - Prefs::messageRingtone + { it.messageRingtone } ); private val groupPrefix = "frost_${name.toLowerCase(Locale.CANADA)}" @@ -112,7 +112,7 @@ enum class NotificationType( * Returns the number of notifications generated, * or -1 if an error occurred */ - suspend fun fetch(context: Context, data: CookieEntity): Int { + suspend fun fetch(context: Context, data: CookieEntity, prefs: Prefs): Int { val notifDao = FrostDatabase.get().notifDao() val response = try { parser.parse(data.cookie) @@ -129,7 +129,7 @@ enum class NotificationType( */ fun validText(text: String?): Boolean { val t = text ?: return true - return Prefs.notificationKeywords.none { + return prefs.notificationKeywords.none { t.contains(it, true) } } @@ -167,7 +167,7 @@ enum class NotificationType( frostEvent("Notifications", "Type" to name, "Count" to notifs.size) if (notifs.size > 1) summaryNotification(context, userId, notifs.size).notify(context) - val ringtone = ringtone() + val ringtone = ringtoneProvider(prefs) notifs.forEachIndexed { i, notif -> // Ring at most twice notif.withAlert(context, i < 2, ringtone).notify(context) @@ -316,9 +316,9 @@ data class FrostNotification( NotificationManagerCompat.from(context).notify(tag, id, notif.build()) } -fun Context.scheduleNotificationsFromPrefs(): Boolean { - val shouldSchedule = Prefs.hasNotifications - return if (shouldSchedule) scheduleNotifications(Prefs.notificationFreq) +fun Context.scheduleNotificationsFromPrefs(prefs: Prefs): Boolean { + val shouldSchedule = prefs.hasNotifications + return if (shouldSchedule) scheduleNotifications(prefs.notificationFreq) else scheduleNotifications(-1) } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt index 95726974..73c97b5e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt @@ -34,6 +34,7 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import kotlinx.coroutines.yield import org.koin.android.ext.android.inject +import org.koin.core.inject /** * Created by Allan Wang on 2017-06-14. @@ -45,7 +46,8 @@ import org.koin.android.ext.android.inject */ class NotificationService : BaseJobService() { - val cookieDao: CookieDao by inject() + private val prefs: Prefs by inject() + private val cookieDao: CookieDao by inject() override fun onStopJob(params: JobParameters?): Boolean { super.onStopJob(params) @@ -64,7 +66,7 @@ class NotificationService : BaseJobService() { frostEvent( "NotificationTime", "Type" to (if (abrupt) "Service force stop" else "Service"), - "IM Included" to Prefs.notificationsInstantMessages, + "IM Included" to prefs.notificationsInstantMessages, "Duration" to time ) } @@ -86,7 +88,7 @@ class NotificationService : BaseJobService() { private suspend fun sendNotifications(params: JobParameters?): Unit = withContext(Dispatchers.Default) { - val currentId = Prefs.userId + val currentId = prefs.userId val cookies = cookieDao.selectAll() yield() val jobId = params?.extras?.getInt(NOTIFICATION_PARAM_ID, -1) ?: -1 @@ -94,12 +96,12 @@ class NotificationService : BaseJobService() { for (cookie in cookies) { yield() val current = cookie.id == currentId - if (Prefs.notificationsGeneral && - (current || Prefs.notificationAllAccounts) + if (prefs.notificationsGeneral && + (current || prefs.notificationAllAccounts) ) notifCount += fetch(jobId, NotificationType.GENERAL, cookie) - if (Prefs.notificationsInstantMessages && - (current || Prefs.notificationsImAllAccounts) + if (prefs.notificationsInstantMessages && + (current || prefs.notificationsImAllAccounts) ) notifCount += fetch(jobId, NotificationType.MESSAGE, cookie) } @@ -117,7 +119,7 @@ class NotificationService : BaseJobService() { * Also normalized the output to return the number of notifications received */ private suspend fun fetch(jobId: Int, type: NotificationType, cookie: CookieEntity): Int { - val count = type.fetch(this, cookie) + val count = type.fetch(this, cookie, prefs) if (count < 0) { if (jobId == NOTIFICATION_JOB_NOW) generalNotification(666, R.string.error_notification, BuildConfig.DEBUG) @@ -133,7 +135,7 @@ class NotificationService : BaseJobService() { private fun generalNotification(id: Int, textRes: Int, withDefaults: Boolean) { val notifBuilder = frostNotification(NOTIF_CHANNEL_GENERAL) - .setFrostAlert(this, withDefaults, Prefs.notificationRingtone) + .setFrostAlert(this, withDefaults, prefs.notificationRingtone) .setContentTitle(string(R.string.frost_name)) .setContentText(string(textRes)) NotificationManagerCompat.from(this).notify(id, notifBuilder.build()) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationUtils.kt index 7352082d..5f01dfd2 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationUtils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationUtils.kt @@ -41,13 +41,13 @@ import com.pitchedapps.frost.utils.frostUri const val NOTIF_CHANNEL_GENERAL = "general" const val NOTIF_CHANNEL_MESSAGES = "messages" -fun setupNotificationChannels(c: Context) { +fun setupNotificationChannels(c: Context, prefs: Prefs) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return val manager = c.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val appName = c.string(R.string.frost_name) val msg = c.string(R.string.messages) - manager.createNotificationChannel(NOTIF_CHANNEL_GENERAL, appName) - manager.createNotificationChannel(NOTIF_CHANNEL_MESSAGES, "$appName: $msg") + manager.createNotificationChannel(NOTIF_CHANNEL_GENERAL, appName, prefs) + manager.createNotificationChannel(NOTIF_CHANNEL_MESSAGES, "$appName: $msg", prefs) manager.notificationChannels .filter { it.id != NOTIF_CHANNEL_GENERAL && @@ -60,14 +60,15 @@ fun setupNotificationChannels(c: Context) { @RequiresApi(Build.VERSION_CODES.O) private fun NotificationManager.createNotificationChannel( id: String, - name: String + name: String, + prefs: Prefs ): NotificationChannel { val channel = NotificationChannel( id, name, NotificationManager.IMPORTANCE_DEFAULT ) channel.enableLights(true) - channel.lightColor = Prefs.accentColor + channel.lightColor = prefs.accentColor channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC createNotificationChannel(channel) return channel @@ -93,6 +94,8 @@ fun NotificationCompat.Builder.setFrostAlert( enable: Boolean, ringtone: String ): NotificationCompat.Builder { + val prefs = Prefs.get() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { setGroupAlertBehavior( if (enable) NotificationCompat.GROUP_ALERT_CHILDREN @@ -102,12 +105,12 @@ fun NotificationCompat.Builder.setFrostAlert( setDefaults(0) } else { var defaults = 0 - if (Prefs.notificationVibrate) defaults = defaults or Notification.DEFAULT_VIBRATE - if (Prefs.notificationSound) { + if (prefs.notificationVibrate) defaults = defaults or Notification.DEFAULT_VIBRATE + if (prefs.notificationSound) { if (ringtone.isNotBlank()) setSound(context.frostUri(ringtone)) else defaults = defaults or Notification.DEFAULT_SOUND } - if (Prefs.notificationLights) defaults = defaults or Notification.DEFAULT_LIGHTS + if (prefs.notificationLights) defaults = defaults or Notification.DEFAULT_LIGHTS setDefaults(defaults) } return this diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/UpdateReceiver.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/UpdateReceiver.kt index 2dbe6b6b..4c419e52 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/UpdateReceiver.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/UpdateReceiver.kt @@ -21,17 +21,21 @@ import android.content.Context import android.content.Intent import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs +import org.koin.core.KoinComponent +import org.koin.core.inject /** * Created by Allan Wang on 2017-05-31. * * Receiver that is triggered whenever the app updates so it can bind the notifications again */ -class UpdateReceiver : BroadcastReceiver() { +class UpdateReceiver : BroadcastReceiver(), KoinComponent { + + private val prefs: Prefs by inject() override fun onReceive(context: Context, intent: Intent) { if (intent.action != Intent.ACTION_MY_PACKAGE_REPLACED) return L.d { "Frost has updated" } - context.scheduleNotifications(Prefs.notificationFreq) // Update notifications + context.scheduleNotifications(prefs.notificationFreq) // Update notifications } } -- cgit v1.2.3