Skip to content

Commit

Permalink
Show the game solved dialog if the app was just started and the curre…
Browse files Browse the repository at this point in the history
…nt grid has already been solved
  • Loading branch information
meikpiep committed Nov 9, 2024
1 parent 19ee28a commit 1970210
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 54 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Show the game solved dialog if the grid shown at the start of the app has alredy been solved.

### Deprecated

### Removed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.piepmeyer.gauguin.game.GameLifecycle
import org.piepmeyer.gauguin.preferences.StatisticsManager
import org.piepmeyer.gauguin.preferences.TypeOfSolution
import org.piepmeyer.gauguin.ui.statistics.StatisticsActivity
import kotlin.time.Duration.Companion.milliseconds

class GameSolvedFragment :
Fragment(R.layout.fragment_main_game_solved),
Expand Down Expand Up @@ -51,8 +52,11 @@ class GameSolvedFragment :
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect {
when (it.state) {
MainUiState.SOLVED -> puzzleSolved(false)
MainUiState.SOLVED_BY_REVEAL -> puzzleSolved(true)
MainUiState.SOLVED, MainUiState.ALREADY_SOLVED -> {
puzzleSolved()

binding.gameSolvedCardView.visibility = View.VISIBLE
}
MainUiState.CALCULATING_NEW_GRID, MainUiState.PLAYING -> {
binding.gameSolvedCardView.visibility = View.GONE
}
Expand All @@ -79,44 +83,51 @@ class GameSolvedFragment :
}
}

