-
-
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.
change medium state machine to repository
- Loading branch information
Showing
9 changed files
with
121 additions
and
212 deletions.
There are no files selected for viewing
48 changes: 0 additions & 48 deletions
48
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/Cache.kt
This file was deleted.
Oops, something went wrong.
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
79 changes: 79 additions & 0 deletions
79
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/MediumRepository.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,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) | ||
} | ||
} | ||
} | ||
} |
115 changes: 0 additions & 115 deletions
115
anilist/src/commonMain/kotlin/dev/datlag/aniflow/anilist/MediumStateMachine.kt
This file was deleted.
Oops, something went wrong.
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
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.