add badge for amount of new videos

This commit is contained in:
Bnyro 2022-09-12 14:22:55 +02:00
parent c10672f837
commit c1120ab487
4 changed files with 34 additions and 8 deletions

View File

@ -7,6 +7,7 @@ import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
@ -30,9 +31,12 @@ 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.TAG
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 +142,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 +162,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 +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 * Remove the focus of the search view in the toolbar
*/ */

View File

@ -31,12 +31,14 @@ 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.paddingLeft * 2, root.setPadding(
root.paddingTop, root.paddingLeft * 2,
root.paddingRight, root.paddingTop,
root.paddingBottom root.paddingRight,
) root.paddingBottom
)
}
} }
} }

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, "")
} }