Skip to content

Commit

Permalink
Merge pull request #3040 from Bnyro/master
Browse files Browse the repository at this point in the history
Improve home page loading behavior
  • Loading branch information
Bnyro authored Feb 12, 2023
2 parents d05e8ec + 883f14f commit 12bb5c7
Showing 1 changed file with 92 additions and 86 deletions.
178 changes: 92 additions & 86 deletions app/src/main/java/com/github/libretube/ui/fragments/HomeFragment.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -60,102 +62,106 @@ class HomeFragment : BaseFragment() {

binding.refresh.setOnRefreshListener {
binding.refresh.isRefreshing = true
lifecycleScope.launch(Dispatchers.IO) {
fetchHome()
}
fetchHomeFeed()
}

lifecycleScope.launch(Dispatchers.IO) {
fetchHome()
}
fetchHomeFeed()
}

private suspend fun fetchHome() {
runOrError {
val feed = SubscriptionHelper.getFeed().take(20)
if (feed.isEmpty()) return@runOrError
runOnUiThread {
makeVisible(binding.featuredRV, binding.featuredTV)
binding.featuredRV.layoutManager = LinearLayoutManager(
context,
LinearLayoutManager.HORIZONTAL,
false
)
binding.featuredRV.adapter = VideosAdapter(
feed.toMutableList(),
forceMode = VideosAdapter.Companion.ForceMode.HOME
)
private fun fetchHomeFeed() {
lifecycleScope.launchWhenCreated {
withContext(Dispatchers.IO) {
loadTrending()
loadBookmarks()
}
}

runOrError {
val region = LocaleHelper.getTrendingRegion(requireContext())
val trending = RetrofitInstance.api.getTrending(region).take(10)
if (trending.isEmpty()) return@runOrError
runOnUiThread {
makeVisible(binding.trendingRV, binding.trendingTV)
binding.trendingRV.layoutManager = GridLayoutManager(context, 2)
binding.trendingRV.adapter = VideosAdapter(
trending.toMutableList(),
forceMode = VideosAdapter.Companion.ForceMode.TRENDING
)
lifecycleScope.launchWhenCreated {
withContext(Dispatchers.IO) {
loadFeed()
loadPlaylists()
}
}
}

runOrError {
val playlists = PlaylistsHelper.getPlaylists().take(20)
if (playlists.isEmpty()) return@runOrError
runOnUiThread {
makeVisible(binding.playlistsRV, binding.playlistsTV)
binding.playlistsRV.layoutManager = LinearLayoutManager(context)
binding.playlistsRV.adapter = PlaylistsAdapter(
playlists.toMutableList(),
PlaylistsHelper.getPrivatePlaylistType()
)
binding.playlistsRV.adapter?.registerAdapterDataObserver(object :
RecyclerView.AdapterDataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
super.onItemRangeRemoved(positionStart, itemCount)
if (itemCount == 0) {
binding.playlistsRV.visibility = View.GONE
binding.playlistsTV.visibility = View.GONE
}
}
})
}
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
)
}
}

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 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(
context,
LinearLayoutManager.HORIZONTAL,
false
)
binding.featuredRV.adapter = VideosAdapter(
feed.toMutableList(),
forceMode = VideosAdapter.Companion.ForceMode.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 loadBookmarks() {
val bookmarkedPlaylists = awaitQuery {
DatabaseHolder.Database.playlistBookmarkDao().getAll()
}
if (bookmarkedPlaylists.isEmpty()) return
runOnUiThread {
makeVisible(binding.bookmarksTV, binding.bookmarksRV)
binding.bookmarksRV.layoutManager = LinearLayoutManager(
context,
LinearLayoutManager.HORIZONTAL,
false
)
binding.bookmarksRV.adapter = PlaylistBookmarkAdapter(
bookmarkedPlaylists,
PlaylistBookmarkAdapter.Companion.BookmarkMode.HOME
)
}
}

private suspend fun loadPlaylists() {
val playlists = PlaylistsHelper.getPlaylists().take(20)
if (playlists.isEmpty()) return

runOnUiThread {
makeVisible(binding.playlistsRV, binding.playlistsTV)
binding.playlistsRV.layoutManager = LinearLayoutManager(context)
binding.playlistsRV.adapter = PlaylistsAdapter(
playlists.toMutableList(),
PlaylistsHelper.getPrivatePlaylistType()
)
binding.playlistsRV.adapter?.registerAdapterDataObserver(object :
RecyclerView.AdapterDataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
super.onItemRangeRemoved(positionStart, itemCount)
if (itemCount == 0) {
binding.playlistsRV.visibility = View.GONE
binding.playlistsTV.visibility = View.GONE
}
}
})
}
}

Expand Down

0 comments on commit 12bb5c7

Please sign in to comment.