
- 在FeedListFeature中添加返回语句,确保认证信息准备好后立即获取动态。 - 移除FeedListView中的冗余注释,提升代码整洁性。 - 更新MainView中的方法名称,从contentView更改为mainContentView,增强代码可读性。
153 lines
5.7 KiB
Swift
153 lines
5.7 KiB
Swift
import SwiftUI
|
|
import ComposableArchitecture
|
|
|
|
struct MainView: View {
|
|
let store: StoreOf<MainFeature>
|
|
var onLogout: (() -> Void)? = nil
|
|
|
|
var body: some View {
|
|
WithPerceptionTracking {
|
|
InternalMainView(store: store)
|
|
.onChange(of: store.isLoggedOut) { _, isLoggedOut in
|
|
if isLoggedOut {
|
|
onLogout?()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct InternalMainView: View {
|
|
let store: StoreOf<MainFeature>
|
|
@State private var path: [MainFeature.Destination] = []
|
|
init(store: StoreOf<MainFeature>) {
|
|
self.store = store
|
|
_path = State(initialValue: store.withState { $0.navigationPath })
|
|
}
|
|
var body: some View {
|
|
WithPerceptionTracking {
|
|
NavigationStack(path: $path) {
|
|
GeometryReader { geometry in
|
|
mainContentView(geometry: geometry)
|
|
.navigationDestination(for: MainFeature.Destination.self) { destination in
|
|
DestinationView(destination: destination, store: self.store)
|
|
}
|
|
.onChange(of: path) { _, path in
|
|
store.send(.navigationPathChanged(path))
|
|
}
|
|
.onChange(of: store.navigationPath) { _, navigationPath in
|
|
if path != navigationPath {
|
|
path = navigationPath
|
|
}
|
|
}
|
|
.onAppear {
|
|
debugInfoSync("🚀 MainView onAppear")
|
|
debugInfoSync(" 当前selectedTab: \(store.selectedTab)")
|
|
store.send(.onAppear)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct DestinationView: View {
|
|
let destination: MainFeature.Destination
|
|
let store: StoreOf<MainFeature>
|
|
|
|
var body: some View {
|
|
switch destination {
|
|
case .appSetting:
|
|
IfLetStore(
|
|
store.scope(state: \.appSettingState, action: \.appSettingAction),
|
|
then: { store in
|
|
WithPerceptionTracking {
|
|
AppSettingView(store: store)
|
|
}
|
|
},
|
|
else: { Text("appSettingState is nil") }
|
|
)
|
|
case .testView:
|
|
TestView()
|
|
}
|
|
}
|
|
}
|
|
|
|
private func mainContentView(geometry: GeometryProxy) -> some View {
|
|
WithPerceptionTracking {
|
|
ZStack {
|
|
// 背景图片
|
|
Image("bg")
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fill)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
.clipped()
|
|
.ignoresSafeArea(.all)
|
|
// 主内容
|
|
MainContentView(
|
|
store: store,
|
|
selectedTab: store.selectedTab
|
|
)
|
|
.onChange(of: store.selectedTab) { _, newTab in
|
|
debugInfoSync("🔄 MainView selectedTab changed: \(newTab)")
|
|
}
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
.padding(.bottom, 80) // 为底部导航栏留出空间
|
|
|
|
// 底部导航栏 - 固定在底部
|
|
VStack {
|
|
Spacer()
|
|
BottomTabView(selectedTab: Binding(
|
|
get: {
|
|
// 将MainFeature.Tab转换为BottomTabView.Tab
|
|
let currentTab = store.selectedTab == .feed ? Tab.feed : Tab.me
|
|
debugInfoSync("🔍 BottomTabView get: MainFeature.Tab.\(store.selectedTab) → BottomTabView.Tab.\(currentTab)")
|
|
return currentTab
|
|
},
|
|
set: { newTab in
|
|
// 将BottomTabView.Tab转换为MainFeature.Tab
|
|
let mainTab: MainFeature.Tab = newTab == .feed ? .feed : .other
|
|
debugInfoSync("🔍 BottomTabView set: BottomTabView.Tab.\(newTab) → MainFeature.Tab.\(mainTab)")
|
|
store.send(.selectTab(mainTab))
|
|
}
|
|
))
|
|
}
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
|
|
.padding(.bottom, 100)
|
|
.ignoresSafeArea(.keyboard, edges: .bottom)
|
|
|
|
// 添加API Loading和错误处理视图
|
|
APILoadingEffectView()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct MainContentView: View {
|
|
let store: StoreOf<MainFeature>
|
|
let selectedTab: MainFeature.Tab
|
|
var body: some View {
|
|
WithPerceptionTracking {
|
|
let _ = debugInfoSync("📱 MainContentView selectedTab: \(selectedTab)")
|
|
let _ = debugInfoSync(" 与store.selectedTab一致: \(selectedTab == store.selectedTab)")
|
|
Group {
|
|
if selectedTab == .feed {
|
|
FeedListView(store: store.scope(
|
|
state: \.feedList,
|
|
action: \.feedList
|
|
))
|
|
} else if selectedTab == .other {
|
|
MeView(
|
|
store: store.scope(
|
|
state: \.me,
|
|
action: \.me
|
|
),
|
|
showCloseButton: false // MainView中不需要关闭按钮
|
|
)
|
|
} else {
|
|
CustomEmptyView(onRetry: {})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|