Skip to content

Commit

Permalink
change medium state machine to repository
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 3, 2024
1 parent d841a64 commit e92e6ae
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 212 deletions.
48 changes: 0 additions & 48 deletions anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/Cache.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ class CharacterRepository(

companion object {
fun fromGraphQL(query: CharacterQuery.Data?): State {
val char = query?.Character?.let { Character(it) }

if (char == null) {
return Error
}
val char = query?.Character?.let { Character(it) } ?: return Error

return Success(char)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package dev.datlag.aniflow.anilist

import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.api.Optional
import dev.datlag.aniflow.anilist.model.Medium
import kotlinx.coroutines.flow.*

class MediumRepository(
private val client: ApolloClient,
private val fallbackClient: ApolloClient
) {

private val id = MutableStateFlow<Int?>(null)
private val query = id.filterNotNull().map {
Query(it)
}
private val fallbackQuery = query.transform {
return@transform emitAll(fallbackClient.query(it.toGraphQL()).toFlow())
}.mapNotNull {
val data = it.data
if (data == null) {
if (it.hasErrors()) {
State.fromGraphQL(data)
} else {
null
}
} else {
State.fromGraphQL(data)
}
}

val medium = query.transform {
return@transform emitAll(client.query(it.toGraphQL()).toFlow())
}.mapNotNull {
val data = it.data
if (data == null) {
if (it.hasErrors()) {
State.fromGraphQL(data)
} else {
null
}
} else {
State.fromGraphQL(data)
}
}.transform {
return@transform if (it is State.Error) {
emitAll(fallbackQuery)
} else {
emit(it)
}
}

fun clear() = id.update { null }

fun load(id: Int) = this.id.update { id }

private data class Query(
val id: Int,
) {
fun toGraphQL() = MediumQuery(
id = Optional.present(id),
statusVersion = Optional.present(2),
html = Optional.present(true)
)
}

sealed interface State {
data class Success(val medium: Medium) : State
data object Error : State

companion object {
fun fromGraphQL(query: MediumQuery.Data?): State {
val medium = query?.Media?.let(::Medium) ?: return Error

return Success(medium)
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,11 @@ data object NetworkModule {
fallbackClient = instance(Constants.AniList.FALLBACK_APOLLO_CLIENT),
)
}
bindSingleton<MediumRepository> {
MediumRepository(
client = instance(Constants.AniList.APOLLO_CLIENT),
fallbackClient = instance(Constants.AniList.FALLBACK_APOLLO_CLIENT)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dev.datlag.aniflow.ui.navigation.screen.medium

import com.arkivanov.decompose.router.slot.ChildSlot
import com.arkivanov.decompose.value.Value
import dev.datlag.aniflow.anilist.MediumStateMachine
import dev.datlag.aniflow.anilist.MediumRepository
import dev.datlag.aniflow.anilist.model.Character
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.type.MediaFormat
Expand All @@ -21,7 +21,7 @@ interface MediumComponent : ContentHolderComponent {
val titleLanguage: Flow<SettingsTitle?>
val charLanguage: Flow<SettingsChar?>

val mediumState: StateFlow<MediumStateMachine.State>
val mediumState: Flow<MediumRepository.State>
val isAdult: Flow<Boolean>
val isAdultAllowed: Flow<Boolean>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fun MediumScreen(component: MediumComponent) {
scrollBehavior = scrollState,
initialMedium = component.initialMedium,
titleLanguageFlow = component.titleLanguage,
mediumStateFlow = component.mediumState,
mediumFlow = component.mediumState,
bannerImageFlow = component.bannerImage,
coverImage = coverImage,
titleFlow = component.title,
Expand Down
Loading

0 comments on commit e92e6ae

Please sign in to comment.