Merge pull request #4275 from Bnyro/master

refactor: cleanup subscriptions fragment
This commit is contained in:
Bnyro 2023-07-20 10:56:37 +02:00 committed by GitHub
commit 5bac90db3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 44 deletions

View File

@ -251,7 +251,7 @@ class MainActivity : BaseActivity() {
return
}
subscriptionsViewModel.fetchSubscriptions()
subscriptionsViewModel.fetchSubscriptions(this)
subscriptionsViewModel.videoFeed.observe(this) { feed ->
val lastSeenVideoIndex = feed.orEmpty()

View File

@ -82,21 +82,13 @@ class SubscriptionsFragment : Fragment() {
binding.filterTV.text = resources.getStringArray(R.array.filterOptions)[selectedFilter]
binding.subRefresh.isEnabled = true
binding.subProgress.visibility = View.VISIBLE
binding.subProgress.isVisible = true
binding.subFeed.layoutManager = VideosAdapter.getLayout(requireContext())
if (!isCurrentTabSubChannels && (viewModel.videoFeed.value == null || !loadFeedInBackground)) {
viewModel.videoFeed.value = null
viewModel.fetchFeed()
}
// listen for error responses
viewModel.errorResponse.observe(viewLifecycleOwner) {
if (!it) return@observe
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
viewModel.errorResponse.value = false
viewModel.fetchFeed(requireContext())
}
viewModel.videoFeed.observe(viewLifecycleOwner) {
@ -108,8 +100,8 @@ class SubscriptionsFragment : Fragment() {
}
binding.subRefresh.setOnRefreshListener {
viewModel.fetchSubscriptions()
viewModel.fetchFeed()
viewModel.fetchSubscriptions(requireContext())
viewModel.fetchFeed(requireContext())
}
binding.sortTV.setOnClickListener {
@ -136,25 +128,24 @@ class SubscriptionsFragment : Fragment() {
}.show(childFragmentManager)
}
binding.toggleSubs.visibility = View.VISIBLE
binding.toggleSubs.isVisible = true
binding.toggleSubs.setOnClickListener {
binding.subProgress.isVisible = true
binding.subRefresh.isRefreshing = true
if (!isCurrentTabSubChannels) {
isCurrentTabSubChannels = !isCurrentTabSubChannels
if (isCurrentTabSubChannels) {
if (viewModel.subscriptions.value == null) {
viewModel.fetchSubscriptions()
viewModel.fetchSubscriptions(requireContext())
} else {
showSubscriptions()
}
binding.subChannelsContainer.visibility = View.VISIBLE
binding.subFeedContainer.visibility = View.GONE
} else {
showFeed()
binding.subChannelsContainer.visibility = View.GONE
binding.subFeedContainer.visibility = View.VISIBLE
}
isCurrentTabSubChannels = !isCurrentTabSubChannels
binding.subChannelsContainer.isVisible = isCurrentTabSubChannels
binding.subFeedContainer.isGone = isCurrentTabSubChannels
}
binding.scrollviewSub.viewTreeObserver.addOnScrollChangedListener {
@ -282,13 +273,13 @@ class SubscriptionsFragment : Fragment() {
}
}
binding.subChannelsContainer.visibility = View.GONE
binding.subChannelsContainer.isGone = true
binding.subProgress.isGone = true
val notLoaded = viewModel.videoFeed.value.isNullOrEmpty()
binding.subFeedContainer.isGone = notLoaded
binding.emptyFeed.isVisible = notLoaded
binding.subProgress.visibility = View.GONE
subscriptionsAdapter = VideosAdapter(
sortedFeed.toMutableList(),
showAllAtOnce = false
@ -306,30 +297,23 @@ class SubscriptionsFragment : Fragment() {
false
)
binding.subChannels.layoutManager = if (legacySubscriptions) {
GridLayoutManager(
if (legacySubscriptions) {
binding.subChannels.layoutManager = GridLayoutManager(
context,
PreferenceHelper.getString(
PreferenceKeys.LEGACY_SUBSCRIPTIONS_COLUMNS,
"4"
).toInt()
)
binding.subChannels.adapter = LegacySubscriptionAdapter(viewModel.subscriptions.value!!)
} else {
LinearLayoutManager(context)
}
// set the adapter of the subscribed channels
binding.subChannels.adapter = if (legacySubscriptions) {
LegacySubscriptionAdapter(viewModel.subscriptions.value!!)
} else {
SubscriptionChannelAdapter(
viewModel.subscriptions.value!!.toMutableList()
)
binding.subChannels.layoutManager = LinearLayoutManager(context)
binding.subChannels.adapter = SubscriptionChannelAdapter(viewModel.subscriptions.value!!.toMutableList())
}
binding.subRefresh.isRefreshing = false
binding.subProgress.isGone = true
binding.subFeedContainer.visibility = View.GONE
binding.subFeedContainer.isGone = true
val notLoaded = viewModel.subscriptions.value.isNullOrEmpty()
binding.subChannelsContainer.isGone = notLoaded

View File

@ -1,23 +1,22 @@
package com.github.libretube.ui.models
import android.content.Context
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.libretube.R
import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.api.obj.Subscription
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.helpers.PreferenceHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class SubscriptionsViewModel : ViewModel() {
var errorResponse = MutableLiveData<Boolean>().apply {
value = false
}
var videoFeed = MutableLiveData<List<StreamItem>?>().apply {
value = null
}
@ -26,12 +25,12 @@ class SubscriptionsViewModel : ViewModel() {
value = null
}
fun fetchFeed() {
fun fetchFeed(context: Context) {
viewModelScope.launch(Dispatchers.IO) {
val videoFeed = try {
SubscriptionHelper.getFeed()
} catch (e: Exception) {
errorResponse.postValue(true)
context.toastFromMainDispatcher(R.string.server_error)
Log.e(TAG(), e.toString())
return@launch
}
@ -43,12 +42,12 @@ class SubscriptionsViewModel : ViewModel() {
}
}
fun fetchSubscriptions() {
fun fetchSubscriptions(context: Context) {
viewModelScope.launch(Dispatchers.IO) {
val subscriptions = try {
SubscriptionHelper.getSubscriptions()
} catch (e: Exception) {
errorResponse.postValue(true)
context.toastFromMainDispatcher(R.string.server_error)
Log.e(TAG(), e.toString())
return@launch
}