Fix crash and sorting methods in library

This commit is contained in:
Blatzar 2023-01-27 14:54:24 +01:00
parent 0aca996bc0
commit 1f67644290
7 changed files with 55 additions and 16 deletions

View file

@ -118,13 +118,16 @@ interface SyncAPI : OAuth2API {
ListSorting.RatingLow -> items.sortedBy { (it.personalRating ?: 0) } ListSorting.RatingLow -> items.sortedBy { (it.personalRating ?: 0) }
ListSorting.AlphabeticalA -> items.sortedBy { it.name } ListSorting.AlphabeticalA -> items.sortedBy { it.name }
ListSorting.AlphabeticalZ -> items.sortedBy { it.name }.reversed() ListSorting.AlphabeticalZ -> items.sortedBy { it.name }.reversed()
ListSorting.UpdatedNew -> items.sortedBy { it.lastUpdatedUnixTime?.times(-1) }
ListSorting.UpdatedOld -> items.sortedBy { it.lastUpdatedUnixTime }
else -> items else -> items
} }
} }
} }
data class LibraryMetadata( data class LibraryMetadata(
val allLibraryLists: List<LibraryList> val allLibraryLists: List<LibraryList>,
val supportedListSorting: Set<ListSorting>
) )
data class LibraryList( data class LibraryList(
@ -141,6 +144,7 @@ interface SyncAPI : OAuth2API {
val episodesTotal: Int?, val episodesTotal: Int?,
/** Out of 100 */ /** Out of 100 */
val personalRating: Int?, val personalRating: Int?,
val lastUpdatedUnixTime: Long?,
override val apiName: String, override val apiName: String,
override var type: TvType?, override var type: TvType?,
override var posterUrl: String?, override var posterUrl: String?,

View file

@ -13,6 +13,7 @@ import com.lagradost.cloudstream3.syncproviders.AccountManager
import com.lagradost.cloudstream3.syncproviders.AuthAPI import com.lagradost.cloudstream3.syncproviders.AuthAPI
import com.lagradost.cloudstream3.syncproviders.SyncAPI import com.lagradost.cloudstream3.syncproviders.SyncAPI
import com.lagradost.cloudstream3.syncproviders.SyncIdName import com.lagradost.cloudstream3.syncproviders.SyncIdName
import com.lagradost.cloudstream3.ui.library.ListSorting
import com.lagradost.cloudstream3.ui.result.txt import com.lagradost.cloudstream3.ui.result.txt
import com.lagradost.cloudstream3.utils.AppUtils.parseJson import com.lagradost.cloudstream3.utils.AppUtils.parseJson
import com.lagradost.cloudstream3.utils.AppUtils.splitQuery import com.lagradost.cloudstream3.utils.AppUtils.splitQuery
@ -621,6 +622,7 @@ class AniListApi(index: Int) : AccountManager(index), SyncAPI {
this.progress, this.progress,
this.media.episodes, this.media.episodes,
this.score, this.score,
this.updatedAt.toLong(),
"AniList", "AniList",
TvType.Anime, TvType.Anime,
this.media.coverImage.extraLarge ?: this.media.coverImage.large this.media.coverImage.extraLarge ?: this.media.coverImage.large
@ -678,7 +680,15 @@ class AniListApi(index: Int) : AccountManager(index), SyncAPI {
} }
return SyncAPI.LibraryMetadata( 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,
)
) )
} }

View file