fun puzzleSolved(troughReveal: Boolean) {
private fun puzzleSolved() {
this.context?.let {
if (!troughReveal) {
val icon =
when (statisticsManager.typeOfSolution(game.grid)) {
TypeOfSolution.FirstGame -> R.drawable.trophy_variant_outline
TypeOfSolution.FirstGameOfKind -> R.drawable.trophy_variant_outline
TypeOfSolution.BestTimeOfKind -> R.drawable.podium_gold
TypeOfSolution.Regular -> null
}
val typeOfSolution =
if (game.grid.isCheated()) {
TypeOfSolution.Regular
} else {
statisticsManager.typeOfSolution(game.grid)
}

val text =
when (statisticsManager.typeOfSolution(game.grid)) {
TypeOfSolution.FirstGame -> it.getString(R.string.puzzle_solved_type_of_solution_first_game_solved)
TypeOfSolution.FirstGameOfKind -> it.getString(R.string.puzzle_solved_type_of_solution_first_game_of_kind_solved)
TypeOfSolution.BestTimeOfKind -> it.getString(R.string.puzzle_solved_type_of_solution_best_time_of_kind_solved)
TypeOfSolution.Regular ->
val icon =
when (typeOfSolution) {
TypeOfSolution.FirstGame -> R.drawable.trophy_variant_outline
TypeOfSolution.FirstGameOfKind -> R.drawable.trophy_variant_outline
TypeOfSolution.BestTimeOfKind -> R.drawable.podium_gold
TypeOfSolution.Regular -> null
}

val text =
when (typeOfSolution) {
TypeOfSolution.FirstGame -> it.getString(R.string.puzzle_solved_type_of_solution_first_game_solved)
TypeOfSolution.FirstGameOfKind -> it.getString(R.string.puzzle_solved_type_of_solution_first_game_of_kind_solved)
TypeOfSolution.BestTimeOfKind -> it.getString(R.string.puzzle_solved_type_of_solution_best_time_of_kind_solved)
TypeOfSolution.Regular -> {
val bestTime = statisticsManager.getBestTime(game.grid)

if (bestTime > 0.milliseconds) {
it.getString(
R.string.puzzle_solved_type_of_solution_regular_display_best_time,
Utils.displayableGameDuration(statisticsManager.getBestTime(game.grid)),
Utils.displayableGameDuration(bestTime),
)
} else {
""
}
}

if (icon != null) {
binding.detailsIcon.setImageResource(icon)
binding.detailsIcon.visibility = View.VISIBLE
} else {
binding.detailsIcon.visibility = View.INVISIBLE
}

binding.detailsText.text = text
binding.detailsText.visibility = View.VISIBLE
if (icon != null) {
binding.detailsIcon.setImageResource(icon)
binding.detailsIcon.visibility = View.VISIBLE
} else {
binding.detailsIcon.visibility = View.INVISIBLE
binding.detailsText.visibility = View.INVISIBLE
}

binding.gameSolvedCardView.visibility = View.VISIBLE
binding.detailsText.text = text
binding.detailsText.visibility = View.VISIBLE
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class GameTopFragment :
lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect {
if (it.state == MainUiState.PLAYING) {
if (it.state == MainUiState.PLAYING || it.state == MainUiState.ALREADY_SOLVED) {
freshGridWasCreated()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ class MainActivity : AppCompatActivity() {
binding.gridview.invalidate()
}

MainUiState.SOLVED_BY_REVEAL -> bottomAppBarService.updateAppBarState()
MainUiState.ALREADY_SOLVED -> bottomAppBarService.updateAppBarState()
MainUiState.SOLVED -> {
bottomAppBarService.updateAppBarState()

KonfettiStarter(binding.konfettiView).startKonfetti()
if (!game.grid.isCheated()) {
KonfettiStarter(binding.konfettiView).startKonfetti()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum class MainUiState {
PLAYING,
CALCULATING_NEW_GRID,
SOLVED,
SOLVED_BY_REVEAL,
ALREADY_SOLVED,
}

data class MainUiStateWithGrid(
Expand All @@ -45,7 +45,7 @@ class MainViewModel :
private val calculationService: GridCalculationService by inject()
private val game: Game by inject()

private val _uiState = MutableStateFlow(MainUiStateWithGrid(MainUiState.PLAYING, game.grid))
private val _uiState = MutableStateFlow(initialUiState())
private val _nextGridState = MutableStateFlow(NextGridState.CALCULATED)
private val _fastFinishingModeState = MutableStateFlow(FastFinishingModeState.INACTIVE)

Expand Down Expand Up @@ -89,13 +89,18 @@ class MainViewModel :
_uiState.value = MainUiStateWithGrid(MainUiState.PLAYING, game.grid)
}

override fun puzzleSolved(troughReveal: Boolean) {
_uiState.value =
if (troughReveal) {
MainUiStateWithGrid(MainUiState.SOLVED_BY_REVEAL, game.grid)
private fun initialUiState() =
MainUiStateWithGrid(
if (game.grid.isSolved()) {
MainUiState.ALREADY_SOLVED
} else {
MainUiStateWithGrid(MainUiState.SOLVED, game.grid)
}
MainUiState.PLAYING
},
game.grid,
)

override fun puzzleSolved() {
_uiState.value = MainUiStateWithGrid(MainUiState.SOLVED, game.grid)
}

override fun changedGameMode() {
Expand Down
25 changes: 11 additions & 14 deletions gauguin-core/src/main/kotlin/org/piepmeyer/gauguin/game/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,16 @@ data class Game(
logger.info { "Updated grid to: ${grid.detailedToString()}" }
}

fun enterNumber(
number: Int,
reveal: Boolean = false,
) {
fun enterNumber(number: Int) {
val selectedCell = grid.selectedCell ?: return
if (!grid.isActive || selectedCell.isCheated) {
if (!grid.isActive) {
return
}

gridHasBeenPlayed()

clearLastModified()
if (!reveal) {
undoManager.saveUndo(selectedCell, false)
}
undoManager.saveUndo(selectedCell, false)
selectedCell.setUserValueExtern(number)

if (applicationPreferences.removePencils()) {
Expand All @@ -93,15 +88,17 @@ data class Game(

ensureNotInFastFinishingMode()

if (!reveal) {
val cheated = grid.isCheated()

if (!cheated) {
statisticsManager.puzzleSolved(grid)
statisticsManager.storeStatisticsAfterFinishedGame(grid)
}

statisticsManager.storeStreak(!reveal)
statisticsManager.storeStreak(!cheated)

vipSolvedListeners.forEach { it.puzzleSolved(reveal) }
solvedListeners.forEach { it.puzzleSolved(reveal) }
vipSolvedListeners.forEach { it.puzzleSolved() }
solvedListeners.forEach { it.puzzleSolved() }
}

grid.userValueChanged()
Expand All @@ -112,9 +109,9 @@ data class Game(

fun revealCell(cell: GridCell) {
if (!cell.isUserValueCorrect) {
selectCell(cell)
enterNumber(cell.value, reveal = true)
cell.isCheated = true
selectCell(cell)
enterNumber(cell.value)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.piepmeyer.gauguin.game

fun interface GameSolvedListener {
fun puzzleSolved(troughReveal: Boolean)
fun puzzleSolved()
}

0 comments on commit 1970210

Please sign in to comment.