mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
Merge pull request #3040 from Bnyro/master
Improve home page loading behavior
This commit is contained in:
commit
12bb5c7d4e
@ -1,10 +1,10 @@
|
||||
package com.github.libretube.ui.fragments
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
@ -14,21 +14,23 @@ import com.github.libretube.R
|
||||
import com.github.libretube.api.PlaylistsHelper
|
||||
import com.github.libretube.api.RetrofitInstance
|
||||
import com.github.libretube.api.SubscriptionHelper
|
||||
import com.github.libretube.constants.PreferenceKeys
|
||||
import com.github.libretube.databinding.FragmentHomeBinding
|
||||
import com.github.libretube.db.DatabaseHolder
|
||||
import com.github.libretube.extensions.awaitQuery
|
||||
import com.github.libretube.extensions.toastFromMainThread
|
||||
import com.github.libretube.helpers.LocaleHelper
|
||||
import com.github.libretube.helpers.PreferenceHelper
|
||||
import com.github.libretube.ui.adapters.PlaylistBookmarkAdapter
|
||||
import com.github.libretube.ui.adapters.PlaylistsAdapter
|
||||
import com.github.libretube.ui.adapters.VideosAdapter
|
||||
import com.github.libretube.ui.base.BaseFragment
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import com.github.libretube.ui.models.SubscriptionsViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class HomeFragment : BaseFragment() {
|
||||
private lateinit var binding: FragmentHomeBinding
|
||||
private val subscriptionsViewModel: SubscriptionsViewModel by activityViewModels()
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
@ -60,20 +62,52 @@ class HomeFragment : BaseFragment() {
|
||||
|
||||
binding.refresh.setOnRefreshListener {
|
||||
binding.refresh.isRefreshing = true
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
fetchHome()
|
||||
fetchHomeFeed()
|
||||
}
|
||||
|
||||
fetchHomeFeed()
|
||||
}
|
||||
|
||||
private fun fetchHomeFeed() {
|
||||
lifecycleScope.launchWhenCreated {
|
||||
withContext(Dispatchers.IO) {
|
||||
loadTrending()
|
||||
loadBookmarks()
|
||||
}
|
||||
}
|
||||
lifecycleScope.launchWhenCreated {
|
||||
withContext(Dispatchers.IO) {
|
||||
loadFeed()
|
||||
loadPlaylists()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
fetchHome()
|
||||
private suspend fun loadTrending() {
|
||||
val region = LocaleHelper.getTrendingRegion(requireContext())
|
||||
val trending = RetrofitInstance.api.getTrending(region).take(10)
|
||||
if (trending.isEmpty()) return
|
||||
|
||||
runOnUiThread {
|
||||
makeVisible(binding.trendingRV, binding.trendingTV)
|
||||
binding.trendingRV.layoutManager = GridLayoutManager(context, 2)
|
||||
binding.trendingRV.adapter = VideosAdapter(
|
||||
trending.toMutableList(),
|
||||
forceMode = VideosAdapter.Companion.ForceMode.TRENDING
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun fetchHome() {
|
||||
runOrError {
|
||||
val feed = SubscriptionHelper.getFeed().take(20)
|
||||
if (feed.isEmpty()) return@runOrError
|
||||
private suspend fun loadFeed() {
|
||||
val feed = if (
|
||||
PreferenceHelper.getBoolean(PreferenceKeys.SAVE_FEED, false) &&
|
||||
!subscriptionsViewModel.videoFeed.value.isNullOrEmpty()
|
||||
) {
|
||||
subscriptionsViewModel.videoFeed.value.orEmpty()
|
||||
} else {
|
||||
SubscriptionHelper.getFeed()
|
||||
}.take(20)
|
||||
if (feed.isEmpty()) return
|
||||
runOnUiThread {
|
||||
makeVisible(binding.featuredRV, binding.featuredTV)
|
||||
binding.featuredRV.layoutManager = LinearLayoutManager(
|
||||
@ -88,23 +122,29 @@ class HomeFragment : BaseFragment() {
|
||||
}
|
||||
}
|
||||
|
||||
runOrError {
|
||||
val region = LocaleHelper.getTrendingRegion(requireContext())
|
||||
val trending = RetrofitInstance.api.getTrending(region).take(10)
|
||||
if (trending.isEmpty()) return@runOrError
|
||||
private fun loadBookmarks() {
|
||||
val bookmarkedPlaylists = awaitQuery {
|
||||
DatabaseHolder.Database.playlistBookmarkDao().getAll()
|
||||
}
|
||||
if (bookmarkedPlaylists.isEmpty()) return
|
||||
runOnUiThread {
|
||||
makeVisible(binding.trendingRV, binding.trendingTV)
|
||||
binding.trendingRV.layoutManager = GridLayoutManager(context, 2)
|
||||
binding.trendingRV.adapter = VideosAdapter(
|
||||
trending.toMutableList(),
|
||||
forceMode = VideosAdapter.Companion.ForceMode.TRENDING
|
||||
makeVisible(binding.bookmarksTV, binding.bookmarksRV)
|
||||
binding.bookmarksRV.layoutManager = LinearLayoutManager(
|
||||
context,
|
||||
LinearLayoutManager.HORIZONTAL,
|
||||
false
|
||||
)
|
||||
binding.bookmarksRV.adapter = PlaylistBookmarkAdapter(
|
||||
bookmarkedPlaylists,
|
||||
PlaylistBookmarkAdapter.Companion.BookmarkMode.HOME
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
runOrError {
|
||||
private suspend fun loadPlaylists() {
|
||||
val playlists = PlaylistsHelper.getPlaylists().take(20)
|
||||
if (playlists.isEmpty()) return@runOrError
|
||||
if (playlists.isEmpty()) return
|
||||
|
||||
runOnUiThread {
|
||||
makeVisible(binding.playlistsRV, binding.playlistsTV)
|
||||
binding.playlistsRV.layoutManager = LinearLayoutManager(context)
|
||||
@ -125,40 +165,6 @@ class HomeFragment : BaseFragment() {
|
||||
}
|
||||
}
|
||||
|
||||
runOrError {
|
||||
val bookmarkedPlaylists = awaitQuery {
|
||||
DatabaseHolder.Database.playlistBookmarkDao().getAll()
|
||||
}
|
||||
if (bookmarkedPlaylists.isEmpty()) return@runOrError
|
||||
runOnUiThread {
|
||||
makeVisible(binding.bookmarksTV, binding.bookmarksRV)
|
||||
binding.bookmarksRV.layoutManager = LinearLayoutManager(
|
||||
context,
|
||||
LinearLayoutManager.HORIZONTAL,
|
||||
false
|
||||
)
|
||||
binding.bookmarksRV.adapter = PlaylistBookmarkAdapter(
|
||||
bookmarkedPlaylists,
|
||||
PlaylistBookmarkAdapter.Companion.BookmarkMode.HOME
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun runOrError(action: suspend () -> Unit) {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
action.invoke()
|
||||
// Can be caused due to activity launch in front view from PiP mode.
|
||||
} catch (e: CancellationException) {
|
||||
Log.e("fetching home tab", e.toString())
|
||||
} catch (e: Exception) {
|
||||
e.localizedMessage?.let { context?.toastFromMainThread(it) }
|
||||
Log.e("fetching home tab", e.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun makeVisible(vararg views: View) {
|
||||
views.forEach {
|
||||
it.visibility = View.VISIBLE
|
||||
|
Loading…
Reference in New Issue
Block a user