Skip to content

Commit

Permalink
Let user choose the dictionary to use when several ones are available…
Browse files Browse the repository at this point in the history
… (partially implements #20)
  • Loading branch information
opatry committed Jan 18, 2022
1 parent 7731b0a commit 1dfbb74
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class WordleViewModel(inDictionary: List<String>, private val repository: Wordle
// to ask for preferred game mode (language, word size)
}

fun onCleared() = Unit

private fun updateGrid() {
val rules = rules ?: return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,55 +23,99 @@
package net.opatry.game.wordle.ui.compose

import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.width
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.intl.Locale
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import net.opatry.game.wordle.Dictionary
import net.opatry.game.wordle.allDictionaries
import net.opatry.game.wordle.data.Settings
import net.opatry.game.wordle.data.WordleRepository
import net.opatry.game.wordle.loadWords
import net.opatry.game.wordle.ui.WordleViewModel
import net.opatry.game.wordle.ui.compose.component.DictionaryPicker
import net.opatry.game.wordle.ui.compose.theme.WordleComposeTheme
import net.opatry.game.wordle.ui.compose.theme.isHighContrastMode
import net.opatry.game.wordle.ui.compose.theme.isSystemInDarkTheme
import java.io.File


private val appDir = File(System.getProperty("user.home"), ".wordle-kt")
private val dataFile = File(appDir, "records.json")
private val settingsFile = File(appDir, "settings.json")

private val settings = Settings(settingsFile)

// FIXME singleton here otherwise recreated at each recomposition, need to be investigated
val validDictionaries = allDictionaries
.filter { it.wordSize in 4..8 }
.sortedWith(compareBy(Dictionary::language, Dictionary::wordSize))
val defaultDictionary = validDictionaries
.find { it.language == Locale.current.language && it.wordSize == 5 }
?: validDictionaries.firstOrNull { it.language == Locale.current.language }
?: validDictionaries.firstOrNull()
?: error("Can't find any dictionary")
private val viewModel = WordleViewModel(defaultDictionary.loadWords(), WordleRepository(dataFile))

@ExperimentalFoundationApi
@ExperimentalAnimationApi
@ExperimentalComposeUiApi
@Composable
fun WordleApp() {
fun WordleApp(settings: Settings, dataFile: File, dictionaries: List<Dictionary>) {
isSystemInDarkTheme = settings.darkMode
isHighContrastMode = settings.highContrastMode

var selectedDictionary by remember {
mutableStateOf(
// force choice if no choice
if (dictionaries.size == 1)
dictionaries.first()
else
null
)
}

WordleComposeTheme {
Box(
Modifier.fillMaxSize(),
Alignment.TopCenter
) {
GameScreen(settings, viewModel)
// FIXME why nesting 2 Boxes is needed to center 400.dp width content...
Box(Modifier.fillMaxSize(), Alignment.TopCenter) {
Box(Modifier.fillMaxHeight().width(400.dp)) {
when (val dict = selectedDictionary) {
null -> {
// TODO define a preselected one?
// 1. last used if any
// 2. find current locale with 5 letters
// 3. none
DictionaryPicker(dictionaries) { dictionary ->
selectedDictionary = dictionary
}
}
else -> {
var words by remember { mutableStateOf(emptyList<String>()) }
LaunchedEffect(dict) {
withContext(Dispatchers.IO) {
words = dict.loadWords()
}
}
if (words.isNotEmpty()) {
val viewModel = WordleViewModel(words, WordleRepository(dataFile))
DisposableEffect(dict) {
onDispose {
viewModel.onCleared()
}
}
GameScreen(settings, viewModel)
} else {
Column(
Modifier.fillMaxSize(),
Arrangement.spacedBy(16.dp, Alignment.CenterVertically),
Alignment.CenterHorizontally
) {
CircularProgressIndicator()
Text("Loading words…")
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* 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.ui.compose.component

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Button
import androidx.compose.material.LocalContentColor
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import net.opatry.game.wordle.Dictionary
import java.util.*


@ExperimentalFoundationApi
@Composable
fun DictionaryPicker(dictionaries: List<Dictionary>, onSelect: (Dictionary) -> Unit) {
var selectedDictionary by remember { mutableStateOf<Dictionary?>(null) }
Column(
Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp, Alignment.CenterVertically)
) {
Text(
"Choose the dictionary to use",
Modifier
.fillMaxWidth()
.padding(8.dp),
style = MaterialTheme.typography.h1
)
LazyColumn(Modifier.weight(1f, false)) {
val groups = dictionaries.groupBy(Dictionary::language)
groups.forEach { (language, dictionaries) ->
stickyHeader {
LanguageRow(
language,
Modifier
.fillMaxWidth()
.background(MaterialTheme.colors.background)
)
}
items(dictionaries) { dictionary ->
WordSizeRow(dictionary.wordSize, dictionary == selectedDictionary) {
selectedDictionary = dictionary
}
}
}
}
Button(
onClick = {
val dictionary = selectedDictionary
if (dictionary != null) {
onSelect(dictionary)
}
},
enabled = selectedDictionary != null
) {
Text("Select")
}
}
}

@Composable
fun LanguageRow(language: String, modifier: Modifier = Modifier) {
val locale = Locale(language)
Row(
modifier.padding(vertical = 4.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(
locale.displayLanguage,
style = MaterialTheme.typography.h3
)
}
}

@Composable
private fun WordSizeRow(wordSize: Int, isSelected: Boolean, onSelect: () -> Unit) {
val (foregroundColor, backgroundColor) = when (isSelected) {
true -> MaterialTheme.colors.onPrimary to MaterialTheme.colors.primary
else -> LocalContentColor.current to Color.Transparent
}

Row(
Modifier
.padding(vertical = 4.dp)
.clip(MaterialTheme.shapes.small)
.clickable(onClick = onSelect)
.background(backgroundColor)
.fillMaxWidth()
.padding(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(
"$wordSize letter words",
color = foregroundColor
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
Expand Down Expand Up @@ -148,10 +148,7 @@ fun GameScreen(settings: Settings, viewModel: WordleViewModel) {
}

Box(
Modifier
.fillMaxHeight()
.width(400.dp)
.padding(2.dp),
Modifier.fillMaxSize(),
contentAlignment = Alignment.TopCenter
) {
Column(
Expand Down Expand Up @@ -252,7 +249,12 @@ fun GameScreen(settings: Settings, viewModel: WordleViewModel) {
}
}

Column(Modifier.padding(top = 80.dp)) {
Column(
Modifier
.fillMaxWidth()
.padding(top = 80.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
userFeedback.forEach { message ->
AutoDismissToast(message, Modifier.padding(bottom = 4.dp), viewModel::consumeMessage)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package net.opatry.game.wordle.ui.compose.theme


import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Colors
import androidx.compose.material.MaterialTheme
Expand All @@ -34,6 +35,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

Expand Down Expand Up @@ -87,7 +89,10 @@ fun WordleComposeTheme(
typography = typography,
shapes = Shapes(small = RoundedCornerShape(4.dp), medium = RoundedCornerShape(4.dp)),
content = {
Surface(color = MaterialTheme.colors.surface) {
Surface(
Modifier.fillMaxSize(),
color = MaterialTheme.colors.surface
) {
content()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package net.opatry.game.wordle.ui.compose

import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
Expand All @@ -39,10 +40,14 @@ import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowPosition
import androidx.compose.ui.window.WindowState
import androidx.compose.ui.window.application
import net.opatry.game.wordle.Dictionary
import net.opatry.game.wordle.allDictionaries
import net.opatry.game.wordle.data.Settings
import net.opatry.game.wordle.ui.compose.theme.AppIcon
import net.opatry.game.wordle.ui.compose.theme.IconProvider
import net.opatry.game.wordle.ui.compose.theme.LocalIconProvider
import java.awt.Dimension
import java.io.File

object DesktopIconProvider : IconProvider {
@Composable
Expand All @@ -60,13 +65,24 @@ object DesktopIconProvider : IconProvider {
}
}

private val appDir = File(System.getProperty("user.home"), ".wordle-kt")
private val dataFile = File(appDir, "records.json")
private val settingsFile = File(appDir, "settings.json")

private val settings = Settings(settingsFile)

@ExperimentalFoundationApi
@ExperimentalAnimationApi
@ExperimentalComposeUiApi
fun main() = application {
val defaultSize = DpSize(500.dp, 720.dp)
val minSize = DpSize(380.dp, defaultSize.height)
val minWindowSize = remember { Dimension(minSize.width.value.toInt(), minSize.height.value.toInt()) }

val validDictionaries = allDictionaries
.filter { it.wordSize in 4..8 }
.sortedWith(compareBy(Dictionary::language, Dictionary::wordSize))

Window(
onCloseRequest = ::exitApplication,
title = "Wordle Compose",
Expand All @@ -85,7 +101,7 @@ fun main() = application {
if (window.minimumSize != minWindowSize) window.minimumSize = minWindowSize

CompositionLocalProvider(LocalIconProvider provides DesktopIconProvider) {
WordleApp()
WordleApp(settings, dataFile, validDictionaries)
}
}
}

0 comments on commit 1dfbb74

Please sign in to comment.