-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switch airing and trending to easier repository
- Loading branch information
Showing
18 changed files
with
296 additions
and
383 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/AdultContent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/AiringTodayRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
152 changes: 0 additions & 152 deletions
152
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/AiringTodayStateMachine.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 0 additions & 133 deletions
133
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/TrendingAnimeStateMachine.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.