Skip to content

Commit

Permalink
transition to new navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 6, 2024
1 parent 30922be commit 96d35e8
Show file tree
Hide file tree
Showing 35 changed files with 752 additions and 851 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.type.AiringSort
import kotlinx.coroutines.flow.*
import kotlinx.datetime.Clock
import kotlinx.serialization.Serializable
import kotlin.time.Duration.Companion.hours

class AiringTodayRepository(
Expand Down Expand Up @@ -85,10 +86,14 @@ class AiringTodayRepository(
}

sealed interface State {
@Serializable
data object None : State

data class Success(
val collection: Collection<AiringQuery.AiringSchedule>
) : State

@Serializable
data object Error : State

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.datlag.aniflow.anilist
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.api.Optional
import dev.datlag.aniflow.anilist.common.nextSeason
import dev.datlag.aniflow.anilist.state.SeasonState
import dev.datlag.aniflow.anilist.state.CollectionState
import dev.datlag.aniflow.anilist.type.MediaSeason
import dev.datlag.aniflow.anilist.type.MediaSort
import dev.datlag.aniflow.anilist.type.MediaType
Expand All @@ -14,24 +14,14 @@ class PopularNextSeasonRepository(
private val apolloClient: ApolloClient,
private val fallbackClient: ApolloClient,
private val nsfw: Flow<Boolean> = flowOf(false),
private val viewManga: Flow<Boolean> = flowOf(false),
) {

private val page = MutableStateFlow(0)
private val type = viewManga.distinctUntilChanged().map {
page.update { 0 }
if (it) {
MediaType.MANGA
} else {
MediaType.ANIME
}
}
private val query = combine(page, type, nsfw.distinctUntilChanged()) { p, t, n ->
private val query = combine(page, nsfw.distinctUntilChanged()) { p, n ->
val (season, year) = Clock.System.now().nextSeason

Query(
page = p,
type = t,
nsfw = n,
season = season,
year = year
Expand All @@ -43,12 +33,12 @@ class PopularNextSeasonRepository(
val data = it.data
if (data == null) {
if (it.hasErrors()) {
SeasonState.fromGraphQL(data)
CollectionState.fromSeasonGraphQL(data)
} else {
null
}
} else {
SeasonState.fromGraphQL(data)
CollectionState.fromSeasonGraphQL(data)
}
}

Expand All @@ -58,15 +48,15 @@ class PopularNextSeasonRepository(
val data = it.data
if (data == null) {
if (it.hasErrors()) {
SeasonState.fromGraphQL(data)
CollectionState.fromSeasonGraphQL(data)
} else {
null
}
} else {
SeasonState.fromGraphQL(data)
CollectionState.fromSeasonGraphQL(data)
}
}.transform {
return@transform if (it is SeasonState.Error) {
return@transform if (it.isError) {
emitAll(fallbackQuery)
} else {
emit(it)
Expand All @@ -83,7 +73,6 @@ class PopularNextSeasonRepository(

private data class Query(
val page: Int,
val type: MediaType,
val nsfw: Boolean,
val season: MediaSeason,
val year: Int
Expand All @@ -96,7 +85,7 @@ class PopularNextSeasonRepository(
} else {
Optional.present(nsfw)
},
type = Optional.present(type),
type = Optional.present(MediaType.ANIME),
sort = Optional.present(listOf(MediaSort.POPULARITY_DESC)),
preventGenres = if (nsfw) {
Optional.absent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dev.datlag.aniflow.anilist

import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.api.Optional
import dev.datlag.aniflow.anilist.state.SeasonState
import dev.datlag.aniflow.anilist.state.CollectionState
import dev.datlag.aniflow.anilist.type.MediaSort
import dev.datlag.aniflow.anilist.type.MediaType
import kotlinx.coroutines.flow.*
Expand Down Expand Up @@ -36,12 +36,12 @@ class PopularSeasonRepository(
val data = it.data
if (data == null) {
if (it.hasErrors()) {
SeasonState.fromGraphQL(data)
CollectionState.fromSeasonGraphQL(data)
} else {
null
}
} else {
SeasonState.fromGraphQL(data)
CollectionState.fromSeasonGraphQL(data)
}
}

Expand All @@ -51,15 +51,15 @@ class PopularSeasonRepository(
val data = it.data
if (data == null) {
if (it.hasErrors()) {
SeasonState.fromGraphQL(data)
CollectionState.fromSeasonGraphQL(data)
} else {
null
}
} else {
SeasonState.fromGraphQL(data)
CollectionState.fromSeasonGraphQL(data)
}
}.transform {
return@transform if (it is SeasonState.Error) {
return@transform if (it.isError) {
emitAll(fallbackQuery)
} else {
emit(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package dev.datlag.aniflow.anilist
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.api.Optional
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.state.CollectionState
import dev.datlag.aniflow.anilist.type.MediaSort
import dev.datlag.aniflow.anilist.type.MediaType
import kotlinx.coroutines.flow.*
import kotlinx.serialization.Serializable

class TrendingRepository(
private val apolloClient: ApolloClient,
Expand Down Expand Up @@ -36,12 +38,12 @@ class TrendingRepository(
val data = it.data
if (data == null) {
if (it.hasErrors()) {
State.fromGraphQL(data)
CollectionState.fromTrendingGraphQL(data)
} else {
null
}
} else {
State.fromGraphQL(data)
CollectionState.fromTrendingGraphQL(data)
}
}

Expand All @@ -51,15 +53,15 @@ class TrendingRepository(
val data = it.data
if (data == null) {
if (it.hasErrors()) {
State.fromGraphQL(data)
CollectionState.fromTrendingGraphQL(data)
} else {
null
}
} else {
State.fromGraphQL(data)
CollectionState.fromTrendingGraphQL(data)
}
}.transform {
return@transform if (it is State.Error) {
return@transform if (it.isError) {
emitAll(fallbackQuery)
} else {
emit(it)
Expand Down Expand Up @@ -98,24 +100,4 @@ class TrendingRepository(
html = Optional.present(true)
)
}

sealed interface State {
data class Success(
val collection: Collection<Medium>
) : State

data object Error : State

companion object {
fun fromGraphQL(data: TrendingQuery.Data?): State {
val mediaList = data?.Page?.mediaFilterNotNull()

if (mediaList.isNullOrEmpty()) {
return Error
}

return Success(mediaList.map { Medium(it) })
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.datlag.aniflow.anilist.state

import dev.datlag.aniflow.anilist.SeasonQuery
import dev.datlag.aniflow.anilist.TrendingQuery
import dev.datlag.aniflow.anilist.model.Medium
import kotlinx.serialization.Serializable

@Serializable
sealed interface CollectionState {

val isSuccess: Boolean
get() = this is Success

val isError: Boolean
get() = this is Error

@Serializable
data object None : CollectionState

@Serializable
data class Success(
val collection: Collection<Medium>
) : CollectionState

@Serializable
data object Error : CollectionState

companion object {
fun fromTrendingGraphQL(data: TrendingQuery.Data?): CollectionState {
val mediaList = data?.Page?.mediaFilterNotNull()

if (mediaList.isNullOrEmpty()) {
return Error
}

return Success(mediaList.map { Medium(it) })
}

fun fromSeasonGraphQL(data: SeasonQuery.Data?): CollectionState {
val mediaList = data?.Page?.mediaFilterNotNull()

if (mediaList.isNullOrEmpty()) {
return Error
}

return Success(mediaList.map { Medium(it) })
}
}
}

val CollectionState?.isLoading: Boolean
get() = this == null || this is CollectionState.None

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ data object NetworkModule {
PopularNextSeasonRepository(
apolloClient = instance(Constants.AniList.APOLLO_CLIENT),
fallbackClient = instance(Constants.AniList.FALLBACK_APOLLO_CLIENT),
nsfw = appSettings.adultContent,
viewManga = appSettings.viewManga
nsfw = appSettings.adultContent
)
}
bindSingleton<CharacterRepository> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.compose.ui.graphics.Color
import com.mayakapps.kache.InMemoryKache
import com.mayakapps.kache.KacheStrategy
import dev.datlag.aniflow.anilist.*
import dev.datlag.aniflow.anilist.state.SeasonState
import dev.datlag.aniflow.anilist.state.CollectionState
import dev.datlag.aniflow.settings.model.AppSettings
import dev.datlag.tooling.async.scopeCatching
import dev.datlag.tooling.async.suspendCatching
Expand Down Expand Up @@ -76,17 +76,17 @@ data object StateSaver {
return state
}

fun updateTrending(state: TrendingRepository.State): TrendingRepository.State {
fun updateTrending(state: CollectionState): CollectionState {
trendingLoading.update { false }
return state
}

fun updatePopularCurrent(state: SeasonState): SeasonState {
fun updatePopularCurrent(state: CollectionState): CollectionState {
popularCurrentLoading.update { false }
return state
}

fun updatePopularNext(state: SeasonState): SeasonState {
fun updatePopularNext(state: CollectionState): CollectionState {
popularNextLoading.update { false }
return state
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.arkivanov.decompose.router.stack.*
import dev.datlag.aniflow.common.onRender
import dev.datlag.aniflow.model.ifValueOrNull
import dev.datlag.aniflow.other.UserHelper
import dev.datlag.aniflow.ui.navigation.screen.home.HomeScreenComponent
import dev.datlag.aniflow.ui.navigation.screen.initial.InitialScreenComponent
import dev.datlag.aniflow.ui.navigation.screen.medium.MediumScreenComponent
import dev.datlag.aniflow.ui.navigation.screen.settings.SettingsScreen
Expand Down Expand Up @@ -43,7 +44,7 @@ class RootComponent(
componentContext: ComponentContext
): Component {
return when (rootConfig) {
is RootConfig.Home -> InitialScreenComponent(
is RootConfig.Home -> HomeScreenComponent(
componentContext = componentContext,
di = di,
onMediumDetails = {
Expand Down
Loading

0 comments on commit 96d35e8

Please sign in to comment.