Skip to content

Commit

Permalink
Extract more UI state control in ViewModel (partially implements #30)
Browse files Browse the repository at this point in the history
  • Loading branch information
opatry committed Jan 17, 2022
1 parent b4db4f0 commit ad38803
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ class WordleViewModel(inDictionary: List<String>, private val repository: Wordle
private set
var requestedDialog by mutableStateOf<AppDialog?>(null)
private set
private val _actionsEnabled: Boolean
get() = rules != null && grid.isNotEmpty() && alphabet.isNotEmpty() && requestedDialog == null
var actionsEnabled by mutableStateOf(_actionsEnabled)
private set

init {
scope.launch(Dispatchers.Main) {
Expand Down Expand Up @@ -221,6 +225,7 @@ class WordleViewModel(inDictionary: List<String>, private val repository: Wordle
}

updateEndOfGame()
actionsEnabled = _actionsEnabled

// save data and compute stats
if (rules.state !is State.Playing && answer.isNotEmpty()) {
Expand Down Expand Up @@ -279,6 +284,7 @@ class WordleViewModel(inDictionary: List<String>, private val repository: Wordle
updateGrid()
updateAlphabet()
updateEndOfGame()
actionsEnabled = _actionsEnabled
}

fun pushMessage(message: String) {
Expand All @@ -294,9 +300,11 @@ class WordleViewModel(inDictionary: List<String>, private val repository: Wordle
fun requestDialog(dialog: AppDialog) {
// FIXME shouldn't we check for another one is already being displayed? priority? stack?
requestedDialog = dialog
actionsEnabled = _actionsEnabled
}

fun dismissDialog() {
requestedDialog = null
actionsEnabled = _actionsEnabled
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ fun WordleViewModel.handleKey(key: Key, letter: Char): Boolean {
fun GameScreen(settings: Settings, viewModel: WordleViewModel) {
val userFeedback by rememberUpdatedState(viewModel.userFeedback)
val requestedDialog by rememberUpdatedState(viewModel.requestedDialog)
// FIXME "complex" decision to move at ViewModel level
val actionsEnabled = requestedDialog == null && viewModel.grid.isNotEmpty() && viewModel.alphabet.isNotEmpty()

val actionsEnabled by rememberUpdatedState(viewModel.actionsEnabled)
val statistics by rememberUpdatedState(viewModel.statistics)
val grid by rememberUpdatedState(viewModel.grid)
val alphabet by rememberUpdatedState(viewModel.alphabet)

val focusRequester = FocusRequester()
LaunchedEffect(Unit) {
Expand Down Expand Up @@ -190,12 +190,12 @@ fun GameScreen(settings: Settings, viewModel: WordleViewModel) {
)

// FIXME "complex" decision to move at ViewModel level
if (viewModel.grid.isNotEmpty() && viewModel.alphabet.isNotEmpty()) {
WordleGrid(viewModel.grid)
if (grid.isNotEmpty() && alphabet.isNotEmpty()) {
WordleGrid(grid)

Spacer(Modifier.weight(1f))

Alphabet(viewModel.alphabet, enabled = actionsEnabled) { letter ->
Alphabet(alphabet, enabled = actionsEnabled) { letter ->
viewModel.handleKey(Key(letter.code), letter)
}
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
Expand Down

0 comments on commit ad38803

Please sign in to comment.