This commit is contained in:
@@ -15,7 +15,6 @@ import androidx.compose.ui.graphics.vector.ImageVector
|
|||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.text.style.TextOverflow
|
import androidx.compose.ui.text.style.TextOverflow
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
|
||||||
import androidx.navigation.NavDestination.Companion.hierarchy
|
import androidx.navigation.NavDestination.Companion.hierarchy
|
||||||
import androidx.navigation.NavGraph.Companion.findStartDestination
|
import androidx.navigation.NavGraph.Companion.findStartDestination
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
@@ -89,8 +88,14 @@ class MainActivity : BaseActivity() {
|
|||||||
composable("counter") {
|
composable("counter") {
|
||||||
Counter(counterViewModel)
|
Counter(counterViewModel)
|
||||||
mainViewModel.topBarActions = (listOf(
|
mainViewModel.topBarActions = (listOf(
|
||||||
TopBarAction(Icons.Outlined.Undo, mainViewModel.isUndoActionActive) { mainViewModel.undoLastRound() },
|
TopBarAction(
|
||||||
TopBarAction(Icons.Outlined.Redo, mainViewModel.isRedoActionActive) { mainViewModel.redoLastRound() }
|
Icons.Outlined.Undo,
|
||||||
|
mainViewModel.isUndoActionActive
|
||||||
|
) { mainViewModel.undoLastRound() },
|
||||||
|
TopBarAction(
|
||||||
|
Icons.Outlined.Redo,
|
||||||
|
mainViewModel.isRedoActionActive
|
||||||
|
) { mainViewModel.redoLastRound() }
|
||||||
|
|
||||||
))
|
))
|
||||||
mainViewModel.topBarIcon = Icons.Outlined.Menu
|
mainViewModel.topBarIcon = Icons.Outlined.Menu
|
||||||
@@ -163,7 +168,9 @@ class MainActivity : BaseActivity() {
|
|||||||
val scope = rememberCoroutineScope()
|
val scope = rememberCoroutineScope()
|
||||||
val navController = rememberNavController()
|
val navController = rememberNavController()
|
||||||
|
|
||||||
val items = listOf(Screen.History, Screen.Settings)
|
val items = listOf(
|
||||||
|
Screen("history", Icons.Outlined.List, R.string.menu_history),Screen("settings", Icons.Outlined.Settings, R.string.menu_settings)
|
||||||
|
)
|
||||||
|
|
||||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
||||||
val currentDestination = navBackStackEntry?.destination
|
val currentDestination = navBackStackEntry?.destination
|
||||||
@@ -236,9 +243,5 @@ class MainActivity : BaseActivity() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
sealed class Screen(val route: String, val icon: ImageVector, @StringRes val resourceId: Int) {
|
private class Screen(val route: String, val icon: ImageVector, @StringRes val resourceId: Int)
|
||||||
object History : Screen("history", Icons.Outlined.List, R.string.menu_history)
|
|
||||||
object Settings : Screen("settings", Icons.Outlined.Settings, R.string.menu_settings)
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
|
|||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
enum class Theme { DEFAULT, DARK, LIGHT }
|
enum class Theme { DEFAULT, DARK, LIGHT }
|
||||||
enum class Language(val value: String) { ENGLISH("en"), GERMAN("de") }
|
enum class Language(val value: String) { ENGLISH("en"), GERMAN("de") }
|
||||||
|
|
||||||
|
|
||||||
class SettingsAdapter @Inject constructor(@ApplicationContext private val context: Context) {
|
class SettingsAdapter @Inject constructor(@ApplicationContext private val context: Context) {
|
||||||
@@ -17,10 +17,10 @@ class SettingsAdapter @Inject constructor(@ApplicationContext private val contex
|
|||||||
val language: Language
|
val language: Language
|
||||||
get() {
|
get() {
|
||||||
var setting = sharedPreferences.getString(Language::class.simpleName, null)
|
var setting = sharedPreferences.getString(Language::class.simpleName, null)
|
||||||
if(setting == null)
|
if (setting == null) {
|
||||||
{
|
|
||||||
setCurrentLanguage()
|
setCurrentLanguage()
|
||||||
setting = sharedPreferences.getString(Language::class.simpleName, Language.ENGLISH.name)
|
setting =
|
||||||
|
sharedPreferences.getString(Language::class.simpleName, Language.ENGLISH.name)
|
||||||
}
|
}
|
||||||
return enumValueOf(setting!!)
|
return enumValueOf(setting!!)
|
||||||
}
|
}
|
||||||
@@ -37,12 +37,13 @@ class SettingsAdapter @Inject constructor(@ApplicationContext private val contex
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun setCurrentLanguage() {
|
private fun setCurrentLanguage() {
|
||||||
var setting = when(getApplicationLocales()[0].toString()) {
|
var setting = when (getApplicationLocales()[0].toString()) {
|
||||||
"de" -> Language.GERMAN
|
"de" -> Language.GERMAN
|
||||||
else -> Language.ENGLISH
|
else -> Language.ENGLISH
|
||||||
}
|
}
|
||||||
setLanguage(setting)
|
setLanguage(setting)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setLanguage(language: Language) {
|
fun setLanguage(language: Language) {
|
||||||
val editor = sharedPreferences.edit()
|
val editor = sharedPreferences.edit()
|
||||||
editor.putString(Language::class.simpleName, language.name)
|
editor.putString(Language::class.simpleName, language.name)
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package me.zobrist.tichucounter.repository
|
|||||||
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.last
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import me.zobrist.tichucounter.data.Game
|
import me.zobrist.tichucounter.data.Game
|
||||||
@@ -63,10 +62,11 @@ class GameRepository @Inject constructor(
|
|||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
roundDao.getAllForGame(activeGame.uid).last()
|
roundDao.getAllForGame(activeGame.uid).last()
|
||||||
}
|
}
|
||||||
} catch (_:NoSuchElementException) {
|
} catch (_: NoSuchElementException) {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun deleteLastRound() {
|
suspend fun deleteLastRound() {
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -2,7 +2,10 @@ package me.zobrist.tichucounter.ui
|
|||||||
|
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.filled.Menu
|
import androidx.compose.material.icons.filled.Menu
|
||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateListOf
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
@@ -12,11 +15,13 @@ import me.zobrist.tichucounter.data.RoundDao
|
|||||||
import me.zobrist.tichucounter.domain.NavigationAction
|
import me.zobrist.tichucounter.domain.NavigationAction
|
||||||
import me.zobrist.tichucounter.domain.TopBarAction
|
import me.zobrist.tichucounter.domain.TopBarAction
|
||||||
import me.zobrist.tichucounter.repository.GameRepository
|
import me.zobrist.tichucounter.repository.GameRepository
|
||||||
import java.util.NoSuchElementException
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class MainViewModel @Inject constructor(private val gameRepository: GameRepository, roundDao: RoundDao) : ViewModel() {
|
class MainViewModel @Inject constructor(
|
||||||
|
private val gameRepository: GameRepository,
|
||||||
|
roundDao: RoundDao
|
||||||
|
) : ViewModel() {
|
||||||
|
|
||||||
private var redoRounds = mutableStateListOf<Round>()
|
private var redoRounds = mutableStateListOf<Round>()
|
||||||
private var expectedRoundCount = 0
|
private var expectedRoundCount = 0
|
||||||
@@ -35,7 +40,7 @@ class MainViewModel @Inject constructor(private val gameRepository: GameReposito
|
|||||||
roundDao.getForActiveGame().collect() {
|
roundDao.getForActiveGame().collect() {
|
||||||
isUndoActionActive = it.isNotEmpty()
|
isUndoActionActive = it.isNotEmpty()
|
||||||
|
|
||||||
if(expectedRoundCount != it.count()) {
|
if (expectedRoundCount != it.count()) {
|
||||||
redoRounds.clear()
|
redoRounds.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,8 +72,7 @@ class MainViewModel @Inject constructor(private val gameRepository: GameReposito
|
|||||||
redoRounds.remove(round)
|
redoRounds.remove(round)
|
||||||
expectedRoundCount++
|
expectedRoundCount++
|
||||||
gameRepository.addRoundToActiveGame(round.scoreA, round.scoreB)
|
gameRepository.addRoundToActiveGame(round.scoreA, round.scoreB)
|
||||||
}catch (_: NoSuchElementException)
|
} catch (_: NoSuchElementException) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,19 +2,14 @@ package me.zobrist.tichucounter.ui.settings
|
|||||||
|
|
||||||
import android.content.res.Configuration
|
import android.content.res.Configuration
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
|
||||||
import androidx.compose.foundation.interaction.collectIsPressedAsState
|
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.outlined.ArrowDropDown
|
import androidx.compose.material.icons.outlined.ArrowDropDown
|
||||||
import androidx.compose.material.icons.outlined.Check
|
import androidx.compose.material.icons.outlined.Check
|
||||||
import androidx.compose.material3.*
|
import androidx.compose.material3.*
|
||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Alignment.Companion.CenterVertically
|
|
||||||
import androidx.compose.ui.Alignment.Companion.End
|
import androidx.compose.ui.Alignment.Companion.End
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.platform.InspectableModifier
|
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.text.style.TextOverflow
|
import androidx.compose.ui.text.style.TextOverflow
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
@@ -82,10 +77,21 @@ fun SettingsView(
|
|||||||
fun BooleanSetting(name: String, value: Boolean, updateValue: (Boolean) -> Unit) {
|
fun BooleanSetting(name: String, value: Boolean, updateValue: (Boolean) -> Unit) {
|
||||||
|
|
||||||
Row(
|
Row(
|
||||||
Modifier.padding(20.dp).fillMaxWidth()) {
|
Modifier
|
||||||
|
.padding(20.dp)
|
||||||
|
.fillMaxWidth()
|
||||||
|
) {
|
||||||
Column(Modifier.weight(5f)) {
|
Column(Modifier.weight(5f)) {
|
||||||
Text(name, maxLines = 1, style = MaterialTheme.typography.bodyLarge, overflow = TextOverflow.Ellipsis)
|
Text(
|
||||||
Text(stringResource(if(value) R.string.on else R.string.off), style = MaterialTheme.typography.labelLarge)
|
name,
|
||||||
|
maxLines = 1,
|
||||||
|
style = MaterialTheme.typography.bodyLarge,
|
||||||
|
overflow = TextOverflow.Ellipsis
|
||||||
|
)
|
||||||
|
Text(
|
||||||
|
stringResource(if (value) R.string.on else R.string.off),
|
||||||
|
style = MaterialTheme.typography.labelLarge
|
||||||
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
Column(Modifier.weight(1f))
|
Column(Modifier.weight(1f))
|
||||||
@@ -93,7 +99,7 @@ fun BooleanSetting(name: String, value: Boolean, updateValue: (Boolean) -> Unit)
|
|||||||
Switch(
|
Switch(
|
||||||
checked = value,
|
checked = value,
|
||||||
modifier = Modifier.align(End),
|
modifier = Modifier.align(End),
|
||||||
onCheckedChange = { updateValue(it)})
|
onCheckedChange = { updateValue(it) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -103,40 +109,41 @@ fun <T> StringSetting(name: String, map: Map<T, Int>, selected: T, onSelected: (
|
|||||||
|
|
||||||
var expanded by remember { mutableStateOf(false) }
|
var expanded by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
Row(
|
Row(
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.padding(20.dp)
|
.padding(20.dp)
|
||||||
.clickable { expanded = true }) {
|
.clickable { expanded = true }) {
|
||||||
Column(Modifier.weight(5f)) {
|
Column(Modifier.weight(5f)) {
|
||||||
Text(name, style = MaterialTheme.typography.bodyLarge, overflow = TextOverflow.Ellipsis)
|
Text(name, style = MaterialTheme.typography.bodyLarge, overflow = TextOverflow.Ellipsis)
|
||||||
Text(stringResource(map[selected]!!), style = MaterialTheme.typography.labelLarge)
|
Text(stringResource(map[selected]!!), style = MaterialTheme.typography.labelLarge)
|
||||||
}
|
}
|
||||||
|
|
||||||
Column(Modifier.weight(1f)) {
|
Column(Modifier.weight(1f)) {
|
||||||
Icon(
|
Icon(
|
||||||
Icons.Outlined.ArrowDropDown,
|
Icons.Outlined.ArrowDropDown,
|
||||||
contentDescription = null,
|
contentDescription = null,
|
||||||
modifier = Modifier.align(End))
|
modifier = Modifier.align(End)
|
||||||
}
|
)
|
||||||
|
}
|
||||||
|
|
||||||
DropdownMenu(
|
DropdownMenu(
|
||||||
expanded = expanded,
|
expanded = expanded,
|
||||||
onDismissRequest = { expanded = false }
|
onDismissRequest = { expanded = false }
|
||||||
) {
|
) {
|
||||||
map.forEach {
|
map.forEach {
|
||||||
DropdownMenuItem(
|
DropdownMenuItem(
|
||||||
onClick = {
|
onClick = {
|
||||||
onSelected(it.key)
|
onSelected(it.key)
|
||||||
expanded = false
|
expanded = false
|
||||||
},
|
},
|
||||||
text = { Text(stringResource(it.value)) },
|
text = { Text(stringResource(it.value)) },
|
||||||
trailingIcon = {
|
trailingIcon = {
|
||||||
if (it.key == selected) {
|
if (it.key == selected) {
|
||||||
Icon(Icons.Outlined.Check, contentDescription = null)
|
Icon(Icons.Outlined.Check, contentDescription = null)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user