-
-
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.
list repository re-write with flowredux
- Loading branch information
Showing
17 changed files
with
515 additions
and
250 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
169 changes: 0 additions & 169 deletions
169
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/ListRepository.kt
This file was deleted.
Oops, something went wrong.
168 changes: 168 additions & 0 deletions
168
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/ListStateMachine.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,168 @@ | ||
package dev.datlag.aniflow.anilist | ||
|
||
import com.apollographql.apollo3.ApolloClient | ||
import com.freeletics.flowredux.dsl.FlowReduxStateMachine | ||
import dev.datlag.aniflow.anilist.model.PageListQuery | ||
import dev.datlag.aniflow.anilist.model.User | ||
import dev.datlag.aniflow.anilist.state.ListAction | ||
import dev.datlag.aniflow.anilist.state.ListState | ||
import dev.datlag.aniflow.anilist.type.MediaListStatus | ||
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.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
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.mapNotNull | ||
import kotlinx.coroutines.flow.transformLatest | ||
import kotlinx.coroutines.flow.update | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class ListStateMachine( | ||
private val client: ApolloClient, | ||
private val fallbackClient: ApolloClient, | ||
private val user: Flow<User?>, | ||
private val viewManga: Flow<Boolean> = flowOf(false), | ||
private val crashlytics: FirebaseFactory.Crashlytics?, | ||
) : FlowReduxStateMachine<ListState, ListAction>( | ||
initialState = currentState | ||
) { | ||
|
||
var currentState: ListState | ||
get() = Companion.currentState | ||
private set(value) { | ||
Companion.currentState = value | ||
} | ||
|
||
private val page = MutableStateFlow(0) | ||
private val _type = MutableStateFlow(MediaType.UNKNOWN__) | ||
private val _status = MutableStateFlow(MediaListStatus.UNKNOWN__) | ||
val status: StateFlow<MediaListStatus> = _status | ||
|
||
val type = _type.transformLatest { | ||
return@transformLatest if (it == MediaType.UNKNOWN__) { | ||
emitAll(viewManga.map { m -> | ||
if (m) { | ||
MediaType.MANGA | ||
} else { | ||
MediaType.ANIME | ||
} | ||
}) | ||
} else { | ||
emit(it) | ||
} | ||
}.distinctUntilChanged() | ||
|
||
private val query = combine( | ||
page, | ||
type, | ||
_status, | ||
user.mapNotNull { it?.id }.distinctUntilChanged() | ||
) { p, t, s, u -> | ||
PageListQuery.ForPage( | ||
page = p, | ||
type = t, | ||
status = s, | ||
userId = u | ||
) | ||
}.distinctUntilChanged() | ||
|
||
init { | ||
spec { | ||
inState<ListState> { | ||
onEnterEffect { | ||
currentState = it | ||
} | ||
onActionEffect<ListAction.Page> { action, _ -> | ||
when (action) { | ||
is ListAction.Page.Next -> { | ||
page.update { it + 1 } | ||
} | ||
} | ||
} | ||
onActionEffect<ListAction.Type> { action, _ -> | ||
when (action) { | ||
is ListAction.Type.Anime -> { | ||
page.update { 0 } | ||
_type.update { | ||
MediaType.ANIME | ||
} | ||
} | ||
is ListAction.Type.Manga -> { | ||
page.update { 0 } | ||
_type.update { | ||
MediaType.MANGA | ||
} | ||
} | ||
is ListAction.Type.Toggle -> { | ||
page.update { 0 } | ||
_type.update { | ||
if (it == MediaType.MANGA) { | ||
MediaType.ANIME | ||
} else { | ||
MediaType.MANGA | ||
} | ||
} | ||
} | ||
} | ||
} | ||
onActionEffect<ListAction.Status> { action, _ -> | ||
page.update { 0 } | ||
_status.update { | ||
action.value | ||
} | ||
} | ||
collectWhileInState(query) { q, state -> | ||
state.override { | ||
val collection = if (q.page == 0) { | ||
emptyList() | ||
} else { | ||
(this as? ListState.Data)?.collection.orEmpty() | ||
} | ||
|
||
ListState.Loading( | ||
query = q, | ||
fallback = false, | ||
collection = collection | ||
) | ||
} | ||
} | ||
} | ||
inState<ListState.Loading> { | ||
collectWhileInState( | ||
flowBuilder = { | ||
val usedClient = if (it.fallback) { | ||
fallbackClient | ||
} else { | ||
client | ||
} | ||
|
||
usedClient.query(it.query.toGraphQL()).toFlow() | ||
} | ||
) { response, state -> | ||
state.override { | ||
fromGraphQL(response) | ||
} | ||
} | ||
} | ||
inState<ListState.Error> { | ||
onEnterEffect { | ||
crashlytics?.log(it.throwable) | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
var currentState | ||
get() = StateSaver.listState | ||
private set(value) { | ||
StateSaver.listState = 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
Oops, something went wrong.