Merge pull request #1283 from Bnyro/master

Indicator for amount of new videos
This commit is contained in:
Bnyro 2022-09-12 14:32:08 +02:00 committed by GitHub
commit eafc7e3d42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 59 additions and 8 deletions

View File

@ -30,9 +30,11 @@ import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.ActivityMainBinding import com.github.libretube.databinding.ActivityMainBinding
import com.github.libretube.dialogs.ErrorDialog import com.github.libretube.dialogs.ErrorDialog
import com.github.libretube.extensions.BaseActivity import com.github.libretube.extensions.BaseActivity
import com.github.libretube.extensions.toID
import com.github.libretube.fragments.PlayerFragment import com.github.libretube.fragments.PlayerFragment
import com.github.libretube.models.PlayerViewModel import com.github.libretube.models.PlayerViewModel
import com.github.libretube.models.SearchViewModel import com.github.libretube.models.SearchViewModel
import com.github.libretube.models.SubscriptionsViewModel
import com.github.libretube.services.ClosingService import com.github.libretube.services.ClosingService
import com.github.libretube.util.NetworkHelper import com.github.libretube.util.NetworkHelper
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
@ -138,6 +140,7 @@ class MainActivity : BaseActivity() {
navController.navigate(R.id.homeFragment) navController.navigate(R.id.homeFragment)
} }
R.id.subscriptionsFragment -> { R.id.subscriptionsFragment -> {
binding.bottomNav.removeBadge(R.id.subscriptionsFragment)
navController.navigate(R.id.subscriptionsFragment) navController.navigate(R.id.subscriptionsFragment)
} }
R.id.libraryFragment -> { R.id.libraryFragment -> {
@ -157,6 +160,8 @@ class MainActivity : BaseActivity() {
setupBreakReminder() setupBreakReminder()
setupSubscriptionsBadge()
// new way of handling back presses // new way of handling back presses
onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) { onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() { 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 * Remove the focus of the search view in the toolbar
*/ */

View File

@ -31,7 +31,8 @@ class BottomSheetAdapter(
} }
// increase padding if there's no drawable // increase padding if there's no drawable
if (item.drawable == null) root.setPadding( if (item.drawable == null) {
root.setPadding(
root.paddingLeft * 2, root.paddingLeft * 2,
root.paddingTop, root.paddingTop,
root.paddingRight, root.paddingRight,
@ -39,6 +40,7 @@ class BottomSheetAdapter(
) )
} }
} }
}
override fun getItemCount(): Int { override fun getItemCount(): Int {
return items.size return items.size

View File

@ -34,6 +34,7 @@ object PreferenceKeys {
const val LEGACY_SUBSCRIPTIONS = "legacy_subscriptions" const val LEGACY_SUBSCRIPTIONS = "legacy_subscriptions"
const val LEGACY_SUBSCRIPTIONS_COLUMNS = "legacy_subscriptions_columns" const val LEGACY_SUBSCRIPTIONS_COLUMNS = "legacy_subscriptions_columns"
const val ALTERNATIVE_TRENDING_LAYOUT = "trending_layout" const val ALTERNATIVE_TRENDING_LAYOUT = "trending_layout"
const val NEW_VIDEOS_BADGE = "new_videos_badge"
/** /**
* Instance * Instance

View File

@ -121,7 +121,7 @@ class NotificationHelper(
return@runBlocking return@runBlocking
} }
val lastSeenStreamId = PreferenceHelper.getLatestVideoId() val lastSeenStreamId = PreferenceHelper.getLastSeenVideoId()
val latestFeedStreamId = videoFeed[0].url!!.toID() val latestFeedStreamId = videoFeed[0].url!!.toID()
// first time notifications enabled or no new video available // first time notifications enabled or no new video available

View File

@ -76,7 +76,7 @@ object PreferenceHelper {
editor.putString(PreferenceKeys.LAST_STREAM_VIDEO_ID, videoId).commit() editor.putString(PreferenceKeys.LAST_STREAM_VIDEO_ID, videoId).commit()
} }
fun getLatestVideoId(): String { fun getLastSeenVideoId(): String {
return getString(PreferenceKeys.LAST_STREAM_VIDEO_ID, "") return getString(PreferenceKeys.LAST_STREAM_VIDEO_ID, "")
} }

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="48"
android:viewportHeight="48">
<path
android:fillColor="#FF000000"
android:pathData="m17.3,45 l-3.8,-6.5 -7.55,-1.55 0.85,-7.35L2,24l4.8,-5.55 -0.85,-7.35 7.55,-1.55L17.3,3 24,6.1 30.7,3l3.85,6.55 7.5,1.55 -0.85,7.35L46,24l-4.8,5.6 0.85,7.35 -7.5,1.55L30.7,45 24,41.9ZM21.85,30.65L33.2,19.4l-2.25,-2.05 -9.1,9 -4.75,-4.95 -2.3,2.25Z" />
</vector>

View File

@ -319,4 +319,6 @@
<string name="renamePlaylist">Rename playlist</string> <string name="renamePlaylist">Rename playlist</string>
<string name="wifi">WiFi</string> <string name="wifi">WiFi</string>
<string name="mobile_data">Mobile data</string> <string name="mobile_data">Mobile data</string>
<string name="new_videos_badge">Indicator for new videos</string>
<string name="new_videos_badge_summary">Show a badge with the amount of new videos if there are some.</string>
</resources> </resources>

View File

@ -24,6 +24,12 @@
app:key="reset_settings" app:key="reset_settings"
app:title="@string/reset" /> app:title="@string/reset" />
<SwitchPreferenceCompat
android:icon="@drawable/ic_badge"
android:summary="@string/new_videos_badge_summary"
app:key="new_videos_badge"
app:title="@string/new_videos_badge" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory app:title="@string/backup_restore"> <PreferenceCategory app:title="@string/backup_restore">