Skip to content

Commit

Permalink
achievements: Show achievement percentage for AchievementsList
Browse files Browse the repository at this point in the history
  • Loading branch information
Omico committed Jun 15, 2024
1 parent b16ce76 commit bb68a14
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2024 Omico. All Rights Reserved.
*/
package dev.omico.wwm.feature.achievements.component

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import dev.omico.wwm.feature.achievements.AchievementsUiState

@Immutable
data class AchievementGroupState(
val groupId: Int,
val achievementCount: Int,
val markedAchievementCount: Int,
) {
@Suppress("MagicNumber")
val achievementPercentage: String = "${markedAchievementCount * 100 / achievementCount}%"
}

@Composable
fun AchievementGroupState(state: AchievementsUiState, groupId: Int): AchievementGroupState {
val achievementCount: Int by remember(groupId, state.achievements) {
derivedStateOf {
state.achievements.count { achievement ->
achievement.groupId == groupId &&
achievement.hidden.not()
}
}
}
val markedAchievementCount: Int by remember(groupId, state.achievements, state.markedAchievementIds) {
derivedStateOf {
state.achievements.count { achievement ->
achievement.groupId == groupId &&
achievement.id in state.markedAchievementIds &&
achievement.hidden.not()
}
}
}
val groupState: AchievementGroupState by remember(groupId, achievementCount, markedAchievementCount) {
derivedStateOf {
AchievementGroupState(
groupId = groupId,
achievementCount = achievementCount,
markedAchievementCount = markedAchievementCount,
)
}
}
return groupState
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,21 @@ internal fun AchievementsDetailTopAppBar(
) {
TopAppBar(
title = {
val achievementCount by remember(achievementGroupId) {
val groupState = AchievementGroupState(
state = state,
groupId = achievementGroupId,
)
val title by remember(achievementGroupName, groupState) {
derivedStateOf {
state.achievements.count { achievement ->
achievement.groupId == achievementGroupId &&
achievement.hidden.not()
buildString {
append(achievementGroupName)
append(" ")
append(groupState.achievementPercentage)
append(" ")
append("(${groupState.markedAchievementCount}/${groupState.achievementCount})")
}
}
}
val markedAchievementCount by remember(achievementGroupId, state.markedAchievementIds) {
derivedStateOf {
state.achievements.count { achievement ->
achievement.groupId == achievementGroupId &&
achievement.id in state.markedAchievementIds &&
achievement.hidden.not()
}
}
}
val achievementPercentage by remember(achievementCount, markedAchievementCount) {
derivedStateOf { markedAchievementCount * 100 / achievementCount }
}
val title by remember(
achievementGroupName,
achievementPercentage,
markedAchievementCount,
achievementCount,
) {
derivedStateOf {
"$achievementGroupName $achievementPercentage% ($markedAchievementCount/$achievementCount)"
}
}
Text(text = title)
},
navigationIcon = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,19 @@ private fun LazyListScope.achievementsCategoryItem(
key = WwAchievementGroup::id,
contentType = { achievementGroup -> achievementGroup::class },
itemContent = { achievementGroup ->
val achievementGroupName = rememberWwText(
multiText = state.multiText,
name = achievementGroup.name,
)
val groupState = AchievementGroupState(
state = state,
groupId = achievementGroup.id,
)
val text by remember(achievementGroupName, groupState) {
derivedStateOf { "$achievementGroupName ${groupState.achievementPercentage}" }
}
AchievementsListItem(
text = rememberWwText(
multiText = state.multiText,
name = achievementGroup.name,
),
text = text,
modifier = run {
Modifier
.padding(start = 16.dp)
Expand Down

0 comments on commit bb68a14

Please sign in to comment.