|
|
@@ -1,36 +1,23 @@
|
|
|
package com.lostc.japp
|
|
|
|
|
|
import android.os.Bundle
|
|
|
-
|
|
|
import androidx.activity.ComponentActivity
|
|
|
import androidx.activity.compose.setContent
|
|
|
import androidx.activity.enableEdgeToEdge
|
|
|
-import androidx.compose.foundation.layout.Box
|
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
|
import androidx.compose.foundation.layout.padding
|
|
|
+import androidx.compose.material3.MaterialTheme
|
|
|
import androidx.compose.material3.Scaffold
|
|
|
+import androidx.compose.material3.Surface
|
|
|
import androidx.compose.material3.Text
|
|
|
-import androidx.compose.material3.NavigationBar
|
|
|
-import androidx.compose.material3.NavigationBarDefaults
|
|
|
-import androidx.compose.material3.NavigationBarItem
|
|
|
import androidx.compose.runtime.Composable
|
|
|
import androidx.compose.ui.Modifier
|
|
|
import androidx.compose.ui.tooling.preview.Preview
|
|
|
-import androidx.compose.ui.Alignment
|
|
|
-import androidx.navigation.compose.NavHost
|
|
|
-import androidx.navigation.compose.composable
|
|
|
-import androidx.compose.material3.Icon
|
|
|
-import androidx.compose.material.icons.Icons
|
|
|
-import androidx.compose.material.icons.filled.Home
|
|
|
-import androidx.compose.material.icons.filled.Notifications
|
|
|
-import androidx.compose.material.icons.filled.Place
|
|
|
import androidx.navigation.compose.rememberNavController
|
|
|
-import androidx.compose.runtime.mutableIntStateOf
|
|
|
-import androidx.compose.runtime.saveable.rememberSaveable
|
|
|
-import androidx.compose.runtime.getValue
|
|
|
-import androidx.compose.runtime.setValue
|
|
|
-import androidx.compose.ui.graphics.vector.ImageVector
|
|
|
-import androidx.navigation.NavHostController
|
|
|
+import com.lostc.japp.interfaces.Navigator
|
|
|
+import com.lostc.japp.interfaces.RealNavigator
|
|
|
+import com.lostc.japp.navigation.NavGraph
|
|
|
+import com.lostc.japp.ui.components.BottomNavBar
|
|
|
import com.lostc.japp.ui.theme.JAPPTheme
|
|
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
|
@@ -39,127 +26,48 @@ class MainActivity : ComponentActivity() {
|
|
|
enableEdgeToEdge()
|
|
|
setContent {
|
|
|
JAPPTheme {
|
|
|
- Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
|
|
- NavigationBarExample(
|
|
|
- modifier = Modifier.padding(innerPadding)
|
|
|
- )
|
|
|
+ Surface(
|
|
|
+ modifier = Modifier.fillMaxSize(),
|
|
|
+ color = MaterialTheme.colorScheme.background
|
|
|
+ ) {
|
|
|
+ MainApp()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-@Composable
|
|
|
-fun Greeting(name: String, modifier: Modifier = Modifier) {
|
|
|
- Text(
|
|
|
- text = "Hello $name!",
|
|
|
- modifier = modifier
|
|
|
- )
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-// ====
|
|
|
-
|
|
|
-enum class Destination(
|
|
|
- val route: String,
|
|
|
- val label: String,
|
|
|
- val icon: ImageVector,
|
|
|
- val contentDescription: String
|
|
|
-) {
|
|
|
- SONGS("songs", "Songs", Icons.Default.Notifications, "Songs"),
|
|
|
- ALBUM("album", "Album", Icons.Default.Home, "Album"),
|
|
|
- PLAYLISTS("playlist", "Playlist", Icons.Default.Place, "Playlist")
|
|
|
-}
|
|
|
|
|
|
@Composable
|
|
|
-fun AppNavHost(
|
|
|
- navController: NavHostController,
|
|
|
- startDestination: Destination,
|
|
|
+fun MainApp(
|
|
|
+ navigator: Navigator = RealNavigator(rememberNavController()),
|
|
|
modifier: Modifier = Modifier
|
|
|
) {
|
|
|
- NavHost(
|
|
|
- navController,
|
|
|
- startDestination = startDestination.route
|
|
|
- ) {
|
|
|
- Destination.entries.forEach { destination ->
|
|
|
- composable(destination.route) {
|
|
|
- when (destination) {
|
|
|
- Destination.SONGS -> SongsScreen()
|
|
|
- Destination.ALBUM -> AlbumScreen()
|
|
|
- Destination.PLAYLISTS -> PlaylistScreen()
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-@Composable
|
|
|
-fun SongsScreen(modifier: Modifier = Modifier) {
|
|
|
- Box(
|
|
|
- modifier = Modifier.fillMaxSize(),
|
|
|
- contentAlignment = Alignment.Center
|
|
|
- ) {
|
|
|
- Text("Songs Screen")
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-@Composable
|
|
|
-fun AlbumScreen(modifier: Modifier = Modifier) {
|
|
|
- Box(
|
|
|
- modifier = Modifier.fillMaxSize(),
|
|
|
- contentAlignment = Alignment.Center
|
|
|
- ) {
|
|
|
- Text("Album Screen")
|
|
|
- }
|
|
|
-}
|
|
|
+ println("NavController initialized: $navigator") // 调试日志
|
|
|
|
|
|
-@Composable
|
|
|
-fun PlaylistScreen(modifier: Modifier = Modifier) {
|
|
|
- Box(
|
|
|
- modifier = Modifier.fillMaxSize(),
|
|
|
- contentAlignment = Alignment.Center
|
|
|
- ) {
|
|
|
- Text("Playlist Screen")
|
|
|
+ Scaffold(
|
|
|
+ bottomBar = {
|
|
|
+ BottomNavBar(navigator = navigator)
|
|
|
+ },
|
|
|
+ modifier = modifier.fillMaxSize()
|
|
|
+ ) { paddingValues ->
|
|
|
+ NavGraph(
|
|
|
+ navigator = navigator,
|
|
|
+ modifier = Modifier.padding(paddingValues)
|
|
|
+ )
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
-fun NavigationBarExample(modifier: Modifier = Modifier) {
|
|
|
- val navController = rememberNavController()
|
|
|
- val startDestination = Destination.SONGS
|
|
|
- var selectedDestination by rememberSaveable { mutableIntStateOf(startDestination.ordinal) }
|
|
|
-
|
|
|
- Scaffold(
|
|
|
- modifier = modifier,
|
|
|
- bottomBar = {
|
|
|
- NavigationBar(windowInsets = NavigationBarDefaults.windowInsets) {
|
|
|
- Destination.entries.forEachIndexed { index, destination ->
|
|
|
- NavigationBarItem(
|
|
|
- selected = selectedDestination == index,
|
|
|
- onClick = {
|
|
|
- navController.navigate(route = destination.route)
|
|
|
- selectedDestination = index
|
|
|
- },
|
|
|
- icon = {
|
|
|
- Icon(
|
|
|
- destination.icon,
|
|
|
- contentDescription = destination.contentDescription
|
|
|
- )
|
|
|
- },
|
|
|
- label = { Text(destination.label) }
|
|
|
- )
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- ) { contentPadding ->
|
|
|
- AppNavHost(navController, startDestination, modifier = Modifier.padding(contentPadding))
|
|
|
- }
|
|
|
+fun Greeting(name: String, modifier: Modifier = Modifier) {
|
|
|
+ Text(
|
|
|
+ text = "Hello $name!",
|
|
|
+ modifier = modifier
|
|
|
+ )
|
|
|
}
|
|
|
|
|
|
-// ===
|
|
|
-
|
|
|
-@Preview(showBackground = true)
|
|
|
+@Preview
|
|
|
@Composable
|
|
|
fun GreetingPreview() {
|
|
|
JAPPTheme {
|