Skip to content

Commit

Permalink
Add setting to test notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
jocmp committed Dec 26, 2024
1 parent d897a75 commit a8bb7bb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 24 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/com/capyreader/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand All @@ -31,34 +31,27 @@ 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(
NotificationCompat.BigTextStyle()
.bigText(notification.title)
)
.setSmallIcon(R.drawable.newsmode)
.setGroup(notification.feedID)
.setGroup(ARTICLE_REFRESH_GROUP)
.setSubText(notification.feedTitle)
.setContentInfo(notification.title)
.setAutoCancel(true)
Expand All @@ -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(
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -206,6 +207,10 @@ fun GeneralSettingsPanelView(
Text(stringResource(R.string.settings_clear_all_articles_button))
}
}

if (BuildConfig.DEBUG) {
TestNotificationRow()
}
}
Spacer(Modifier.height(16.dp))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
}
}
}

0 comments on commit a8bb7bb

Please sign in to comment.