import Foundation import ComposableArchitecture @Reducer struct AppSettingFeature { @ObservableState struct State: Equatable { var nickname: String = "" var avatarURL: String? = nil var userInfo: UserInfo? = nil var isLoadingUserInfo: Bool = false var userInfoError: String? = nil // WebView 导航状态 var showUserAgreement: Bool = false var showPrivacyPolicy: Bool = false } enum Action: Equatable { case onAppear case editNicknameTapped case logoutTapped // 用户信息相关 case loadUserInfo case userInfoResponse(Result) // WebView 导航 case personalInfoPermissionsTapped case helpTapped case clearCacheTapped case checkUpdatesTapped case aboutUsTapped // WebView 关闭 case userAgreementDismissed case privacyPolicyDismissed } @Dependency(\.apiService) var apiService var body: some ReducerOf { Reduce { state, action in switch action { case .onAppear: return .send(.loadUserInfo) case .editNicknameTapped: // 预留编辑昵称逻辑 return .none case .logoutTapped: // 清理所有认证信息,并向上层发送登出事件 return .run { send in await UserInfoManager.clearAllAuthenticationData() // 向上层Feature传递登出事件(需在MainFeature处理) // 这里直接返回.none,由MainFeature监听AppSettingFeature.Action.logoutTapped后处理 } case .loadUserInfo: state.isLoadingUserInfo = true state.userInfoError = nil return .run { send in do { if let userInfo = await UserInfoManager.getUserInfo() { await send(.userInfoResponse(.success(userInfo))) } else { await send(.userInfoResponse(.failure(APIError.custom("用户信息不存在")))) } } catch { let apiError: APIError if let error = error as? APIError { apiError = error } else { apiError = APIError.custom(error.localizedDescription) } await send(.userInfoResponse(.failure(apiError))) } } case let .userInfoResponse(.success(userInfo)): state.userInfo = userInfo state.nickname = userInfo.nick ?? "" state.avatarURL = userInfo.avatar state.isLoadingUserInfo = false return .none case let .userInfoResponse(.failure(error)): state.userInfoError = error.localizedDescription state.isLoadingUserInfo = false return .none case .personalInfoPermissionsTapped: state.showPrivacyPolicy = true return .none case .helpTapped: state.showUserAgreement = true return .none case .clearCacheTapped: // 预留清除缓存逻辑 return .none case .checkUpdatesTapped: // 预留检查更新逻辑 return .none case .aboutUsTapped: // 预留关于我们逻辑 return .none case .userAgreementDismissed: state.showUserAgreement = false return .none case .privacyPolicyDismissed: state.showPrivacyPolicy = false return .none } } } }