This commit is contained in:
@@ -26,6 +26,7 @@ import com.google.accompanist.systemuicontroller.rememberSystemUiController
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import me.zobrist.tichucounter.domain.NavigationAction
|
||||
import me.zobrist.tichucounter.domain.TopBarAction
|
||||
import me.zobrist.tichucounter.repository.GameRepository
|
||||
import me.zobrist.tichucounter.ui.AppTheme
|
||||
@@ -70,7 +71,14 @@ class MainActivity : BaseActivity() {
|
||||
systemUiController.setStatusBarColor(MaterialTheme.colorScheme.background)
|
||||
|
||||
Scaffold(
|
||||
topBar = { TopBar(drawerState, scope, mainViewModel.topBarActions) }) {
|
||||
topBar = {
|
||||
TopBar(
|
||||
mainViewModel.topBarTitle,
|
||||
mainViewModel.topBarIcon,
|
||||
{ mainViewModel.onNavigateClicked() },
|
||||
mainViewModel.topBarActions
|
||||
)
|
||||
}) {
|
||||
|
||||
NavHost(
|
||||
navController = navController,
|
||||
@@ -79,16 +87,27 @@ class MainActivity : BaseActivity() {
|
||||
) {
|
||||
composable("counter") {
|
||||
Counter(counterViewModel)
|
||||
mainViewModel.setActions(listOf(undoAction, newGameAction))
|
||||
mainViewModel.topBarActions = (listOf(undoAction, newGameAction))
|
||||
mainViewModel.topBarIcon = Icons.Outlined.Menu
|
||||
mainViewModel.topBarTitle = stringResource(R.string.app_name)
|
||||
mainViewModel.topBarNavigationAction =
|
||||
NavigationAction { scope.launch { drawerState.open() } }
|
||||
}
|
||||
composable("history") {
|
||||
HistoryList(historyViewModel)
|
||||
mainViewModel.setActions(emptyList())
|
||||
|
||||
mainViewModel.topBarActions = emptyList()
|
||||
mainViewModel.topBarIcon = Icons.Outlined.ArrowBack
|
||||
mainViewModel.topBarTitle = stringResource(R.string.menu_history)
|
||||
mainViewModel.topBarNavigationAction =
|
||||
NavigationAction { navController.navigate("counter") }
|
||||
}
|
||||
composable("settings") {
|
||||
SettingsView(settingsViewModel)
|
||||
mainViewModel.setActions(emptyList())
|
||||
mainViewModel.topBarActions = emptyList()
|
||||
mainViewModel.topBarIcon = Icons.Outlined.ArrowBack
|
||||
mainViewModel.topBarTitle = stringResource(R.string.menu_settings)
|
||||
mainViewModel.topBarNavigationAction =
|
||||
NavigationAction { navController.navigate("counter") }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,22 +117,23 @@ class MainActivity : BaseActivity() {
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
private fun TopBar(
|
||||
drawerState: DrawerState,
|
||||
scope: CoroutineScope,
|
||||
title: String,
|
||||
icon: ImageVector,
|
||||
navigateAction: () -> Unit,
|
||||
actions: List<TopBarAction>
|
||||
) {
|
||||
CenterAlignedTopAppBar(
|
||||
TopAppBar(
|
||||
title = {
|
||||
Text(
|
||||
stringResource(R.string.app_name),
|
||||
title,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis
|
||||
)
|
||||
},
|
||||
navigationIcon = {
|
||||
IconButton(onClick = { scope.launch { drawerState.open() } }) {
|
||||
IconButton(onClick = { navigateAction() }) {
|
||||
Icon(
|
||||
imageVector = Icons.Filled.Menu,
|
||||
imageVector = icon,
|
||||
contentDescription = "Localized description"
|
||||
)
|
||||
}
|
||||
@@ -138,7 +158,7 @@ class MainActivity : BaseActivity() {
|
||||
val scope = rememberCoroutineScope()
|
||||
val navController = rememberNavController()
|
||||
|
||||
val items = listOf(Screen.Calculator, Screen.History, Screen.Settings)
|
||||
val items = listOf(Screen.History, Screen.Settings)
|
||||
|
||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
||||
val currentDestination = navBackStackEntry?.destination
|
||||
@@ -179,13 +199,11 @@ class MainActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
sealed class Screen(val route: String, val icon: ImageVector, @StringRes val resourceId: Int) {
|
||||
object Calculator : Screen("counter", Icons.Outlined.Calculate, R.string.menu_counter)
|
||||
object History : Screen("history", Icons.Outlined.List, R.string.menu_history)
|
||||
object Settings : Screen("settings", Icons.Outlined.Settings, R.string.menu_settings)
|
||||
|
||||
}
|
||||
|
||||
|
||||
private val undoAction = TopBarAction(Icons.Outlined.Undo) { mainViewModel.undoLastRound() }
|
||||
private val newGameAction = TopBarAction(Icons.Outlined.Add) { mainViewModel.newGame() }
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
package me.zobrist.tichucounter.domain
|
||||
|
||||
class NavigationAction(val aciton: () -> Unit)
|
||||
@@ -1,5 +1,7 @@
|
||||
package me.zobrist.tichucounter.ui
|
||||
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Menu
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
@@ -7,6 +9,7 @@ import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
import me.zobrist.tichucounter.domain.NavigationAction
|
||||
import me.zobrist.tichucounter.domain.TopBarAction
|
||||
import me.zobrist.tichucounter.repository.GameRepository
|
||||
import javax.inject.Inject
|
||||
@@ -16,14 +19,13 @@ class MainViewModel @Inject constructor(private val gameRepository: GameReposito
|
||||
|
||||
var topBarTitle by mutableStateOf("")
|
||||
var topBarActions by mutableStateOf(emptyList<TopBarAction>())
|
||||
private set
|
||||
var topBarIcon by mutableStateOf(Icons.Filled.Menu)
|
||||
|
||||
fun setActions(actions: List<TopBarAction>) {
|
||||
topBarActions = actions
|
||||
}
|
||||
|
||||
fun setTitle(title: String) {
|
||||
topBarTitle = title
|
||||
var topBarNavigationAction by mutableStateOf(NavigationAction {})
|
||||
|
||||
fun onNavigateClicked() {
|
||||
topBarNavigationAction.aciton()
|
||||
}
|
||||
|
||||
fun undoLastRound() {
|
||||
|
||||
@@ -114,8 +114,9 @@ fun CounterViewPreview() {
|
||||
}
|
||||
}
|
||||
|
||||
internal class PreviewViewModel: ICounterViewModel {
|
||||
override var roundScoreList: List<Round> = listOf(Round(1, 10, 90), Round(1,50,50), Round(1,70,30))
|
||||
internal class PreviewViewModel : ICounterViewModel {
|
||||
override var roundScoreList: List<Round> =
|
||||
listOf(Round(1, 10, 90), Round(1, 50, 50), Round(1, 70, 30))
|
||||
override var totalScoreA: Int = 350
|
||||
override var totalScoreB: Int = 750
|
||||
override var teamNameA: String = "Team A"
|
||||
|
||||
@@ -3,7 +3,6 @@ package me.zobrist.tichucounter.ui.counter
|
||||
import android.content.res.Configuration
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Backspace
|
||||
@@ -17,13 +16,8 @@ import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.focus.onFocusChanged
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||
import androidx.compose.ui.platform.SoftwareKeyboardController
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.constraintlayout.helper.widget.MotionPlaceholder
|
||||
import me.zobrist.tichucounter.R
|
||||
import me.zobrist.tichucounter.ui.AppTheme
|
||||
|
||||
|
||||
|
||||
@@ -24,7 +24,9 @@ fun TeamScoresView(scoreA: Int, scoreB: Int) {
|
||||
Text(
|
||||
style = MaterialTheme.typography.headlineSmall,
|
||||
text = scoreA.toString(),
|
||||
modifier = Modifier.weight(5f).padding(6.dp),
|
||||
modifier = Modifier
|
||||
.weight(5f)
|
||||
.padding(6.dp),
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
|
||||
@@ -33,7 +35,9 @@ fun TeamScoresView(scoreA: Int, scoreB: Int) {
|
||||
Text(
|
||||
style = MaterialTheme.typography.headlineSmall,
|
||||
text = scoreB.toString(),
|
||||
modifier = Modifier.weight(5f).padding(6.dp),
|
||||
modifier = Modifier
|
||||
.weight(5f)
|
||||
.padding(6.dp),
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user