Добавлена навигация

This commit is contained in:
mezhendosina 2023-06-21 14:34:43 +05:00
parent b9376be372
commit 4bbba45d49
3 changed files with 119 additions and 2 deletions

View file

@ -0,0 +1,92 @@
package ru.nm17.narodmon.ui.navHost
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Menu
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavController
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import ru.nm17.narodmon.ui.messages.MessagesScreen
import ru.nm17.narodmon.ui.sensorsScreen.SensorsScreen
import ru.nm17.narodmon.ui.settings.SettingsScreen
import ru.nm17.narodmon.ui.theme.NarodMonTheme
import ru.nm17.narodmon.ui.webCamsScreen.WebCamsScreen
val items = listOf(
MainScreenSealed.Sensors,
MainScreenSealed.Webcams,
MainScreenSealed.Messages
)
@Composable
fun MainScreen(outerNavController: NavController) {
val navController = rememberNavController()
Scaffold(
bottomBar = {
NavigationBar {
items.forEach { screen ->
NavigationBarItem(
selected = navController.currentDestination?.route == screen.route,
onClick = {
navController.navigate(screen.route) {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
},
label = { Text(text = stringResource(id = screen.resourceId)) },
icon = {
Icon(
painter = painterResource(id = screen.iconId),
contentDescription = ""
)
})
}
}
},
modifier = Modifier.fillMaxSize()
) {
NavHost(
navController,
startDestination = MainScreenSealed.Sensors.route,
Modifier.padding(it)
) {
composable(MainScreenSealed.Sensors.route) {
SensorsScreen(navController) {
outerNavController.navigate("settings")
}
}
composable(MainScreenSealed.Webcams.route) { WebCamsScreen(navController) }
composable(MainScreenSealed.Messages.route) { }
}
}
}
@Preview
@Composable
fun PreviewMainScreen() {
NarodMonTheme {
MainScreen(rememberNavController())
}
}

View file

@ -0,0 +1,23 @@
package ru.nm17.narodmon.ui.navHost
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Menu
import androidx.compose.ui.graphics.vector.ImageVector
import ru.nm17.narodmon.R
sealed class MainScreenSealed(
val route: String,
@StringRes val resourceId: Int,
@DrawableRes val iconId: Int
) {
object Sensors : MainScreenSealed("sensors", R.string.sensors_page_title, R.drawable.ic_home)
object Webcams : MainScreenSealed("webcams", R.string.webcams, R.drawable.ic_webcam)
object Messages : MainScreenSealed("messages", R.string.messages, R.drawable.ic_message)
object Settings : MainScreenSealed("settings", R.string.settings, R.drawable.ic_settings)
}

View file

@ -11,10 +11,12 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import ru.nm17.narodmon.ui.theme.NarodMonTheme
@Composable
fun WebCamsScreen() {
fun WebCamsScreen(navController: NavController) {
var webCams by remember {
mutableStateOf(
@ -58,6 +60,6 @@ fun WebCamsScreen() {
@Composable
fun PreviewWebCams() {
NarodMonTheme {
WebCamsScreen()
WebCamsScreen(rememberNavController())
}
}