From 459759bfe5950db2e7203f5675e422c2014b083d Mon Sep 17 00:00:00 2001 From: Hunter Nickel Date: Sat, 30 Oct 2021 10:32:51 -0600 Subject: [PATCH] Add badge to bottom bar Updates tab indicating how many unread chapter updates are available (#5620) Co-authored-by: arkon --- .../tachiyomi/data/library/LibraryUpdateService.kt | 3 +++ .../tachiyomi/data/preference/PreferenceKeys.kt | 2 ++ .../tachiyomi/data/preference/PreferencesHelper.kt | 3 +++ .../eu/kanade/tachiyomi/ui/main/MainActivity.kt | 14 ++++++++++++++ .../ui/recent/updates/UpdatesPresenter.kt | 3 +++ .../ui/setting/SettingsLibraryController.kt | 5 +++++ app/src/main/res/values/strings.xml | 1 + 7 files changed, 31 insertions(+) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index 8acc0d4c5..03a9c15a5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt @@ -291,6 +291,7 @@ class LibraryUpdateService( val failedUpdates = CopyOnWriteArrayList>() val hasDownloads = AtomicBoolean(false) val loggedServices by lazy { trackManager.services.filter { it.isLogged } } + val currentUnreadUpdatesCount = preferences.libraryUnreadUpdatesCount().get() withIOContext { mangaToUpdate.groupBy { it.source } @@ -354,6 +355,8 @@ class LibraryUpdateService( if (newUpdates.isNotEmpty()) { notifier.showUpdateNotifications(newUpdates) + val newChapterCount = newUpdates.sumOf { it.second.size } + preferences.libraryUnreadUpdatesCount().set(currentUnreadUpdatesCount + newChapterCount) if (hasDownloads.get()) { DownloadService.start(this) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index f780fd542..f013d73de 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -133,6 +133,8 @@ object PreferenceKeys { const val libraryUpdateRestriction = "library_update_restriction" + const val libraryUpdateShowTabBadge = "library_update_show_tab_badge" + const val libraryUpdateCategories = "library_update_categories" const val libraryUpdateCategoriesExclude = "library_update_categories_exclude" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index ac737be49..79a13eb82 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -238,6 +238,9 @@ class PreferencesHelper(val context: Context) { fun libraryUpdateRestriction() = flowPrefs.getStringSet(Keys.libraryUpdateRestriction, setOf(ONLY_ON_WIFI)) + fun libraryUpdateShowTabBadge() = flowPrefs.getBoolean(Keys.libraryUpdateShowTabBadge, true) + fun libraryUnreadUpdatesCount() = flowPrefs.getInt("library_unread_updates_count", 0) + fun libraryUpdateCategories() = flowPrefs.getStringSet(Keys.libraryUpdateCategories, emptySet()) fun libraryUpdateCategoriesExclude() = flowPrefs.getStringSet(Keys.libraryUpdateCategoriesExclude, emptySet()) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index 07ce03a59..ec87d8357 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -68,6 +68,7 @@ import eu.kanade.tachiyomi.util.view.setNavigationBarTransparentCompat import kotlinx.coroutines.delay import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.merge import kotlinx.coroutines.flow.onEach import logcat.LogPriority @@ -227,6 +228,10 @@ class MainActivity : BaseViewBindingActivity() { } } + merge(preferences.libraryUpdateShowTabBadge().asFlow(), preferences.libraryUnreadUpdatesCount().asFlow()) + .onEach { setUnreadUpdatesBadge() } + .launchIn(lifecycleScope) + preferences.extensionUpdatesCount() .asImmediateFlow { setExtensionsBadge() } .launchIn(lifecycleScope) @@ -349,6 +354,15 @@ class MainActivity : BaseViewBindingActivity() { } } + private fun setUnreadUpdatesBadge() { + val updates = if (preferences.libraryUpdateShowTabBadge().get()) preferences.libraryUnreadUpdatesCount().get() else 0 + if (updates > 0) { + nav.getOrCreateBadge(R.id.nav_updates).number = updates + } else { + nav.removeBadge(R.id.nav_updates) + } + } + private fun setExtensionsBadge() { val updates = preferences.extensionUpdatesCount().get() if (updates > 0) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt index 24f450a5a..96efe3db8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt @@ -105,6 +105,9 @@ class UpdatesPresenter : BasePresenter() { } setDownloadedChapters(list) chapters = list + + //Set unread chapter count for bottom bar badge + preferences.libraryUnreadUpdatesCount().set(list.count { !it.read }) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt index a8e98b323..83b1d8a0a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt @@ -199,6 +199,11 @@ class SettingsLibraryController : SettingsController() { titleRes = R.string.pref_update_only_non_completed defaultValue = false } + switchPreference { + key = Keys.libraryUpdateShowTabBadge + titleRes = R.string.pref_library_update_show_tab_badge + defaultValue = true + } preference { key = Keys.libraryUpdateCategories titleRes = R.string.categories diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 708b1dd83..d7b483bf7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -223,6 +223,7 @@ Charging Restrictions: %s Only update ongoing manga + Show update count on bottom bar Automatically refresh metadata Check for new cover and details when updating library Automatically refresh trackers