Skip to content

Commit

Permalink
switch airing and trending to easier repository
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 2, 2024
1 parent b224935 commit 6e0b77c
Show file tree
Hide file tree
Showing 18 changed files with 296 additions and 383 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package dev.datlag.aniflow.anilist

internal data object AdultContent {

fun isAdultContent(schedule: AiringQuery.AiringSchedule): Boolean {
return schedule.media?.isAdult == true || schedule.media?.genresFilterNotNull()?.any {
Genre.exists(it)
} == true
}

sealed interface Genre : CharSequence {
val tag: String

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package dev.datlag.aniflow.anilist

import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.api.Optional
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.type.AiringSort
import kotlinx.coroutines.flow.*
import kotlinx.datetime.Clock
import kotlin.time.Duration.Companion.hours

class AiringTodayRepository(
private val apolloClient: ApolloClient,
private val nsfw: Flow<Boolean> = flowOf(false),
) {

private val page = MutableStateFlow(0)
private val query = combine(page, nsfw.distinctUntilChanged()) { p, n ->
Query(
page = p,
nsfw = n,
)
}

private val airingPreFilter = query.transform {
return@transform emitAll(apolloClient.query(it.toGraphQL()).toFlow())
}
val airing = combine(airingPreFilter, nsfw) { q, n ->
State.fromGraphQL(q.data, n)
}

fun nextPage() = page.getAndUpdate {
it + 1
}

fun previousPage() = page.getAndUpdate {
it - 1
}

private data class Query(
val page: Int,
val nsfw: Boolean
) {
fun toGraphQL() = AiringQuery(
page = Optional.present(page),
perPage = Optional.present(20),
sort = Optional.present(listOf(AiringSort.TIME)),
airingAtGreater = Optional.present(
Clock.System.now().minus(1.hours).epochSeconds.toInt()
),
statusVersion = Optional.present(2),
html = Optional.present(true)
)
}

sealed interface State {
data class Success(
val collection: Collection<AiringQuery.AiringSchedule>
) : State

data object Error : State

companion object {
fun fromGraphQL(data: AiringQuery.Data?, nsfw: Boolean): State {
val airingList = data?.Page?.airingSchedulesFilterNotNull()?.mapNotNull {
if (nsfw) {
it
} else {
if (AdultContent.isAdultContent(it)) {
null
} else {
it
}
}
}

if (airingList.isNullOrEmpty()) {
return Error
}

return Success(airingList)
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import dev.datlag.aniflow.anilist.state.SeasonState
import kotlinx.datetime.Clock

internal object StateSaver {
var trendingAnime: TrendingAnimeStateMachine.State = TrendingAnimeStateMachine.State.Loading(page = 0)
var airing: AiringTodayStateMachine.State = AiringTodayStateMachine.State.Loading(page = 0)
var popularSeason: SeasonState = SeasonState.Loading(page = 0)
var popularNextSeason: SeasonState = run {
val (nextSeason, nextYear) = Clock.System.now().nextSeason
Expand Down

This file was deleted.

Loading

0 comments on commit 6e0b77c

Please sign in to comment.