Skip to content

Commit

Permalink
some refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
yamin8000 committed Oct 22, 2023
1 parent 4f53907 commit f96eeca
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
setContent {
MainNavigation(adContent = { })
}
setContent { MainNavigation(adContent = { }) }
WindowCompat.setDecorFitsSystemWindows(window, false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import io.github.yamin8000.dooz.ui.composables.PersianText
import io.github.yamin8000.dooz.ui.composables.SingleLinePersianText
import io.github.yamin8000.dooz.ui.composables.isFontScaleNormal

@OptIn(ExperimentalAnimationApi::class, ExperimentalMaterial3Api::class)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun GameContent(
onNavigateToSettings: () -> Unit,
Expand Down Expand Up @@ -278,7 +278,6 @@ private fun GameBoard(
}
}

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun DoozItem(
shape: Shape,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ package io.github.yamin8000.dooz.content.game
import android.content.Context
import android.media.MediaPlayer
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableIntState
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
Expand Down Expand Up @@ -60,7 +62,7 @@ class GameState(
private val context: Context,
private val scope: LifecycleCoroutineScope,
var gameCells: MutableState<List<List<DoozCell>>>,
val gameSize: MutableState<Int>,
val gameSize: MutableIntState,
var currentPlayer: MutableState<Player?>,
var players: MutableState<List<Player>>,
var gamePlayersType: MutableState<GamePlayersType>,
Expand Down Expand Up @@ -152,7 +154,7 @@ class GameState(
}

private suspend fun prepareGameRules() {
gameSize.value = dataStore.getInt(Constants.gameSize) ?: gameDefaultSize
gameSize.intValue = dataStore.getInt(Constants.gameSize) ?: gameDefaultSize
gamePlayersType.value = GamePlayersType.valueOf(
dataStore.getString(Constants.gamePlayersType) ?: GamePlayersType.PvC.name
)
Expand All @@ -167,15 +169,15 @@ class GameState(
private fun prepareGameLogic() {
when (gameType.value) {
GameType.Simple -> gameLogic =
SimpleGameLogic(gameCells.value, gameSize.value, aiDifficulty.value)
SimpleGameLogic(gameCells.value, gameSize.intValue, aiDifficulty.value)
}
}

private fun getEmptyBoard(): List<List<DoozCell>> {
val columns = mutableListOf<List<DoozCell>>()
for (x in 0 until gameSize.value) {
for (x in 0 until gameSize.intValue) {
val row = mutableListOf<DoozCell>()
for (y in 0 until gameSize.value)
for (y in 0 until gameSize.intValue)
row.add(DoozCell(x, y))
columns.add(row)
}
Expand Down Expand Up @@ -368,7 +370,7 @@ fun rememberHomeState(
context: Context = LocalContext.current,
coroutineScope: LifecycleCoroutineScope = LocalLifecycleOwner.current.lifecycleScope,
doozCells: MutableState<List<List<DoozCell>>> = rememberSaveable { mutableStateOf(emptyList()) },
gameSize: MutableState<Int> = rememberSaveable { mutableStateOf(gameDefaultSize) },
gameSize: MutableIntState = rememberSaveable { mutableIntStateOf(gameDefaultSize) },
currentPlayer: MutableState<Player?> = rememberSaveable { mutableStateOf(null) },
players: MutableState<List<Player>> = rememberSaveable { mutableStateOf(listOf()) },
gamePlayersType: MutableState<GamePlayersType> = rememberSaveable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ package io.github.yamin8000.dooz.content.settings
import android.content.Context
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableIntState
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
Expand All @@ -45,7 +47,7 @@ class SettingsState(
private val context: Context,
private val scope: LifecycleCoroutineScope,
private val _gamePlayersType: MutableState<GamePlayersType>,
private val _gameSize: MutableState<Int>,
private val _gameSize: MutableIntState,
private val _firstPlayerName: MutableState<String>,
private val _secondPlayerName: MutableState<String>,
private val _firstPlayerShape: MutableState<String>,
Expand All @@ -64,10 +66,10 @@ class SettingsState(
}

var gameSize: Int
get() = _gameSize.value
get() = _gameSize.intValue
set(value) {
if (value in Constants.gameSizeRange) {
_gameSize.value = value
if (value in GameConstants.gameSizeRange) {
_gameSize.intValue = value
scope.launch { dataStore.setInt(Constants.gameSize, value) }
}
}
Expand Down Expand Up @@ -168,7 +170,7 @@ class SettingsState(
_gamePlayersType.value = GamePlayersType.valueOf(
dataStore.getString(Constants.gamePlayersType) ?: GamePlayersType.PvC.name
)
_gameSize.value = dataStore.getInt(Constants.gameSize) ?: GameConstants.gameDefaultSize
_gameSize.intValue = dataStore.getInt(Constants.gameSize) ?: GameConstants.gameDefaultSize
_firstPlayerName.value =
dataStore.getString(Constants.firstPlayerName) ?: defaultFirstPlayerName
_secondPlayerName.value =
Expand Down Expand Up @@ -197,7 +199,7 @@ fun rememberSettingsState(
gamePlayersType: MutableState<GamePlayersType> = rememberSaveable {
mutableStateOf(GamePlayersType.PvC)
},
gameSize: MutableState<Int> = rememberSaveable { mutableStateOf(GameConstants.gameDefaultSize) },
gameSize: MutableIntState = rememberSaveable { mutableIntStateOf(GameConstants.gameDefaultSize) },
firstPlayerName: MutableState<String> = rememberSaveable { mutableStateOf(context.getString(R.string.first_player_default_name)) },
secondPlayerName: MutableState<String> = rememberSaveable { mutableStateOf(context.getString(R.string.second_player_default_name)) },
firstPlayerShape: MutableState<String> = rememberSaveable { mutableStateOf(Constants.Shapes.xShape) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -158,7 +157,6 @@ internal fun PlayerNamesCustomizer(
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun NameField(
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -75,19 +75,19 @@ fun SettingsContent(
stringResource(R.string.players)
)

val tabIndex = rememberSaveable { mutableStateOf(1) }
val tabIndex = rememberSaveable { mutableIntStateOf(1) }
ScrollableTabRow(
selectedTabIndex = tabIndex.value,
selectedTabIndex = tabIndex.intValue,
tabs = {
tabTitles.forEachIndexed { index, title ->
Tab(
selected = tabIndex.value == index,
onClick = { tabIndex.value = index },
selected = tabIndex.intValue == index,
onClick = { tabIndex.intValue = index },
text = { PersianText(title) })
}
}
)
when (tabTitles[tabIndex.value]) {
when (tabTitles[tabIndex.intValue]) {
stringResource(R.string.general) -> {
ThemeChangerCard(state.themeSetting) { newTheme ->
state.themeSetting = newTheme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
package io.github.yamin8000.dooz.game

object GameConstants {
const val gameMaxSize = 9
val gameSizeRange = 3..7
const val gameDefaultSize = 3
}
41 changes: 0 additions & 41 deletions app/src/main/java/io/github/yamin8000/dooz/ui/Tree.kt

This file was deleted.

17 changes: 6 additions & 11 deletions app/src/main/java/io/github/yamin8000/dooz/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@ import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.unit.Density
import androidx.core.view.WindowCompat

private val LightColors = lightColorScheme(
private val lightColors = lightColorScheme(
primary = md_theme_light_primary,
onPrimary = md_theme_light_onPrimary,
primaryContainer = md_theme_light_primaryContainer,
Expand Down Expand Up @@ -70,7 +67,7 @@ private val LightColors = lightColorScheme(
)


private val DarkColors = darkColorScheme(
private val darkColors = darkColorScheme(
primary = md_theme_dark_primary,
onPrimary = md_theme_dark_onPrimary,
primaryContainer = md_theme_dark_primaryContainer,
Expand Down Expand Up @@ -107,22 +104,20 @@ fun DoozTheme(
isDynamicColor: Boolean,
content: @Composable () -> Unit
) {
//CompositionLocalProvider(LocalDensity provides Density(LocalDensity.current.density, 1f)) {}

val isDynamicColorReadyDevice =
isDynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S

val colors = when {
isDynamicColorReadyDevice && isDarkTheme -> {
dynamicDarkColorScheme(LocalContext.current).injectBrandColors(DarkColors)
dynamicDarkColorScheme(LocalContext.current).injectBrandColors(darkColors)
}

isDynamicColorReadyDevice && !isDarkTheme -> {
dynamicLightColorScheme(LocalContext.current).injectBrandColors(LightColors)
dynamicLightColorScheme(LocalContext.current).injectBrandColors(lightColors)
}

isDarkTheme -> DarkColors
else -> LightColors
isDarkTheme -> darkColors
else -> lightColors
}

if (!isPreviewing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ object Constants {
const val theme = "theme"

const val gameSize = "gameSize"
val gameSizeRange = 3..7

const val gamePlayersType = "gamePlayersType"
const val firstPlayerName = "firstPlayerName"
Expand Down

0 comments on commit f96eeca

Please sign in to comment.