-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from arkivanov/settings
Added settings dialog
- Loading branch information
Showing
11 changed files
with
288 additions
and
28 deletions.
There are no files selected for viewing
15 changes: 0 additions & 15 deletions
15
composeApp/src/commonMain/kotlin/com/arkivanov/minesweeper/App.kt
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
composeApp/src/commonMain/kotlin/com/arkivanov/minesweeper/NavUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.arkivanov.minesweeper | ||
|
||
import com.arkivanov.decompose.ComponentContext | ||
import com.arkivanov.decompose.router.children.ChildNavState | ||
import com.arkivanov.decompose.router.children.NavState | ||
import com.arkivanov.decompose.router.children.NavigationSource | ||
import com.arkivanov.decompose.router.children.SimpleChildNavState | ||
import com.arkivanov.decompose.router.children.children | ||
import com.arkivanov.decompose.value.Value | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializable | ||
|
||
internal fun <C : Any, T : Any> ComponentContext.child( | ||
source: NavigationSource<C>, | ||
serializer: KSerializer<C>, | ||
initialConfiguration: () -> C, | ||
key: String = "child", | ||
childFactory: (C, ComponentContext) -> T, | ||
): Value<T> = | ||
children( | ||
source = source, | ||
stateSerializer = SimpleNavState.serializer(typeSerial0 = serializer), | ||
initialState = { SimpleNavState(initialConfiguration()) }, | ||
key = key, | ||
navTransformer = { _, config -> SimpleNavState(config) }, | ||
stateMapper = { _, children -> requireNotNull(children.single().instance) }, | ||
childFactory = childFactory, | ||
) | ||
|
||
@Serializable | ||
private data class SimpleNavState<C : Any>( | ||
private val config: C, | ||
) : NavState<C> { | ||
override val children: List<ChildNavState<C>> = | ||
listOf(SimpleChildNavState(config, ChildNavState.Status.RESUMED)) | ||
} |
9 changes: 6 additions & 3 deletions
9
composeApp/src/commonMain/kotlin/com/arkivanov/minesweeper/game/GameSettings.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
package com.arkivanov.minesweeper.game | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class GameSettings( | ||
val width: Int, | ||
val height: Int, | ||
val maxMines: Int, | ||
val width: Int = 20, | ||
val height: Int = 20, | ||
val maxMines: Int = 30, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 7 additions & 1 deletion
8
composeApp/src/commonMain/kotlin/com/arkivanov/minesweeper/root/RootComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
package com.arkivanov.minesweeper.root | ||
|
||
import com.arkivanov.decompose.router.slot.ChildSlot | ||
import com.arkivanov.decompose.value.Value | ||
import com.arkivanov.minesweeper.game.GameComponent | ||
import com.arkivanov.minesweeper.settings.EditSettingsComponent | ||
|
||
internal interface RootComponent { | ||
|
||
val gameComponent: GameComponent | ||
val gameComponent: Value<GameComponent> | ||
val editSettingsComponent: Value<ChildSlot<*, EditSettingsComponent>> | ||
|
||
fun onEditSettingsClicked() | ||
} |
45 changes: 45 additions & 0 deletions
45
composeApp/src/commonMain/kotlin/com/arkivanov/minesweeper/root/RootContent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.arkivanov.minesweeper.root | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material.Icon | ||
import androidx.compose.material.IconButton | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.TopAppBar | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.Settings | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import com.arkivanov.decompose.extensions.compose.subscribeAsState | ||
import com.arkivanov.minesweeper.game.GameContent | ||
import com.arkivanov.minesweeper.settings.EditSettingsContent | ||
|
||
@Composable | ||
internal fun RootContent(component: RootComponent) { | ||
val gameComponent by component.gameComponent.subscribeAsState() | ||
val editSettingsComponentSlot by component.editSettingsComponent.subscribeAsState() | ||
|
||
MaterialTheme { | ||
Column(modifier = Modifier.fillMaxSize()) { | ||
TopAppBar( | ||
title = { Text("Minesweeper") }, | ||
actions = { | ||
IconButton(onClick = component::onEditSettingsClicked) { | ||
Icon( | ||
imageVector = Icons.Default.Settings, | ||
contentDescription = "Settings", | ||
) | ||
} | ||
}, | ||
) | ||
|
||
GameContent(component = gameComponent, modifier = Modifier.fillMaxSize()) | ||
} | ||
|
||
editSettingsComponentSlot.child?.instance?.also { editSettingsComponent -> | ||
EditSettingsContent(component = editSettingsComponent) | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
.../src/commonMain/kotlin/com/arkivanov/minesweeper/settings/DefaultEditSettingsComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.arkivanov.minesweeper.settings | ||
|
||
import com.arkivanov.decompose.value.MutableValue | ||
import com.arkivanov.decompose.value.Value | ||
import com.arkivanov.decompose.value.update | ||
import com.arkivanov.minesweeper.game.GameSettings | ||
import com.arkivanov.minesweeper.settings.EditSettingsComponent.Model | ||
|
||
internal class DefaultEditSettingsComponent( | ||
settings: GameSettings, | ||
private val onConfirmed: (GameSettings) -> Unit, | ||
private val onDismissed: () -> Unit, | ||
) : EditSettingsComponent { | ||
|
||
private val _model = | ||
MutableValue( | ||
Model( | ||
width = settings.width.toString(), | ||
height = settings.height.toString(), | ||
maxMines = settings.maxMines.toString(), | ||
) | ||
) | ||
|
||
override val model: Value<Model> = _model | ||
|
||
override fun onWidthChanged(text: String) { | ||
_model.update { it.copy(width = text) } | ||
} | ||
|
||
override fun onHeightChanged(text: String) { | ||
_model.update { it.copy(height = text) } | ||
} | ||
|
||
override fun onMaxMinesChanged(text: String) { | ||
_model.update { it.copy(maxMines = text) } | ||
} | ||
|
||
override fun onConfirmClicked() { | ||
val width = _model.value.width.toIntOrNull() ?: return | ||
val height = _model.value.height.toIntOrNull() ?: return | ||
val maxMines = _model.value.maxMines.toIntOrNull() ?: return | ||
|
||
onConfirmed( | ||
GameSettings( | ||
width = width, | ||
height = height, | ||
maxMines = maxMines, | ||
) | ||
) | ||
} | ||
|
||
override fun onDismissRequested() { | ||
onDismissed() | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
composeApp/src/commonMain/kotlin/com/arkivanov/minesweeper/settings/EditSettingsComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.arkivanov.minesweeper.settings | ||
|
||
import com.arkivanov.decompose.value.Value | ||
import com.arkivanov.minesweeper.game.GameSettings | ||
|
||
internal interface EditSettingsComponent { | ||
|
||
val model: Value<Model> | ||
|
||
fun onWidthChanged(text: String) | ||
fun onHeightChanged(text: String) | ||
fun onMaxMinesChanged(text: String) | ||
fun onConfirmClicked() | ||
fun onDismissRequested() | ||
|
||
data class Model( | ||
val width: String, | ||
val height: String, | ||
val maxMines: String, | ||
) | ||
|
||
fun interface Factory { | ||
operator fun invoke( | ||
settings: GameSettings, | ||
onConfirmed: (GameSettings) -> Unit, | ||
onCancelled: () -> Unit, | ||
): EditSettingsComponent | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
composeApp/src/commonMain/kotlin/com/arkivanov/minesweeper/settings/EditSettingsContent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.arkivanov.minesweeper.settings | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Surface | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.TextField | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.window.Dialog | ||
import com.arkivanov.decompose.extensions.compose.subscribeAsState | ||
|
||
@Composable | ||
internal fun EditSettingsContent(component: EditSettingsComponent) { | ||
val model by component.model.subscribeAsState() | ||
|
||
Dialog(onDismissRequest = component::onDismissRequested) { | ||
Surface(shape = MaterialTheme.shapes.medium) { | ||
Column( | ||
modifier = Modifier.padding(16.dp), | ||
verticalArrangement = Arrangement.spacedBy(space = 16.dp), | ||
) { | ||
TextField( | ||
value = model.width, | ||
onValueChange = component::onWidthChanged, | ||
label = { Text(text = "Width") }, | ||
) | ||
|
||
TextField( | ||
value = model.height, | ||
onValueChange = component::onHeightChanged, | ||
label = { Text(text = "Height") }, | ||
) | ||
|
||
TextField( | ||
value = model.maxMines, | ||
onValueChange = component::onMaxMinesChanged, | ||
label = { Text(text = "Mine count") }, | ||
) | ||
|
||
Row( | ||
modifier = Modifier.align(Alignment.CenterHorizontally), | ||
horizontalArrangement = Arrangement.spacedBy(16.dp), | ||
) { | ||
Button(onClick = component::onConfirmClicked) { | ||
Text(text = "Apply") | ||
} | ||
|
||
Button(onClick = component::onConfirmClicked) { | ||
Text(text = "Cancel") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters