fix new home tab issues

This commit is contained in:
Bnyro 2022-11-18 15:57:16 +01:00
parent 0b157b560c
commit df00881cff
5 changed files with 54 additions and 50 deletions

View File

@ -3,6 +3,8 @@ package com.github.libretube.api
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import com.github.libretube.R 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.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.LocalSubscription import com.github.libretube.db.obj.LocalSubscription
@ -131,4 +133,28 @@ object SubscriptionHelper {
val localSubscriptions = getLocalSubscriptions() val localSubscriptions = getLocalSubscriptions()
return localSubscriptions.joinToString(",") { it.channelId } return localSubscriptions.joinToString(",") { it.channelId }
} }
suspend fun getSubscriptions(): List<Subscription> {
return if (PreferenceHelper.getToken() != "") {
RetrofitInstance.authApi.subscriptions(
PreferenceHelper.getToken()
)
} else {
RetrofitInstance.authApi.unauthenticatedSubscriptions(
getFormattedLocalSubscriptions()
)
}
}
suspend fun getFeed(): List<StreamItem> {
return if (PreferenceHelper.getToken() != "") {
RetrofitInstance.authApi.getFeed(
PreferenceHelper.getToken()
)
} else {
RetrofitInstance.authApi.getUnauthenticatedFeed(
getFormattedLocalSubscriptions()
)
}
}
} }

View File

