From 1e02ad2041d2f2fbd905dbdbb0c844d11b4a2e92 Mon Sep 17 00:00:00 2001 From: Amine Date: Tue, 6 Nov 2018 20:29:00 +0100 Subject: [PATCH] Closes #201. --- .../apps/amine/bou/readerforselfoss/MyApp.kt | 9 ++++++++- .../readerforselfoss/background/background.kt | 17 ++++++++++++++++- .../amine/bou/readerforselfoss/utils/Config.kt | 2 ++ app/src/main/res/values/strings.xml | 4 ++++ app/src/main/res/xml/pref_offline.xml | 6 ++++++ 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt index 165c8de..642128e 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt @@ -68,11 +68,18 @@ class MyApp : MultiDexApplication() { private fun handleNotificationChannels() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager + val name = getString(R.string.notification_channel_sync) val importance = NotificationManager.IMPORTANCE_LOW val mChannel = NotificationChannel(Config.syncChannelId, name, importance) - val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager + + val newItemsChannelname = getString(R.string.new_items_channel_sync) + val newItemsChannelimportance = NotificationManager.IMPORTANCE_DEFAULT + val newItemsChannelmChannel = NotificationChannel(Config.newItemsChannelId, newItemsChannelname, newItemsChannelimportance) + notificationManager.createNotificationChannel(mChannel) + notificationManager.createNotificationChannel(newItemsChannelmChannel) } } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/background/background.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/background/background.kt index eebecb2..8a38e52 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/background/background.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/background/background.kt @@ -2,9 +2,9 @@ package apps.amine.bou.readerforselfoss.background import android.app.NotificationManager import android.content.Context -import android.os.Handler import android.preference.PreferenceManager import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationCompat.PRIORITY_DEFAULT import androidx.core.app.NotificationCompat.PRIORITY_LOW import androidx.room.Room import androidx.work.Worker @@ -51,6 +51,7 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con this.context.getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE) val sharedPref = PreferenceManager.getDefaultSharedPreferences(this.context) val shouldLogEverything = sharedPref.getBoolean("should_log_everything", false) + val notifyNewItems = sharedPref.getBoolean("notify_new_items", false) db = Room.databaseBuilder( applicationContext, @@ -81,6 +82,20 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con db.itemsDao().deleteAllItems() db.itemsDao() .insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray()) + + val newSize = apiItems.filter { it.unread }.size + if (notifyNewItems && newSize > 0) { + val newItemsNotification = NotificationCompat.Builder(applicationContext, Config.newItemsChannelId) + .setContentTitle(context.getString(R.string.new_items_notification_title)) + .setContentText(context.getString(R.string.new_items_notification_text, newSize)) + .setPriority(PRIORITY_DEFAULT) + .setChannelId(Config.newItemsChannelId) + .setSmallIcon(R.drawable.ic_fiber_new_black_24dp) + + Timer("", false).schedule(4000) { + notificationManager.notify(2, newItemsNotification.build()) + } + } } Timer("", false).schedule(4000) { notificationManager.cancel(1) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/Config.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/Config.kt index 0956737..9047012 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/Config.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/Config.kt @@ -38,6 +38,8 @@ class Config(c: Context) { const val syncChannelId = "sync-channel-id" + const val newItemsChannelId = "new-items-channel-id" + fun logoutAndRedirect( c: Context, callingActivity: Activity, diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a053277..a5c55af 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -161,4 +161,8 @@ Loading ... Selfoss is syncing your articles Sync notification + New items notification + New items ! + %1$d new items loaded. + Notify on new items synced. diff --git a/app/src/main/res/xml/pref_offline.xml b/app/src/main/res/xml/pref_offline.xml index c75185c..a10821a 100644 --- a/app/src/main/res/xml/pref_offline.xml +++ b/app/src/main/res/xml/pref_offline.xml @@ -28,4 +28,10 @@ android:key="refresh_when_charging" android:dependency="periodic_refresh" android:title="@string/pref_switch_refresh_when_charging" /> + +