Skip to content

Commit

Permalink
[Mosaic] Use underlying platform copy to clipboard mechanism on victo…
Browse files Browse the repository at this point in the history
…ry in Mosaic version
  • Loading branch information
opatry committed Jan 3, 2024
1 parent 09feb5e commit dc8db17
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <TODO_wordleId> ${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 <TODO_wordleId> 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("")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -63,7 +68,7 @@ private fun State.toClipboard(): String {
else -> ""
}
)
answers.forEach(buffer::appendClipboardAnswer)
answers.forEach(buffer::appendAnswer)

return buffer.toString()
}
Expand All @@ -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("")
Expand Down

0 comments on commit dc8db17

Please sign in to comment.