-
-
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.
prepare character request and bs sync
- Loading branch information
Showing
13 changed files
with
252 additions
and
14 deletions.
There are no files selected for viewing
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,25 @@ | ||
query CharacterQuery($id: Int, $html: Boolean) { | ||
Character(id: $id) { | ||
id, | ||
name { | ||
first | ||
middle | ||
last | ||
full | ||
native | ||
userPreferred | ||
}, | ||
image { | ||
large | ||
medium | ||
}, | ||
description(asHtml:$html) | ||
gender, | ||
dateOfBirth { | ||
year | ||
month | ||
day | ||
}, | ||
bloodType | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...commonMain/graphql/MediaListEntry.graphql → ...nMain/graphql/MediaListEntryQuery.graphql
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
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
112 changes: 112 additions & 0 deletions
112
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/CharacterStateMachine.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,112 @@ | ||
package dev.datlag.aniflow.anilist | ||
|
||
import com.apollographql.apollo3.ApolloClient | ||
import com.apollographql.apollo3.api.Optional | ||
import com.freeletics.flowredux.dsl.FlowReduxStateMachine | ||
import dev.datlag.aniflow.anilist.model.Character | ||
import dev.datlag.aniflow.firebase.FirebaseFactory | ||
import dev.datlag.aniflow.model.CatchResult | ||
import dev.datlag.aniflow.model.mapError | ||
import dev.datlag.aniflow.model.saveFirstOrNull | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class CharacterStateMachine( | ||
private val client: ApolloClient, | ||
private val fallbackClient: ApolloClient, | ||
private val crashlytics: FirebaseFactory.Crashlytics? | ||
) : FlowReduxStateMachine<CharacterStateMachine.State, CharacterStateMachine.Action>( | ||
initialState = currentState | ||
) { | ||
|
||
init { | ||
spec { | ||
inState<State.Waiting> { | ||
onEnterEffect { | ||
currentState = it | ||
} | ||
on<Action.Load> { action, state -> | ||
state.override { State.Loading(action.id) } | ||
} | ||
} | ||
inState<State.Loading> { | ||
onEnterEffect { | ||
currentState = it | ||
} | ||
onEnter { state -> | ||
Cache.getCharacter(state.snapshot.query)?.let { | ||
return@onEnter state.override { State.Success(state.snapshot.query, it) } | ||
} | ||
|
||
val response = CatchResult.repeat(2, timeoutDuration = 30.seconds) { | ||
val query = client.query(state.snapshot.query) | ||
|
||
query.execute().data ?: query.toFlow().saveFirstOrNull()?.dataOrThrow() | ||
}.mapError { | ||
val query = fallbackClient.query(state.snapshot.query) | ||
|
||
query.execute().data ?: query.toFlow().saveFirstOrNull()?.dataOrThrow() | ||
}.mapSuccess<State> { | ||
it.Character?.let { data -> | ||
State.Success(state.snapshot.query, Character(data)) | ||
} | ||
} | ||
|
||
state.override { | ||
response.asSuccess { | ||
crashlytics?.log(it) | ||
|
||
State.Error | ||
} | ||
} | ||
} | ||
} | ||
inState<State.Success> { | ||
onEnterEffect { | ||
Cache.setCharacter(it.query, it.character) | ||
currentState = it | ||
} | ||
on<Action.Load> { action, state -> | ||
state.override { State.Loading(action.id) } | ||
} | ||
} | ||
inState<State.Error> { | ||
onEnterEffect { | ||
currentState = it | ||
} | ||
on<Action.Load> { action, state -> | ||
state.override { State.Loading(action.id) } | ||
} | ||
} | ||
} | ||
} | ||
|
||
sealed interface State { | ||
data object Waiting : State | ||
data class Loading(internal val query: CharacterQuery) : State { | ||
constructor(id: Int) : this( | ||
query = CharacterQuery( | ||
id = Optional.present(id) | ||
) | ||
) | ||
} | ||
data class Success( | ||
internal val query: CharacterQuery, | ||
val character: Character | ||
) : State | ||
data object Error : State | ||
} | ||
|
||
sealed interface Action { | ||
data class Load(val id: Int) : Action | ||
} | ||
|
||
companion object { | ||
var currentState: State | ||
get() = StateSaver.character | ||
set(value) { | ||
StateSaver.character = value | ||
} | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/model/Character.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,17 @@ | ||
package dev.datlag.aniflow.anilist.model | ||
|
||
import dev.datlag.aniflow.anilist.CharacterQuery | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
open class Character( | ||
open val id: Int, | ||
open val gender: String?, | ||
open val bloodType: String?, | ||
) { | ||
constructor(char: CharacterQuery.Character) : this( | ||
id = char.id, | ||
gender = char.gender?.ifBlank { null }, | ||
bloodType = char.bloodType?.ifBlank { null }, | ||
) | ||
} |
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
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
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
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
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
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
Oops, something went wrong.