mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 16:30:31 +05:30
Merge pull request #4275 from Bnyro/master
refactor: cleanup subscriptions fragment
This commit is contained in:
commit
5bac90db3d
@ -251,7 +251,7 @@ class MainActivity : BaseActivity() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
subscriptionsViewModel.fetchSubscriptions()
|
subscriptionsViewModel.fetchSubscriptions(this)
|
||||||
|
|
||||||
subscriptionsViewModel.videoFeed.observe(this) { feed ->
|
subscriptionsViewModel.videoFeed.observe(this) { feed ->
|
||||||
val lastSeenVideoIndex = feed.orEmpty()
|
val lastSeenVideoIndex = feed.orEmpty()
|
||||||
|
@ -82,21 +82,13 @@ class SubscriptionsFragment : Fragment() {
|
|||||||
binding.filterTV.text = resources.getStringArray(R.array.filterOptions)[selectedFilter]
|
binding.filterTV.text = resources.getStringArray(R.array.filterOptions)[selectedFilter]
|
||||||
|
|
||||||
binding.subRefresh.isEnabled = true
|
binding.subRefresh.isEnabled = true
|
||||||
|
binding.subProgress.isVisible = true
|
||||||
binding.subProgress.visibility = View.VISIBLE
|
|
||||||
|
|
||||||
binding.subFeed.layoutManager = VideosAdapter.getLayout(requireContext())
|
binding.subFeed.layoutManager = VideosAdapter.getLayout(requireContext())
|
||||||
|
|
||||||
if (!isCurrentTabSubChannels && (viewModel.videoFeed.value == null || !loadFeedInBackground)) {
|
if (!isCurrentTabSubChannels && (viewModel.videoFeed.value == null || !loadFeedInBackground)) {
|
||||||
viewModel.videoFeed.value = null
|
viewModel.videoFeed.value = null
|
||||||
viewModel.fetchFeed()
|
viewModel.fetchFeed(requireContext())
|
||||||
}
|
|
||||||
|
|
||||||
// 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.videoFeed.observe(viewLifecycleOwner) {
|
viewModel.videoFeed.observe(viewLifecycleOwner) {
|
||||||
@ -108,8 +100,8 @@ class SubscriptionsFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
binding.subRefresh.setOnRefreshListener {
|
binding.subRefresh.setOnRefreshListener {
|
||||||
viewModel.fetchSubscriptions()
|
viewModel.fetchSubscriptions(requireContext())
|
||||||
viewModel.fetchFeed()
|
viewModel.fetchFeed(requireContext())
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.sortTV.setOnClickListener {
|
binding.sortTV.setOnClickListener {
|
||||||
@ -136,25 +128,24 @@ class SubscriptionsFragment : Fragment() {
|
|||||||
}.show(childFragmentManager)
|
}.show(childFragmentManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.toggleSubs.visibility = View.VISIBLE
|
binding.toggleSubs.isVisible = true
|
||||||
|
|
||||||
binding.toggleSubs.setOnClickListener {
|
binding.toggleSubs.setOnClickListener {
|
||||||
binding.subProgress.isVisible = true
|
binding.subProgress.isVisible = true
|
||||||
binding.subRefresh.isRefreshing = true
|
binding.subRefresh.isRefreshing = true
|
||||||
if (!isCurrentTabSubChannels) {
|
isCurrentTabSubChannels = !isCurrentTabSubChannels
|
||||||
|
|
||||||
|
if (isCurrentTabSubChannels) {
|
||||||
if (viewModel.subscriptions.value == null) {
|
if (viewModel.subscriptions.value == null) {
|
||||||
viewModel.fetchSubscriptions()
|
viewModel.fetchSubscriptions(requireContext())
|
||||||
} else {
|
} else {
|
||||||
showSubscriptions()
|
showSubscriptions()
|
||||||
}
|
}
|
||||||
binding.subChannelsContainer.visibility = View.VISIBLE
|
|
||||||
binding.subFeedContainer.visibility = View.GONE
|
|
||||||
} else {
|
} else {
|
||||||
showFeed()
|
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 {
|
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()
|
val notLoaded = viewModel.videoFeed.value.isNullOrEmpty()
|
||||||
binding.subFeedContainer.isGone = notLoaded
|
binding.subFeedContainer.isGone = notLoaded
|
||||||
binding.emptyFeed.isVisible = notLoaded
|
binding.emptyFeed.isVisible = notLoaded
|
||||||
|
|
||||||
binding.subProgress.visibility = View.GONE
|
|
||||||
subscriptionsAdapter = VideosAdapter(
|
subscriptionsAdapter = VideosAdapter(
|
||||||
sortedFeed.toMutableList(),
|
sortedFeed.toMutableList(),
|
||||||
showAllAtOnce = false
|
showAllAtOnce = false
|
||||||
@ -306,30 +297,23 @@ class SubscriptionsFragment : Fragment() {
|
|||||||
false
|
false
|
||||||
)
|
)
|
||||||
|
|
||||||
binding.subChannels.layoutManager = if (legacySubscriptions) {
|
if (legacySubscriptions) {
|
||||||
GridLayoutManager(
|
binding.subChannels.layoutManager = GridLayoutManager(
|
||||||
context,
|
context,
|
||||||
PreferenceHelper.getString(
|
PreferenceHelper.getString(
|
||||||
PreferenceKeys.LEGACY_SUBSCRIPTIONS_COLUMNS,
|
PreferenceKeys.LEGACY_SUBSCRIPTIONS_COLUMNS,
|
||||||
"4"
|
"4"
|
||||||
).toInt()
|
).toInt()
|
||||||
)
|
)
|
||||||
|
binding.subChannels.adapter = LegacySubscriptionAdapter(viewModel.subscriptions.value!!)
|
||||||
} else {
|
} else {
|
||||||
LinearLayoutManager(context)
|
binding.subChannels.layoutManager = LinearLayoutManager(context)
|
||||||
}
|
binding.subChannels.adapter = SubscriptionChannelAdapter(viewModel.subscriptions.value!!.toMutableList())
|
||||||
|
|
||||||
// set the adapter of the subscribed channels
|
|
||||||
binding.subChannels.adapter = if (legacySubscriptions) {
|
|
||||||
LegacySubscriptionAdapter(viewModel.subscriptions.value!!)
|
|
||||||
} else {
|
|
||||||
SubscriptionChannelAdapter(
|
|
||||||
viewModel.subscriptions.value!!.toMutableList()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.subRefresh.isRefreshing = false
|
binding.subRefresh.isRefreshing = false
|
||||||
binding.subProgress.isGone = true
|
binding.subProgress.isGone = true
|
||||||
binding.subFeedContainer.visibility = View.GONE
|
binding.subFeedContainer.isGone = true
|
||||||
|
|
||||||
val notLoaded = viewModel.subscriptions.value.isNullOrEmpty()
|
val notLoaded = viewModel.subscriptions.value.isNullOrEmpty()
|
||||||
binding.subChannelsContainer.isGone = notLoaded
|
binding.subChannelsContainer.isGone = notLoaded
|
||||||
|
@ -1,23 +1,22 @@
|
|||||||
package com.github.libretube.ui.models
|
package com.github.libretube.ui.models
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import com.github.libretube.R
|
||||||
import com.github.libretube.api.SubscriptionHelper
|
import com.github.libretube.api.SubscriptionHelper
|
||||||
import com.github.libretube.api.obj.StreamItem
|
import com.github.libretube.api.obj.StreamItem
|
||||||
import com.github.libretube.api.obj.Subscription
|
import com.github.libretube.api.obj.Subscription
|
||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
import com.github.libretube.extensions.toID
|
import com.github.libretube.extensions.toID
|
||||||
|
import com.github.libretube.extensions.toastFromMainDispatcher
|
||||||
import com.github.libretube.helpers.PreferenceHelper
|
import com.github.libretube.helpers.PreferenceHelper
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class SubscriptionsViewModel : ViewModel() {
|
class SubscriptionsViewModel : ViewModel() {
|
||||||
var errorResponse = MutableLiveData<Boolean>().apply {
|
|
||||||
value = false
|
|
||||||
}
|
|
||||||
|
|
||||||
var videoFeed = MutableLiveData<List<StreamItem>?>().apply {
|
var videoFeed = MutableLiveData<List<StreamItem>?>().apply {
|
||||||
value = null
|
value = null
|
||||||
}
|
}
|
||||||
@ -26,12 +25,12 @@ class SubscriptionsViewModel : ViewModel() {
|
|||||||
value = null
|
value = null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun fetchFeed() {
|
fun fetchFeed(context: Context) {
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
val videoFeed = try {
|
val videoFeed = try {
|
||||||
SubscriptionHelper.getFeed()
|
SubscriptionHelper.getFeed()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
errorResponse.postValue(true)
|
context.toastFromMainDispatcher(R.string.server_error)
|
||||||
Log.e(TAG(), e.toString())
|
Log.e(TAG(), e.toString())
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
@ -43,12 +42,12 @@ class SubscriptionsViewModel : ViewModel() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun fetchSubscriptions() {
|
fun fetchSubscriptions(context: Context) {
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
val subscriptions = try {
|
val subscriptions = try {
|
||||||
SubscriptionHelper.getSubscriptions()
|
SubscriptionHelper.getSubscriptions()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
errorResponse.postValue(true)
|
context.toastFromMainDispatcher(R.string.server_error)
|
||||||
Log.e(TAG(), e.toString())
|
Log.e(TAG(), e.toString())
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user