diff --git a/wordle-compose-mosaic/src/main/java/net/opatry/game/wordle/mosaic/gameScreen.kt b/wordle-compose-mosaic/src/main/java/net/opatry/game/wordle/mosaic/gameScreen.kt index 0b66023..a8f7adb 100644 --- a/wordle-compose-mosaic/src/main/java/net/opatry/game/wordle/mosaic/gameScreen.kt +++ b/wordle-compose-mosaic/src/main/java/net/opatry/game/wordle/mosaic/gameScreen.kt @@ -23,10 +23,14 @@ package net.opatry.game.wordle.mosaic import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import com.jakewharton.mosaic.ui.Color import com.jakewharton.mosaic.ui.Column import com.jakewharton.mosaic.ui.Row import com.jakewharton.mosaic.ui.Text +import com.jakewharton.mosaic.ui.TextStyle import net.opatry.game.wordle.State +import net.opatry.game.wordle.copyToClipboard import net.opatry.game.wordle.mosaic.component.Alphabet import net.opatry.game.wordle.mosaic.component.WordleGrid import net.opatry.game.wordle.ui.WordleViewModel @@ -46,31 +50,38 @@ fun GameScreen(viewModel: WordleViewModel) { Text("") + // There must be stable number of lines for nice UI state. + // All states should display 3 lines. when (val state = viewModel.state) { is State.Won -> { + LaunchedEffect(viewModel.state) { + viewModel.stateLabel.copyToClipboard() + } + Text("Wordle ${state.answers.size}/${state.maxTries}") - Text(viewModel.answer) + Text("Results copied to clipboard!") // FIXME depends on copyToClipboard success + Text(" 🔄 Play again? (y/N)?") } is State.Lost -> { Text("Wordle X/${state.maxTries}") - Text(viewModel.answer) + Row { + Text("The answer was ") + Text( + viewModel.answer, + color = Color.BrightWhite, + background = Color.Green, + style = TextStyle.Bold + ) + } + Text(" 🔄 Play again? (y/N)?") } is State.Playing -> { - Text(" ➡️ Enter a 5 letter english word") Text("") // TODO display error here if any or define a placeholder on top of grid + Text("") + Text(" ➡️ Enter a 5 letter english word") } } - -// viewModel.state.toClipboard() -// println("Results copied to clipboard!") - - // there must be stable number of lines for nice UI state - if (viewModel.state !is State.Playing) { - Text(" 🔄 Play again? (y/N)? ${viewModel.userInput}") - } else { - Text("") - } } } diff --git a/wordle-compose-mosaic/src/main/java/net/opatry/game/wordle/ui/WordleViewModel.kt b/wordle-compose-mosaic/src/main/java/net/opatry/game/wordle/ui/WordleViewModel.kt index c1bfb32..e86dc5d 100644 --- a/wordle-compose-mosaic/src/main/java/net/opatry/game/wordle/ui/WordleViewModel.kt +++ b/wordle-compose-mosaic/src/main/java/net/opatry/game/wordle/ui/WordleViewModel.kt @@ -49,12 +49,17 @@ private val State.message: String } } -private fun StringBuffer.appendClipboardAnswer(answer: Answer) { - answer.flags.map(AnswerFlag::toEmoji).forEach(::append) - append('\n') +private fun StringBuffer.appendAnswer(answer: Answer) { + append( + answer.flags.joinToString( + separator = " ", + postfix = "\n", + transform = AnswerFlag::toEmoji + ) + ).trimEnd() } -private fun State.toClipboard(): String { +private fun State.toResultString(): String { val buffer = StringBuffer() buffer.append( when (this) { @@ -63,7 +68,7 @@ private fun State.toClipboard(): String { else -> "" } ) - answers.forEach(buffer::appendClipboardAnswer) + answers.forEach(buffer::appendAnswer) return buffer.toString() } @@ -73,7 +78,7 @@ class WordleViewModel(private var rules: WordleRules) { private set var state by mutableStateOf(rules.state) val stateLabel: String - get() = rules.state.toClipboard() + get() = rules.state.toResultString() var victory by mutableStateOf(rules.state is State.Won) private set var answer by mutableStateOf("")