-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add achivement click animation #1235 #1265
base: main
Are you sure you want to change the base?
Changes from 5 commits
737f67b
885a662
5265e35
131d8a7
598402d
80cd664
91d88e3
813b64f
1b38dfa
b61e6f6
78b1e2d
69d3235
fcfd005
220c85d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
package io.github.droidkaigi.confsched2023.achievements | ||
|
||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.WindowInsets | ||
import androidx.compose.foundation.layout.calculateEndPadding | ||
import androidx.compose.foundation.layout.calculateStartPadding | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.foundation.rememberScrollState | ||
|
@@ -16,8 +18,10 @@ import androidx.compose.material3.MaterialTheme | |
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.SnackbarHost | ||
import androidx.compose.material3.SnackbarHostState | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.remember | ||
|
@@ -31,8 +35,11 @@ import androidx.navigation.NavController | |
import androidx.navigation.NavGraph.Companion.findStartDestination | ||
import androidx.navigation.NavGraphBuilder | ||
import androidx.navigation.compose.composable | ||
import io.github.droidkaigi.confsched2023.achievements.ClickedAchievementState.Clicked | ||
import io.github.droidkaigi.confsched2023.achievements.component.AchievementHighlightAnimation | ||
import io.github.droidkaigi.confsched2023.achievements.section.AchievementList | ||
import io.github.droidkaigi.confsched2023.achievements.section.AchievementListUiState | ||
import io.github.droidkaigi.confsched2023.model.Achievement | ||
import io.github.droidkaigi.confsched2023.ui.SnackbarMessageEffect | ||
|
||
const val achievementsScreenRoute = "achievements" | ||
|
@@ -83,51 +90,85 @@ fun AchievementsScreen( | |
snackbarHostState = snackbarHostState, | ||
contentPadding = contentPadding, | ||
onReset = viewModel::onReset, | ||
showAnimation = { achievement -> viewModel.onClickAchievement(achievement) }, | ||
finishAnimation = viewModel::onFinishAnimation, | ||
onDisplayedInitialDialog = viewModel::onDisplayedInitialDialog, | ||
) | ||
} | ||
|
||
data class AchievementsScreenUiState( | ||
val achievementListUiState: AchievementListUiState, | ||
val isShowInitialDialog: Boolean, | ||
val clickedAchievement: ClickedAchievementState, | ||
) | ||
|
||
sealed interface ClickedAchievementState { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about naming this AchievementAnimationState and AchievementAnimationState.Animating AchievementAnimationState.NotAnimating? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The namings look better. I will rename them. |
||
data class Clicked( | ||
val achievement: Achievement, | ||
val animationRawId: Int, | ||
) : ClickedAchievementState | ||
data object NotClicked : ClickedAchievementState | ||
} | ||
|
||
@Composable | ||
private fun AchievementsScreen( | ||
uiState: AchievementsScreenUiState, | ||
snackbarHostState: SnackbarHostState, | ||
contentPadding: PaddingValues, | ||
onReset: () -> Unit, | ||
showAnimation: (Achievement) -> Unit, | ||
finishAnimation: () -> Unit, | ||
onDisplayedInitialDialog: () -> Unit, | ||
) { | ||
val layoutDirection = LocalLayoutDirection.current | ||
Scaffold( | ||
modifier = Modifier.testTag(AchievementsScreenTestTag), | ||
snackbarHost = { SnackbarHost(snackbarHostState) }, | ||
contentWindowInsets = WindowInsets( | ||
left = contentPadding.calculateLeftPadding(layoutDirection), | ||
top = contentPadding.calculateTopPadding(), | ||
right = contentPadding.calculateRightPadding(layoutDirection), | ||
bottom = contentPadding.calculateBottomPadding(), | ||
), | ||
content = { innerPadding -> | ||
if (uiState.isShowInitialDialog) { | ||
AchievementScreenDialog( | ||
onDismissRequest = onDisplayedInitialDialog, | ||
Box { | ||
Scaffold( | ||
modifier = Modifier.testTag(AchievementsScreenTestTag), | ||
snackbarHost = { SnackbarHost(snackbarHostState) }, | ||
contentWindowInsets = WindowInsets( | ||
left = contentPadding.calculateLeftPadding(layoutDirection), | ||
top = contentPadding.calculateTopPadding(), | ||
right = contentPadding.calculateRightPadding(layoutDirection), | ||
bottom = contentPadding.calculateBottomPadding(), | ||
), | ||
content = { innerPadding -> | ||
if (uiState.isShowInitialDialog) { | ||
AchievementScreenDialog( | ||
onDismissRequest = onDisplayedInitialDialog, | ||
) | ||
} | ||
AchievementList( | ||
uiState = uiState.achievementListUiState, | ||
contentPadding = innerPadding, | ||
onReset = onReset, | ||
showAnimation = showAnimation, | ||
modifier = Modifier.padding( | ||
top = innerPadding.calculateTopPadding(), | ||
start = innerPadding.calculateStartPadding(layoutDirection), | ||
end = innerPadding.calculateEndPadding(layoutDirection), | ||
), | ||
) | ||
}, | ||
) | ||
if (uiState.clickedAchievement is Clicked) { | ||
DisposableEffect(uiState.clickedAchievement) { | ||
onDispose { | ||
finishAnimation() | ||
} | ||
} | ||
AchievementList( | ||
uiState = uiState.achievementListUiState, | ||
contentPadding = innerPadding, | ||
onReset = onReset, | ||
modifier = Modifier.padding( | ||
top = innerPadding.calculateTopPadding(), | ||
start = innerPadding.calculateStartPadding(layoutDirection), | ||
end = innerPadding.calculateEndPadding(layoutDirection), | ||
), | ||
) | ||
}, | ||
) | ||
Surface( | ||
modifier = Modifier.fillMaxSize(), | ||
color = MaterialTheme.colorScheme.background.copy(alpha = 0.6F), | ||
) { | ||
AchievementHighlightAnimation( | ||
animationRawId = uiState.clickedAchievement.animationRawId, | ||
onFinishAnimation = { | ||
finishAnimation() | ||
}, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.github.droidkaigi.confsched2023.achievements.component | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import com.airbnb.lottie.compose.LottieAnimation | ||
import com.airbnb.lottie.compose.LottieCompositionSpec.RawRes | ||
import com.airbnb.lottie.compose.animateLottieCompositionAsState | ||
import com.airbnb.lottie.compose.rememberLottieComposition | ||
|
||
@Composable | ||
fun AchievementHighlightAnimation( | ||
animationRawId: Int, | ||
onFinishAnimation: () -> Unit, | ||
) { | ||
val lottieComposition by rememberLottieComposition(RawRes(animationRawId)) | ||
val progress by animateLottieCompositionAsState( | ||
composition = lottieComposition, | ||
isPlaying = true, | ||
restartOnPlay = true, | ||
) | ||
if (progress == 1f) { | ||
onFinishAnimation() | ||
} | ||
LottieAnimation( | ||
composition = lottieComposition, | ||
progress = { progress }, | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,40 @@ | ||
package io.github.droidkaigi.confsched2023.achievements.component | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.interaction.MutableInteractionSource | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.unit.dp | ||
import io.github.droidkaigi.confsched2023.model.Achievement | ||
import io.github.droidkaigi.confsched2023.model.AchievementAnimation | ||
|
||
@Composable | ||
fun AchievementImage( | ||
achievementAnimation: AchievementAnimation, | ||
modifier: Modifier = Modifier, | ||
showAnimation: (Achievement) -> Unit, | ||
) { | ||
val interactionSource = remember { MutableInteractionSource() } | ||
Image( | ||
painter = painterResource(id = achievementAnimation.getDrawableResId()), | ||
contentDescription = achievementAnimation.contentDescription, | ||
modifier = modifier | ||
.padding(horizontal = 21.dp), | ||
.padding(horizontal = 21.dp) | ||
.then( | ||
if (achievementAnimation.hasAchievement) { | ||
Modifier.clickable( | ||
interactionSource = interactionSource, | ||
indication = null, | ||
) { | ||
showAnimation(achievementAnimation.achievement) | ||
} | ||
} else { | ||
Modifier | ||
}, | ||
), | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We would like to promote the best practice that a StateHolder should be notified events.
So how about naming this
onAchivementClicked
and judge if we run a animation in ViewModel forshowAnimation
?And use "onAnimationFinished" as a name for
finishAnimation
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed showAnimation to onAchivementClick and finishAnimation to onAnimationFinish.