From 1f67644290aeb176e30b7aaf28e6265d02c6d1bc Mon Sep 17 00:00:00 2001 From: Blatzar <46196380+Blatzar@users.noreply.github.com> Date: Fri, 27 Jan 2023 14:54:24 +0100 Subject: [PATCH] Fix crash and sorting methods in library --- .../cloudstream3/syncproviders/SyncAPI.kt | 6 ++++- .../syncproviders/providers/AniListApi.kt | 12 +++++++++- .../syncproviders/providers/LocalList.kt | 11 +++++++++- .../syncproviders/providers/MALApi.kt | 22 ++++++++++++++++++- .../ui/library/LibraryViewModel.kt | 15 +++++-------- .../cloudstream3/ui/search/SearchAdaptor.kt | 4 +--- .../cloudstream3/utils/DataStoreHelper.kt | 1 + 7 files changed, 55 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/SyncAPI.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/SyncAPI.kt index 71ea374d..2f5e0050 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/SyncAPI.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/SyncAPI.kt @@ -118,13 +118,16 @@ interface SyncAPI : OAuth2API { ListSorting.RatingLow -> items.sortedBy { (it.personalRating ?: 0) } ListSorting.AlphabeticalA -> items.sortedBy { it.name } ListSorting.AlphabeticalZ -> items.sortedBy { it.name }.reversed() + ListSorting.UpdatedNew -> items.sortedBy { it.lastUpdatedUnixTime?.times(-1) } + ListSorting.UpdatedOld -> items.sortedBy { it.lastUpdatedUnixTime } else -> items } } } data class LibraryMetadata( - val allLibraryLists: List + val allLibraryLists: List, + val supportedListSorting: Set ) data class LibraryList( @@ -141,6 +144,7 @@ interface SyncAPI : OAuth2API { val episodesTotal: Int?, /** Out of 100 */ val personalRating: Int?, + val lastUpdatedUnixTime: Long?, override val apiName: String, override var type: TvType?, override var posterUrl: String?, diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/AniListApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/AniListApi.kt index ed0c3f78..7d9de43a 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/AniListApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/AniListApi.kt @@ -13,6 +13,7 @@ import com.lagradost.cloudstream3.syncproviders.AccountManager import com.lagradost.cloudstream3.syncproviders.AuthAPI import com.lagradost.cloudstream3.syncproviders.SyncAPI import com.lagradost.cloudstream3.syncproviders.SyncIdName +import com.lagradost.cloudstream3.ui.library.ListSorting import com.lagradost.cloudstream3.ui.result.txt import com.lagradost.cloudstream3.utils.AppUtils.parseJson import com.lagradost.cloudstream3.utils.AppUtils.splitQuery @@ -621,6 +622,7 @@ class AniListApi(index: Int) : AccountManager(index), SyncAPI { this.progress, this.media.episodes, this.score, + this.updatedAt.toLong(), "AniList", TvType.Anime, this.media.coverImage.extraLarge ?: this.media.coverImage.large @@ -678,7 +680,15 @@ class AniListApi(index: Int) : AccountManager(index), SyncAPI { } return SyncAPI.LibraryMetadata( - (baseMap + list).map { SyncAPI.LibraryList(txt(it.key), it.value) } + (baseMap + list).map { SyncAPI.LibraryList(txt(it.key), it.value) }, + setOf( + ListSorting.AlphabeticalA, + ListSorting.AlphabeticalZ, + ListSorting.UpdatedNew, + ListSorting.UpdatedOld, + ListSorting.RatingHigh, + ListSorting.RatingLow, + ) ) } diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/LocalList.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/LocalList.kt index 44384c92..61e9e294 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/LocalList.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/LocalList.kt @@ -6,6 +6,7 @@ import com.lagradost.cloudstream3.syncproviders.AuthAPI import com.lagradost.cloudstream3.syncproviders.SyncAPI import com.lagradost.cloudstream3.syncproviders.SyncIdName import com.lagradost.cloudstream3.ui.WatchType +import com.lagradost.cloudstream3.ui.library.ListSorting import com.lagradost.cloudstream3.ui.result.txt import com.lagradost.cloudstream3.utils.Coroutines.ioWork import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllWatchStateIds @@ -81,7 +82,15 @@ class LocalList : SyncAPI { it.stringRes to emptyList() } return SyncAPI.LibraryMetadata( - (baseMap + list).map { SyncAPI.LibraryList(txt(it.key), it.value) } + (baseMap + list).map { SyncAPI.LibraryList(txt(it.key), it.value) }, + setOf( + ListSorting.AlphabeticalA, + ListSorting.AlphabeticalZ, +// ListSorting.UpdatedNew, +// ListSorting.UpdatedOld, +// ListSorting.RatingHigh, +// ListSorting.RatingLow, + ) ) } diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/MALApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/MALApi.kt index b91c9759..5164b606 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/MALApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/MALApi.kt @@ -16,6 +16,7 @@ import com.lagradost.cloudstream3.syncproviders.AccountManager import com.lagradost.cloudstream3.syncproviders.AuthAPI import com.lagradost.cloudstream3.syncproviders.SyncAPI import com.lagradost.cloudstream3.syncproviders.SyncIdName +import com.lagradost.cloudstream3.ui.library.ListSorting import com.lagradost.cloudstream3.ui.result.txt import com.lagradost.cloudstream3.utils.AppUtils.parseJson import com.lagradost.cloudstream3.utils.AppUtils.splitQuery @@ -285,6 +286,16 @@ class MALApi(index: Int) : AccountManager(index), SyncAPI { else -> MalStatusType.None } } + + private fun parseDateLong(string: String?): Long? { + return try { + SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse( + string ?: return null + )?.time?.div(1000) + } catch (e: Exception) { + null + } + } } override suspend fun handleRedirect(url: String): Boolean { @@ -425,6 +436,7 @@ class MALApi(index: Int) : AccountManager(index), SyncAPI { this.list_status?.num_episodes_watched, this.node.num_episodes, this.list_status?.score?.times(10), + parseDateLong(this.list_status?.updated_at), "MAL", TvType.Anime, this.node.main_picture?.large ?: this.node.main_picture?.medium, @@ -488,7 +500,15 @@ class MALApi(index: Int) : AccountManager(index), SyncAPI { } return SyncAPI.LibraryMetadata( - (baseMap + list).map { SyncAPI.LibraryList(txt(it.key), it.value) } + (baseMap + list).map { SyncAPI.LibraryList(txt(it.key), it.value) }, + setOf( + ListSorting.AlphabeticalA, + ListSorting.AlphabeticalZ, + ListSorting.UpdatedNew, + ListSorting.UpdatedOld, + ListSorting.RatingHigh, + ListSorting.RatingLow, + ) ) } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryViewModel.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryViewModel.kt index d01c699a..14d31356 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryViewModel.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/library/LibraryViewModel.kt @@ -46,16 +46,10 @@ class LibraryViewModel : ViewModel() { val availableApiNames: List get() = availableSyncApis.map { it.name } - val sortingMethods = listOf( - ListSorting.RatingHigh, - ListSorting.RatingLow, -// ListSorting.UpdatedNew, -// ListSorting.UpdatedOld, - ListSorting.AlphabeticalA, - ListSorting.AlphabeticalZ, - ) + var sortingMethods = emptyList() + private set - var currentSortingMethod: ListSorting = sortingMethods.first() + var currentSortingMethod: ListSorting? = sortingMethods.firstOrNull() private set fun switchList(name: String) { @@ -90,6 +84,9 @@ 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 { diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchAdaptor.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchAdaptor.kt index 265489f7..649641c8 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchAdaptor.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/search/SearchAdaptor.kt @@ -68,9 +68,7 @@ class SearchAdapter( cardList.clear() cardList.addAll(newList) - main { - diffResult.dispatchUpdatesTo(this) - } + diffResult.dispatchUpdatesTo(this) } class CardViewHolder diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/DataStoreHelper.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/DataStoreHelper.kt index df6f209d..4e9c646f 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/DataStoreHelper.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/DataStoreHelper.kt @@ -63,6 +63,7 @@ object DataStoreHelper { null, null, null, + null, apiName, type, posterUrl, posterHeaders, quality, id ) }