
- 在Package.swift中更新依赖路径,确保项目结构清晰。 - 在AppSettingFeature中新增初始化方法,支持用户信息、头像和昵称的设置。 - 更新FeedListFeature和MainFeature,新增测试按钮和导航功能,提升用户交互体验。 - 在MeFeature中优化用户信息加载逻辑,增强错误处理能力。 - 新增TestView以支持测试功能,验证导航跳转的有效性。 - 更新多个视图以整合新功能,提升代码可读性与维护性。
122 lines
4.5 KiB
Swift
122 lines
4.5 KiB
Swift
import Foundation
|
||
import ComposableArchitecture
|
||
import CasePaths
|
||
|
||
@Reducer
|
||
struct MainFeature {
|
||
enum Tab: Int, Equatable, CaseIterable {
|
||
case feed, other
|
||
}
|
||
|
||
@ObservableState
|
||
struct State: Equatable {
|
||
var selectedTab: Tab = .feed
|
||
var feedList: FeedListFeature.State = .init()
|
||
var me: MeFeature.State = .init()
|
||
var accountModel: AccountModel? = nil
|
||
// 新增:导航路径和设置页面 State
|
||
var navigationPath: [Destination] = []
|
||
var appSettingState: AppSettingFeature.State? = nil
|
||
// 新增:登出标志
|
||
var isLoggedOut: Bool = false
|
||
}
|
||
|
||
// 新增:导航目标
|
||
enum Destination: Hashable, Codable, CaseIterable {
|
||
case appSetting
|
||
case testView
|
||
}
|
||
|
||
@CasePathable
|
||
enum Action: Equatable {
|
||
case onAppear
|
||
case selectTab(Tab)
|
||
case feedList(FeedListFeature.Action)
|
||
case me(MeFeature.Action)
|
||
case accountModelLoaded(AccountModel?)
|
||
// 新增:导航相关
|
||
case navigationPathChanged([Destination])
|
||
case appSettingButtonTapped
|
||
case appSettingAction(AppSettingFeature.Action)
|
||
// 新增:登出
|
||
case logout
|
||
}
|
||
|
||
var body: some ReducerOf<Self> {
|
||
Scope(state: \ .feedList, action: \ .feedList) {
|
||
FeedListFeature()
|
||
}
|
||
Scope(state: \ .me, action: \ .me) {
|
||
MeFeature()
|
||
}
|
||
Reduce { state, action in
|
||
debugInfoSync("MainFeature action: \(action)")
|
||
debugInfoSync("MainFeature state: \(state)")
|
||
switch action {
|
||
case .onAppear:
|
||
return .run { send in
|
||
let accountModel = await UserInfoManager.getAccountModel()
|
||
await send(.accountModelLoaded(accountModel))
|
||
}
|
||
case .selectTab(let tab):
|
||
state.selectedTab = tab
|
||
state.navigationPath = []
|
||
if tab == .other, let uidStr = state.accountModel?.uid, let uid = Int(uidStr), uid > 0 {
|
||
if state.me.uid != uid {
|
||
state.me.uid = uid
|
||
state.me.isFirstLoad = true // 仅当用户切换时才重置首次加载
|
||
}
|
||
return .send(.me(.onAppear))
|
||
}
|
||
return .none
|
||
case .feedList(.testButtonTapped):
|
||
state.navigationPath.append(.testView)
|
||
return .none
|
||
case .feedList:
|
||
return .none
|
||
case let .accountModelLoaded(accountModel):
|
||
state.accountModel = accountModel
|
||
return .none
|
||
case .me(.settingButtonTapped):
|
||
// 触发 push 到设置页,带入当前用户信息
|
||
let userInfo = state.me.userInfo
|
||
let avatarURL = userInfo?.avatar
|
||
let nickname = userInfo?.nick ?? ""
|
||
state.appSettingState = AppSettingFeature.State(nickname: nickname, avatarURL: avatarURL, userInfo: userInfo)
|
||
state.navigationPath.append(.appSetting)
|
||
debugInfoSync("\(state.navigationPath)")
|
||
return .none
|
||
case .me:
|
||
return .none
|
||
case .navigationPathChanged(let newPath):
|
||
// pop 回来时清空 settingState
|
||
state.navigationPath = newPath
|
||
return .none
|
||
case .appSettingButtonTapped:
|
||
let userInfo = state.me.userInfo
|
||
let avatarURL = userInfo?.avatar
|
||
let nickname = userInfo?.nick ?? ""
|
||
state.appSettingState = AppSettingFeature.State(nickname: nickname, avatarURL: avatarURL, userInfo: userInfo)
|
||
state.navigationPath.append(.appSetting)
|
||
return .none
|
||
case .appSettingAction(.logoutTapped):
|
||
// 监听到登出,设置登出标志
|
||
state.isLoggedOut = true
|
||
return .none
|
||
case .appSettingAction(.updateUser(.success)):
|
||
// 设置页用户信息变更成功,刷新Me页数据
|
||
return .send(.me(.refresh))
|
||
case .appSettingAction:
|
||
return .none
|
||
case .logout:
|
||
// 由上层(SplashView/SplashFeature)监听,切换到登录页
|
||
return .none
|
||
}
|
||
}
|
||
// 设置页作用域
|
||
.ifLet(\ .appSettingState, action: \ .appSettingAction) {
|
||
AppSettingFeature()
|
||
}
|
||
}
|
||
}
|