
- 创建SettingPage视图,包含用户信息管理、头像设置、昵称编辑等功能。 - 实现SettingViewModel,处理设置页面的业务逻辑,包括头像上传、昵称更新等。 - 添加相机和相册选择功能,支持头像更换。 - 更新MainPage和MainViewModel,添加导航逻辑以支持设置页面的访问。 - 完善本地化支持,确保多语言兼容性。 - 新增相关测试建议,确保功能完整性和用户体验。
111 lines
4.2 KiB
Swift
111 lines
4.2 KiB
Swift
import SwiftUI
|
|
import Combine
|
|
|
|
// MARK: - MomentListHome ViewModel
|
|
|
|
@MainActor
|
|
class MomentListHomeViewModel: ObservableObject {
|
|
// MARK: - Published Properties
|
|
@Published var isLoading: Bool = false
|
|
@Published var error: String? = nil
|
|
@Published var moments: [MomentsInfo] = []
|
|
@Published var isLoaded: Bool = false
|
|
|
|
// MARK: - Private Properties
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
// MARK: - Public Methods
|
|
func onAppear() {
|
|
debugInfoSync("📱 MomentListHomeViewModel onAppear")
|
|
guard !isLoaded else {
|
|
debugInfoSync("✅ MomentListHomeViewModel: 数据已加载,跳过重复请求")
|
|
return
|
|
}
|
|
fetchLatestDynamics()
|
|
}
|
|
|
|
// MARK: - Private Methods
|
|
private func fetchLatestDynamics() {
|
|
isLoading = true
|
|
error = nil
|
|
debugInfoSync("🔄 MomentListHomeViewModel: 开始获取最新动态")
|
|
|
|
Task {
|
|
// 检查认证信息
|
|
let accountModel = await UserInfoManager.getAccountModel()
|
|
if accountModel?.uid != nil {
|
|
debugInfoSync("✅ MomentListHomeViewModel: 认证信息已准备好,开始获取动态")
|
|
await performAPICall()
|
|
} else {
|
|
debugInfoSync("⏳ MomentListHomeViewModel: 认证信息未准备好,等待...")
|
|
// 增加等待时间和重试次数
|
|
for attempt in 1...3 {
|
|
try? await Task.sleep(nanoseconds: 500_000_000) // 0.5秒
|
|
let retryAccountModel = await UserInfoManager.getAccountModel()
|
|
if retryAccountModel?.uid != nil {
|
|
debugInfoSync("✅ MomentListHomeViewModel: 第\(attempt)次重试成功,认证信息已保存,开始获取动态")
|
|
await performAPICall()
|
|
return
|
|
} else {
|
|
debugInfoSync("⏳ MomentListHomeViewModel: 第\(attempt)次重试,认证信息仍未准备好")
|
|
}
|
|
}
|
|
debugInfoSync("❌ MomentListHomeViewModel: 多次重试后认证信息仍未准备好")
|
|
await MainActor.run {
|
|
self.isLoading = false
|
|
self.error = "认证信息未准备好"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func performAPICall() async {
|
|
let apiService = LiveAPIService()
|
|
|
|
do {
|
|
let request = LatestDynamicsRequest(dynamicId: "", pageSize: 20, types: [.text, .picture])
|
|
debugInfoSync("📡 MomentListHomeViewModel: 发送请求: \(request.endpoint)")
|
|
debugInfoSync(" 参数: dynamicId=\(request.dynamicId), pageSize=\(request.pageSize)")
|
|
|
|
let response: MomentsLatestResponse = try await apiService.request(request)
|
|
|
|
await MainActor.run {
|
|
self.handleAPISuccess(response)
|
|
}
|
|
} catch {
|
|
await MainActor.run {
|
|
self.handleAPIError(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func handleAPISuccess(_ response: MomentsLatestResponse) {
|
|
isLoading = false
|
|
isLoaded = true
|
|
debugInfoSync("✅ MomentListHomeViewModel: API 请求成功")
|
|
debugInfoSync(" 响应码: \(response.code)")
|
|
debugInfoSync(" 消息: \(response.message)")
|
|
debugInfoSync(" 数据数量: \(response.data?.dynamicList.count ?? 0)")
|
|
|
|
if let list = response.data?.dynamicList {
|
|
moments = list
|
|
error = nil
|
|
debugInfoSync("✅ MomentListHomeViewModel: 数据加载成功")
|
|
debugInfoSync(" 动态数量: \(list.count)")
|
|
} else {
|
|
moments = []
|
|
error = response.message
|
|
debugErrorSync("❌ MomentListHomeViewModel: 数据为空")
|
|
debugErrorSync(" 错误消息: \(response.message)")
|
|
}
|
|
}
|
|
|
|
private func handleAPIError(_ error: Error) {
|
|
isLoading = false
|
|
moments = []
|
|
self.error = error.localizedDescription
|
|
debugErrorSync("❌ MomentListHomeViewModel: API 请求失败")
|
|
debugErrorSync(" 错误: \(error.localizedDescription)")
|
|
}
|
|
}
|