You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zero/app/src/main/java/com/example/myapplication/MainActivity.kt

76 lines
2.8 KiB

package com.example.myapplication
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.example.myapplication.ui.NoteEditorScreen
import com.example.myapplication.ui.NoteListScreen
import com.example.myapplication.ui.theme.XiaomiNoteTheme
import com.example.myapplication.viewmodel.NoteViewModel
/**
* 小米便签主活动 - 孟欣瑞负责
*
* 应用入口点,负责初始化 Compose UI 和导航
* 使用 MVVM 架构,通过 ViewModel 管理业务逻辑
*/
class MainActivity : ComponentActivity() {
// ViewModel 实例,由 Android 自动管理生命周期
private val viewModel: NoteViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 使用 Compose 设置 UI
setContent {
XiaomiNoteTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
// 导航宿主
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = "list"
) {
// 列表页面
composable("list") {
NoteListScreen(
navController = navController,
viewModel = viewModel
)
}
// 编辑页面
composable(
route = "editor/{noteId}",
arguments = listOf(
navArgument("noteId") { type = NavType.IntType }
)
) { backStackEntry ->
val noteId = backStackEntry.arguments?.getInt("noteId") ?: 0
NoteEditorScreen(
navController = navController,
viewModel = viewModel,
noteId = noteId
)
}
}
}
}
}
}
}