-
-
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.
airing repository re-write with flowredux
- Loading branch information
Showing
11 changed files
with
217 additions
and
156 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
127 changes: 0 additions & 127 deletions
127
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/AiringTodayRepository.kt
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/AiringTodayStateMachine.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.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.distinctUntilChanged | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.flow.mapLatest | ||
|
||
@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 | ||
get() = Companion.currentState | ||
private set(value) { | ||
Companion.currentState = value | ||
} | ||
|
||
private val query = nsfw.distinctUntilChanged().mapLatest { | ||
PageAiringQuery.Today( | ||
nsfw = it | ||
) | ||
}.distinctUntilChanged() | ||
|
||
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 | ||
} | ||
|
||
usedClient.query(it.query.toGraphQL()).toFlow() | ||
} | ||
) { response, state -> | ||
state.override { | ||
fromGraphQL(response) | ||
} | ||
} | ||
} | ||
inState<HomeAiringState.Error> { | ||
onEnterEffect { | ||
crashlytics?.log(it.throwable) | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
var currentState: HomeAiringState | ||
get() = StateSaver.airingState | ||
private set(value) { | ||
StateSaver.airingState = value | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/StateSaver.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
30 changes: 30 additions & 0 deletions
30
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/model/PageAiringQuery.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,30 @@ | ||
package dev.datlag.aniflow.anilist.model | ||
|
||
import com.apollographql.apollo3.api.Optional | ||
import dev.datlag.aniflow.anilist.AiringQuery | ||
import dev.datlag.aniflow.anilist.common.presentAsList | ||
import dev.datlag.aniflow.anilist.type.AiringSort | ||
import kotlinx.datetime.Clock | ||
import kotlin.time.Duration.Companion.hours | ||
import dev.datlag.aniflow.anilist.AiringQuery as AiringGraphQL | ||
|
||
sealed interface PageAiringQuery { | ||
|
||
val nsfw: Boolean | ||
|
||
fun toGraphQL(): AiringGraphQL | ||
|
||
data class Today( | ||
override val nsfw: Boolean | ||
) : PageAiringQuery { | ||
override fun toGraphQL() = AiringQuery( | ||
perPage = Optional.present(20), | ||
sort = Optional.presentAsList(AiringSort.TIME), | ||
airingAtGreater = Optional.present( | ||
Clock.System.now().minus(1.hours).epochSeconds.toInt() | ||
), | ||
statusVersion = 2, | ||
html = true | ||
) | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/state/HomeAiringState.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,71 @@ | ||
package dev.datlag.aniflow.anilist.state | ||
|
||
import com.apollographql.apollo3.api.ApolloResponse | ||
import dev.datlag.aniflow.anilist.AdultContent | ||
import dev.datlag.aniflow.anilist.model.PageAiringQuery | ||
|
||
import dev.datlag.aniflow.anilist.AiringQuery as PageAiringGraphQL | ||
|
||
sealed interface HomeAiringState { | ||
|
||
val isLoading: Boolean | ||
get() = this !is PostLoading | ||
|
||
val isError: Boolean | ||
get() = this is Error | ||
|
||
data object None : HomeAiringState | ||
|
||
data class Loading( | ||
internal val query: PageAiringQuery, | ||
internal val fallback: Boolean | ||
) : HomeAiringState { | ||
|
||
private val nsfw: Boolean | ||
get() = query.nsfw | ||
|
||
fun fromGraphQL(response: ApolloResponse<PageAiringGraphQL.Data>): HomeAiringState { | ||
val data = response.data | ||
|
||
return if (data == null) { | ||
if (fallback) { | ||
Error(throwable = response.exception) | ||
} else { | ||
copy(fallback = true) | ||
} | ||
} else { | ||
val airingList = data.Page?.airingSchedulesFilterNotNull()?.mapNotNull { | ||
if (nsfw) { | ||
it | ||
} else { | ||
if (AdultContent.isAdultContent(it)) { | ||
null | ||
} else { | ||
it | ||
} | ||
} | ||
} | ||
|
||
if (airingList.isNullOrEmpty()) { | ||
if (fallback) { | ||
Error(throwable = response.exception) | ||
} else { | ||
copy(fallback = true) | ||
} | ||
} else { | ||
Success(airingList) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private sealed interface PostLoading : HomeAiringState | ||
|
||
data class Success( | ||
val collection: Collection<PageAiringGraphQL.AiringSchedule> | ||
) : PostLoading | ||
|
||
data class Error( | ||
internal val throwable: Throwable? | ||
) : PostLoading | ||
} |
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.