
- 在ContentView中根据编译模式初始化日志级别,确保调试信息的灵活性。 - 在APILogger中使用actor封装日志级别,增强并发安全性。 - 新增通用底部Tab栏组件,优化MainPage中的底部导航逻辑,提升代码可维护性。 - 移除冗余的AppRootView和MainView,简化视图结构,提升代码整洁性。
150 lines
4.8 KiB
Swift
150 lines
4.8 KiB
Swift
import SwiftUI
|
||
|
||
// MARK: - Main View
|
||
|
||
struct MainPage: View {
|
||
@StateObject private var viewModel = MainViewModel()
|
||
let onLogout: () -> Void
|
||
|
||
var body: some View {
|
||
NavigationStack(path: $viewModel.navigationPath) {
|
||
GeometryReader { geometry in
|
||
ZStack {
|
||
// 背景图片
|
||
LoginBackgroundView()
|
||
// 主内容
|
||
mainContentView(geometry: geometry)
|
||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||
|
||
VStack {
|
||
HStack {
|
||
Spacer()
|
||
// 右上角按钮
|
||
topRightButton
|
||
}
|
||
Spacer()
|
||
// 底部导航栏(组件化)
|
||
BottomTabBar(
|
||
items: [
|
||
TabBarItem(id: MainViewModel.Tab.feed.rawValue, title: MainViewModel.Tab.feed.title, systemIconName: MainViewModel.Tab.feed.iconName),
|
||
TabBarItem(id: MainViewModel.Tab.me.rawValue, title: MainViewModel.Tab.me.title, systemIconName: MainViewModel.Tab.me.iconName)
|
||
],
|
||
selectedId: Binding(
|
||
get: { viewModel.selectedTab.rawValue },
|
||
set: { raw in
|
||
if let tab = MainViewModel.Tab(rawValue: raw) {
|
||
viewModel.onTabChanged(tab)
|
||
}
|
||
}
|
||
),
|
||
onSelect: { _ in }
|
||
)
|
||
.frame(height: 80)
|
||
.padding(.horizontal, 24)
|
||
.padding(.bottom, 100)
|
||
}
|
||
}
|
||
}
|
||
.navigationDestination(for: String.self) { _ in EmptyView() }
|
||
.navigationDestination(for: AppRoute.self) { route in
|
||
switch route {
|
||
case .setting:
|
||
SettingPage(
|
||
onBack: {
|
||
viewModel.navigationPath.removeLast()
|
||
},
|
||
onLogout: {
|
||
viewModel.onLogoutTapped()
|
||
}
|
||
)
|
||
.navigationBarHidden(true)
|
||
case .publish:
|
||
CreateFeedPage(
|
||
onDismiss: {
|
||
viewModel.navigationPath.removeLast()
|
||
}
|
||
)
|
||
default:
|
||
EmptyView()
|
||
}
|
||
}
|
||
}
|
||
.onAppear {
|
||
viewModel.onLogout = onLogout
|
||
viewModel.onAddButtonTapped = {
|
||
// TODO: 处理添加按钮点击事件
|
||
debugInfoSync("➕ 添加按钮被点击")
|
||
}
|
||
viewModel.onAppear()
|
||
}
|
||
.onChange(of: viewModel.isLoggedOut) { _, isLoggedOut in
|
||
if isLoggedOut {
|
||
onLogout()
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - UI Components
|
||
|
||
private func mainContentView(geometry: GeometryProxy) -> some View {
|
||
Group {
|
||
switch viewModel.selectedTab {
|
||
case .feed:
|
||
MomentListHomePage()
|
||
case .me:
|
||
TempMePage()
|
||
}
|
||
}
|
||
}
|
||
|
||
// 底部栏已组件化
|
||
|
||
// MARK: - 右上角按钮
|
||
private var topRightButton: some View {
|
||
Button(action: {
|
||
viewModel.onTopRightButtonTapped()
|
||
}) {
|
||
Group {
|
||
switch viewModel.selectedTab {
|
||
case .feed:
|
||
Image("add icon")
|
||
.resizable()
|
||
.aspectRatio(contentMode: .fit)
|
||
.frame(width: 40, height: 40)
|
||
case .me:
|
||
Image(systemName: "gearshape")
|
||
.font(.system(size: 24, weight: .medium))
|
||
.foregroundColor(.white)
|
||
.frame(width: 40, height: 40)
|
||
.background(Color.black.opacity(0.3))
|
||
.clipShape(Circle())
|
||
}
|
||
}
|
||
}
|
||
.padding(.trailing, 16)
|
||
.padding(.top, 8)
|
||
}
|
||
}
|
||
|
||
|
||
|
||
// MARK: - MeView (简化版本)
|
||
|
||
struct TempMePage: View {
|
||
var body: some View {
|
||
VStack {
|
||
Text("Me View")
|
||
.font(.title)
|
||
.foregroundColor(.white)
|
||
|
||
Text("This is a simplified MeView")
|
||
.font(.body)
|
||
.foregroundColor(.white.opacity(0.8))
|
||
}
|
||
}
|
||
}
|
||
|
||
//#Preview {
|
||
// MainPage(onLogout: {})
|
||
//}
|