diff --git a/app/src/main/java/com/github/libretube/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/activities/MainActivity.kt index 920c93bb2..1d09692b9 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -30,9 +30,11 @@ import com.github.libretube.constants.PreferenceKeys import com.github.libretube.databinding.ActivityMainBinding import com.github.libretube.dialogs.ErrorDialog import com.github.libretube.extensions.BaseActivity +import com.github.libretube.extensions.toID import com.github.libretube.fragments.PlayerFragment import com.github.libretube.models.PlayerViewModel import com.github.libretube.models.SearchViewModel +import com.github.libretube.models.SubscriptionsViewModel import com.github.libretube.services.ClosingService import com.github.libretube.util.NetworkHelper import com.github.libretube.util.PreferenceHelper @@ -138,6 +140,7 @@ class MainActivity : BaseActivity() { navController.navigate(R.id.homeFragment) } R.id.subscriptionsFragment -> { + binding.bottomNav.removeBadge(R.id.subscriptionsFragment) navController.navigate(R.id.subscriptionsFragment) } R.id.libraryFragment -> { @@ -157,6 +160,8 @@ class MainActivity : BaseActivity() { setupBreakReminder() + setupSubscriptionsBadge() + // new way of handling back presses onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) { override fun handleOnBackPressed() { @@ -223,6 +228,31 @@ class MainActivity : BaseActivity() { ) } + /** + * Initialize the notification badge showing the amount of new videos + */ + private fun setupSubscriptionsBadge() { + if (!PreferenceHelper.getBoolean( + PreferenceKeys.NEW_VIDEOS_BADGE, + false + ) + ) { + return + } + + val subscriptionsViewModel = ViewModelProvider(this)[SubscriptionsViewModel::class.java] + subscriptionsViewModel.fetchSubscriptions() + + subscriptionsViewModel.videoFeed.observe(this) { + val lastSeenVideoId = PreferenceHelper.getLastSeenVideoId() + val lastSeenVideoIndex = subscriptionsViewModel.videoFeed.value?.indexOfFirst { + lastSeenVideoId == it.url?.toID() + } ?: return@observe + if (lastSeenVideoIndex < 1) return@observe + binding.bottomNav.getOrCreateBadge(R.id.subscriptionsFragment).number = lastSeenVideoIndex + } + } + /** * Remove the focus of the search view in the toolbar */ diff --git a/app/src/main/java/com/github/libretube/adapters/BottomSheetAdapter.kt b/app/src/main/java/com/github/libretube/adapters/BottomSheetAdapter.kt index 37fe03d24..14120381b 100644 --- a/app/src/main/java/com/github/libretube/adapters/BottomSheetAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/BottomSheetAdapter.kt @@ -31,12 +31,14 @@ class BottomSheetAdapter( } // increase padding if there's no drawable - if (item.drawable == null) root.setPadding( - root.paddingLeft * 2, - root.paddingTop, - root.paddingRight, - root.paddingBottom - ) + if (item.drawable == null) { + root.setPadding( + root.paddingLeft * 2, + root.paddingTop, + root.paddingRight, + root.paddingBottom + ) + } } } diff --git a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt index 712b55c02..747346c5a 100644 --- a/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt @@ -34,6 +34,7 @@ object PreferenceKeys { const val LEGACY_SUBSCRIPTIONS = "legacy_subscriptions" const val LEGACY_SUBSCRIPTIONS_COLUMNS = "legacy_subscriptions_columns" const val ALTERNATIVE_TRENDING_LAYOUT = "trending_layout" + const val NEW_VIDEOS_BADGE = "new_videos_badge" /** * Instance diff --git a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt index b433fc32a..c8b2e9369 100644 --- a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt @@ -121,7 +121,7 @@ class NotificationHelper( return@runBlocking } - val lastSeenStreamId = PreferenceHelper.getLatestVideoId() + val lastSeenStreamId = PreferenceHelper.getLastSeenVideoId() val latestFeedStreamId = videoFeed[0].url!!.toID() // first time notifications enabled or no new video available diff --git a/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt index b63e95210..8665b1a49 100644 --- a/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt @@ -76,7 +76,7 @@ object PreferenceHelper { editor.putString(PreferenceKeys.LAST_STREAM_VIDEO_ID, videoId).commit() } - fun getLatestVideoId(): String { + fun getLastSeenVideoId(): String { return getString(PreferenceKeys.LAST_STREAM_VIDEO_ID, "") } diff --git a/app/src/main/res/drawable/ic_badge.xml b/app/src/main/res/drawable/ic_badge.xml new file mode 100644 index 000000000..843102c2b --- /dev/null +++ b/app/src/main/res/drawable/ic_badge.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b8edbc4d2..1d713b67d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -319,4 +319,6 @@ Rename playlist WiFi Mobile data + Indicator for new videos + Show a badge with the amount of new videos if there are some. diff --git a/app/src/main/res/xml/advanced_settings.xml b/app/src/main/res/xml/advanced_settings.xml index 88f0613de..bd0bc574e 100644 --- a/app/src/main/res/xml/advanced_settings.xml +++ b/app/src/main/res/xml/advanced_settings.xml @@ -24,6 +24,12 @@ app:key="reset_settings" app:title="@string/reset" /> + +