Skip to content

Commit

Permalink
save anime/manga switch
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 4, 2024
1 parent fdbf9f0 commit 877b7f6
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ 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 = MutableStateFlow(MediaType.ANIME)
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 ->
val (season, year) = Clock.System.now().nextSeason

Expand Down Expand Up @@ -73,28 +81,6 @@ class PopularNextSeasonRepository(
it - 1
}

fun viewAnime() {
type.getAndUpdate {
if (it == MediaType.ANIME) {
it
} else {
page.update { 0 }
MediaType.ANIME
}
}
}

fun viewManga() {
type.getAndUpdate {
if (it == MediaType.MANGA) {
it
} else {
page.update { 0 }
MediaType.MANGA
}
}
}

private data class Query(
val page: Int,
val type: MediaType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ class PopularSeasonRepository(
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 = MutableStateFlow(MediaType.ANIME)
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 ->
Query(
page = p,
Expand Down Expand Up @@ -66,28 +74,6 @@ class PopularSeasonRepository(
it - 1
}

fun viewAnime() {
type.getAndUpdate {
if (it == MediaType.ANIME) {
it
} else {
page.update { 0 }
MediaType.ANIME
}
}
}

fun viewManga() {
type.getAndUpdate {
if (it == MediaType.MANGA) {
it
} else {
page.update { 0 }
MediaType.MANGA
}
}
}

private data class Query(
val page: Int,
val type: MediaType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ class TrendingRepository(
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 = MutableStateFlow(MediaType.ANIME)
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 ->
Query(
page = p,
Expand Down Expand Up @@ -66,28 +74,6 @@ class TrendingRepository(
it - 1
}

fun viewAnime() {
type.getAndUpdate {
if (it == MediaType.ANIME) {
it
} else {
page.update { 0 }
MediaType.ANIME
}
}
}

fun viewManga() {
type.getAndUpdate {
if (it == MediaType.MANGA) {
it
} else {
page.update { 0 }
MediaType.MANGA
}
}
}

private data class Query(
val page: Int,
val type: MediaType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ data object NetworkModule {
TrendingRepository(
apolloClient = instance(Constants.AniList.APOLLO_CLIENT),
fallbackClient = instance(Constants.AniList.FALLBACK_APOLLO_CLIENT),
nsfw = appSettings.adultContent
nsfw = appSettings.adultContent,
viewManga = appSettings.viewManga
)
}
bindSingleton<AiringTodayRepository> {
Expand All @@ -143,7 +144,8 @@ data object NetworkModule {
PopularSeasonRepository(
apolloClient = instance(Constants.AniList.APOLLO_CLIENT),
fallbackClient = instance(Constants.AniList.FALLBACK_APOLLO_CLIENT),
nsfw = appSettings.adultContent
nsfw = appSettings.adultContent,
viewManga = appSettings.viewManga
)
}
bindSingleton<PopularNextSeasonRepository> {
Expand All @@ -152,7 +154,8 @@ data object NetworkModule {
PopularNextSeasonRepository(
apolloClient = instance(Constants.AniList.APOLLO_CLIENT),
fallbackClient = instance(Constants.AniList.FALLBACK_APOLLO_CLIENT),
nsfw = appSettings.adultContent
nsfw = appSettings.adultContent,
viewManga = appSettings.viewManga
)
}
bindSingleton<CharacterRepository> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import kotlinx.coroutines.flow.StateFlow
import dev.datlag.aniflow.settings.model.TitleLanguage as SettingsTitle

interface HomeComponent : ContentHolderComponent {
val viewing: Value<MediaType>
val viewing: Flow<MediaType>
val titleLanguage: Flow<SettingsTitle?>

val airingState: Flow<AiringTodayRepository.State>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,18 @@ private fun MainView(component: HomeComponent, modifier: Modifier = Modifier) {
else -> { }
}

val type by component.viewing.collectAsStateWithLifecycle(MediaType.UNKNOWN__)
val isManga = remember(type) {
type == MediaType.MANGA
}

LazyColumn(
state = listState,
modifier = modifier.haze(state = LocalHaze.current),
contentPadding = LocalPaddingValues.current?.plus(padding) ?: padding,
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
item {
val type by component.viewing.subscribeAsState()
val isManga = remember(type) {
type == MediaType.MANGA
}

Box(
modifier = Modifier.fillParentMaxWidth().height(200.dp),
contentAlignment = Alignment.BottomEnd
Expand Down Expand Up @@ -194,20 +194,22 @@ private fun MainView(component: HomeComponent, modifier: Modifier = Modifier) {
}
}
}
item {
Text(
modifier = Modifier.padding(horizontal = 16.dp),
text = "Schedule",
style = MaterialTheme.typography.headlineMedium,
fontWeight = FontWeight.Bold
)
}
item {
AiringOverview(
state = component.airingState,
titleLanguage = titleLanguage,
onClick = component::details
)
if (!isManga) {
item {
Text(
modifier = Modifier.padding(horizontal = 16.dp),
text = "Schedule",
style = MaterialTheme.typography.headlineMedium,
fontWeight = FontWeight.Bold
)
}
item {
AiringOverview(
state = component.airingState,
titleLanguage = titleLanguage,
onClick = component::details
)
}
}
item {
Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.state.SeasonState
import dev.datlag.aniflow.anilist.type.MediaType
import dev.datlag.aniflow.common.onRender
import dev.datlag.aniflow.model.coroutines.Executor
import dev.datlag.aniflow.other.StateSaver
import dev.datlag.aniflow.settings.Settings
import dev.datlag.aniflow.settings.model.AppSettings
Expand Down Expand Up @@ -64,7 +65,15 @@ class HomeScreenComponent(
context = ioDispatcher()
)

override val viewing = MutableValue(MediaType.UNKNOWN__)
private val viewTypeExecutor = Executor()

override val viewing = appSettings.viewManga.map {
if (it) {
MediaType.MANGA
} else {
MediaType.ANIME
}
}

@Composable
override fun render() {
Expand All @@ -88,16 +97,18 @@ class HomeScreenComponent(
}

override fun viewAnime() {
viewing.update { MediaType.ANIME }
trendingRepository.viewAnime()
popularSeasonRepository.viewAnime()
popularNextSeasonRepository.viewAnime()
launchIO {
viewTypeExecutor.enqueue {
appSettings.setViewManga(false)
}
}
}

override fun viewManga() {
viewing.update { MediaType.MANGA }
trendingRepository.viewManga()
popularSeasonRepository.viewManga()
popularNextSeasonRepository.viewManga()
launchIO {
viewTypeExecutor.enqueue {
appSettings.setViewManga(true)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,61 @@ import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map

class DataStoreAppSettings(
private val dateStore: DataStore<AppSettings>
private val dataStore: DataStore<AppSettings>
) : Settings.PlatformAppSettings {
override val adultContent: Flow<Boolean> = dateStore.data.map { it.adultContent }.distinctUntilChanged()
override val color: Flow<Color?> = dateStore.data.map { it.color }.distinctUntilChanged()
override val titleLanguage: Flow<TitleLanguage?> = dateStore.data.map { it.titleLanguage }.distinctUntilChanged()
override val charLanguage: Flow<CharLanguage?> = dateStore.data.map { it.charLanguage }.distinctUntilChanged()
override val adultContent: Flow<Boolean> = dataStore.data.map { it.adultContent }.distinctUntilChanged()
override val color: Flow<Color?> = dataStore.data.map { it.color }.distinctUntilChanged()
override val titleLanguage: Flow<TitleLanguage?> = dataStore.data.map { it.titleLanguage }.distinctUntilChanged()
override val charLanguage: Flow<CharLanguage?> = dataStore.data.map { it.charLanguage }.distinctUntilChanged()
override val viewManga: Flow<Boolean> = dataStore.data.map { it.viewManga }.distinctUntilChanged()

override suspend fun setAdultContent(value: Boolean) {
dateStore.updateData {
dataStore.updateData {
it.copy(
adultContent = value
)
}
}

override suspend fun setColor(value: Color?) {
dateStore.updateData {
dataStore.updateData {
it.copy(
color = value
)
}
}

override suspend fun setTitleLanguage(value: TitleLanguage?) {
dateStore.updateData {
dataStore.updateData {
it.copy(
titleLanguage = value
)
}
}

override suspend fun setCharLanguage(value: CharLanguage?) {
dateStore.updateData {
dataStore.updateData {
it.copy(
charLanguage = value
)
}
}

override suspend fun setViewManga(value: Boolean) {
dataStore.updateData {
it.copy(
viewManga = value
)
}
}

override suspend fun setData(
adultContent: Boolean,
color: Color?,
titleLanguage: TitleLanguage?,
charLanguage: CharLanguage?,
) {
dateStore.updateData {
dataStore.updateData {
it.copy(
adultContent = adultContent,
color = color,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ data object Settings {
val color: Flow<Color?>
val titleLanguage: Flow<TitleLanguage?>
val charLanguage: Flow<CharLanguage?>
val viewManga: Flow<Boolean>

suspend fun setAdultContent(value: Boolean)
suspend fun setColor(value: Color?)
Expand All @@ -30,6 +31,7 @@ data object Settings {
})
suspend fun setTitleLanguage(value: TitleLanguage?)
suspend fun setCharLanguage(value: CharLanguage?)
suspend fun setViewManga(value: Boolean)
suspend fun setData(
adultContent: Boolean,
color: Color?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ data class AppSettings(
@ProtoNumber(1) val adultContent: Boolean = false,
@ProtoNumber(2) val color: Color?,
@ProtoNumber(3) val titleLanguage: TitleLanguage?,
@ProtoNumber(4) val charLanguage: CharLanguage?
@ProtoNumber(4) val charLanguage: CharLanguage?,
@ProtoNumber(5) val viewManga: Boolean = false
)

0 comments on commit 877b7f6

Please sign in to comment.