Skip to content

Commit

Permalink
improved performance
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Apr 30, 2024
1 parent 7bd9f2a commit 38a55d2
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,18 @@ import dev.datlag.tooling.decompose.lifecycle.collectAsStateWithLifecycle
import dev.icerock.moko.resources.StringResource
import org.kodein.di.instance

@Composable
fun Medium.preferred(setting: AppSettings.TitleLanguage? = null): String {
val appSettings by LocalDI.current.instance<Settings.PlatformAppSettings>()
val title by appSettings.titleLanguage.collectAsStateWithLifecycle(null)

return this.title.preferred(setting ?: title).ifBlank { this.id.toString() }
return this.title.preferred(setting).ifBlank { this.id.toString() }
}

@Composable
fun Medium.notPreferred(setting: AppSettings.TitleLanguage? = null): String? {
val appSettings by LocalDI.current.instance<Settings.PlatformAppSettings>()
val title by appSettings.titleLanguage.collectAsStateWithLifecycle(null)

return this.title.notPreferred(setting ?: title)?.ifBlank { null }
return this.title.notPreferred(setting)?.ifBlank { null }
}

expect fun Medium.Title.preferred(setting: AppSettings.TitleLanguage? = null): String

@Composable
fun Medium.Title.notPreferred(setting: AppSettings.TitleLanguage? = null): String? {
val appSettings by LocalDI.current.instance<Settings.PlatformAppSettings>()
val title by appSettings.titleLanguage.collectAsStateWithLifecycle(null)

val preferred = this.preferred(setting ?: title).trim()
val preferred = this.preferred(setting).trim()
val notPreferred = when {
this.native?.trim().equals(preferred, ignoreCase = true) -> {
when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ data object StateSaver {
t1.isLoadingOrWaiting && t2.isLoadingOrWaiting && t3.isLoadingOrWaiting && t4.isLoadingOrWaiting
}.flowOn(
context = ioDispatcher()
)
).distinctUntilChanged()

fun updateAiring(state: AiringTodayStateMachine.State) = _airingState.updateAndGet {
state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.datlag.aniflow.anilist.PopularSeasonStateMachine
import dev.datlag.aniflow.anilist.TrendingAnimeStateMachine
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.state.SeasonState
import dev.datlag.aniflow.settings.model.AppSettings
import dev.datlag.aniflow.trace.TraceStateMachine
import dev.datlag.aniflow.ui.navigation.Component
import dev.datlag.aniflow.ui.navigation.ContentHolderComponent
Expand All @@ -16,6 +17,8 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow

interface HomeComponent : ContentHolderComponent {
val titleLanguage: Flow<AppSettings.TitleLanguage?>

val airingState: Flow<AiringTodayStateMachine.State>
val trendingState: Flow<TrendingAnimeStateMachine.State>
val popularSeasonState: Flow<SeasonState>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ private fun MainView(component: HomeComponent, modifier: Modifier = Modifier) {
)
}
val useCase = rememberUseCaseState(visible = results.isNotEmpty())
val titleLanguage by component.titleLanguage.collectAsStateWithLifecycle(null)

OptionDialog(
state = useCase,
selection = OptionSelection.Single(
options = results.map {
Option(
titleText = it.aniList.asMedium().title.preferred(),
titleText = it.aniList.asMedium().title.preferred(titleLanguage),
details = OptionDetails(
title = stringResource(SharedRes.strings.similarity_title),
body = if (it.isSingle) {
Expand Down Expand Up @@ -151,6 +152,7 @@ private fun MainView(component: HomeComponent, modifier: Modifier = Modifier) {
item {
AiringOverview(
state = component.airingState,
titleLanguage = component.titleLanguage,
onClick = component::details
)
}
Expand All @@ -165,6 +167,7 @@ private fun MainView(component: HomeComponent, modifier: Modifier = Modifier) {
item {
TrendingOverview(
state = component.trendingState,
titleLanguage = component.titleLanguage,
onClick = component::details
)
}
Expand All @@ -180,6 +183,7 @@ private fun MainView(component: HomeComponent, modifier: Modifier = Modifier) {
PopularSeasonOverview(
state = component.popularSeasonState,
current = true,
titleLanguage = component.titleLanguage,
onClick = component::details,
)
}
Expand All @@ -195,6 +199,7 @@ private fun MainView(component: HomeComponent, modifier: Modifier = Modifier) {
PopularSeasonOverview(
state = component.popularNextSeasonState,
current = false,
titleLanguage = component.titleLanguage,
onClick = component::details
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package dev.datlag.aniflow.ui.navigation.screen.initial.home

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.router.slot.*
import com.arkivanov.decompose.value.Value
import dev.datlag.aniflow.LocalDI
import dev.datlag.aniflow.anilist.AiringTodayStateMachine
import dev.datlag.aniflow.anilist.PopularNextSeasonStateMachine
import dev.datlag.aniflow.anilist.PopularSeasonStateMachine
Expand All @@ -12,11 +14,14 @@ import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.state.SeasonState
import dev.datlag.aniflow.common.onRender
import dev.datlag.aniflow.other.StateSaver
import dev.datlag.aniflow.settings.Settings
import dev.datlag.aniflow.settings.model.AppSettings
import dev.datlag.aniflow.trace.TraceStateMachine
import dev.datlag.aniflow.ui.navigation.Component
import dev.datlag.aniflow.ui.navigation.screen.medium.MediumScreenComponent
import dev.datlag.tooling.compose.ioDispatcher
import dev.datlag.tooling.decompose.ioScope
import dev.datlag.tooling.decompose.lifecycle.collectAsStateWithLifecycle
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
Expand All @@ -30,33 +35,36 @@ class HomeScreenComponent(
private val onMediumDetails: (Medium) -> Unit
) : HomeComponent, ComponentContext by componentContext {

private val appSettings by di.instance<Settings.PlatformAppSettings>()
override val titleLanguage: Flow<AppSettings.TitleLanguage?> = appSettings.titleLanguage.flowOn(ioDispatcher())

private val airingTodayStateMachine by di.instance<AiringTodayStateMachine>()
override val airingState: Flow<AiringTodayStateMachine.State> = airingTodayStateMachine.state.map {
StateSaver.Home.updateAiring(it)
}.flowOn(
context = ioDispatcher()
)
).distinctUntilChanged()

private val trendingAnimeStateMachine by di.instance<TrendingAnimeStateMachine>()
override val trendingState: Flow<TrendingAnimeStateMachine.State> = trendingAnimeStateMachine.state.map {
StateSaver.Home.updateTrending(it)
}.flowOn(
context = ioDispatcher()
)
).distinctUntilChanged()

private val popularSeasonStateMachine by di.instance<PopularSeasonStateMachine>()
override val popularSeasonState: Flow<SeasonState> = popularSeasonStateMachine.state.map {
StateSaver.Home.updatePopularCurrent(it)
}.flowOn(
context = ioDispatcher()
)
).distinctUntilChanged()

private val popularNextSeasonStateMachine by di.instance<PopularNextSeasonStateMachine>()
override val popularNextSeasonState: Flow<SeasonState> = popularNextSeasonStateMachine.state.map {
StateSaver.Home.updatePopularNext(it)
}.flowOn(
context = ioDispatcher()
)
).distinctUntilChanged()

private val traceStateMachine by di.instance<TraceStateMachine>()
override val traceState: Flow<TraceStateMachine.State> = traceStateMachine.state.flowOn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import dev.datlag.aniflow.settings.Settings
import dev.datlag.aniflow.settings.model.AppSettings
import dev.datlag.aniflow.ui.theme.SchemeTheme
import dev.datlag.tooling.decompose.lifecycle.collectAsStateWithLifecycle
import kotlinx.coroutines.flow.Flow
import org.kodein.di.instance
import org.kodein.di.instanceOrNull

@Composable
fun AiringCard(
airing: AiringQuery.AiringSchedule,
titleLanguageFlow: Flow<AppSettings.TitleLanguage?>,
modifier: Modifier = Modifier,
onClick: (Medium) -> Unit
) {
Expand All @@ -45,10 +47,12 @@ fun AiringCard(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
val titleLanguage by titleLanguageFlow.collectAsStateWithLifecycle(null)

AsyncImage(
modifier = Modifier.widthIn(min = 100.dp, max = 120.dp).fillMaxHeight().clip(MaterialTheme.shapes.medium),
model = media.coverImage.extraLarge,
contentDescription = media.preferred(),
contentDescription = media.preferred(titleLanguage),
contentScale = ContentScale.Crop,
error = rememberAsyncImagePainter(
model = media.coverImage.large,
Expand Down Expand Up @@ -85,7 +89,7 @@ fun AiringCard(
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Text(
text = media.preferred(),
text = media.preferred(titleLanguage),
style = MaterialTheme.typography.titleLarge,
overflow = TextOverflow.Ellipsis,
softWrap = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import dev.datlag.aniflow.anilist.AiringTodayStateMachine
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.common.shimmer
import dev.datlag.aniflow.other.StateSaver
import dev.datlag.aniflow.settings.model.AppSettings
import dev.datlag.tooling.decompose.lifecycle.collectAsStateWithLifecycle
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow

@Composable
fun AiringOverview(
state: Flow<AiringTodayStateMachine.State>,
titleLanguage: Flow<AppSettings.TitleLanguage?>,
onClick: (Medium) -> Unit
) {
val loadingState by state.collectAsStateWithLifecycle(StateSaver.Home.airingState)
Expand All @@ -40,6 +42,7 @@ fun AiringOverview(
is AiringTodayStateMachine.State.Success -> {
SuccessContent(
data = reachedState.data.Page?.airingSchedulesFilterNotNull() ?: emptyList(),
titleLanguage = titleLanguage,
onClick = onClick
)
}
Expand All @@ -65,6 +68,7 @@ private fun Loading() {
@Composable
private fun SuccessContent(
data: List<AiringQuery.AiringSchedule>,
titleLanguage: Flow<AppSettings.TitleLanguage?>,
onClick: (Medium) -> Unit
) {
val state = rememberLazyListState(
Expand All @@ -82,6 +86,7 @@ private fun SuccessContent(
items(data, key = { it.episode to it.media?.id }) { media ->
AiringCard(
airing = media,
titleLanguageFlow = titleLanguage,
modifier = Modifier
.height(150.dp)
.fillParentMaxWidth(fraction = 0.9F)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ import coil3.compose.rememberAsyncImagePainter
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.common.bottomShadowBrush
import dev.datlag.aniflow.common.preferred
import dev.datlag.aniflow.settings.model.AppSettings
import dev.datlag.aniflow.ui.custom.alignment.rememberParallaxAlignment
import dev.datlag.aniflow.ui.theme.SchemeTheme
import dev.datlag.aniflow.ui.theme.rememberSchemeThemeDominantColorState
import dev.datlag.tooling.decompose.lifecycle.collectAsStateWithLifecycle
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow

@OptIn(ExperimentalStdlibApi::class)
@Composable
fun MediumCard(
medium: Medium,
titleLanguageFlow: Flow<AppSettings.TitleLanguage?>,
modifier: Modifier = Modifier,
onClick: (Medium) -> Unit
) {
Expand Down Expand Up @@ -106,8 +110,10 @@ fun MediumCard(
.padding(top = 16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
val titleLanguage by titleLanguageFlow.collectAsStateWithLifecycle(null)

Text(
text = medium.preferred(),
text = medium.preferred(titleLanguage),
style = MaterialTheme.typography.titleLarge,
maxLines = if (medium.averageScore > 0F) {
1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.state.SeasonState
import dev.datlag.aniflow.common.shimmer
import dev.datlag.aniflow.other.StateSaver
import dev.datlag.aniflow.settings.model.AppSettings
import dev.datlag.tooling.decompose.lifecycle.collectAsStateWithLifecycle
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
Expand All @@ -30,6 +31,7 @@ import kotlinx.coroutines.flow.distinctUntilChanged
fun PopularSeasonOverview(
state: Flow<SeasonState>,
current: Boolean,
titleLanguage: Flow<AppSettings.TitleLanguage?>,
onClick: (Medium) -> Unit,
) {
val loadingState by state.collectAsStateWithLifecycle(
Expand All @@ -48,6 +50,7 @@ fun PopularSeasonOverview(
SuccessContent(
data = reachedState.data.Page?.mediaFilterNotNull() ?: emptyList(),
current = current,
titleLanguage = titleLanguage,
onClick = onClick
)
}
Expand All @@ -74,6 +77,7 @@ private fun Loading() {
private fun SuccessContent(
data: List<SeasonQuery.Medium>,
current: Boolean,
titleLanguage: Flow<AppSettings.TitleLanguage?>,
onClick: (Medium) -> Unit
) {
val listState = rememberLazyListState(
Expand All @@ -98,6 +102,7 @@ private fun SuccessContent(
itemsIndexed(data, key = { _, it -> it.id }) { _, medium ->
MediumCard(
medium = Medium(medium),
titleLanguageFlow = titleLanguage,
modifier = Modifier
.width(200.dp)
.height(280.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import dev.datlag.aniflow.anilist.TrendingQuery
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.common.shimmer
import dev.datlag.aniflow.other.StateSaver
import dev.datlag.aniflow.settings.model.AppSettings
import dev.datlag.tooling.decompose.lifecycle.collectAsStateWithLifecycle
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
Expand All @@ -27,6 +28,7 @@ import kotlinx.coroutines.flow.distinctUntilChanged
@Composable
fun TrendingOverview(
state: Flow<TrendingAnimeStateMachine.State>,
titleLanguage: Flow<AppSettings.TitleLanguage?>,
onClick: (Medium) -> Unit,
) {
val loadingState by state.collectAsStateWithLifecycle(StateSaver.Home.trendingState)
Expand All @@ -38,6 +40,7 @@ fun TrendingOverview(
is TrendingAnimeStateMachine.State.Success -> {
SuccessContent(
data = reachedState.data.Page?.mediaFilterNotNull() ?: emptyList(),
titleLanguage = titleLanguage,
onClick = onClick
)
}
Expand All @@ -63,6 +66,7 @@ private fun Loading() {
@Composable
private fun SuccessContent(
data: List<TrendingQuery.Medium>,
titleLanguage: Flow<AppSettings.TitleLanguage?>,
onClick: (Medium) -> Unit
) {
val listState = rememberLazyListState(
Expand All @@ -79,6 +83,7 @@ private fun SuccessContent(
itemsIndexed(data, key = { _, it -> it.id }) { _, medium ->
MediumCard(
medium = Medium(medium),
titleLanguageFlow = titleLanguage,
modifier = Modifier
.width(200.dp)
.height(280.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.datlag.aniflow.anilist.model.Character
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.type.MediaFormat
import dev.datlag.aniflow.anilist.type.MediaStatus
import dev.datlag.aniflow.settings.model.AppSettings
import dev.datlag.aniflow.ui.navigation.Component
import dev.datlag.aniflow.ui.navigation.ContentHolderComponent
import dev.datlag.aniflow.ui.navigation.DialogComponent
Expand All @@ -15,6 +16,7 @@ import kotlinx.coroutines.flow.StateFlow

interface MediumComponent : ContentHolderComponent {
val initialMedium: Medium
val titleLanguage: Flow<AppSettings.TitleLanguage?>

val mediumState: StateFlow<MediumStateMachine.State>
val isAdult: Flow<Boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ fun MediumScreen(component: MediumComponent) {
state = appBarState,
scrollBehavior = scrollState,
initialMedium = component.initialMedium,
titleLanguageFlow = component.titleLanguage,
mediumStateFlow = component.mediumState,
bannerImageFlow = component.bannerImage,
coverImage = coverImage,
Expand Down
Loading

0 comments on commit 38a55d2

Please sign in to comment.