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.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<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) {
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
}
}

View File

@ -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<List<StreamItem>>()
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 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)
)

View File

@ -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<List<com.github.libretube.api.obj.StreamItem>?>().apply {
var videoFeed = MutableLiveData<List<StreamItem>?>().apply {
value = null
}
var subscriptions = MutableLiveData<List<com.github.libretube.api.obj.Subscription>?>().apply {
var subscriptions = MutableLiveData<List<Subscription>?>().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())

View File

@ -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 {