Skip to content

Commit

Permalink
[Mosaic] Extract Mosaic Wordle related Composable in their own file
Browse files Browse the repository at this point in the history
  • Loading branch information
opatry committed Jan 11, 2022
1 parent 867405c commit 8a3227d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2022 Olivier Patry
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package net.opatry.game.wordle.mosaic.component

import androidx.compose.runtime.Composable
import com.jakewharton.mosaic.Color
import com.jakewharton.mosaic.Column
import com.jakewharton.mosaic.Row
import com.jakewharton.mosaic.Text
import com.jakewharton.mosaic.TextStyle
import net.opatry.game.wordle.Answer
import net.opatry.game.wordle.AnswerFlag


@Composable
fun WordleGrid(grid: List<Answer>) {
Column {
grid.forEach { row ->
WordleWordRow(row)
}
}
}

@Composable
fun WordleWordRow(row: Answer) {
Row {
row.letters.forEachIndexed { index, char ->
WordleCharCell(char, row.flags[index])
}
}
}

fun AnswerFlag.cellColors(): Pair<Color, Color> = when (this) {
AnswerFlag.NONE -> Color.Black to Color.White
AnswerFlag.PRESENT -> Color.Black to Color.Yellow
AnswerFlag.ABSENT -> Color.BrightWhite to Color.Black
AnswerFlag.CORRECT -> Color.BrightWhite to Color.Green
}

@Composable
fun WordleCharCell(char: Char, flag: AnswerFlag) {
val (foregroundColor, backgroundColor) =
if (flag == AnswerFlag.NONE && !char.isWhitespace())
Color.Black to Color.BrightWhite
else
flag.cellColors()

// TODO AnnotatedString " $char " https://github.com/JakeWharton/mosaic/issues/9
Column {
Row {
Text(" ")
Text(
" $char ",
color = foregroundColor,
background = backgroundColor,
style = TextStyle.Bold
)
Text(" ")
}
Text("")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,52 +136,3 @@ fun GameScreen(viewModel: WordleViewModel) {
}
}
}

@Composable
fun WordleGrid(grid: List<Answer>) {
Column {
grid.forEach { row ->
WordleWordRow(row)
}
}
}

@Composable
fun WordleWordRow(row: Answer) {
Row {
row.letters.forEachIndexed { index, char ->
WordleCharCell(char, row.flags[index])
}
}
}

fun AnswerFlag.cellColors(): Pair<Color, Color> = when (this) {
AnswerFlag.NONE -> Color.Black to Color.White
AnswerFlag.PRESENT -> Color.Black to Color.Yellow
AnswerFlag.ABSENT -> Color.White to Color.Black
AnswerFlag.CORRECT -> Color.Black to Color.Green
}

@Composable
fun WordleCharCell(char: Char, flag: AnswerFlag) {
val (foregroundColor, backgroundColor) =
if (flag == AnswerFlag.NONE && !char.isWhitespace())
Color.Black to Color.BrightWhite
else
flag.cellColors()

// TODO AnnotatedString " $char " https://github.com/JakeWharton/mosaic/issues/9
Column {
Row {
Text(" ")
Text(
" $char ",
color = foregroundColor,
background = backgroundColor,
style = TextStyle.Bold
)
Text(" ")
}
Text("")
}
}

0 comments on commit 8a3227d

Please sign in to comment.