Skip to content

Commit

Permalink
Finally fix updating countdown clock bugs adn add to tablet UI
Browse files Browse the repository at this point in the history
  • Loading branch information
quickdesh committed Jun 25, 2023
1 parent 278a9d0 commit 60e0208
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, Long>) {
awaitSetNextEpisodeToAir(id, flags.first.toLong().addHexZeros(zeros = 2))
awaitSetNextEpisodeAiringAt(id, flags.second.addHexZeros(zeros = 6))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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),
)
}
}
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -208,6 +209,7 @@ class AnimeInfoScreenModel(
)
fetchFromSourceTasks.awaitAll()
updateSuccessState { it.copy(isRefreshingData = false) }
successState?.let { updateAiringTime(it.anime, it.trackItems, manualFetch) }
}
}

Expand Down Expand Up @@ -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<AnimeTrackItem>, 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
Expand Down Expand Up @@ -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.
Expand Down
38 changes: 16 additions & 22 deletions app/src/main/java/eu/kanade/tachiyomi/util/AniChartApi.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,28 +20,24 @@ class AniChartApi {
private val setAnimeViewerFlags: SetAnimeViewerFlags = Injekt.get()

internal suspend fun loadAiringTime(anime: Anime, trackItems: List<AnimeTrackItem>, useCache: Boolean): Pair<Int, Long> {
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
}
}

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 60e0208

Please sign in to comment.