Handle errors properly on home fragmemt

This commit is contained in:
Bnyro 2023-02-12 16:01:51 +01:00
parent 883f14fd56
commit 59a1378531
3 changed files with 37 additions and 20 deletions

View File

@ -0,0 +1,14 @@
package com.github.libretube.extensions
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
fun Fragment.launchWhenCreatedIO(block: suspend () -> Unit) {
lifecycleScope.launchWhenCreated {
withContext(Dispatchers.IO) {
block.invoke()
}
}
}

View File

@ -190,4 +190,9 @@ class DownloadDialog(
dismiss() dismiss()
} }
} }
companion object {
const val AUDIO_DOWNLOAD_PREF_KEY = "audio_download_selection"
const val VIDEO_DOWNLOAD_PREF_KEY = "video_download_selection"
}
} }

View File

@ -5,7 +5,6 @@ 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.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@ -18,6 +17,7 @@ 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.launchWhenCreatedIO
import com.github.libretube.helpers.LocaleHelper import com.github.libretube.helpers.LocaleHelper
import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.adapters.PlaylistBookmarkAdapter import com.github.libretube.ui.adapters.PlaylistBookmarkAdapter
@ -25,8 +25,6 @@ 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 com.github.libretube.ui.models.SubscriptionsViewModel import com.github.libretube.ui.models.SubscriptionsViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class HomeFragment : BaseFragment() { class HomeFragment : BaseFragment() {
private lateinit var binding: FragmentHomeBinding private lateinit var binding: FragmentHomeBinding
@ -69,24 +67,21 @@ class HomeFragment : BaseFragment() {
} }
private fun fetchHomeFeed() { private fun fetchHomeFeed() {
lifecycleScope.launchWhenCreated { launchWhenCreatedIO {
withContext(Dispatchers.IO) { loadTrending()
loadTrending() loadBookmarks()
loadBookmarks()
}
} }
lifecycleScope.launchWhenCreated { launchWhenCreatedIO {
withContext(Dispatchers.IO) { loadFeed()
loadFeed() loadPlaylists()
loadPlaylists()
}
} }
} }
private suspend fun loadTrending() { private suspend fun loadTrending() {
val region = LocaleHelper.getTrendingRegion(requireContext()) val region = LocaleHelper.getTrendingRegion(requireContext())
val trending = RetrofitInstance.api.getTrending(region).take(10) val trending = runCatching {
if (trending.isEmpty()) return RetrofitInstance.api.getTrending(region).take(10)
}.getOrNull().takeIf { it?.isNotEmpty() == true } ?: return
runOnUiThread { runOnUiThread {
makeVisible(binding.trendingRV, binding.trendingTV) makeVisible(binding.trendingRV, binding.trendingTV)
@ -105,9 +100,11 @@ class HomeFragment : BaseFragment() {
) { ) {
subscriptionsViewModel.videoFeed.value.orEmpty() subscriptionsViewModel.videoFeed.value.orEmpty()
} else { } else {
SubscriptionHelper.getFeed() runCatching {
}.take(20) SubscriptionHelper.getFeed()
if (feed.isEmpty()) return }.getOrElse { return }
}.takeIf { it.isNotEmpty() }?.take(20) ?: return
runOnUiThread { runOnUiThread {
makeVisible(binding.featuredRV, binding.featuredTV) makeVisible(binding.featuredRV, binding.featuredTV)
binding.featuredRV.layoutManager = LinearLayoutManager( binding.featuredRV.layoutManager = LinearLayoutManager(
@ -142,8 +139,9 @@ class HomeFragment : BaseFragment() {
} }
private suspend fun loadPlaylists() { private suspend fun loadPlaylists() {
val playlists = PlaylistsHelper.getPlaylists().take(20) val playlists = runCatching {
if (playlists.isEmpty()) return PlaylistsHelper.getPlaylists().take(20)
}.getOrNull().takeIf { it?.isNotEmpty() == true } ?: return
runOnUiThread { runOnUiThread {
makeVisible(binding.playlistsRV, binding.playlistsTV) makeVisible(binding.playlistsRV, binding.playlistsTV)