From 137d833d4ae20d6a4e04bcaabe246c49963c71ad Mon Sep 17 00:00:00 2001 From: self-similarity <137652432+self-similarity@users.noreply.github.com> Date: Sat, 28 Oct 2023 22:55:49 +0000 Subject: [PATCH] Improved Simkl autosync and fixed syncing seasons (#722) * Improved simkl autosync and fixed syncing seasons --- .../com/lagradost/cloudstream3/MainAPI.kt | 21 ++++++++ .../syncproviders/providers/SimklApi.kt | 54 +++++++++++-------- .../cloudstream3/ui/player/GeneratorPlayer.kt | 2 +- .../cloudstream3/ui/result/ResultFragment.kt | 8 ++- .../ui/result/ResultViewModel2.kt | 22 ++++++-- 5 files changed, 76 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt b/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt index bfe86224..35a628a3 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt @@ -1465,6 +1465,15 @@ interface EpisodeResponse { var nextAiring: NextAiring? var seasonNames: List? fun getLatestEpisodes(): Map + + /** Count all episodes in all previous seasons up until this episode to get a total count. + * Example: + * Season 1: 10 episodes. + * Season 2: 6 episodes. + * + * getTotalEpisodeIndex(episode = 3, season = 2) -> 10 + 3 = 13 + * */ + fun getTotalEpisodeIndex(episode: Int, season: Int): Int } @JvmName("addSeasonNamesString") @@ -1544,6 +1553,12 @@ data class AnimeLoadResponse( .takeUnless { it == Int.MIN_VALUE } }.toMap() } + + override fun getTotalEpisodeIndex(episode: Int, season: Int): Int { + return this.episodes.maxOf { (_, episodes) -> + episodes.count { ((it.season ?: Int.MIN_VALUE) < season) && it.season != 0 } + } + episode + } } /** @@ -1752,6 +1767,12 @@ data class TvSeriesLoadResponse( .takeUnless { it == Int.MIN_VALUE } return mapOf(DubStatus.None to max) } + + override fun getTotalEpisodeIndex(episode: Int, season: Int): Int { + return episodes.count { + (it.season ?: Int.MIN_VALUE) < season && it.season != 0 + } + episode + } } suspend fun MainAPI.newTvSeriesLoadResponse( diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/SimklApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/SimklApi.kt index bd7979f5..3a37a228 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/SimklApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/SimklApi.kt @@ -376,6 +376,8 @@ class SimklApi(index: Int) : AccountManager(index), SyncAPI { private var status: Int? = null, private var addEpisodes: Pair?, List?>? = null, private var removeEpisodes: Pair?, List?>? = null, + // Required for knowing if the status should be overwritten + private var onList: Boolean = false ) { fun interceptor(interceptor: Interceptor) = apply { this.interceptor = interceptor } fun apiUrl(url: String) = apply { this.url = url } @@ -387,6 +389,7 @@ class SimklApi(index: Int) : AccountManager(index), SyncAPI { } fun status(newStatus: Int?, oldStatus: Int?) = apply { + onList = oldStatus != null // Only set status if its new if (newStatus != oldStatus) { this.status = newStatus @@ -412,6 +415,11 @@ class SimklApi(index: Int) : AccountManager(index), SyncAPI { // Do not add episodes if there is no change if (newEpisodes > (oldEpisodes ?: 0)) { this.addEpisodes = getEpisodes(allEpisodes.take(newEpisodes)) + + // Set to watching if episodes are added and there is no current status + if (!onList) { + status = SimklListStatusType.Watching.value + } } if ((oldEpisodes ?: 0) > newEpisodes) { this.removeEpisodes = getEpisodes(allEpisodes.drop(newEpisodes)) @@ -431,6 +439,28 @@ class SimklApi(index: Int) : AccountManager(index), SyncAPI { interceptor = interceptor ).isSuccessful } else { + val statusResponse = status?.let { setStatus -> + val newStatus = + SimklListStatusType.values() + .firstOrNull { it.value == setStatus }?.originalName + ?: SimklListStatusType.Watching.originalName!! + + app.post( + "${this.url}/sync/add-to-list", + json = StatusRequest( + shows = listOf( + StatusMediaObject( + null, + null, + ids, + newStatus, + ) + ), movies = emptyList() + ), + interceptor = interceptor + ).isSuccessful + } ?: true + val episodeRemovalResponse = removeEpisodes?.let { (seasons, episodes) -> app.post( "${this.url}/sync/history/remove", @@ -472,28 +502,6 @@ class SimklApi(index: Int) : AccountManager(index), SyncAPI { true } - val statusResponse = status?.let { setStatus -> - val newStatus = - SimklListStatusType.values() - .firstOrNull { it.value == setStatus }?.originalName - ?: SimklListStatusType.Watching.originalName!! - - app.post( - "${this.url}/sync/add-to-list", - json = StatusRequest( - shows = listOf( - StatusMediaObject( - null, - null, - ids, - newStatus, - ) - ), movies = emptyList() - ), - interceptor = interceptor - ).isSuccessful - } ?: true - statusResponse && episodeRemovalResponse && historyResponse } } @@ -1051,4 +1059,4 @@ class SimklApi(index: Int) : AccountManager(index), SyncAPI { return true } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt index 1c751897..7c8d975a 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt @@ -1023,7 +1023,7 @@ class GeneratorPlayer : FullScreenPlayer() { ctx.getString(R.string.episode_sync_enabled_key), true ) ) maxEpisodeSet = meta.episode - sync.modifyMaxEpisode(meta.episode) + sync.modifyMaxEpisode(meta.totalEpisodeIndex ?: meta.episode) } } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt index 7617bc11..a1574eec 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt @@ -47,7 +47,9 @@ data class ResultEpisode( /** * Conveys if the episode itself is marked as watched **/ - val videoWatchState: VideoWatchState + val videoWatchState: VideoWatchState, + /** Sum of all previous season episode counts + episode */ + val totalEpisodeIndex: Int? = null, ) fun ResultEpisode.getRealPosition(): Long { @@ -82,6 +84,7 @@ fun buildResultEpisode( isFiller: Boolean? = null, tvType: TvType, parentId: Int, + totalEpisodeIndex: Int? = null, ): ResultEpisode { val posDur = getViewPos(id) val videoWatchState = getVideoWatchState(id) ?: VideoWatchState.None @@ -103,7 +106,8 @@ fun buildResultEpisode( isFiller, tvType, parentId, - videoWatchState + videoWatchState, + totalEpisodeIndex ) } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel2.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel2.kt index 1631b706..d744fac5 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel2.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel2.kt @@ -2215,6 +2215,10 @@ class ResultViewModel2 : ViewModel() { val id = mainId + episode + idIndex * 1_000_000 + (i.season?.times(10_000) ?: 0) + + val totalIndex = + i.season?.let { season -> loadResponse.getTotalEpisodeIndex(episode, season) } + if (!existingEpisodes.contains(id)) { existingEpisodes.add(id) val seasonData = loadResponse.seasonNames.getSeason(i.season) @@ -2234,7 +2238,8 @@ class ResultViewModel2 : ViewModel() { i.description, fillers.getOrDefault(episode, false), loadResponse.type, - mainId + mainId, + totalIndex ) val season = eps.seasonIndex ?: 0 @@ -2263,6 +2268,9 @@ class ResultViewModel2 : ViewModel() { val seasonData = loadResponse.seasonNames.getSeason(episode.season) + val totalIndex = + episode.season?.let { season -> loadResponse.getTotalEpisodeIndex(episodeIndex, season) } + val ep = buildResultEpisode( loadResponse.name, @@ -2279,7 +2287,8 @@ class ResultViewModel2 : ViewModel() { episode.description, null, loadResponse.type, - mainId + mainId, + totalIndex ) val season = ep.seasonIndex ?: 0 @@ -2310,7 +2319,8 @@ class ResultViewModel2 : ViewModel() { null, null, loadResponse.type, - mainId + mainId, + null ) ) } @@ -2332,7 +2342,8 @@ class ResultViewModel2 : ViewModel() { null, null, loadResponse.type, - mainId + mainId, + null ) ) } @@ -2354,7 +2365,8 @@ class ResultViewModel2 : ViewModel() { null, null, loadResponse.type, - mainId + mainId, + null ) ) }