Clean up.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-01-20 19:33:34 +01:00
parent cd39384207
commit 45746aef0b
5 changed files with 78 additions and 63 deletions

View File

@@ -15,7 +15,6 @@ import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
@@ -89,8 +88,14 @@ class MainActivity : BaseActivity() {
composable("counter") {
Counter(counterViewModel)
mainViewModel.topBarActions = (listOf(
TopBarAction(Icons.Outlined.Undo, mainViewModel.isUndoActionActive) { mainViewModel.undoLastRound() },
TopBarAction(Icons.Outlined.Redo, mainViewModel.isRedoActionActive) { mainViewModel.redoLastRound() }
TopBarAction(
Icons.Outlined.Undo,
mainViewModel.isUndoActionActive
) { mainViewModel.undoLastRound() },
TopBarAction(
Icons.Outlined.Redo,
mainViewModel.isRedoActionActive
) { mainViewModel.redoLastRound() }
))
mainViewModel.topBarIcon = Icons.Outlined.Menu
@@ -163,7 +168,9 @@ class MainActivity : BaseActivity() {
val scope = rememberCoroutineScope()
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 currentDestination = navBackStackEntry?.destination
@@ -236,9 +243,5 @@ class MainActivity : BaseActivity() {
)
}
sealed 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)
}
private class Screen(val route: String, val icon: ImageVector, @StringRes val resourceId: Int)
}

View File

@@ -7,7 +7,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
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) {
@@ -17,10 +17,10 @@ class SettingsAdapter @Inject constructor(@ApplicationContext private val contex
val language: Language
get() {
var setting = sharedPreferences.getString(Language::class.simpleName, null)
if(setting == null)
{
if (setting == null) {
setCurrentLanguage()
setting = sharedPreferences.getString(Language::class.simpleName, Language.ENGLISH.name)
setting =
sharedPreferences.getString(Language::class.simpleName, Language.ENGLISH.name)
}
return enumValueOf(setting!!)
}
@@ -37,12 +37,13 @@ class SettingsAdapter @Inject constructor(@ApplicationContext private val contex
}
private fun setCurrentLanguage() {
var setting = when(getApplicationLocales()[0].toString()) {
var setting = when (getApplicationLocales()[0].toString()) {
"de" -> Language.GERMAN
else -> Language.ENGLISH
}
setLanguage(setting)
}
fun setLanguage(language: Language) {
val editor = sharedPreferences.edit()
editor.putString(Language::class.simpleName, language.name)

View File

@@ -2,7 +2,6 @@ package me.zobrist.tichucounter.repository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.last
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.zobrist.tichucounter.data.Game
@@ -63,10 +62,11 @@ class GameRepository @Inject constructor(
withContext(Dispatchers.IO) {
roundDao.getAllForGame(activeGame.uid).last()
}
} catch (_:NoSuchElementException) {
} catch (_: NoSuchElementException) {
null
}
}
suspend fun deleteLastRound() {
withContext(Dispatchers.IO) {
try {

View File

@@ -2,7 +2,10 @@ package me.zobrist.tichucounter.ui
import androidx.compose.material.icons.Icons
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.viewModelScope
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.TopBarAction
import me.zobrist.tichucounter.repository.GameRepository
import java.util.NoSuchElementException
import javax.inject.Inject
@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 expectedRoundCount = 0
@@ -35,7 +40,7 @@ class MainViewModel @Inject constructor(private val gameRepository: GameReposito
roundDao.getForActiveGame().collect() {
isUndoActionActive = it.isNotEmpty()
if(expectedRoundCount != it.count()) {
if (expectedRoundCount != it.count()) {
redoRounds.clear()
}
@@ -67,8 +72,7 @@ class MainViewModel @Inject constructor(private val gameRepository: GameReposito
redoRounds.remove(round)
expectedRoundCount++
gameRepository.addRoundToActiveGame(round.scoreA, round.scoreB)
}catch (_: NoSuchElementException)
{
} catch (_: NoSuchElementException) {
}
}
}

View File

@@ -2,19 +2,14 @@ package me.zobrist.tichucounter.ui.settings
import android.content.res.Configuration
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.material.icons.Icons
import androidx.compose.material.icons.outlined.ArrowDropDown
import androidx.compose.material.icons.outlined.Check
import androidx.compose.material3.*
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.Modifier
import androidx.compose.ui.platform.InspectableModifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
@@ -82,10 +77,21 @@ fun SettingsView(
fun BooleanSetting(name: String, value: Boolean, updateValue: (Boolean) -> Unit) {
Row(
Modifier.padding(20.dp).fillMaxWidth()) {
Modifier
.padding(20.dp)
.fillMaxWidth()
) {
Column(Modifier.weight(5f)) {
Text(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)
Text(
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))
@@ -93,7 +99,7 @@ fun BooleanSetting(name: String, value: Boolean, updateValue: (Boolean) -> Unit)
Switch(
checked = value,
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) }
Row(
Modifier
.fillMaxWidth()
.padding(20.dp)
.clickable { expanded = true }) {
Column(Modifier.weight(5f)) {
Text(name, style = MaterialTheme.typography.bodyLarge, overflow = TextOverflow.Ellipsis)
Text(stringResource(map[selected]!!), style = MaterialTheme.typography.labelLarge)
}
Row(
Modifier
.fillMaxWidth()
.padding(20.dp)
.clickable { expanded = true }) {
Column(Modifier.weight(5f)) {
Text(name, style = MaterialTheme.typography.bodyLarge, overflow = TextOverflow.Ellipsis)
Text(stringResource(map[selected]!!), style = MaterialTheme.typography.labelLarge)
}
Column(Modifier.weight(1f)) {
Icon(
Icons.Outlined.ArrowDropDown,
contentDescription = null,
modifier = Modifier.align(End))
}
Column(Modifier.weight(1f)) {
Icon(
Icons.Outlined.ArrowDropDown,
contentDescription = null,
modifier = Modifier.align(End)
)
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
map.forEach {
DropdownMenuItem(
onClick = {
onSelected(it.key)
expanded = false
},
text = { Text(stringResource(it.value)) },
trailingIcon = {
if (it.key == selected) {
Icon(Icons.Outlined.Check, contentDescription = null)
}
})
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
map.forEach {
DropdownMenuItem(
onClick = {
onSelected(it.key)
expanded = false
},
text = { Text(stringResource(it.value)) },
trailingIcon = {
if (it.key == selected) {
Icon(Icons.Outlined.Check, contentDescription = null)
}
})
}
}
}
}