Skip to content

Commit

Permalink
initial repository re-write with flowredux
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 21, 2024
1 parent 1756e67 commit 03a0168
Show file tree
Hide file tree
Showing 22 changed files with 579 additions and 680 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
query TrendingQuery(
query PageMediaQuery(
$page: Int,
$perPage: Int,
$type: MediaType,
$statusVersion: Int,
$html: Boolean,
$sort: [MediaSort],
$year: Int,
$season: MediaSeason,
$preventGenres: [String],
$type: MediaType,
$adultContent: Boolean,
$preventGenres: [String]
$statusVersion: Int!,
$html: Boolean!,
) {
Page(page: $page, perPage: $perPage) {
media(
type: $type,
sort: $sort,
isAdult: $adultContent,
genre_not_in: $preventGenres
seasonYear: $year,
season: $season,
genre_not_in: $preventGenres,
type: $type,
isAdult: $adultContent
) {
id,
idMal,
Expand Down
108 changes: 0 additions & 108 deletions anilist/src/commonMain/graphql/SeasonQuery.graphql

This file was deleted.

This file was deleted.

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
}
}
}
Loading

0 comments on commit 03a0168

Please sign in to comment.