Skip to content

Commit

Permalink
Avoids showing an initial fake grid when opening the new grid screen …
Browse files Browse the repository at this point in the history
…for the first time after app start
  • Loading branch information
meikpiep committed Jan 9, 2025
1 parent 2ab8f3e commit a2a9dc5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,19 @@ class GridShapeOptionsFragment :

private fun previewGridCalculated(gridPreview: GridPreviewState) {
binding.newGridPreview.let {
it.grid = gridPreview.grid
it.rebuildCellsFromGrid()
it.updateTheme()
it.setPreviewStillCalculating(gridPreview.stillCalculating)
it.invalidate()
it.visibility =
if (gridPreview.grid != null) {
View.VISIBLE
} else {
View.INVISIBLE
}
if (gridPreview.grid != null) {
it.grid = gridPreview.grid
it.rebuildCellsFromGrid()
it.updateTheme()
it.setPreviewStillCalculating(gridPreview.calculationState == GridCalculationState.STILL_CALCULATING)
it.invalidate()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ import org.koin.core.component.inject
import org.piepmeyer.gauguin.calculation.GridCalculationService
import org.piepmeyer.gauguin.calculation.GridPreviewCalculationService
import org.piepmeyer.gauguin.calculation.GridPreviewListener
import org.piepmeyer.gauguin.creation.GridBuilder
import org.piepmeyer.gauguin.creation.GridCalculatorFactory
import org.piepmeyer.gauguin.game.GameLifecycle
import org.piepmeyer.gauguin.grid.Grid
import org.piepmeyer.gauguin.grid.GridSize
import org.piepmeyer.gauguin.options.GameVariant
import org.piepmeyer.gauguin.preferences.ApplicationPreferences

enum class GridCalculationState {
CALCULATED,
STILL_CALCULATING,
NO_GRID_AVAILABLE_YET,
}

data class GridPreviewState(
val grid: Grid,
val stillCalculating: Boolean,
val grid: Grid?,
val calculationState: GridCalculationState,
)

enum class GridCalculationAlgorithm {
Expand Down Expand Up @@ -68,17 +73,9 @@ class NewGameViewModel :
val grid = calculationService.consumeNextGrid()
previewService.takeCalculatedGrid(grid)

return GridPreviewState(grid, false)
return GridPreviewState(grid, GridCalculationState.CALCULATED)
} else {
return GridPreviewState(
GridBuilder(2)
.addSingleCage(1, 0)
.addSingleCage(2, 1)
.addSingleCage(2, 2)
.addSingleCage(1, 3)
.createGrid(),
true,
)
return GridPreviewState(null, GridCalculationState.NO_GRID_AVAILABLE_YET)
}
}

Expand Down Expand Up @@ -109,11 +106,19 @@ class NewGameViewModel :
previewStillCalculating: Boolean,
) {
grid.options.numeralSystem = applicationPreferences.gameOptionsVariant.numeralSystem
mutablePreviewGridState.value = GridPreviewState(grid, previewStillCalculating)

val calculationState =
if (previewStillCalculating) {
GridCalculationState.STILL_CALCULATING
} else {
GridCalculationState.CALCULATED
}

mutablePreviewGridState.value = GridPreviewState(grid, calculationState)
}

override fun previewGridCalculated(grid: Grid) {
mutablePreviewGridState.value = GridPreviewState(grid, false)
mutablePreviewGridState.value = GridPreviewState(grid, GridCalculationState.CALCULATED)
}

fun calculateGrid() {
Expand Down

0 comments on commit a2a9dc5

Please sign in to comment.