From 60e02088157a253a8eb41ad525089453fc411e97 Mon Sep 17 00:00:00 2001 From: Quickdesh Date: Sun, 25 Jun 2023 21:22:55 +0530 Subject: [PATCH] Finally fix updating countdown clock bugs adn add to tablet UI --- .../anime/interactor/SetAnimeViewerFlags.kt | 1 - .../presentation/entries/anime/AnimeScreen.kt | 36 ++++++++++++++---- .../ui/entries/anime/AnimeScreenModel.kt | 14 ++++--- .../eu/kanade/tachiyomi/util/AniChartApi.kt | 38 ++++++++----------- 4 files changed, 54 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/eu/kanade/domain/entries/anime/interactor/SetAnimeViewerFlags.kt b/app/src/main/java/eu/kanade/domain/entries/anime/interactor/SetAnimeViewerFlags.kt index 1fa1b70c9e..2937f867a9 100644 --- a/app/src/main/java/eu/kanade/domain/entries/anime/interactor/SetAnimeViewerFlags.kt +++ b/app/src/main/java/eu/kanade/domain/entries/anime/interactor/SetAnimeViewerFlags.kt @@ -19,7 +19,6 @@ class SetAnimeViewerFlags( ) } - // TODO: Add Aired at, completed anime support, and fix for user re entering anime screen suspend fun awaitSetNextEpisodeAiring(id: Long, flags: Pair) { awaitSetNextEpisodeToAir(id, flags.first.toLong().addHexZeros(zeros = 2)) awaitSetNextEpisodeAiringAt(id, flags.second.addHexZeros(zeros = 6)) diff --git a/app/src/main/java/eu/kanade/presentation/entries/anime/AnimeScreen.kt b/app/src/main/java/eu/kanade/presentation/entries/anime/AnimeScreen.kt index 3446f549e6..ada1dadff3 100644 --- a/app/src/main/java/eu/kanade/presentation/entries/anime/AnimeScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/entries/anime/AnimeScreen.kt @@ -85,7 +85,6 @@ import tachiyomi.presentation.core.util.isScrollingUp import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get import java.text.DateFormat -import java.util.Calendar import java.util.Date import java.util.concurrent.TimeUnit @@ -425,10 +424,8 @@ private fun AnimeScreenSmallImpl( key = EntryScreenItem.AIRING_TIME, contentType = EntryScreenItem.AIRING_TIME, ) { - val time = state.airingTime - .times(1000L) - .minus(Calendar.getInstance().timeInMillis) - var timer by remember { mutableStateOf(time) } + // Handles the second by second countdown + var timer by remember { mutableStateOf(state.airingTime) } LaunchedEffect(key1 = timer) { if (timer > 0L) { delay(1000L) @@ -439,9 +436,9 @@ private fun AnimeScreenSmallImpl( NextEpisodeAiringListItem( title = stringResource( R.string.display_mode_episode, - episodeDecimalFormat.format(state.airingEpisodeNumber.toDouble()), + episodeDecimalFormat.format(state.airingEpisodeNumber), ), - date = formatTime(timer, useDayFormat = true), + date = formatTime(state.airingTime, useDayFormat = true), ) } } @@ -664,6 +661,31 @@ fun AnimeScreenLargeImpl( ) } + if (state.airingTime > 0L) { + item( + key = EntryScreenItem.AIRING_TIME, + contentType = EntryScreenItem.AIRING_TIME, + ) { + // Handles the second by second countdown + var timer by remember { mutableStateOf(state.airingTime) } + LaunchedEffect(key1 = timer) { + if (timer > 0L) { + delay(1000L) + timer -= 1000L + } + } + if (timer > 0L) { + NextEpisodeAiringListItem( + title = stringResource( + R.string.display_mode_episode, + episodeDecimalFormat.format(state.airingEpisodeNumber), + ), + date = formatTime(state.airingTime, useDayFormat = true), + ) + } + } + } + sharedEpisodeItems( anime = state.anime, episodes = episodes, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/entries/anime/AnimeScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/entries/anime/AnimeScreenModel.kt index e4bdfa17c3..b4889a5b1c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/entries/anime/AnimeScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/entries/anime/AnimeScreenModel.kt @@ -79,6 +79,7 @@ import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get import java.text.DecimalFormat import java.text.DecimalFormatSymbols +import java.util.Calendar class AnimeInfoScreenModel( val context: Context, @@ -208,6 +209,7 @@ class AnimeInfoScreenModel( ) fetchFromSourceTasks.awaitAll() updateSuccessState { it.copy(isRefreshingData = false) } + successState?.let { updateAiringTime(it.anime, it.trackItems, manualFetch) } } } @@ -913,14 +915,14 @@ class AnimeInfoScreenModel( .distinctUntilChanged() .collectLatest { trackItems -> updateSuccessState { it.copy(trackItems = trackItems) } - updateAiringTime(anime, trackItems, useCache = false) + updateAiringTime(anime, trackItems, useCache = true) } } } private suspend fun updateAiringTime(anime: Anime, trackItems: List, useCache: Boolean) { - val airingTime = AniChartApi().loadAiringTime(anime, trackItems, useCache) - updateSuccessState { it.copy(nextAiringEpisode = airingTime) } + val airingEpisode = AniChartApi().loadAiringTime(anime, trackItems, useCache) + updateSuccessState { it.copy(nextAiringEpisode = airingEpisode) } } // Track sheet - end @@ -1026,9 +1028,11 @@ sealed class AnimeScreenState { val trackingCount: Int get() = trackItems.count { it.track != null } - val airingEpisodeNumber = nextAiringEpisode.first + val airingEpisodeNumber: Double + get() = nextAiringEpisode.first.toDouble() - val airingTime = nextAiringEpisode.second + val airingTime: Long + get() = nextAiringEpisode.second.times(1000L).minus(Calendar.getInstance().timeInMillis) /** * Applies the view filters to the list of chapters obtained from the database. diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/AniChartApi.kt b/app/src/main/java/eu/kanade/tachiyomi/util/AniChartApi.kt index 064b97dddb..34333c1d10 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/AniChartApi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/AniChartApi.kt @@ -1,7 +1,6 @@ package eu.kanade.tachiyomi.util - -import android.util.Log import eu.kanade.domain.entries.anime.interactor.SetAnimeViewerFlags +import eu.kanade.tachiyomi.animesource.model.SAnime import eu.kanade.tachiyomi.data.track.anilist.Anilist import eu.kanade.tachiyomi.data.track.myanimelist.MyAnimeList import eu.kanade.tachiyomi.network.POST @@ -21,28 +20,24 @@ class AniChartApi { private val setAnimeViewerFlags: SetAnimeViewerFlags = Injekt.get() internal suspend fun loadAiringTime(anime: Anime, trackItems: List, useCache: Boolean): Pair { - if (useCache) { - Log.i("YESSIR F cache", "THEY ${anime.nextEpisodeToAir} YES ${anime.nextEpisodeAiringAt}") - return Pair(anime.nextEpisodeToAir, anime.nextEpisodeAiringAt) - } else { - return withIOContext { - var alId: Long? = 0L - var airingTime = Pair(0, 0L) - trackItems.forEach { - if (it.track != null) { - alId = when (it.service) { - is Anilist -> it.track.media_id - is MyAnimeList -> getAlIdFromMal(it.track.media_id) - else -> null - } + if (anime.status == SAnime.COMPLETED.toLong() && useCache) return Pair(anime.nextEpisodeToAir, anime.nextEpisodeAiringAt) + return withIOContext { + var alId = 0L + var airingTime = Pair(0, 0L) + trackItems.forEach { + if (it.track != null) { + alId = when (it.service) { + is Anilist -> it.track.media_id + is MyAnimeList -> getAlIdFromMal(it.track.media_id) + else -> 0L } } - if (alId != null) { - airingTime = getAiringAt(alId!!) - setAnimeViewerFlags.awaitSetNextEpisodeAiring(anime.id, airingTime) - } - return@withIOContext airingTime } + if (alId != 0L) { + airingTime = getAiringAt(alId) + setAnimeViewerFlags.awaitSetNextEpisodeAiring(anime.id, airingTime) + } + return@withIOContext airingTime } } @@ -97,7 +92,6 @@ class AniChartApi { return@withIOContext Pair(0, 0L) } val data = response.body.string() - Log.i("HERROBRO", data.substringAfter("episode\":").substringBefore(",")) val episodeNumber = data.substringAfter("episode\":").substringBefore(",").toIntOrNull() ?: 0 val airingAt = data.substringAfter("airingAt\":").substringBefore("}").toLongOrNull() ?: 0L