Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
meikpiep committed Nov 29, 2024
1 parent df2caa0 commit 4b35bd1
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ class MainViewModel :
private val calculationService: GridCalculationService by inject()
private val game: Game by inject()

private val _uiState = MutableStateFlow(initialUiState())
private val _nextGridState = MutableStateFlow(NextGridState.CALCULATED)
private val _fastFinishingModeState = MutableStateFlow(FastFinishingModeState.INACTIVE)
private val mutableUiState = MutableStateFlow(initialUiState())
private val mutableNextGridState = MutableStateFlow(NextGridState.CALCULATED)
private val mutableFastFinishingModeState = MutableStateFlow(FastFinishingModeState.INACTIVE)

val uiState: StateFlow<MainUiStateWithGrid> = _uiState.asStateFlow()
val nextGridState: StateFlow<NextGridState> = _nextGridState.asStateFlow()
val fastFinishingModeState: StateFlow<FastFinishingModeState> = _fastFinishingModeState.asStateFlow()
val uiState: StateFlow<MainUiStateWithGrid> = mutableUiState.asStateFlow()
val nextGridState: StateFlow<NextGridState> = mutableNextGridState.asStateFlow()
val fastFinishingModeState: StateFlow<FastFinishingModeState> = mutableFastFinishingModeState.asStateFlow()

init {
calculationService.addListener(createGridCalculationListener())
Expand All @@ -69,24 +69,24 @@ class MainViewModel :
private fun createGridCalculationListener(): GridCalculationListener =
object : GridCalculationListener {
override fun startingCurrentGridCalculation() {
_uiState.value = MainUiStateWithGrid(MainUiState.CALCULATING_NEW_GRID, game.grid)
mutableUiState.value = MainUiStateWithGrid(MainUiState.CALCULATING_NEW_GRID, game.grid)
}

override fun currentGridCalculated() {
_uiState.value = MainUiStateWithGrid(MainUiState.PLAYING, game.grid)
mutableUiState.value = MainUiStateWithGrid(MainUiState.PLAYING, game.grid)
}

override fun startingNextGridCalculation() {
_nextGridState.value = NextGridState.CURRENTLY_CALCULATING
mutableNextGridState.value = NextGridState.CURRENTLY_CALCULATING
}

override fun nextGridCalculated() {
_nextGridState.value = NextGridState.CALCULATED
mutableNextGridState.value = NextGridState.CALCULATED
}
}

override fun freshGridWasCreated() {
_uiState.value = MainUiStateWithGrid(MainUiState.PLAYING, game.grid)
mutableUiState.value = MainUiStateWithGrid(MainUiState.PLAYING, game.grid)
}

private fun initialUiState() =
Expand All @@ -100,11 +100,11 @@ class MainViewModel :
)

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

override fun changedGameMode() {
_fastFinishingModeState.value =
mutableFastFinishingModeState.value =
if (game.isInFastFinishingMode()) {
FastFinishingModeState.ACTIVE
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import android.view.ViewGroup
import android.widget.CompoundButton
import androidx.core.view.forEach
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import kotlinx.coroutines.launch
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.piepmeyer.gauguin.R
Expand All @@ -23,17 +28,15 @@ import org.piepmeyer.gauguin.options.SingleCageUsage
import org.piepmeyer.gauguin.preferences.ApplicationPreferences
import org.piepmeyer.gauguin.ui.difficulty.MainGameDifficultyLevelBalloon

class GridCellOptionsFragment : Fragment(R.layout.fragment_new_game_options), KoinComponent {
private lateinit var variant: GameVariant
class GridCellOptionsFragment :
Fragment(R.layout.fragment_new_game_options),
KoinComponent {
private val applicationPreferences: ApplicationPreferences by inject()
private var gridPreviewHolder: GridPreviewHolder? = null
private val viewModel: NewGameViewModel by viewModels()

private lateinit var binding: FragmentNewGameOptionsBinding
private val rater = GameDifficultyRater()

fun setGridPreviewHolder(gridPreviewHolder: GridPreviewHolder) {
this.gridPreviewHolder = gridPreviewHolder
}

override fun onCreateView(
inflater: LayoutInflater,
parent: ViewGroup?,
Expand All @@ -42,6 +45,8 @@ class GridCellOptionsFragment : Fragment(R.layout.fragment_new_game_options), Ko
binding = FragmentNewGameOptionsBinding.inflate(inflater, parent, false)

binding.difficultyInfoIcon.setOnClickListener {
val variant = viewModel.gameVariantState.value

val difficultyOrNull =
if (rater.isSupported(variant) && variant.options.difficultySetting != DifficultySetting.ANY) {
variant.options.difficultySetting.gameDifficulty
Expand Down Expand Up @@ -94,7 +99,13 @@ class GridCellOptionsFragment : Fragment(R.layout.fragment_new_game_options), Ko
}
binding.showOperationsSwitch.isChecked = applicationPreferences.showOperators()

gameVariantChanged()
lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.gameVariantState.collect {
gameVariantChanged(it)
}
}
}
}

private fun updateVisibility(tab: TabLayout.Tab) {
Expand Down Expand Up @@ -134,13 +145,15 @@ class GridCellOptionsFragment : Fragment(R.layout.fragment_new_game_options), Ko
val singleCageOption = singleCellUsageIdMap[binding.singleCellUsageChipGroup.checkedChipId]!!

applicationPreferences.singleCageUsage = singleCageOption
gridPreviewHolder?.refreshGrid()
viewModel.calculateGrid()
}

binding.singleCellUsageChipGroup.check(
singleCellUsageIdMap.filterValues {
it == applicationPreferences.singleCageUsage
}.keys.first(),
singleCellUsageIdMap
.filterValues {
it == applicationPreferences.singleCageUsage
}.keys
.first(),
)
}

Expand All @@ -157,13 +170,15 @@ class GridCellOptionsFragment : Fragment(R.layout.fragment_new_game_options), Ko
val operations = operationsIdMap[binding.operationsChipGroup.checkedChipId]!!

applicationPreferences.operations = operations
gridPreviewHolder?.refreshGrid()
viewModel.calculateGrid()
}

binding.operationsChipGroup.check(
operationsIdMap.filterValues {
it == applicationPreferences.operations
}.keys.first(),
operationsIdMap
.filterValues {
it == applicationPreferences.operations
}.keys
.first(),
)
}

