From a8bb7bb87d6a8db222f3b6e25b2e7d5f86566a77 Mon Sep 17 00:00:00 2001 From: Josiah Campbell <9521010+jocmp@users.noreply.github.com> Date: Wed, 25 Dec 2024 20:38:10 -0500 Subject: [PATCH] Add setting to test notifications --- .../java/com/capyreader/app/MainActivity.kt | 6 +-- .../capyreader/app/refresher/FeedRefresher.kt | 4 +- ...Notifications.kt => NotificationHelper.kt} | 43 +++++++++++-------- .../settings/panels/GeneralSettingsPanel.kt | 5 +++ .../ui/settings/panels/TestNotificationRow.kt | 33 ++++++++++++++ 5 files changed, 67 insertions(+), 24 deletions(-) rename app/src/main/java/com/capyreader/app/refresher/{ArticleNotifications.kt => NotificationHelper.kt} (86%) create mode 100644 app/src/main/java/com/capyreader/app/ui/settings/panels/TestNotificationRow.kt diff --git a/app/src/main/java/com/capyreader/app/MainActivity.kt b/app/src/main/java/com/capyreader/app/MainActivity.kt index 15996b3f..ca0cda71 100644 --- a/app/src/main/java/com/capyreader/app/MainActivity.kt +++ b/app/src/main/java/com/capyreader/app/MainActivity.kt @@ -10,7 +10,7 @@ import androidx.activity.enableEdgeToEdge import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import com.capyreader.app.common.AppPreferences -import com.capyreader.app.refresher.ArticleNotifications +import com.capyreader.app.refresher.NotificationHelper import com.capyreader.app.ui.App import com.capyreader.app.ui.Route import org.koin.android.ext.android.get @@ -23,7 +23,7 @@ class MainActivity : ComponentActivity() { enableStrictModeOnDebug() enableEdgeToEdge() super.onCreate(savedInstanceState) - ArticleNotifications.handleResult(intent, appPreferences = appPreferences) + NotificationHelper.handleResult(intent, appPreferences = appPreferences) val theme = appPreferences.theme @@ -39,7 +39,7 @@ class MainActivity : ComponentActivity() { override fun onNewIntent(intent: Intent) { super.onNewIntent(intent) - ArticleNotifications.handleResult(intent, appPreferences = appPreferences) + NotificationHelper.handleResult(intent, appPreferences = appPreferences) } private fun startDestination(): Route { diff --git a/app/src/main/java/com/capyreader/app/refresher/FeedRefresher.kt b/app/src/main/java/com/capyreader/app/refresher/FeedRefresher.kt index fa73f8b4..1d5d2369 100644 --- a/app/src/main/java/com/capyreader/app/refresher/FeedRefresher.kt +++ b/app/src/main/java/com/capyreader/app/refresher/FeedRefresher.kt @@ -10,14 +10,14 @@ class FeedRefresher( private val account: Account, applicationContext: Context, ) { - private val notifications = ArticleNotifications(account = account, applicationContext) + private val notificationHelper = NotificationHelper(account = account, applicationContext) suspend fun refresh() { val since = TimeHelpers.nowUTC() return withContext(Dispatchers.IO) { account.refresh() - notifications.notify(since = since) + notificationHelper.notify(since = since) } } } diff --git a/app/src/main/java/com/capyreader/app/refresher/ArticleNotifications.kt b/app/src/main/java/com/capyreader/app/refresher/NotificationHelper.kt similarity index 86% rename from app/src/main/java/com/capyreader/app/refresher/ArticleNotifications.kt rename to app/src/main/java/com/capyreader/app/refresher/NotificationHelper.kt index 5f241780..d7fe3d39 100644 --- a/app/src/main/java/com/capyreader/app/refresher/ArticleNotifications.kt +++ b/app/src/main/java/com/capyreader/app/refresher/NotificationHelper.kt @@ -13,15 +13,15 @@ import com.capyreader.app.Notifications.FEED_UPDATE import com.capyreader.app.R import com.capyreader.app.common.AppPreferences import com.capyreader.app.common.notificationManager -import com.capyreader.app.refresher.ArticleNotifications.Companion.ARTICLE_ID_KEY -import com.capyreader.app.refresher.ArticleNotifications.Companion.FEED_ID_KEY +import com.capyreader.app.refresher.NotificationHelper.Companion.ARTICLE_ID_KEY +import com.capyreader.app.refresher.NotificationHelper.Companion.FEED_ID_KEY import com.jocmp.capy.Account import com.jocmp.capy.ArticleFilter import com.jocmp.capy.ArticleStatus import com.jocmp.capy.notifications.ArticleNotification import java.time.ZonedDateTime -class ArticleNotifications( +class NotificationHelper( private val account: Account, private val applicationContext: Context, ) { @@ -31,26 +31,19 @@ class ArticleNotifications( createChannel() account.findNotifications(since = since) - .grouped() .forEach { - notify(it) + sendNotification(it) } - } - - private fun notify(group: FeedNotification) { - val builder = NotificationCompat.Builder(applicationContext, FEED_UPDATE.channelID) - .setContentTitle(group.title) - .setSmallIcon(R.drawable.newsmode) - .setGroup(group.id) - .setGroupSummary(true) - .setStyle(group.inboxStyle()) - group.notifications.forEach { notifyArticle(it) } - - notificationManager.notify(group.notificationID, builder.build()) + sendGroupNotification() } - private fun notifyArticle(notification: ArticleNotification) { + private fun sendNotification(notification: ArticleNotification) { + +// +// group.notifications.forEach { notifyArticle(it) } +// +// notificationManager.notify(group.notificationID, builder.build()) val builder = NotificationCompat.Builder(applicationContext, FEED_UPDATE.channelID) .setContentText(notification.title) .setStyle( @@ -58,7 +51,7 @@ class ArticleNotifications( .bigText(notification.title) ) .setSmallIcon(R.drawable.newsmode) - .setGroup(notification.feedID) + .setGroup(ARTICLE_REFRESH_GROUP) .setSubText(notification.feedTitle) .setContentInfo(notification.title) .setAutoCancel(true) @@ -67,6 +60,17 @@ class ArticleNotifications( notificationManager.notify(notification.notificationID, builder.build()) } + private fun sendGroupNotification() { + val builder = NotificationCompat.Builder(applicationContext, FEED_UPDATE.channelID) + .setContentTitle(group.title) + .setSmallIcon(R.drawable.newsmode) + .setGroup(group.id) + .setGroupSummary(true) + .setStyle(group.inboxStyle()) + + notificationManager.notify(group.notificationID, builder.build()) + } + private fun createChannel() { val name = applicationContext.getString(R.string.notifications_channel_title_feed_update) val channel = NotificationChannel( @@ -81,6 +85,7 @@ class ArticleNotifications( companion object { const val ARTICLE_ID_KEY = "article_id" const val FEED_ID_KEY = "feed_id" + private const val ARTICLE_REFRESH_GROUP ="article_refresh" fun handleResult(intent: Intent, appPreferences: AppPreferences) { val articleID = intent.getStringExtra(ARTICLE_ID_KEY) ?: return diff --git a/app/src/main/java/com/capyreader/app/ui/settings/panels/GeneralSettingsPanel.kt b/app/src/main/java/com/capyreader/app/ui/settings/panels/GeneralSettingsPanel.kt index 96182176..11e54a8b 100644 --- a/app/src/main/java/com/capyreader/app/ui/settings/panels/GeneralSettingsPanel.kt +++ b/app/src/main/java/com/capyreader/app/ui/settings/panels/GeneralSettingsPanel.kt @@ -35,6 +35,7 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp +import com.capyreader.app.BuildConfig import com.capyreader.app.Notifications import com.capyreader.app.R import com.capyreader.app.common.RowItem @@ -206,6 +207,10 @@ fun GeneralSettingsPanelView( Text(stringResource(R.string.settings_clear_all_articles_button)) } } + + if (BuildConfig.DEBUG) { + TestNotificationRow() + } } Spacer(Modifier.height(16.dp)) } diff --git a/app/src/main/java/com/capyreader/app/ui/settings/panels/TestNotificationRow.kt b/app/src/main/java/com/capyreader/app/ui/settings/panels/TestNotificationRow.kt new file mode 100644 index 00000000..aff73c1a --- /dev/null +++ b/app/src/main/java/com/capyreader/app/ui/settings/panels/TestNotificationRow.kt @@ -0,0 +1,33 @@ +package com.capyreader.app.ui.settings.panels + +import androidx.compose.material3.Button +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.ui.platform.LocalContext +import com.capyreader.app.common.RowItem +import com.capyreader.app.refresher.NotificationHelper +import com.jocmp.capy.Account +import com.jocmp.capy.common.launchIO +import org.koin.compose.koinInject +import java.time.ZonedDateTime + +@Composable +fun TestNotificationRow(account: Account = koinInject()) { + val scope = rememberCoroutineScope() + val context = LocalContext.current + + fun notify() { + scope.launchIO { + NotificationHelper(account = account, context).notify( + since = ZonedDateTime.now().minusHours(24) + ) + } + } + + RowItem { + Button(onClick = { notify() }) { + Text("Test Notifications") + } + } +}