This commit is contained in:
LagradOst 2023-10-07 21:48:24 +02:00
parent f14557fe6a
commit 33eb3a3b29
2 changed files with 25 additions and 9 deletions

View File

@ -12,6 +12,7 @@ import com.lagradost.cloudstream3.mvvm.Resource
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.SyncApis
import com.lagradost.cloudstream3.syncproviders.SyncAPI
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.DataStoreHelper.currentAccount
enum class ListSorting(@StringRes val stringRes: Int) {
@ -30,7 +31,7 @@ class LibraryViewModel : ViewModel() {
private val _pages: MutableLiveData<Resource<List<SyncAPI.Page>>> = MutableLiveData(null)
val pages: LiveData<Resource<List<SyncAPI.Page>>> = _pages
var currentPage : Int = 0
var currentPage: Int = 0
private val _currentApiName: MutableLiveData<String> = MutableLiveData("")
val currentApiName: LiveData<String> = _currentApiName
@ -62,13 +63,21 @@ class LibraryViewModel : ViewModel() {
reloadPages(true)
}
fun sort(method: ListSorting, query: String? = null) {
val currentList = pages.value ?: return
fun sort(method: ListSorting, query: String? = null) = ioSafe {
val value = _pages.value ?: return@ioSafe
if (value is Resource.Success) {
sort(method, query, value.value)
}
}
private fun sort(method: ListSorting, query: String? = null, items: List<SyncAPI.Page>) {
currentSortingMethod = method
(currentList as? Resource.Success)?.value?.forEachIndexed { _, page ->
DataStoreHelper.librarySortingMode = method.ordinal
items.forEach { page ->
page.sort(method, query)
}
_pages.postValue(currentList)
_pages.postValue(Resource.Success(items))
}
fun reloadPages(forceReload: Boolean) {
@ -89,8 +98,6 @@ class LibraryViewModel : ViewModel() {
val library = (libraryResource as? Resource.Success)?.value ?: return@let
sortingMethods = library.supportedListSorting.toList()
currentSortingMethod = null
repo.requireLibraryRefresh = false
val pages = library.allLibraryLists.map {
@ -100,11 +107,18 @@ class LibraryViewModel : ViewModel() {
)
}
_pages.postValue(Resource.Success(pages))
val desiredSortingMethod =
ListSorting.values().getOrNull(DataStoreHelper.librarySortingMode)
if (desiredSortingMethod != null && library.supportedListSorting.contains(desiredSortingMethod)) {
sort(desiredSortingMethod, null, pages)
} else {
// null query = no sorting
sort(ListSorting.Query, null, pages)
}
}
}
}
init {
MainActivity.reloadHomeEvent += ::reloadPages
}

View File

@ -26,6 +26,7 @@ import com.lagradost.cloudstream3.syncproviders.AccountManager
import com.lagradost.cloudstream3.syncproviders.SyncAPI
import com.lagradost.cloudstream3.ui.WatchType
import com.lagradost.cloudstream3.ui.WhoIsWatchingAdapter
import com.lagradost.cloudstream3.ui.library.ListSorting
import com.lagradost.cloudstream3.ui.result.FOCUS_SELF
import com.lagradost.cloudstream3.ui.result.UiImage
import com.lagradost.cloudstream3.ui.result.VideoWatchState
@ -123,6 +124,7 @@ object DataStoreHelper {
var homeBookmarkedList : IntArray by UserPreferenceDelegate("home_bookmarked_last_list", IntArray(0))
var playBackSpeed : Float by UserPreferenceDelegate("playback_speed", 1.0f)
var resizeMode : Int by UserPreferenceDelegate("resize_mode", 0)
var librarySortingMode : Int by UserPreferenceDelegate("library_sorting_mode", ListSorting.AlphabeticalA.ordinal)
data class Account(
@JsonProperty("keyIndex")