@ -6,6 +6,7 @@ import com.lagradost.cloudstream3.syncproviders.AuthAPI
import com.lagradost.cloudstream3.syncproviders.SyncAPI import com.lagradost.cloudstream3.syncproviders.SyncAPI
import com.lagradost.cloudstream3.syncproviders.SyncIdName import com.lagradost.cloudstream3.syncproviders.SyncIdName
import com.lagradost.cloudstream3.ui.WatchType import com.lagradost.cloudstream3.ui.WatchType
import com.lagradost.cloudstream3.ui.library.ListSorting
import com.lagradost.cloudstream3.ui.result.txt import com.lagradost.cloudstream3.ui.result.txt
import com.lagradost.cloudstream3.utils.Coroutines.ioWork import com.lagradost.cloudstream3.utils.Coroutines.ioWork
import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllWatchStateIds import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllWatchStateIds
@ -81,7 +82,15 @@ class LocalList : SyncAPI {
it.stringRes to emptyList<SyncAPI.LibraryItem>() it.stringRes to emptyList<SyncAPI.LibraryItem>()
} }
return SyncAPI.LibraryMetadata( 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,
)
) )
} }

View file

@ -16,6 +16,7 @@ import com.lagradost.cloudstream3.syncproviders.AccountManager
import com.lagradost.cloudstream3.syncproviders.AuthAPI import com.lagradost.cloudstream3.syncproviders.AuthAPI
import com.lagradost.cloudstream3.syncproviders.SyncAPI import com.lagradost.cloudstream3.syncproviders.SyncAPI
import com.lagradost.cloudstream3.syncproviders.SyncIdName import com.lagradost.cloudstream3.syncproviders.SyncIdName
import com.lagradost.cloudstream3.ui.library.ListSorting
import com.lagradost.cloudstream3.ui.result.txt import com.lagradost.cloudstream3.ui.result.txt
import com.lagradost.cloudstream3.utils.AppUtils.parseJson import com.lagradost.cloudstream3.utils.AppUtils.parseJson
import com.lagradost.cloudstream3.utils.AppUtils.splitQuery import com.lagradost.cloudstream3.utils.AppUtils.splitQuery
@ -285,6 +286,16 @@ class MALApi(index: Int) : AccountManager(index), SyncAPI {
else -> MalStatusType.None 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 { 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.list_status?.num_episodes_watched,
this.node.num_episodes, this.node.num_episodes,
this.list_status?.score?.times(10), this.list_status?.score?.times(10),
parseDateLong(this.list_status?.updated_at),
"MAL", "MAL",
TvType.Anime, TvType.Anime,
this.node.main_picture?.large ?: this.node.main_picture?.medium, this.node.main_picture?.large ?: this.node.main_picture?.medium,
@ -488,7 +500,15 @@ class MALApi(index: Int) : AccountManager(index), SyncAPI {
} }
return SyncAPI.LibraryMetadata( 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,
)
) )
} }

View file

@ -46,16 +46,10 @@ class LibraryViewModel : ViewModel() {
val availableApiNames: List<String> val availableApiNames: List<String>
get() = availableSyncApis.map { it.name } get() = availableSyncApis.map { it.name }
val sortingMethods = listOf( var sortingMethods = emptyList<ListSorting>()
ListSorting.RatingHigh, private set
ListSorting.RatingLow,
// ListSorting.UpdatedNew,
// ListSorting.UpdatedOld,
ListSorting.AlphabeticalA,
ListSorting.AlphabeticalZ,
)
var currentSortingMethod: ListSorting = sortingMethods.first() var currentSortingMethod: ListSorting? = sortingMethods.firstOrNull()
private set private set
fun switchList(name: String) { fun switchList(name: String) {
@ -90,6 +84,9 @@ 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()
currentSortingMethod = null
repo.requireLibraryRefresh = false repo.requireLibraryRefresh = false
val pages = library.allLibraryLists.map { val pages = library.allLibraryLists.map {

View file

@ -68,9 +68,7 @@ class SearchAdapter(
cardList.clear() cardList.clear()
cardList.addAll(newList) cardList.addAll(newList)
main { diffResult.dispatchUpdatesTo(this)
diffResult.dispatchUpdatesTo(this)
}
} }
class CardViewHolder class CardViewHolder

View file

@ -63,6 +63,7 @@ object DataStoreHelper {
null, null,
null, null,
null, null,
null,
apiName, type, posterUrl, posterHeaders, quality, id apiName, type, posterUrl, posterHeaders, quality, id
) )
} }