feat: 更新AppSettingFeature以改进用户信息加载逻辑

- 重构用户信息加载逻辑,采用Result类型处理成功与失败的响应,提升错误处理能力。
- 更新状态管理,确保用户信息加载状态与错误信息的准确反映。
- 移除冗余代码,简化用户信息获取流程,增强代码可读性。
This commit is contained in:
edwinQQQ
2025-07-24 14:03:51 +08:00
parent 71c40e465d
commit cb325724dc
2 changed files with 21 additions and 17 deletions

View File

@@ -23,8 +23,7 @@ struct AppSettingFeature {
//
case loadUserInfo
case userInfoLoaded(UserInfo?)
case userInfoLoadFailed(String)
case userInfoResponse(Result<UserInfo, APIError>)
// WebView
case personalInfoPermissionsTapped
@@ -58,27 +57,33 @@ struct AppSettingFeature {
state.isLoadingUserInfo = true
state.userInfoError = nil
return .run { send in
let currentUid = await UserInfoManager.getCurrentUserId()
if let userInfo = await UserInfoManager.fetchUserInfoFromServer(
uid: currentUid,
apiService: apiService
) {
await send(.userInfoLoaded(userInfo))
} else {
await send(.userInfoLoadFailed("获取用户信息失败"))
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 .userInfoLoaded(userInfo):
state.isLoadingUserInfo = false
case let .userInfoResponse(.success(userInfo)):
state.userInfo = userInfo
state.nickname = userInfo?.nick ?? "hahahaha"
state.avatarURL = userInfo?.avatar
state.nickname = userInfo.nick ?? ""
state.avatarURL = userInfo.avatar
state.isLoadingUserInfo = false
return .none
case let .userInfoLoadFailed(error):
case let .userInfoResponse(.failure(error)):
state.userInfoError = error.localizedDescription
state.isLoadingUserInfo = false
state.userInfoError = error
return .none
case .personalInfoPermissionsTapped:

View File

@@ -1,5 +1,4 @@
import Foundation
import ComposableArchitecture
import QCloudCOSXML
// MARK: - COS