Maintain library sort filter

This commit is contained in:
Luna712 2023-10-06 18:25:22 -06:00
parent 0a327ccbda
commit 10a3ba0a70
2 changed files with 32 additions and 10 deletions

View file

@ -39,6 +39,10 @@ import com.lagradost.cloudstream3.utils.DataStoreHelper.currentAccount
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialog import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialog
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
import com.lagradost.cloudstream3.utils.UIHelper.getSpanCount import com.lagradost.cloudstream3.utils.UIHelper.getSpanCount
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.math.abs import kotlin.math.abs
const val LIBRARY_FOLDER = "library_folder" const val LIBRARY_FOLDER = "library_folder"
@ -336,11 +340,8 @@ class LibraryFragment : Fragment() {
observe(libraryViewModel.pages) { resource -> observe(libraryViewModel.pages) { resource ->
when (resource) { when (resource) {
is Resource.Success -> { is Resource.Success -> {
handler.removeCallbacks(startLoading)
val pages = resource.value val pages = resource.value
val showNotice = pages.all { it.items.isEmpty() } val showNotice = pages.all { it.items.isEmpty() }
binding?.apply { binding?.apply {
emptyListTextview.isVisible = showNotice emptyListTextview.isVisible = showNotice
if (showNotice) { if (showNotice) {
@ -359,7 +360,7 @@ class LibraryFragment : Fragment() {
) )
// Only stop loading after 300ms to hide the fade effect the viewpager produces when updating // Only stop loading after 300ms to hide the fade effect the viewpager produces when updating
// Without this there would be a flashing effect: // Without this, there would be a flashing effect:
// loading -> show old viewpager -> black screen -> show new viewpager // loading -> show old viewpager -> black screen -> show new viewpager
handler.postDelayed(stopLoading, 300) handler.postDelayed(stopLoading, 300)
@ -370,7 +371,7 @@ class LibraryFragment : Fragment() {
savedInstanceState.putInt(VIEWPAGER_ITEM_KEY, -1) savedInstanceState.putInt(VIEWPAGER_ITEM_KEY, -1)
} }
// Since the animation to scroll multiple items is so much its better to just hide // Since the animation to scroll multiple items is so much it's better to just hide
// the viewpager a bit while the fastest animation is running // the viewpager a bit while the fastest animation is running
fun hideViewpager(distance: Int) { fun hideViewpager(distance: Int) {
if (distance < 3) return if (distance < 3) return
@ -388,6 +389,21 @@ class LibraryFragment : Fragment() {
viewpager.startAnimation(showAnimation) viewpager.startAnimation(showAnimation)
} }
CoroutineScope(Dispatchers.Default).launch {
// Perform sorting in the background
libraryViewModel.currentSortingMethod?.let { sortingMethod ->
val currentList = pages
currentList.forEachIndexed { _, page ->
page.sort(sortingMethod)
}
// Update the UI on the main thread
withContext(Dispatchers.Main) {
(viewpager.adapter as? ViewpagerAdapter)?.pages = currentList
viewpager.adapter?.notifyDataSetChanged()
}
}
}
TabLayoutMediator( TabLayoutMediator(
libraryTabLayout, libraryTabLayout,
viewpager, viewpager,
@ -402,12 +418,10 @@ class LibraryFragment : Fragment() {
}.attach() }.attach()
} }
} }
is Resource.Loading -> { is Resource.Loading -> {
// Only start loading after 200ms to prevent loading cached lists // Only start loading after 200ms to prevent loading cached lists
handler.postDelayed(startLoading, 200) handler.postDelayed(startLoading, 200)
} }
is Resource.Failure -> { is Resource.Failure -> {
stopLoading.run() stopLoading.run()
// No user indication it failed :( // No user indication it failed :(

View file

@ -25,6 +25,7 @@ enum class ListSorting(@StringRes val stringRes: Int) {
} }
const val LAST_SYNC_API_KEY = "last_sync_api" const val LAST_SYNC_API_KEY = "last_sync_api"
const val SORTING_METHOD_KEY = "sorting_method"
class LibraryViewModel : ViewModel() { class LibraryViewModel : ViewModel() {
private val _pages: MutableLiveData<Resource<List<SyncAPI.Page>>> = MutableLiveData(null) private val _pages: MutableLiveData<Resource<List<SyncAPI.Page>>> = MutableLiveData(null)
@ -51,8 +52,15 @@ class LibraryViewModel : ViewModel() {
var sortingMethods = emptyList<ListSorting>() var sortingMethods = emptyList<ListSorting>()
private set private set
var currentSortingMethod: ListSorting? = sortingMethods.firstOrNull() var currentSortingMethod: ListSorting? = null
private set private set(value) {
field = value
setKey("$currentAccount/$SORTING_METHOD_KEY", value?.name)
}
get() {
val methodName = getKey<String>("$currentAccount/$SORTING_METHOD_KEY")
return sortingMethods.firstOrNull { it.name == methodName }
}
fun switchList(name: String) { fun switchList(name: String) {
currentSyncApi = availableSyncApis[availableApiNames.indexOf(name)] currentSyncApi = availableSyncApis[availableApiNames.indexOf(name)]
@ -87,7 +95,7 @@ class LibraryViewModel : ViewModel() {
val library = (libraryResource as? Resource.Success)?.value ?: return@let val library = (libraryResource as? Resource.Success)?.value ?: return@let
sortingMethods = library.supportedListSorting.toList() sortingMethods = library.supportedListSorting.toList()
currentSortingMethod = null //currentSortingMethod = null
repo.requireLibraryRefresh = false repo.requireLibraryRefresh = false