
- 在APIEndpoints中新增用户信息更新端点。 - 实现UpdateUserRequest和UpdateUserResponse结构体,支持用户信息更新请求和响应。 - 在APIService中添加updateUser方法,处理用户信息更新请求。 - 更新AppSettingFeature以支持头像和昵称的修改,整合用户信息更新逻辑。 - 在AppSettingView中实现头像选择和昵称编辑功能,提升用户体验。
114 lines
3.9 KiB
Swift
114 lines
3.9 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, Equatable {
|
||
case test
|
||
case appSetting
|
||
}
|
||
|
||
@CasePathable
|
||
enum Action: Equatable {
|
||
case onAppear
|
||
case selectTab(Tab)
|
||
case feedList(FeedListFeature.Action)
|
||
case me(MeFeature.Action)
|
||
case accountModelLoaded(AccountModel?)
|
||
// 新增:导航相关
|
||
case navigationPathChanged([Destination])
|
||
case testButtonTapped
|
||
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
|
||
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:
|
||
return .none
|
||
case let .accountModelLoaded(accountModel):
|
||
state.accountModel = accountModel
|
||
return .none
|
||
case .me(.settingButtonTapped):
|
||
// 触发 push 到设置页
|
||
state.appSettingState = AppSettingFeature.State()
|
||
state.navigationPath.append(.appSetting)
|
||
return .none
|
||
case .me:
|
||
return .none
|
||
case .navigationPathChanged(let newPath):
|
||
// pop 回来时清空 settingState
|
||
if !newPath.contains(.appSetting) {
|
||
state.appSettingState = nil
|
||
}
|
||
state.navigationPath = newPath
|
||
return .none
|
||
case .testButtonTapped:
|
||
state.navigationPath.append(.test)
|
||
return .none
|
||
case .appSettingButtonTapped:
|
||
state.appSettingState = AppSettingFeature.State()
|
||
state.navigationPath.append(.appSetting)
|
||
return .none
|
||
case .appSettingAction(.logoutTapped):
|
||
// 监听到登出,设置登出标志
|
||
state.isLoggedOut = true
|
||
return .none
|
||
case .appSettingAction:
|
||
return .none
|
||
case .logout:
|
||
// 由上层(SplashView/SplashFeature)监听,切换到登录页
|
||
return .none
|
||
}
|
||
}
|
||
// 设置页作用域
|
||
.ifLet(\ .appSettingState, action: \ .appSettingAction) {
|
||
AppSettingFeature()
|
||
}
|
||
}
|
||
}
|