@ -53,9 +53,7 @@ class HomeFragment : BaseFragment() {
} }
viewModel.feed.observe(viewLifecycleOwner) { viewModel.feed.observe(viewLifecycleOwner) {
binding.featuredTV.visibility = View.VISIBLE makeVisible(binding.featuredRV, binding.featuredTV)
binding.featuredRV.visibility = View.VISIBLE
binding.progress.visibility = View.GONE
binding.featuredRV.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) binding.featuredRV.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
binding.featuredRV.adapter = VideosAdapter( binding.featuredRV.adapter = VideosAdapter(
it.toMutableList(), it.toMutableList(),
@ -66,9 +64,7 @@ class HomeFragment : BaseFragment() {
viewModel.trending.observe(viewLifecycleOwner) { viewModel.trending.observe(viewLifecycleOwner) {
if (it.isEmpty()) return@observe if (it.isEmpty()) return@observe
binding.trendingTV.visibility = View.VISIBLE makeVisible(binding.trendingRV, binding.trendingTV)
binding.trendingRV.visibility = View.VISIBLE
binding.progress.visibility = View.GONE
binding.trendingRV.layoutManager = GridLayoutManager(context, 2) binding.trendingRV.layoutManager = GridLayoutManager(context, 2)
binding.trendingRV.adapter = VideosAdapter( binding.trendingRV.adapter = VideosAdapter(
it.toMutableList(), it.toMutableList(),
@ -79,9 +75,7 @@ class HomeFragment : BaseFragment() {
viewModel.playlists.observe(viewLifecycleOwner) { viewModel.playlists.observe(viewLifecycleOwner) {
if (it.isEmpty()) return@observe if (it.isEmpty()) return@observe
binding.playlistsRV.visibility = View.VISIBLE makeVisible(binding.playlistsRV, binding.playlistsTV)
binding.playlistsTV.visibility = View.VISIBLE
binding.progress.visibility = View.GONE
binding.playlistsRV.layoutManager = LinearLayoutManager(context) binding.playlistsRV.layoutManager = LinearLayoutManager(context)
binding.playlistsRV.adapter = PlaylistsAdapter(it.toMutableList(), childFragmentManager) binding.playlistsRV.adapter = PlaylistsAdapter(it.toMutableList(), childFragmentManager)
binding.playlistsRV.adapter?.registerAdapterDataObserver(object : 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
}
} }

View File

@ -4,6 +4,7 @@ import android.content.Context
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import com.github.libretube.api.RetrofitInstance 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.Playlists
import com.github.libretube.api.obj.StreamItem import com.github.libretube.api.obj.StreamItem
import com.github.libretube.extensions.toastFromMainThread import com.github.libretube.extensions.toastFromMainThread
@ -18,27 +19,25 @@ class HomeModel : ViewModel() {
var trending = MutableLiveData<List<StreamItem>>() var trending = MutableLiveData<List<StreamItem>>()
val playlists = MutableLiveData<List<Playlists>>() val playlists = MutableLiveData<List<Playlists>>()
suspend fun fetchHome(context: Context, trendingRegion: String) { suspend fun fetchHome(context: Context, trendingRegion: String, forceReload: Boolean = false) {
val token = PreferenceHelper.getToken() val token = PreferenceHelper.getToken()
val appContext = context.applicationContext val appContext = context.applicationContext
runOrError(appContext) { runOrError(appContext) {
if (trending.value.isNullOrEmpty()) { if (!feed.value.isNullOrEmpty() && !forceReload) return@runOrError
trending.postValue( feed.postValue(
RetrofitInstance.api.getTrending(trendingRegion).withMaxSize(10) SubscriptionHelper.getFeed().withMaxSize(20)
) )
}
} }
runOrError(appContext) { runOrError(appContext) {
if (feed.value.isNullOrEmpty()) { if (!trending.value.isNullOrEmpty() && !forceReload) return@runOrError
feed.postValue( trending.postValue(
RetrofitInstance.authApi.getFeed(token).withMaxSize(20) RetrofitInstance.api.getTrending(trendingRegion).withMaxSize(10)
) )
}
} }
runOrError(appContext) { runOrError(appContext) {
if (token == "" || !playlists.value.isNullOrEmpty()) return@runOrError if ((token == "" || playlists.value.isNullOrEmpty()) && !forceReload) return@runOrError
playlists.postValue( playlists.postValue(
RetrofitInstance.authApi.getUserPlaylists(token).withMaxSize(20) RetrofitInstance.authApi.getUserPlaylists(token).withMaxSize(20)
) )

View File

@ -5,6 +5,8 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.SubscriptionHelper 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.TAG
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
@ -17,26 +19,18 @@ class SubscriptionsViewModel : ViewModel() {
value = false value = false
} }
var videoFeed = MutableLiveData<List<com.github.libretube.api.obj.StreamItem>?>().apply { var videoFeed = MutableLiveData<List<StreamItem>?>().apply {
value = null value = null
} }
var subscriptions = MutableLiveData<List<com.github.libretube.api.obj.Subscription>?>().apply { var subscriptions = MutableLiveData<List<Subscription>?>().apply {
value = null value = null
} }
fun fetchFeed() { fun fetchFeed() {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val videoFeed = try { val videoFeed = try {
if (PreferenceHelper.getToken() != "") { SubscriptionHelper.getFeed()
RetrofitInstance.authApi.getFeed(
PreferenceHelper.getToken()
)
} else {
RetrofitInstance.authApi.getUnauthenticatedFeed(
SubscriptionHelper.getFormattedLocalSubscriptions()
)
}
} catch (e: Exception) { } catch (e: Exception) {
errorResponse.postValue(true) errorResponse.postValue(true)
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())
@ -53,15 +47,7 @@ class SubscriptionsViewModel : ViewModel() {
fun fetchSubscriptions() { fun fetchSubscriptions() {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val subscriptions = try { val subscriptions = try {
if (PreferenceHelper.getToken() != "") { SubscriptionHelper.getSubscriptions()
RetrofitInstance.authApi.subscriptions(
PreferenceHelper.getToken()
)
} else {
RetrofitInstance.authApi.unauthenticatedSubscriptions(
SubscriptionHelper.getFormattedLocalSubscriptions()
)
}
} catch (e: Exception) { } catch (e: Exception) {
errorResponse.postValue(true) errorResponse.postValue(true)
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())

View File

@ -10,7 +10,6 @@ import androidx.core.app.NotificationManagerCompat
import androidx.work.Worker import androidx.work.Worker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.SubscriptionHelper import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.constants.PUSH_CHANNEL_ID import com.github.libretube.constants.PUSH_CHANNEL_ID
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
@ -82,16 +81,9 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
private fun checkForNewStreams(): Boolean { private fun checkForNewStreams(): Boolean {
var success = true var success = true
val token = PreferenceHelper.getToken()
runBlocking { runBlocking {
val task = async { val task = async {
if (token != "") { SubscriptionHelper.getFeed()
RetrofitInstance.authApi.getFeed(token)
} else {
RetrofitInstance.authApi.getUnauthenticatedFeed(
SubscriptionHelper.getFormattedLocalSubscriptions()
)
}
} }
// fetch the users feed // fetch the users feed
val videoFeed = try { val videoFeed = try {