From c1120ab487dd4877ca3f3ea7921b56082ffa3a0e Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 12 Sep 2022 14:22:55 +0200 Subject: [PATCH 1/3] add badge for amount of new videos --- .../libretube/activities/MainActivity.kt | 24 +++++++++++++++++++ .../libretube/adapters/BottomSheetAdapter.kt | 14 ++++++----- .../libretube/util/NotificationHelper.kt | 2 +- .../github/libretube/util/PreferenceHelper.kt | 2 +- 4 files changed, 34 insertions(+), 8 deletions(-) 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..cb7b75480 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -7,6 +7,7 @@ import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Looper +import android.util.Log import android.view.Menu import android.view.MenuItem import android.view.View @@ -30,9 +31,12 @@ 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.TAG +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 +142,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 +162,8 @@ class MainActivity : BaseActivity() { setupBreakReminder() + setupSubscriptionsBadge() + // new way of handling back presses onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) { override fun handleOnBackPressed() { @@ -223,6 +230,23 @@ class MainActivity : BaseActivity() { ) } + /** + * Initialize the notification badge showing the amount of new videos + */ + private fun setupSubscriptionsBadge() { + val subscriptionsViewModel = ViewModelProvider(this)[SubscriptionsViewModel::class.java] + subscriptionsViewModel.fetchSubscriptions() + + val lastSeenVideoId = PreferenceHelper.getLastSeenVideoId() + subscriptionsViewModel.videoFeed.observe(this) { + 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/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, "") } From d5bddba863b28dc5703d9c47dc89cbea77680b42 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 12 Sep 2022 14:30:48 +0200 Subject: [PATCH 2/3] add a pref for it --- .../com/github/libretube/activities/MainActivity.kt | 7 ++++++- .../com/github/libretube/constants/PreferenceKeys.kt | 1 + app/src/main/res/drawable/ic_badge.xml | 10 ++++++++++ app/src/main/res/values/strings.xml | 2 ++ app/src/main/res/xml/advanced_settings.xml | 6 ++++++ 5 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 app/src/main/res/drawable/ic_badge.xml 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 cb7b75480..2662bd21e 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -234,11 +234,16 @@ 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() - val lastSeenVideoId = PreferenceHelper.getLastSeenVideoId() subscriptionsViewModel.videoFeed.observe(this) { + val lastSeenVideoId = PreferenceHelper.getLastSeenVideoId() val lastSeenVideoIndex = subscriptionsViewModel.videoFeed.value?.indexOfFirst { lastSeenVideoId == it.url?.toID() } ?: return@observe 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/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..80ac7eb7a 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 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" /> + + From 1501b2660e27610566735d71ab5d0ed61f2c8542 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 12 Sep 2022 14:32:00 +0200 Subject: [PATCH 3/3] improve summary --- .../java/com/github/libretube/activities/MainActivity.kt | 7 ++++--- app/src/main/res/values/strings.xml | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) 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 2662bd21e..1d09692b9 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -7,7 +7,6 @@ import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Looper -import android.util.Log import android.view.Menu import android.view.MenuItem import android.view.View @@ -31,7 +30,6 @@ 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.TAG import com.github.libretube.extensions.toID import com.github.libretube.fragments.PlayerFragment import com.github.libretube.models.PlayerViewModel @@ -237,7 +235,10 @@ class MainActivity : BaseActivity() { if (!PreferenceHelper.getBoolean( PreferenceKeys.NEW_VIDEOS_BADGE, false - )) return + ) + ) { + return + } val subscriptionsViewModel = ViewModelProvider(this)[SubscriptionsViewModel::class.java] subscriptionsViewModel.fetchSubscriptions() diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 80ac7eb7a..1d713b67d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -320,5 +320,5 @@ WiFi Mobile data Indicator for new videos - Show a badge with the amount of new videos + Show a badge with the amount of new videos if there are some.