Expand All @@ -183,13 +198,15 @@ class GridCellOptionsFragment : Fragment(R.layout.fragment_new_game_options), Ko
val digits = digitsIdMap[binding.digitsChipGroup.checkedChipId]!!

applicationPreferences.digitSetting = digits
gridPreviewHolder?.refreshGrid()
viewModel.calculateGrid()
}

binding.digitsChipGroup.check(
digitsIdMap.filterValues {
it == applicationPreferences.digitSetting
}.keys.first(),
digitsIdMap
.filterValues {
it == applicationPreferences.digitSetting
}.keys
.first(),
)
}

Expand All @@ -208,13 +225,15 @@ class GridCellOptionsFragment : Fragment(R.layout.fragment_new_game_options), Ko
val digitdifficulty = difficultyIdMap[binding.difficultyChipGroup.checkedChipId]!!

applicationPreferences.difficultySetting = digitdifficulty
gridPreviewHolder?.refreshGrid()
viewModel.calculateGrid()
}

binding.difficultyChipGroup.check(
difficultyIdMap.filterValues {
it == applicationPreferences.difficultySetting
}.keys.first(),
difficultyIdMap
.filterValues {
it == applicationPreferences.difficultySetting
}.keys
.first(),
)
}

Expand All @@ -232,52 +251,44 @@ class GridCellOptionsFragment : Fragment(R.layout.fragment_new_game_options), Ko
val numeralSystem = numeralSystemIdMap[binding.numeralSystemChipGroup.checkedChipId]!!

applicationPreferences.numeralSystem = numeralSystem
gridPreviewHolder?.updateNumeralSystem()
viewModel.calculateGrid()
}

binding.numeralSystemChipGroup.check(
numeralSystemIdMap.filterValues {
it == applicationPreferences.numeralSystem
}.keys.first(),
numeralSystemIdMap
.filterValues {
it == applicationPreferences.numeralSystem
}.keys
.first(),
)
}

private fun showOperationsChanged(isChecked: Boolean) {
applicationPreferences.setShowOperators(isChecked)
gridPreviewHolder!!.refreshGrid()
}

fun setGameVariant(variant: GameVariant) {
this.variant = variant

if (this::binding.isInitialized) {
gameVariantChanged()
}
viewModel.calculateGrid()
}

private fun gameVariantChanged() {
if (this::variant.isInitialized) {
val supportedVariant = rater.isSupported(variant)
private fun gameVariantChanged(variant: GameVariant) {
val supportedVariant = rater.isSupported(variant)

binding.difficultyChipGroup.forEach { it.isEnabled = supportedVariant }
binding.difficultyChipGroup.forEach { it.isEnabled = supportedVariant }

val numbersBadgeShouldBeVisible =
binding.digitsChipGroup.checkedChipId != binding.chipDigitsFromOne.id ||
binding.numeralSystemChipGroup.checkedChipId != binding.chipNumeralSystemDecimal.id
val advancedBadgeShouldBeVisible =
binding.singleCellUsageChipGroup.checkedChipId != binding.chipSingleCagesFixedNumber.id ||
!binding.showOperationsSwitch.isChecked
val numbersBadgeShouldBeVisible =
binding.digitsChipGroup.checkedChipId != binding.chipDigitsFromOne.id ||
binding.numeralSystemChipGroup.checkedChipId != binding.chipNumeralSystemDecimal.id
val advancedBadgeShouldBeVisible =
binding.singleCellUsageChipGroup.checkedChipId != binding.chipSingleCagesFixedNumber.id ||
!binding.showOperationsSwitch.isChecked

setBadgeVisibility(
numbersBadgeShouldBeVisible,
binding.newGameOptionsTablayout.getTabAt(1)!!,
)
setBadgeVisibility(
numbersBadgeShouldBeVisible,
binding.newGameOptionsTablayout.getTabAt(1)!!,
)

setBadgeVisibility(
advancedBadgeShouldBeVisible,
binding.newGameOptionsTablayout.getTabAt(2)!!,
)
}
setBadgeVisibility(
advancedBadgeShouldBeVisible,
binding.newGameOptionsTablayout.getTabAt(2)!!,
)
}

private fun setBadgeVisibility(
Expand Down

This file was deleted.

Loading

0 comments on commit 4b35bd1

Please sign in to comment.