Skip to content

Commit

Permalink
support apollo cache in state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 25, 2024
1 parent 0887041 commit 27e87fe
Show file tree
Hide file tree
Showing 37 changed files with 911 additions and 1,062 deletions.
12 changes: 9 additions & 3 deletions anilist/src/commonMain/graphql/PageMediaQuery.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ query PageMediaQuery(
$page: Int,
$perPage: Int,
$query: String,
$sort: [MediaSort],
$sort: [MediaSort!],
$year: Int,
$season: MediaSeason,
$preventGenres: [String],
$preventGenres: [String!],
$type: MediaType,
$adultContent: Boolean,
$statusVersion: Int!,
$html: Boolean!,
$wantedGenres: [String!],
$preventIds: [Int!],
$onList: Boolean
) {
Page(page: $page, perPage: $perPage) {
media(
Expand All @@ -19,7 +22,10 @@ query PageMediaQuery(
season: $season,
genre_not_in: $preventGenres,
type: $type,
isAdult: $adultContent
isAdult: $adultContent,
genre_in: $wantedGenres,
id_not_in: $preventIds,
onList: $onList
) {
id,
idMal,
Expand Down
111 changes: 0 additions & 111 deletions anilist/src/commonMain/graphql/RecommendationQuery.graphql

This file was deleted.

12 changes: 12 additions & 0 deletions anilist/src/commonMain/graphql/extra.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extend type User @typePolicy(keyFields: "id")
extend type Media @typePolicy(keyFields: "id")
extend type Character @typePolicy(keyFields: "id")
extend type Staff @typePolicy(keyFields: "id")
extend type Studio @typePolicy(keyFields: "id")
extend type MediaList @typePolicy(keyFields: "id mediaId")

extend type Query @fieldPolicy(forField: "User", keyArgs: "id")
extend type Query @fieldPolicy(forField: "Media", keyArgs: "id")
extend type Query @fieldPolicy(forField: "Character", keyArgs: "id")
extend type Query @fieldPolicy(forField: "Staff", keyArgs: "id")
extend type Query @fieldPolicy(forField: "Studio", keyArgs: "id")
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ import com.apollographql.apollo3.ApolloClient
import com.freeletics.flowredux.dsl.FlowReduxStateMachine
import dev.datlag.aniflow.anilist.model.PageAiringQuery
import dev.datlag.aniflow.anilist.state.HomeAiringState
import dev.datlag.aniflow.anilist.state.HomeDefaultAction
import dev.datlag.aniflow.firebase.FirebaseFactory
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.transformLatest

@OptIn(ExperimentalCoroutinesApi::class)
class AiringTodayStateMachine(
private val client: ApolloClient,
private val fallbackClient: ApolloClient,
private val nsfw: Flow<Boolean> = flowOf(false),
private val crashlytics: FirebaseFactory.Crashlytics?
) : FlowReduxStateMachine<HomeAiringState, HomeDefaultAction>(
initialState = currentState
) {

var currentState: HomeAiringState
Expand All @@ -28,49 +29,25 @@ class AiringTodayStateMachine(
Companion.currentState = value
}

private val query = nsfw.distinctUntilChanged().mapLatest {
PageAiringQuery.Today(
nsfw = it
)
}.distinctUntilChanged()
private val fallbackResponse = fallbackClient.query(PageAiringQuery.Today.toGraphQL()).toFlow()

init {
spec {
inState<HomeAiringState> {
onEnterEffect {
currentState = it
}
collectWhileInState(query) { q, state ->
state.override {
HomeAiringState.Loading(
query = q,
fallback = false
)
}
}
}
inState<HomeAiringState.Loading> {
collectWhileInState(
flowBuilder = {
val usedClient = if (it.fallback) {
fallbackClient
} else {
client
}
private val response = client.query(PageAiringQuery.Today.toGraphQL()).toFlow().transformLatest {
return@transformLatest if (it.hasErrors()) {
emitAll(fallbackResponse)
} else {
emit(it)
}
}

usedClient.query(it.query.toGraphQL()).toFlow()
}
) { response, state ->
state.override {
fromGraphQL(response)
}
}
}
inState<HomeAiringState.Error> {
onEnterEffect {
crashlytics?.log(it.throwable)
}
val airing = combine(
response,
nsfw.distinctUntilChanged()
) { r, n ->
HomeAiringState.fromResponse(n, r).also { state ->
(state as? HomeAiringState.Failure)?.throwable?.let {
crashlytics?.log(it)
}
currentState = state
}
}

Expand Down
Loading

0 comments on commit 27e87fe

Please sign in to comment.