-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
achievements: Show achievement percentage for AchievementsList
- Loading branch information
Showing
3 changed files
with
75 additions
and
30 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...c/commonMain/kotlin/dev/omico/wwm/feature/achievements/component/AchievementGroupState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters