Skip to content

Commit

Permalink
change airing loading
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Mar 28, 2024
1 parent 85cf887 commit 7243848
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import dev.datlag.aniflow.anilist.type.AiringSort
import dev.datlag.aniflow.firebase.FirebaseFactory
import dev.datlag.aniflow.model.CatchResult
import dev.datlag.tooling.async.suspendCatching
import dev.datlag.tooling.safeSubList
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
import kotlinx.datetime.atStartOfDayIn
import kotlinx.datetime.toLocalDateTime
import kotlin.time.Duration.Companion.hours

@OptIn(ExperimentalCoroutinesApi::class)
class AiringTodayStateMachine(
Expand All @@ -29,25 +31,12 @@ class AiringTodayStateMachine(
currentState = it
}
onEnter { state ->
val query = AiringQuery(
page = Optional.present(state.snapshot.page),
perPage = Optional.present(state.snapshot.perPage),
sort = Optional.present(listOf(AiringSort.TIME)),
airingAtGreater = Optional.present(
Clock.System.now().toLocalDateTime(
TimeZone.currentSystemDefault()
).date.atStartOfDayIn(
TimeZone.currentSystemDefault()
).epochSeconds.toInt()
)
)

Cache.getAiring(query)?.let {
Cache.getAiring(state.snapshot.query)?.let {
return@onEnter state.override { State.Success(query, it) }
}

val response = CatchResult.result {
client.query(query).execute().dataOrThrow()
client.query(state.snapshot.query).execute().dataOrThrow()
}.mapSuccess<State> {
val wantedContent = if (!state.snapshot.adultContent) {
val content = it.Page?.airingSchedulesFilterNotNull() ?: emptyList()
Expand All @@ -66,10 +55,10 @@ class AiringTodayStateMachine(
}

State.Success(
query,
state.snapshot.query,
it.copy(
Page = it.Page?.copy(
airingSchedules = wantedContent
airingSchedules = wantedContent.safeSubList(0, 10)
)
)
)
Expand All @@ -79,7 +68,7 @@ class AiringTodayStateMachine(
response.asSuccess {
crashlytics?.log(it)

State.Error(state.snapshot.page, state.snapshot.perPage, state.snapshot.adultContent)
State.Error(state.snapshot.query, state.snapshot.adultContent)
}
}
}
Expand All @@ -97,7 +86,7 @@ class AiringTodayStateMachine(
}
on<Action.Retry> { _, state ->
state.override {
State.Loading(state.snapshot.page, state.snapshot.perPage, state.snapshot.adultContent)
State.Loading(state.snapshot.query, state.snapshot.adultContent)
}
}
}
Expand All @@ -106,20 +95,34 @@ class AiringTodayStateMachine(

sealed interface State {
data class Loading(
val page: Int,
val perPage: Int = 10,
val adultContent: Boolean = false,
) : State
internal val query: AiringQuery,
val adultContent: Boolean = false
) : State {
constructor(
page: Int,
perPage: Int = 20,
adultContent: Boolean = false
) : this(
query = AiringQuery(
page = Optional.present(page),
perPage = Optional.present(perPage),
sort = Optional.present(listOf(AiringSort.TIME)),
airingAtGreater = Optional.present(
Clock.System.now().minus(1.hours).epochSeconds.toInt()
)
),
adultContent = adultContent
)
}

data class Success(
internal val query: AiringQuery,
val data: AiringQuery.Data
) : State

data class Error(
val page: Int,
val perPage: Int = 10,
val adultContent: Boolean = false,
internal val query: AiringQuery,
val adultContent: Boolean = false
) : State
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private fun SuccessContent(
items(data, key = { it.episode to it.media?.id }) { media ->
AiringCard(
airing = media,
modifier = Modifier.size(width = 300.dp, height = 150.dp),
modifier = Modifier.height(150.dp).fillParentMaxWidth(fraction = 0.9F),
onClick = onClick
)
}
Expand Down

0 comments on commit 7243848

Please sign in to comment.