From df00881cff3c6d4258e7f806ea2495c376c19037 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 18 Nov 2022 15:57:16 +0100 Subject: [PATCH] fix new home tab issues --- .../libretube/api/SubscriptionHelper.kt | 26 +++++++++++++++++++ .../libretube/ui/fragments/HomeFragment.kt | 19 +++++++------- .../github/libretube/ui/models/HomeModel.kt | 23 ++++++++-------- .../ui/models/SubscriptionsViewModel.kt | 26 +++++-------------- .../libretube/workers/NotificationWorker.kt | 10 +------ 5 files changed, 54 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt b/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt index 42af1e63f..a67dc3a2c 100644 --- a/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt +++ b/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt @@ -3,6 +3,8 @@ package com.github.libretube.api import android.content.Context import android.util.Log import com.github.libretube.R +import com.github.libretube.api.obj.StreamItem +import com.github.libretube.api.obj.Subscription import com.github.libretube.constants.PreferenceKeys import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.obj.LocalSubscription @@ -131,4 +133,28 @@ object SubscriptionHelper { val localSubscriptions = getLocalSubscriptions() return localSubscriptions.joinToString(",") { it.channelId } } + + suspend fun getSubscriptions(): List { + return if (PreferenceHelper.getToken() != "") { + RetrofitInstance.authApi.subscriptions( + PreferenceHelper.getToken() + ) + } else { + RetrofitInstance.authApi.unauthenticatedSubscriptions( + getFormattedLocalSubscriptions() + ) + } + } + + suspend fun getFeed(): List { + return if (PreferenceHelper.getToken() != "") { + RetrofitInstance.authApi.getFeed( + PreferenceHelper.getToken() + ) + } else { + RetrofitInstance.authApi.getUnauthenticatedFeed( + getFormattedLocalSubscriptions() + ) + } + } } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt index 2cf8bf955..52624b7dc 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt @@ -53,9 +53,7 @@ class HomeFragment : BaseFragment() { } viewModel.feed.observe(viewLifecycleOwner) { - binding.featuredTV.visibility = View.VISIBLE - binding.featuredRV.visibility = View.VISIBLE - binding.progress.visibility = View.GONE + makeVisible(binding.featuredRV, binding.featuredTV) binding.featuredRV.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) binding.featuredRV.adapter = VideosAdapter( it.toMutableList(), @@ -66,9 +64,7 @@ class HomeFragment : BaseFragment() { viewModel.trending.observe(viewLifecycleOwner) { if (it.isEmpty()) return@observe - binding.trendingTV.visibility = View.VISIBLE - binding.trendingRV.visibility = View.VISIBLE - binding.progress.visibility = View.GONE + makeVisible(binding.trendingRV, binding.trendingTV) binding.trendingRV.layoutManager = GridLayoutManager(context, 2) binding.trendingRV.adapter = VideosAdapter( it.toMutableList(), @@ -79,9 +75,7 @@ class HomeFragment : BaseFragment() { viewModel.playlists.observe(viewLifecycleOwner) { if (it.isEmpty()) return@observe - binding.playlistsRV.visibility = View.VISIBLE - binding.playlistsTV.visibility = View.VISIBLE - binding.progress.visibility = View.GONE + makeVisible(binding.playlistsRV, binding.playlistsTV) binding.playlistsRV.layoutManager = LinearLayoutManager(context) binding.playlistsRV.adapter = PlaylistsAdapter(it.toMutableList(), childFragmentManager) binding.playlistsRV.adapter?.registerAdapterDataObserver(object : @@ -96,4 +90,11 @@ class HomeFragment : BaseFragment() { }) } } + + private fun makeVisible(vararg views: View) { + views.forEach { + it.visibility = View.VISIBLE + } + binding.progress.visibility = View.GONE + } } diff --git a/app/src/main/java/com/github/libretube/ui/models/HomeModel.kt b/app/src/main/java/com/github/libretube/ui/models/HomeModel.kt index d20232f32..09ef92015 100644 --- a/app/src/main/java/com/github/libretube/ui/models/HomeModel.kt +++ b/app/src/main/java/com/github/libretube/ui/models/HomeModel.kt @@ -4,6 +4,7 @@ import android.content.Context import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.github.libretube.api.RetrofitInstance +import com.github.libretube.api.SubscriptionHelper import com.github.libretube.api.obj.Playlists import com.github.libretube.api.obj.StreamItem import com.github.libretube.extensions.toastFromMainThread @@ -18,27 +19,25 @@ class HomeModel : ViewModel() { var trending = MutableLiveData>() val playlists = MutableLiveData>() - suspend fun fetchHome(context: Context, trendingRegion: String) { + suspend fun fetchHome(context: Context, trendingRegion: String, forceReload: Boolean = false) { val token = PreferenceHelper.getToken() val appContext = context.applicationContext runOrError(appContext) { - if (trending.value.isNullOrEmpty()) { - trending.postValue( - RetrofitInstance.api.getTrending(trendingRegion).withMaxSize(10) - ) - } + if (!feed.value.isNullOrEmpty() && !forceReload) return@runOrError + feed.postValue( + SubscriptionHelper.getFeed().withMaxSize(20) + ) } runOrError(appContext) { - if (feed.value.isNullOrEmpty()) { - feed.postValue( - RetrofitInstance.authApi.getFeed(token).withMaxSize(20) - ) - } + if (!trending.value.isNullOrEmpty() && !forceReload) return@runOrError + trending.postValue( + RetrofitInstance.api.getTrending(trendingRegion).withMaxSize(10) + ) } runOrError(appContext) { - if (token == "" || !playlists.value.isNullOrEmpty()) return@runOrError + if ((token == "" || playlists.value.isNullOrEmpty()) && !forceReload) return@runOrError playlists.postValue( RetrofitInstance.authApi.getUserPlaylists(token).withMaxSize(20) ) diff --git a/app/src/main/java/com/github/libretube/ui/models/SubscriptionsViewModel.kt b/app/src/main/java/com/github/libretube/ui/models/SubscriptionsViewModel.kt index 76e08ec26..6252d0d4c 100644 --- a/app/src/main/java/com/github/libretube/ui/models/SubscriptionsViewModel.kt +++ b/app/src/main/java/com/github/libretube/ui/models/SubscriptionsViewModel.kt @@ -5,6 +5,8 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.SubscriptionHelper +import com.github.libretube.api.obj.StreamItem +import com.github.libretube.api.obj.Subscription import com.github.libretube.extensions.TAG import com.github.libretube.extensions.toID import com.github.libretube.util.PreferenceHelper @@ -17,26 +19,18 @@ class SubscriptionsViewModel : ViewModel() { value = false } - var videoFeed = MutableLiveData?>().apply { + var videoFeed = MutableLiveData?>().apply { value = null } - var subscriptions = MutableLiveData?>().apply { + var subscriptions = MutableLiveData?>().apply { value = null } fun fetchFeed() { CoroutineScope(Dispatchers.IO).launch { val videoFeed = try { - if (PreferenceHelper.getToken() != "") { - RetrofitInstance.authApi.getFeed( - PreferenceHelper.getToken() - ) - } else { - RetrofitInstance.authApi.getUnauthenticatedFeed( - SubscriptionHelper.getFormattedLocalSubscriptions() - ) - } + SubscriptionHelper.getFeed() } catch (e: Exception) { errorResponse.postValue(true) Log.e(TAG(), e.toString()) @@ -53,15 +47,7 @@ class SubscriptionsViewModel : ViewModel() { fun fetchSubscriptions() { CoroutineScope(Dispatchers.IO).launch { val subscriptions = try { - if (PreferenceHelper.getToken() != "") { - RetrofitInstance.authApi.subscriptions( - PreferenceHelper.getToken() - ) - } else { - RetrofitInstance.authApi.unauthenticatedSubscriptions( - SubscriptionHelper.getFormattedLocalSubscriptions() - ) - } + SubscriptionHelper.getSubscriptions() } catch (e: Exception) { errorResponse.postValue(true) Log.e(TAG(), e.toString()) diff --git a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt index 1c4c85466..7d5e9b801 100644 --- a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt +++ b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt @@ -10,7 +10,6 @@ import androidx.core.app.NotificationManagerCompat import androidx.work.Worker import androidx.work.WorkerParameters import com.github.libretube.R -import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.SubscriptionHelper import com.github.libretube.constants.PUSH_CHANNEL_ID import com.github.libretube.constants.PreferenceKeys @@ -82,16 +81,9 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : private fun checkForNewStreams(): Boolean { var success = true - val token = PreferenceHelper.getToken() runBlocking { val task = async { - if (token != "") { - RetrofitInstance.authApi.getFeed(token) - } else { - RetrofitInstance.authApi.getUnauthenticatedFeed( - SubscriptionHelper.getFormattedLocalSubscriptions() - ) - } + SubscriptionHelper.getFeed() } // fetch the users feed val videoFeed = try {