From a8fca6c3156ed785e7217f29cf8e8de916db0982 Mon Sep 17 00:00:00 2001 From: Olivier Patry Date: Thu, 13 Jan 2022 22:27:42 +0100 Subject: [PATCH] [Mosaic] Use underlying platform copy to clipboard mechanism on victory in Mosaic version --- .../opatry/game/wordle/mosaic/gameScreen.kt | 37 ++++++++++++------- .../opatry/game/wordle/ui/WordleViewModel.kt | 17 ++++++--- 2 files changed, 35 insertions(+), 19 deletions(-) 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 9e32661..a09577d 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 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 androidx.compose.runtime.LaunchedEffect 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,29 +50,36 @@ 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 78f6aec..073810f 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("")