From 7c7b3a3e5a9239409be3fa61ce8be959026c4014 Mon Sep 17 00:00:00 2001 From: LagradOst <11805592+LagradOst@users.noreply.github.com> Date: Sun, 19 Jun 2022 01:03:25 +0200 Subject: [PATCH] fixed posters, trailer and anilist/mal + added option for posters --- .../cloudstream3/syncproviders/SyncRepo.kt | 1 + .../syncproviders/providers/AniListApi.kt | 6 +-- .../syncproviders/providers/KitsuApi.kt | 6 ++- .../syncproviders/providers/MALApi.kt | 8 ++-- .../cloudstream3/ui/player/CS3IPlayer.kt | 2 +- .../cloudstream3/ui/result/ResultFragment.kt | 6 +++ .../ui/result/ResultTrailerPlayer.kt | 3 ++ .../cloudstream3/ui/result/ResultViewModel.kt | 20 +++++--- .../cloudstream3/ui/result/SyncViewModel.kt | 27 +++++++++-- app/src/main/res/drawable/kitsu_icon.xml | 15 ++++++ .../main/res/layout/trailer_custom_layout.xml | 48 ++++++++++--------- app/src/main/res/values/strings.xml | 3 ++ app/src/main/res/xml/settins_ui.xml | 5 ++ 13 files changed, 108 insertions(+), 42 deletions(-) create mode 100644 app/src/main/res/drawable/kitsu_icon.xml diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/SyncRepo.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/SyncRepo.kt index daddcc2a..b621e81a 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/SyncRepo.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/SyncRepo.kt @@ -10,6 +10,7 @@ class SyncRepo(private val repo: SyncAPI) { val name = repo.name val icon = repo.icon val mainUrl = repo.mainUrl + val requiresLogin = repo.requiresLogin suspend fun score(id: String, status: SyncAPI.SyncStatus): Resource { return safeApiCall { repo.score(id, status) } 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 9bab9381..471447e8 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 @@ -29,7 +29,7 @@ class AniListApi(index: Int) : AccountManager(index), SyncAPI { override val idPrefix = "anilist" override var mainUrl = "https://anilist.co" override val icon = R.drawable.ic_anilist_icon - override val requiresLogin = true + override val requiresLogin = false override val createAccountUrl = "$mainUrl/signup" override fun loginInfo(): AuthAPI.LoginInfo? { @@ -90,9 +90,9 @@ class AniListApi(index: Int) : AccountManager(index), SyncAPI { } } - override suspend fun getResult(id: String): SyncAPI.SyncResult? { + override suspend fun getResult(id: String): SyncAPI.SyncResult { val internalId = (Regex("anilist\\.co/anime/(\\d*)").find(id)?.groupValues?.getOrNull(1) - ?: id).toIntOrNull() ?: return null + ?: id).toIntOrNull() ?: throw ErrorLoadingException("Invalid internalId") val season = getSeason(internalId).data.Media return SyncAPI.SyncResult( diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/KitsuApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/KitsuApi.kt index 84c540b3..724d7216 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/KitsuApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/KitsuApi.kt @@ -26,10 +26,14 @@ object Kitsu { private val cache: MutableMap, Map> = mutableMapOf() + var isEnabled = true + suspend fun getEpisodesDetails( malId: String?, - anilistId: String? + anilistId: String?, + isResponseRequired: Boolean = true, // overrides isEnabled ): Map? { + if (!isResponseRequired && !isEnabled) return null if (anilistId != null) { try { val map = getKitsuEpisodesDetails(anilistId, "ANILIST_ANIME") 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 28a23731..2654f3de 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 @@ -35,7 +35,7 @@ class MALApi(index: Int) : AccountManager(index), SyncAPI { override var mainUrl = "https://myanimelist.net" val apiUrl = "https://api.myanimelist.net" override val icon = R.drawable.mal_logo - override val requiresLogin = true + override val requiresLogin = false override val createAccountUrl = "$mainUrl/register.php" @@ -189,9 +189,11 @@ class MALApi(index: Int) : AccountManager(index), SyncAPI { val internalId = id.toIntOrNull() ?: return null val url = "$apiUrl/v2/anime/$internalId?fields=id,title,main_picture,alternative_titles,start_date,end_date,synopsis,mean,rank,popularity,num_list_users,num_scoring_users,nsfw,created_at,updated_at,media_type,status,genres,my_list_status,num_episodes,start_season,broadcast,source,average_episode_duration,rating,pictures,background,related_anime,related_manga,recommendations,studios,statistics" + + val auth = getAuth() val res = app.get( - url, headers = mapOf( - "Authorization" to "Bearer " + (getAuth() ?: return null) + url, headers = if (auth == null) emptyMap() else mapOf( + "Authorization" to "Bearer $auth" ) ).text return mapper.readValue(res).let { malAnime -> diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/CS3IPlayer.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/CS3IPlayer.kt index d1f6a993..25ec38e0 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/CS3IPlayer.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/CS3IPlayer.kt @@ -736,7 +736,7 @@ class CS3IPlayer : IPlayer { super.onPlaybackStateChanged(playbackState) when (playbackState) { Player.STATE_READY -> { - requestAutoFocus?.invoke() + } Player.STATE_ENDED -> { handleEvent(CSPlayerEvent.NextEpisode) 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 a787bcf0..0ca7bf63 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 @@ -46,6 +46,7 @@ import com.lagradost.cloudstream3.AcraApplication.Companion.setKey import com.lagradost.cloudstream3.CommonActivity.getCastSession import com.lagradost.cloudstream3.CommonActivity.showToast import com.lagradost.cloudstream3.mvvm.* +import com.lagradost.cloudstream3.syncproviders.providers.Kitsu import com.lagradost.cloudstream3.ui.WatchType import com.lagradost.cloudstream3.ui.download.DOWNLOAD_ACTION_DOWNLOAD import com.lagradost.cloudstream3.ui.download.DOWNLOAD_NAVIGATE_TO @@ -642,6 +643,8 @@ class ResultFragment : ResultTrailerPlayer() { false } result_trailer_loading?.isVisible = isSuccess + result_smallscreen_holder?.isVisible = !isSuccess && !isFullScreenPlayer + result_fullscreen_holder?.isVisible = !isSuccess && isFullScreenPlayer } private fun setTrailers(trailers: List?) { @@ -2114,6 +2117,9 @@ class ResultFragment : ResultTrailerPlayer() { val showFillers = settingsManager.getBoolean(ctx.getString(R.string.show_fillers_key), false) + Kitsu.isEnabled = + settingsManager.getBoolean(ctx.getString(R.string.show_kitsu_posters_key), true) + val tempUrl = url if (tempUrl != null) { result_reload_connectionerror.setOnClickListener { diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultTrailerPlayer.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultTrailerPlayer.kt index b513a4c1..83fedf23 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultTrailerPlayer.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultTrailerPlayer.kt @@ -54,6 +54,9 @@ open class ResultTrailerPlayer : com.lagradost.cloudstream3.ui.player.FullScreen } result_trailer_loading?.isVisible = false + result_smallscreen_holder?.isVisible = !isFullScreenPlayer + result_fullscreen_holder?.isVisible = isFullScreenPlayer + player_background?.apply { isVisible = true layoutParams = diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel.kt index cdeda5b2..ca5d27b2 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel.kt @@ -5,6 +5,7 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import androidx.preference.PreferenceManager import com.lagradost.cloudstream3.* import com.lagradost.cloudstream3.APIHolder.getApiDubstatusSettings import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull @@ -28,6 +29,7 @@ import com.lagradost.cloudstream3.ui.WatchType import com.lagradost.cloudstream3.ui.player.IGenerator import com.lagradost.cloudstream3.ui.player.RepoLinkGenerator import com.lagradost.cloudstream3.ui.player.SubtitleData +import com.lagradost.cloudstream3.ui.search.SearchResultBuilder import com.lagradost.cloudstream3.utils.Coroutines.ioWork import com.lagradost.cloudstream3.utils.DOWNLOAD_HEADER_CACHE import com.lagradost.cloudstream3.utils.DataStoreHelper @@ -160,30 +162,35 @@ class ResultViewModel : ViewModel() { argamap({ addTrailer(meta.trailers) }, { + if (this !is AnimeLoadResponse) return@argamap - val map = getEpisodesDetails(getMalId(), getAniListId()) + val map = getEpisodesDetails(getMalId(), getAniListId(), isResponseRequired = false) if (map.isNullOrEmpty()) return@argamap updateEpisodes = DubStatus.values().map { dubStatus -> val current = - this.episodes[dubStatus]?.sortedBy { it.episode ?: 0 }?.toMutableList() + this.episodes[dubStatus]?.mapIndexed { index, episode -> + episode.apply { + this.episode = this.episode ?: (index + 1) + } + }?.sortedBy { it.episode ?: 0 }?.toMutableList() if (current.isNullOrEmpty()) return@map false - val episodes = current.mapIndexed { index, ep -> ep.episode ?: (index + 1) } + val episodeNumbers = current.map { ep -> ep.episode!! } var updateCount = 0 map.forEach { (episode, node) -> - episodes.binarySearch(episode).let { index -> + episodeNumbers.binarySearch(episode).let { index -> current.getOrNull(index)?.let { currentEp -> current[index] = currentEp.apply { updateCount++ + val currentBack = this this.description = this.description ?: node.description?.en this.name = this.name ?: node.titles?.canonical - this.episode = this.episode ?: node.num ?: episodes[index] + this.episode = this.episode ?: node.num ?: episodeNumbers[index] this.posterUrl = this.posterUrl ?: node.thumbnail?.original?.url } } } } this.episodes[dubStatus] = current - updateCount > 0 }.any { it } }) @@ -432,6 +439,7 @@ class ResultViewModel : ViewModel() { res[dubStatus]?.let { episodes -> updateEpisodes(mainId, episodes, -1) } + _dubStatus.postValue(dubStatus) _dubSubSelections.postValue(loadResponse.episodes.keys) } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/SyncViewModel.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/SyncViewModel.kt index 81b1e1d8..cc42cc8d 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/SyncViewModel.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/SyncViewModel.kt @@ -13,6 +13,7 @@ import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.malApi import com.lagradost.cloudstream3.syncproviders.SyncAPI import com.lagradost.cloudstream3.utils.SyncUtil import kotlinx.coroutines.launch +import java.util.* data class CurrentSynced( @@ -178,7 +179,12 @@ class SyncViewModel : ViewModel() { fun modifyMaxEpisode(episodeNum: Int) { Log.i(TAG, "modifyMaxEpisode = $episodeNum") modifyData { status -> - status.copy(watchedEpisodes = maxOf(episodeNum, status.watchedEpisodes ?: return@modifyData null)) + status.copy( + watchedEpisodes = maxOf( + episodeNum, + status.watchedEpisodes ?: return@modifyData null + ) + ) } } @@ -194,7 +200,7 @@ class SyncViewModel : ViewModel() { Log.i(TAG, "modifyData ${repo.name} => $newData") repo.score(id, newData) } - } else if (result is Resource.Failure){ + } else if (result is Resource.Failure) { Log.e(TAG, "modifyData getStatus error ${result.errorString}") } } @@ -228,15 +234,26 @@ class SyncViewModel : ViewModel() { _metaResponse.postValue(Resource.Loading()) var lastError: Resource = Resource.Failure(false, null, null, "No data") - syncs.forEach { (prefix, id) -> + val current = syncs.toList() + + // shitty way to sort anilist first, as it has trailers while mal does not + if (syncs.containsKey(aniListApi.idPrefix)) { + Collections.swap(current, current.indexOfFirst { it.first == aniListApi.idPrefix }, 0) + } + + current.forEach { (prefix, id) -> repos.firstOrNull { it.idPrefix == prefix }?.let { repo -> - if (repo.hasAccount()) { + if (!repo.requiresLogin || repo.hasAccount()) { + Log.i(TAG, "updateMetadata loading ${repo.idPrefix}") val result = repo.getResult(id) if (result is Resource.Success) { _metaResponse.postValue(result) return@launch } else if (result is Resource.Failure) { - Log.e(TAG, "updateMetadata error ${result.errorString}") + Log.e( + TAG, + "updateMetadata error $id at ${repo.idPrefix} ${result.errorString}" + ) lastError = result } } diff --git a/app/src/main/res/drawable/kitsu_icon.xml b/app/src/main/res/drawable/kitsu_icon.xml new file mode 100644 index 00000000..4692fad4 --- /dev/null +++ b/app/src/main/res/drawable/kitsu_icon.xml @@ -0,0 +1,15 @@ + + + + diff --git a/app/src/main/res/layout/trailer_custom_layout.xml b/app/src/main/res/layout/trailer_custom_layout.xml index ac155ad1..22137a1c 100644 --- a/app/src/main/res/layout/trailer_custom_layout.xml +++ b/app/src/main/res/layout/trailer_custom_layout.xml @@ -8,6 +8,31 @@ android:screenOrientation="landscape" tools:orientation="vertical"> + + + + + + + + + - - - - - - - display_sub_key show_fillers_key show_trailers_key + show_kitsu_posters_key random_button_key provider_lang_key dns_key @@ -252,6 +253,8 @@ Sends no data Show filler episode for anime Show trailers + Show posters from kitsu + Show app updates Automatically search for new updates on start Update to prereleases diff --git a/app/src/main/res/xml/settins_ui.xml b/app/src/main/res/xml/settins_ui.xml index cbb0a654..f670bc42 100644 --- a/app/src/main/res/xml/settins_ui.xml +++ b/app/src/main/res/xml/settins_ui.xml @@ -34,6 +34,11 @@ android:icon="@drawable/baseline_theaters_24" android:title="@string/show_trailers_settings" android:defaultValue="true" /> +