Skip to content

Commit

Permalink
make sure the favorite information is available
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Apr 26, 2024
1 parent e2ab9f5 commit eb1f62d
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class CharacterStateMachine(
}

sealed interface State {
val isLoading: Boolean
get() = this is Loading

val isSuccess: Boolean
get() = this is Success

data class Loading(internal val query: CharacterQuery) : State {
constructor(id: Int) : this(
query = CharacterQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ class MediumStateMachine(
}

sealed interface State {

val isLoading: Boolean
get() = this is Loading

val isSuccess: Boolean
get() = this is Success

data class Loading(
internal val query: MediumQuery
) : State {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +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.model.Character
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.anilist.type.MediaFormat
Expand All @@ -14,6 +15,7 @@ import kotlinx.coroutines.flow.StateFlow
interface MediumComponent : ContentHolderComponent {
val initialMedium: Medium

val mediumState: StateFlow<MediumStateMachine.State>
val bannerImage: StateFlow<String?>
val coverImage: StateFlow<Medium.CoverImage>
val title: StateFlow<Medium.Title>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ fun MediumScreen(component: MediumComponent) {
CollapsingToolbar(
state = appBarState,
scrollBehavior = scrollState,
mediumStateFlow = component.mediumState,
bannerImageFlow = component.bannerImage,
coverImage = coverImage,
titleFlow = component.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class MediumScreenComponent(
crashlytics = di.nullableFirebaseInstance()?.crashlytics,
id = initialMedium.id
)
private val mediumState = mediumStateMachine.state.flowOn(
override val mediumState = mediumStateMachine.state.flowOn(
context = ioDispatcher()
).stateIn(
scope = ioScope(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.datlag.aniflow.ui.navigation.screen.medium.component

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand All @@ -26,6 +29,7 @@ import dev.chrisbanes.haze.hazeChild
import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi
import dev.chrisbanes.haze.materials.HazeMaterials
import dev.datlag.aniflow.LocalHaze
import dev.datlag.aniflow.anilist.MediumStateMachine
import dev.datlag.aniflow.anilist.model.Medium
import dev.datlag.aniflow.common.notPreferred
import dev.datlag.aniflow.common.preferred
Expand All @@ -39,6 +43,7 @@ import kotlin.math.min
fun CollapsingToolbar(
state: TopAppBarState,
scrollBehavior: TopAppBarScrollBehavior,
mediumStateFlow: StateFlow<MediumStateMachine.State>,
bannerImageFlow: StateFlow<String?>,
coverImage: Medium.CoverImage,
titleFlow: StateFlow<Medium.Title>,
Expand Down Expand Up @@ -141,30 +146,37 @@ fun CollapsingToolbar(
}
},
actions = {
val mediumState by mediumStateFlow.collectAsStateWithLifecycle()
val isFavoriteBlocked by isFavoriteBlockedFlow.collectAsStateWithLifecycle()
val isFavorite by isFavoriteFlow.collectAsStateWithLifecycle()
var favoriteChanged by remember(isFavorite) { mutableStateOf<Boolean?>(null) }

IconButton(
modifier = if (isCollapsed) {
Modifier
} else {
Modifier.background(MaterialTheme.colorScheme.surface.copy(alpha = 0.75F), CircleShape)
},
onClick = {
favoriteChanged = !(favoriteChanged ?: isFavorite)
onToggleFavorite()
},
enabled = !isFavoriteBlocked
AnimatedVisibility(
visible = mediumState.isSuccess,
enter = fadeIn(),
exit = fadeOut()
) {
Icon(
imageVector = if (favoriteChanged ?: isFavorite) {
Icons.Default.Favorite
IconButton(
modifier = if (isCollapsed) {
Modifier
} else {
Icons.Default.FavoriteBorder
Modifier.background(MaterialTheme.colorScheme.surface.copy(alpha = 0.75F), CircleShape)
},
contentDescription = null
)
onClick = {
favoriteChanged = !(favoriteChanged ?: isFavorite)
onToggleFavorite()
},
enabled = !isFavoriteBlocked
) {
Icon(
imageVector = if (favoriteChanged ?: isFavorite) {
Icons.Default.Favorite
} else {
Icons.Default.FavoriteBorder
},
contentDescription = null
)
}
}
},
scrollBehavior = scrollBehavior,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ fun CharacterDialog(component: CharacterComponent) {
contentAlignment = Alignment.Center
) {
val image by component.image.collectAsStateWithLifecycle()
val state by component.state.collectAsStateWithLifecycle()
val isFavoriteBlocked by component.isFavoriteBlocked.collectAsStateWithLifecycle()
val isFavorite by component.isFavorite.collectAsStateWithLifecycle()
var favoriteChanged by remember(isFavorite) { mutableStateOf<Boolean?>(null) }
Expand Down Expand Up @@ -95,22 +96,28 @@ fun CharacterDialog(component: CharacterComponent) {
contentDescription = component.initialChar.preferredName()
)

IconButton(
this@ModalBottomSheet.AnimatedVisibility(
modifier = Modifier.align(Alignment.CenterEnd),
onClick = {
favoriteChanged = !(favoriteChanged ?: isFavorite)
component.toggleFavorite()
},
enabled = !isFavoriteBlocked
visible = state.isSuccess,
enter = fadeIn(),
exit = fadeOut()
) {
Icon(
imageVector = if (favoriteChanged ?: isFavorite) {
Icons.Default.Favorite
} else {
Icons.Default.FavoriteBorder
IconButton(
onClick = {
favoriteChanged = !(favoriteChanged ?: isFavorite)
component.toggleFavorite()
},
contentDescription = null,
)
enabled = !isFavoriteBlocked
) {
Icon(
imageVector = if (favoriteChanged ?: isFavorite) {
Icons.Default.Favorite
} else {
Icons.Default.FavoriteBorder
},
contentDescription = null,
)
}
}
}

Expand Down

0 comments on commit eb1f62d

Please sign in to comment.