Move drawerContent to own file.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -4,7 +4,6 @@ import android.os.Bundle
|
|||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
import androidx.annotation.StringRes
|
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
@@ -14,9 +13,7 @@ import androidx.compose.material.icons.outlined.*
|
|||||||
import androidx.compose.material3.*
|
import androidx.compose.material3.*
|
||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.vector.ImageVector
|
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import androidx.navigation.compose.NavHost
|
import androidx.navigation.compose.NavHost
|
||||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||||
@@ -26,8 +23,8 @@ import dagger.hilt.android.AndroidEntryPoint
|
|||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import me.zobrist.tichucounter.domain.*
|
import me.zobrist.tichucounter.domain.*
|
||||||
import me.zobrist.tichucounter.repository.GameRepository
|
|
||||||
import me.zobrist.tichucounter.ui.AppTheme
|
import me.zobrist.tichucounter.ui.AppTheme
|
||||||
|
import me.zobrist.tichucounter.ui.DrawerContent
|
||||||
import me.zobrist.tichucounter.ui.MainViewModel
|
import me.zobrist.tichucounter.ui.MainViewModel
|
||||||
import me.zobrist.tichucounter.ui.TopBar
|
import me.zobrist.tichucounter.ui.TopBar
|
||||||
import me.zobrist.tichucounter.ui.composables.DropDownMenu
|
import me.zobrist.tichucounter.ui.composables.DropDownMenu
|
||||||
@@ -41,9 +38,6 @@ import javax.inject.Inject
|
|||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class MainActivity : AppCompatActivity(), ISettingsChangeListener {
|
class MainActivity : AppCompatActivity(), ISettingsChangeListener {
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var gameRepository: GameRepository
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var settingsAdapter: SettingsAdapter
|
lateinit var settingsAdapter: SettingsAdapter
|
||||||
|
|
||||||
@@ -93,6 +87,47 @@ class MainActivity : AppCompatActivity(), ISettingsChangeListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
|
@Composable
|
||||||
|
private fun NavigationDrawer() {
|
||||||
|
val drawerState = rememberDrawerState(DrawerValue.Closed)
|
||||||
|
val scope = rememberCoroutineScope()
|
||||||
|
val navController = rememberNavController()
|
||||||
|
|
||||||
|
val items = listOf(
|
||||||
|
Screen(Route.COUNTER, Icons.Outlined.Calculate, R.string.app_name),
|
||||||
|
Screen(Route.HISTORY, Icons.Outlined.List, R.string.menu_history),
|
||||||
|
Screen(Route.SETTINGS, Icons.Outlined.Settings, R.string.menu_settings)
|
||||||
|
)
|
||||||
|
|
||||||
|
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
||||||
|
val currentDestination =
|
||||||
|
Route.valueOf(navBackStackEntry?.destination?.route ?: Route.COUNTER.name)
|
||||||
|
|
||||||
|
ModalNavigationDrawer(
|
||||||
|
drawerState = drawerState,
|
||||||
|
gesturesEnabled = drawerState.isOpen,
|
||||||
|
drawerContent = {
|
||||||
|
DrawerContent(
|
||||||
|
screens = items,
|
||||||
|
selectedScreen = items.first { it.route == currentDestination }) {
|
||||||
|
scope.launch {
|
||||||
|
drawerState.close()
|
||||||
|
|
||||||
|
}
|
||||||
|
navController.navigate(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
MyScaffoldLayout(
|
||||||
|
drawerState,
|
||||||
|
scope,
|
||||||
|
navController,
|
||||||
|
counterViewModel.keyboardHidden && (currentDestination == Route.COUNTER)
|
||||||
|
) { counterViewModel.showKeyboard() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun MyScaffoldLayout(
|
fun MyScaffoldLayout(
|
||||||
@@ -175,72 +210,4 @@ class MainActivity : AppCompatActivity(), ISettingsChangeListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
|
||||||
private fun NavigationDrawer() {
|
|
||||||
val drawerState = rememberDrawerState(DrawerValue.Closed)
|
|
||||||
val scope = rememberCoroutineScope()
|
|
||||||
val navController = rememberNavController()
|
|
||||||
|
|
||||||
val items = listOf(
|
|
||||||
Screen(Route.COUNTER, Icons.Outlined.Calculate, R.string.app_name),
|
|
||||||
Screen(Route.HISTORY, Icons.Outlined.List, R.string.menu_history),
|
|
||||||
Screen(Route.SETTINGS, Icons.Outlined.Settings, R.string.menu_settings)
|
|
||||||
)
|
|
||||||
|
|
||||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
|
||||||
val currentDestination =
|
|
||||||
Route.valueOf(navBackStackEntry?.destination?.route ?: Route.COUNTER.name)
|
|
||||||
|
|
||||||
ModalNavigationDrawer(
|
|
||||||
drawerState = drawerState,
|
|
||||||
gesturesEnabled = drawerState.isOpen,
|
|
||||||
drawerContent = {
|
|
||||||
DrawerContent(
|
|
||||||
screens = items,
|
|
||||||
selectedScreen = items.first { it.route == currentDestination }) {
|
|
||||||
scope.launch {
|
|
||||||
drawerState.close()
|
|
||||||
|
|
||||||
}
|
|
||||||
navController.navigate(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
MyScaffoldLayout(
|
|
||||||
drawerState,
|
|
||||||
scope,
|
|
||||||
navController,
|
|
||||||
counterViewModel.keyboardHidden && (currentDestination == Route.COUNTER)
|
|
||||||
) { counterViewModel.showKeyboard() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
|
||||||
private fun DrawerContent(
|
|
||||||
screens: List<Screen>,
|
|
||||||
selectedScreen: Screen,
|
|
||||||
onElementClicked: (Route) -> Unit
|
|
||||||
) {
|
|
||||||
|
|
||||||
ModalDrawerSheet {
|
|
||||||
|
|
||||||
Spacer(Modifier.height(20.dp))
|
|
||||||
|
|
||||||
screens.forEach { screen ->
|
|
||||||
NavigationDrawerItem(
|
|
||||||
icon = { Icon(screen.icon, contentDescription = null) },
|
|
||||||
label = { Text(stringResource(screen.resourceId)) },
|
|
||||||
selected = screen == selectedScreen,
|
|
||||||
onClick = { onElementClicked(screen.route) },
|
|
||||||
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class Screen(val route: Route, val icon: ImageVector, @StringRes val resourceId: Int)
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package me.zobrist.tichucounter.domain
|
||||||
|
|
||||||
|
import androidx.annotation.StringRes
|
||||||
|
import androidx.compose.ui.graphics.vector.ImageVector
|
||||||
|
|
||||||
|
class Screen(val route: Route, val icon: ImageVector, @StringRes val resourceId: Int)
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package me.zobrist.tichucounter.ui
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Spacer
|
||||||
|
import androidx.compose.foundation.layout.height
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.material3.*
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import me.zobrist.tichucounter.domain.Route
|
||||||
|
import me.zobrist.tichucounter.domain.Screen
|
||||||
|
|
||||||
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
|
@Composable
|
||||||
|
fun DrawerContent(
|
||||||
|
screens: List<Screen>,
|
||||||
|
selectedScreen: Screen,
|
||||||
|
onElementClicked: (Route) -> Unit
|
||||||
|
) {
|
||||||
|
|
||||||
|
ModalDrawerSheet {
|
||||||
|
|
||||||
|
Spacer(Modifier.height(20.dp))
|
||||||
|
|
||||||
|
screens.forEach { screen ->
|
||||||
|
NavigationDrawerItem(
|
||||||
|
icon = { Icon(screen.icon, contentDescription = null) },
|
||||||
|
label = { Text(stringResource(screen.resourceId)) },
|
||||||
|
selected = screen == selectedScreen,
|
||||||
|
onClick = { onElementClicked(screen.route) },
|
||||||
|
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user