-
-
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.
initial repository re-write with flowredux
- Loading branch information
Showing
22 changed files
with
579 additions
and
680 deletions.
There are no files selected for viewing
20 changes: 12 additions & 8 deletions
20
.../commonMain/graphql/TrendingQuery.graphql → ...commonMain/graphql/PageMediaQuery.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 was deleted.
Oops, something went wrong.
110 changes: 0 additions & 110 deletions
110
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/PopularNextSeasonRepository.kt
This file was deleted.
Oops, something went wrong.
98 changes: 98 additions & 0 deletions
98
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/PopularNextSeasonStateMachine.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,98 @@ | ||
package dev.datlag.aniflow.anilist | ||
|
||
import com.apollographql.apollo3.ApolloClient | ||
import com.freeletics.flowredux.dsl.FlowReduxStateMachine | ||
import dev.datlag.aniflow.anilist.model.PageMediaQuery | ||
import dev.datlag.aniflow.anilist.state.HomeDefaultAction | ||
import dev.datlag.aniflow.anilist.state.HomeDefaultState | ||
import dev.datlag.aniflow.anilist.type.MediaType | ||
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.mapLatest | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class PopularNextSeasonStateMachine( | ||
private val client: ApolloClient, | ||
private val fallbackClient: ApolloClient, | ||
private val nsfw: Flow<Boolean>, | ||
private val viewManga: Flow<Boolean>, | ||
private val crashlytics: FirebaseFactory.Crashlytics? | ||
) : FlowReduxStateMachine<HomeDefaultState, HomeDefaultAction>( | ||
initialState = currentState | ||
) { | ||
|
||
var currentState: HomeDefaultState | ||
get() = Companion.currentState | ||
private set(value) { | ||
Companion.currentState = value | ||
} | ||
|
||
private val type = viewManga.mapLatest { | ||
if (it) { | ||
MediaType.MANGA | ||
} else { | ||
MediaType.ANIME | ||
} | ||
}.distinctUntilChanged() | ||
|
||
private val query = combine( | ||
type, | ||
nsfw.distinctUntilChanged() | ||
) { t, n -> | ||
PageMediaQuery.PopularNextSeason( | ||
type = t, | ||
nsfw = n | ||
) | ||
}.distinctUntilChanged() | ||
|
||
init { | ||
spec { | ||
inState<HomeDefaultState> { | ||
onEnterEffect { | ||
currentState = it | ||
} | ||
collectWhileInState(query) { q, state -> | ||
state.override { | ||
HomeDefaultState.Loading( | ||
query = q, | ||
fallback = false | ||
) | ||
} | ||
} | ||
} | ||
inState<HomeDefaultState.Loading> { | ||
collectWhileInState( | ||
flowBuilder = { | ||
val usedClient = if (it.fallback) { | ||
fallbackClient | ||
} else { | ||
client | ||
} | ||
|
||
usedClient.query(it.query.toGraphQL()).toFlow() | ||
} | ||
) { response, state -> | ||
state.override { | ||
fromGraphQL(response) | ||
} | ||
} | ||
} | ||
inState<HomeDefaultState.Error> { | ||
onEnterEffect { | ||
crashlytics?.log(it.throwable) | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
var currentState: HomeDefaultState | ||
get() = StateSaver.popularNextSeasonState | ||
private set(value) { | ||
StateSaver.popularNextSeasonState = value | ||
} | ||
} | ||
} |
Oops, something went wrong.