mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-15 06:40: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
|
package com.github.libretube.ui.fragments
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
|
import androidx.fragment.app.activityViewModels
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.navigation.fragment.findNavController
|
import androidx.navigation.fragment.findNavController
|
||||||
import androidx.recyclerview.widget.GridLayoutManager
|
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.PlaylistsHelper
|
||||||
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.constants.PreferenceKeys
|
||||||
import com.github.libretube.databinding.FragmentHomeBinding
|
import com.github.libretube.databinding.FragmentHomeBinding
|
||||||
import com.github.libretube.db.DatabaseHolder
|
import com.github.libretube.db.DatabaseHolder
|
||||||
import com.github.libretube.extensions.awaitQuery
|
import com.github.libretube.extensions.awaitQuery
|
||||||
import com.github.libretube.extensions.toastFromMainThread
|
|
||||||
import com.github.libretube.helpers.LocaleHelper
|
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.PlaylistBookmarkAdapter
|
||||||
import com.github.libretube.ui.adapters.PlaylistsAdapter
|
import com.github.libretube.ui.adapters.PlaylistsAdapter
|
||||||
import com.github.libretube.ui.adapters.VideosAdapter
|
import com.github.libretube.ui.adapters.VideosAdapter
|
||||||
import com.github.libretube.ui.base.BaseFragment
|
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.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
class HomeFragment : BaseFragment() {
|
class HomeFragment : BaseFragment() {
|
||||||
private lateinit var binding: FragmentHomeBinding
|
private lateinit var binding: FragmentHomeBinding
|
||||||
|
private val subscriptionsViewModel: SubscriptionsViewModel by activityViewModels()
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater,
|
inflater: LayoutInflater,
|
||||||
@ -60,20 +62,52 @@ class HomeFragment : BaseFragment() {
|
|||||||
|
|
||||||
binding.refresh.setOnRefreshListener {
|
binding.refresh.setOnRefreshListener {
|
||||||
binding.refresh.isRefreshing = true
|
binding.refresh.isRefreshing = true
|
||||||
lifecycleScope.launch(Dispatchers.IO) {
|
fetchHomeFeed()
|
||||||
fetchHome()
|
}
|
||||||
|
|
||||||
|
fetchHomeFeed()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun fetchHomeFeed() {
|
||||||
|
lifecycleScope.launchWhenCreated {
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
loadTrending()
|
||||||
|
loadBookmarks()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lifecycleScope.launchWhenCreated {
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
loadFeed()
|
||||||
|
loadPlaylists()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lifecycleScope.launch(Dispatchers.IO) {
|
private suspend fun loadTrending() {
|
||||||
fetchHome()
|
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() {
|
private suspend fun loadFeed() {
|
||||||
runOrError {
|
val feed = if (
|
||||||
val feed = SubscriptionHelper.getFeed().take(20)
|
PreferenceHelper.getBoolean(PreferenceKeys.SAVE_FEED, false) &&
|
||||||
if (feed.isEmpty()) return@runOrError
|
!subscriptionsViewModel.videoFeed.value.isNullOrEmpty()
|
||||||
|
) {
|
||||||
|
subscriptionsViewModel.videoFeed.value.orEmpty()
|
||||||
|
} else {
|
||||||
|
SubscriptionHelper.getFeed()
|
||||||
|
}.take(20)
|
||||||
|
if (feed.isEmpty()) return
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
makeVisible(binding.featuredRV, binding.featuredTV)
|
makeVisible(binding.featuredRV, binding.featuredTV)
|
||||||
binding.featuredRV.layoutManager = LinearLayoutManager(
|
binding.featuredRV.layoutManager = LinearLayoutManager(
|
||||||
@ -88,23 +122,29 @@ class HomeFragment : BaseFragment() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
runOrError {
|
private fun loadBookmarks() {
|
||||||
val region = LocaleHelper.getTrendingRegion(requireContext())
|
val bookmarkedPlaylists = awaitQuery {
|
||||||
val trending = RetrofitInstance.api.getTrending(region).take(10)
|
DatabaseHolder.Database.playlistBookmarkDao().getAll()
|
||||||
if (trending.isEmpty()) return@runOrError
|
}
|
||||||
|
if (bookmarkedPlaylists.isEmpty()) return
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
makeVisible(binding.trendingRV, binding.trendingTV)
|
makeVisible(binding.bookmarksTV, binding.bookmarksRV)
|
||||||
binding.trendingRV.layoutManager = GridLayoutManager(context, 2)
|
binding.bookmarksRV.layoutManager = LinearLayoutManager(
|
||||||
binding.trendingRV.adapter = VideosAdapter(
|
context,
|
||||||
trending.toMutableList(),
|
LinearLayoutManager.HORIZONTAL,
|
||||||
forceMode = VideosAdapter.Companion.ForceMode.TRENDING
|
false
|
||||||
|
)
|
||||||
|
binding.bookmarksRV.adapter = PlaylistBookmarkAdapter(
|
||||||
|
bookmarkedPlaylists,
|
||||||
|
PlaylistBookmarkAdapter.Companion.BookmarkMode.HOME
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
runOrError {
|
private suspend fun loadPlaylists() {
|
||||||
val playlists = PlaylistsHelper.getPlaylists().take(20)
|
val playlists = PlaylistsHelper.getPlaylists().take(20)
|
||||||
if (playlists.isEmpty()) return@runOrError
|
if (playlists.isEmpty()) return
|
||||||
|
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
makeVisible(binding.playlistsRV, binding.playlistsTV)
|
makeVisible(binding.playlistsRV, binding.playlistsTV)
|
||||||
binding.playlistsRV.layoutManager = LinearLayoutManager(context)
|
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) {
|
private fun makeVisible(vararg views: View) {
|
||||||
views.forEach {
|
views.forEach {
|
||||||
it.visibility = View.VISIBLE
|
it.visibility = View.VISIBLE
|
||||||
|
Loading…
Reference in New Issue
Block a user