mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-01-07 18:10:31 +05:30
Merge pull request #5498 from IndusAryan/mvvm
feat: seperate trends fragment into a viewmodel to improve ux
This commit is contained in:
commit
ec8c9bad4b
@ -2,32 +2,23 @@ package com.github.libretube.ui.fragments
|
|||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
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 android.widget.Toast
|
|
||||||
import androidx.core.view.isGone
|
import androidx.core.view.isGone
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.fragment.app.activityViewModels
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.api.RetrofitInstance
|
|
||||||
import com.github.libretube.databinding.FragmentTrendsBinding
|
import com.github.libretube.databinding.FragmentTrendsBinding
|
||||||
import com.github.libretube.extensions.TAG
|
|
||||||
import com.github.libretube.helpers.LocaleHelper
|
|
||||||
import com.github.libretube.ui.activities.SettingsActivity
|
import com.github.libretube.ui.activities.SettingsActivity
|
||||||
import com.github.libretube.ui.adapters.VideosAdapter
|
import com.github.libretube.ui.adapters.VideosAdapter
|
||||||
import com.github.libretube.ui.base.DynamicLayoutManagerFragment
|
import com.github.libretube.ui.base.DynamicLayoutManagerFragment
|
||||||
import com.github.libretube.util.deArrow
|
import com.github.libretube.ui.models.TrendsViewModel
|
||||||
import com.google.android.material.snackbar.Snackbar
|
import com.google.android.material.snackbar.Snackbar
|
||||||
import java.io.IOException
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import retrofit2.HttpException
|
|
||||||
|
|
||||||
class TrendsFragment : DynamicLayoutManagerFragment() {
|
class TrendsFragment : DynamicLayoutManagerFragment() {
|
||||||
private var _binding: FragmentTrendsBinding? = null
|
private var _binding: FragmentTrendsBinding? = null
|
||||||
private val binding get() = _binding!!
|
private val binding get() = _binding!!
|
||||||
|
private val viewModel: TrendsViewModel by activityViewModels()
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater,
|
inflater: LayoutInflater,
|
||||||
@ -45,52 +36,33 @@ class TrendsFragment : DynamicLayoutManagerFragment() {
|
|||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
|
||||||
fetchTrending()
|
viewModel.trendingVideos.observe(viewLifecycleOwner) { videos ->
|
||||||
binding.homeRefresh.isEnabled = true
|
if (videos == null) return@observe
|
||||||
binding.homeRefresh.setOnRefreshListener {
|
|
||||||
fetchTrending()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroyView() {
|
binding.recview.adapter = VideosAdapter(videos.toMutableList())
|
||||||
super.onDestroyView()
|
|
||||||
_binding = null
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun fetchTrending() {
|
|
||||||
lifecycleScope.launch {
|
|
||||||
val response = try {
|
|
||||||
withContext(Dispatchers.IO) {
|
|
||||||
val region = LocaleHelper.getTrendingRegion(requireContext())
|
|
||||||
RetrofitInstance.api.getTrending(region).deArrow()
|
|
||||||
}
|
|
||||||
} catch (e: IOException) {
|
|
||||||
println(e)
|
|
||||||
Log.e(TAG(), "IOException, you might not have internet connection")
|
|
||||||
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
|
|
||||||
return@launch
|
|
||||||
} catch (e: HttpException) {
|
|
||||||
Log.e(TAG(), "HttpException, unexpected response")
|
|
||||||
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
|
|
||||||
return@launch
|
|
||||||
}
|
|
||||||
|
|
||||||
val binding = _binding ?: return@launch
|
|
||||||
binding.homeRefresh.isRefreshing = false
|
binding.homeRefresh.isRefreshing = false
|
||||||
binding.progressBar.isGone = true
|
binding.progressBar.isGone = true
|
||||||
|
|
||||||
// show a [SnackBar] if there are no trending videos available
|
if (videos.isEmpty()) {
|
||||||
if (response.isEmpty()) {
|
|
||||||
Snackbar.make(binding.root, R.string.change_region, Snackbar.LENGTH_LONG)
|
Snackbar.make(binding.root, R.string.change_region, Snackbar.LENGTH_LONG)
|
||||||
.setAction(R.string.settings) {
|
.setAction(R.string.settings) {
|
||||||
val settingsIntent = Intent(context, SettingsActivity::class.java)
|
val settingsIntent = Intent(context, SettingsActivity::class.java)
|
||||||
startActivity(settingsIntent)
|
startActivity(settingsIntent)
|
||||||
}
|
}
|
||||||
.show()
|
.show()
|
||||||
return@launch
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.recview.adapter = VideosAdapter(response.toMutableList())
|
binding.homeRefresh.isEnabled = true
|
||||||
}
|
binding.homeRefresh.setOnRefreshListener {
|
||||||
|
viewModel.fetchTrending(requireContext())
|
||||||
|
}
|
||||||
|
|
||||||
|
viewModel.fetchTrending(requireContext())
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroyView() {
|
||||||
|
super.onDestroyView()
|
||||||
|
_binding = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.github.libretube.ui.models
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.util.Log
|
||||||
|
import android.widget.Toast
|
||||||
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import com.github.libretube.R
|
||||||
|
import com.github.libretube.api.RetrofitInstance
|
||||||
|
import com.github.libretube.api.obj.StreamItem
|
||||||
|
import com.github.libretube.extensions.TAG
|
||||||
|
import com.github.libretube.helpers.LocaleHelper
|
||||||
|
import com.github.libretube.util.deArrow
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import retrofit2.HttpException
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
class TrendsViewModel: ViewModel() {
|
||||||
|
val trendingVideos = MutableLiveData<List<StreamItem>>()
|
||||||
|
|
||||||
|
fun fetchTrending(context: Context) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
try {
|
||||||
|
val region = LocaleHelper.getTrendingRegion(context)
|
||||||
|
val response = withContext(Dispatchers.IO) {
|
||||||
|
RetrofitInstance.api.getTrending(region).deArrow()
|
||||||
|
}
|
||||||
|
trendingVideos.postValue(response)
|
||||||
|
} catch (e: IOException) {
|
||||||
|
println(e)
|
||||||
|
Log.e(TAG(), "IOException, you might not have internet connection")
|
||||||
|
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
|
||||||
|
return@launch
|
||||||
|
} catch (e: HttpException) {
|
||||||
|
Log.e(TAG(), "HttpException, unexpected response")
|
||||||
|
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user