feat: 重构用户动态功能,整合MeFeature并更新MainFeature

- 将MeDynamicFeature重命名为MeFeature,并在MainFeature中进行相应更新。
- 更新MainFeature的状态管理,整合用户动态相关逻辑。
- 新增MeFeature以管理用户信息和动态加载,提升代码结构清晰度。
- 更新MainView和MeView以适应新的MeFeature,优化用户体验。
- 删除冗余的MeDynamicView,简化视图结构,提升代码可维护性。
This commit is contained in:
edwinQQQ
2025-07-23 19:31:17 +08:00
parent 8b09653c4c
commit 772543243f
5 changed files with 218 additions and 312 deletions

View File

@@ -2,232 +2,106 @@ import SwiftUI
import ComposableArchitecture
struct MeView: View {
let meDynamicStore: StoreOf<MeDynamicFeature>
let accountModel: AccountModel
@State private var showLogoutConfirmation = false
@State private var showSetting = false
@State private var userInfo: UserInfo?
@State private var isLoadingUserInfo = true
@State private var errorMessage: String?
@State private var hasLoaded = false
let onLogout: () -> Void
let store: StoreOf<MeFeature>
var body: some View {
GeometryReader { geometry in
ZStack {
// - 使"bg"
Image("bg")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.clipped()
.ignoresSafeArea(.all)
VStack(spacing: 0) {
// -
UserProfileSection(
userInfo: userInfo,
isLoading: isLoadingUserInfo,
errorMessage: errorMessage,
onSettingTapped: { showSetting = true }
)
//
MeDynamicView(store: meDynamicStore)
WithViewStore(self.store, observe: { $0 }) { viewStore in
GeometryReader { geometry in
ZStack {
Image("bg")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
}
.onAppear {
if !hasLoaded {
loadUserInfo()
hasLoaded = true
}
}
.alert("确认退出", isPresented: $showLogoutConfirmation) {
Button("取消", role: .cancel) { }
Button("退出", role: .destructive) {
Task { await performLogout() }
}
} message: {
Text("确定要退出登录吗?")
}
.sheet(isPresented: $showSetting) {
SettingView(store: Store(initialState: SettingFeature.State()) { SettingFeature() })
}
}
// MARK: -
private func loadUserInfo() {
Task {
isLoadingUserInfo = true
errorMessage = nil
debugInfoSync("📱 MeView: 开始加载用户信息")
// ID
guard let currentUserId = await UserInfoManager.getCurrentUserId() else {
debugErrorSync("❌ MeView: 无法获取当前用户ID")
await MainActor.run {
errorMessage = "用户未登录"
isLoadingUserInfo = false
}
return
}
debugInfoSync("📱 MeView: 当前用户ID: \(currentUserId)")
// APIService
let apiService: APIServiceProtocol = LiveAPIService()
//
if let cachedUserInfo = await UserInfoManager.getUserInfo() {
debugInfoSync("📱 MeView: 使用本地缓存的用户信息")
await MainActor.run {
self.userInfo = cachedUserInfo
self.isLoadingUserInfo = false
}
}
//
debugInfoSync("🌐 MeView: 从服务器获取最新用户信息")
let freshUserInfo = await UserInfoManager.fetchUserInfoFromServer(
uid: currentUserId,
apiService: apiService
)
await MainActor.run {
if let freshUserInfo = freshUserInfo {
debugInfoSync("✅ MeView: 成功获取最新用户信息")
debugInfoSync(" 用户名: \(freshUserInfo.nick ?? freshUserInfo.nick ?? "未知")")
debugInfoSync(" 用户ID: \(String(freshUserInfo.uid ?? 0))")
self.userInfo = freshUserInfo
self.errorMessage = nil
} else {
debugErrorSync("❌ MeView: 无法从服务器获取用户信息")
if self.userInfo == nil {
self.errorMessage = "无法获取用户信息"
}
}
self.isLoadingUserInfo = false
}
}
}
// MARK: - 退
private func performLogout() async {
debugInfoSync("🔓 开始执行退出登录...")
await UserInfoManager.clearAllAuthenticationData()
onLogout()
debugInfoSync("✅ 退出登录完成")
}
// MARK: -
private func onSettingTapped() {
showSetting = true
}
// MARK: - ID
private func copyUserId() {
if let userId = userInfo?.userId {
UIPasteboard.general.string = userId
debugInfoSync("📋 MeView: 用户ID已复制到剪贴板: \(userId)")
}
}
}
// MARK: -
struct UserProfileSection: View {
let userInfo: UserInfo?
let isLoading: Bool
let errorMessage: String?
let onSettingTapped: () -> Void
var body: some View {
VStack(spacing: 16) {
//
HStack {
Spacer()
Button(action: onSettingTapped) {
Image(systemName: "gearshape")
.font(.system(size: 22, weight: .regular))
.foregroundColor(.white)
}
.padding(.trailing, 20)
}
//
if isLoading {
Circle()
.fill(Color.white.opacity(0.2))
.frame(width: 130, height: 130)
.overlay(
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: .white))
)
} else {
Circle()
.fill(Color.white.opacity(0.2))
.frame(width: 130, height: 130)
.overlay(
Group {
if let avatarUrl = userInfo?.avatar, !avatarUrl.isEmpty {
AsyncImage(url: URL(string: avatarUrl)) { image in
image
.resizable()
.aspectRatio(contentMode: .fill)
.clipShape(Circle())
} placeholder: {
Image(systemName: "person.fill")
.font(.system(size: 40))
.foregroundColor(.white)
.clipped()
.ignoresSafeArea(.all)
VStack(spacing: 0) {
//
if viewStore.isLoadingUserInfo {
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: .white))
.frame(height: 130)
} else if let error = viewStore.userInfoError {
Text(error)
.font(.system(size: 14))
.foregroundColor(.red)
.frame(height: 130)
} else if let userInfo = viewStore.userInfo {
VStack(spacing: 8) {
if let avatarUrl = userInfo.avatar, !avatarUrl.isEmpty {
AsyncImage(url: URL(string: avatarUrl)) { image in
image.resizable().aspectRatio(contentMode: .fill).clipShape(Circle())
} placeholder: {
Image(systemName: "person.fill").font(.system(size: 40)).foregroundColor(.white)
}
.frame(width: 90, height: 90)
} else {
Image(systemName: "person.fill").font(.system(size: 40)).foregroundColor(.white)
.frame(width: 90, height: 90)
}
} else {
Image(systemName: "person.fill")
.font(.system(size: 40))
Text(userInfo.nick ?? "用户昵称")
.font(.system(size: 18, weight: .medium))
.foregroundColor(.white)
Text("ID: \(userInfo.uid ?? 0)")
.font(.system(size: 14))
.foregroundColor(.white.opacity(0.7))
}
.frame(height: 130)
} else {
Spacer().frame(height: 130)
}
//
if viewStore.isLoadingMoments && viewStore.moments.isEmpty {
ProgressView("加载中...")
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else if let error = viewStore.momentsError {
VStack(spacing: 16) {
Image(systemName: "exclamationmark.triangle.fill")
.font(.system(size: 40))
.foregroundColor(.yellow)
Text(error)
.foregroundColor(.red)
Button("重试") {
viewStore.send(.onAppear)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else if viewStore.moments.isEmpty {
VStack(spacing: 16) {
Image(systemName: "tray")
.font(.system(size: 40))
.foregroundColor(.gray)
Text("暂无动态")
.foregroundColor(.white.opacity(0.8))
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else {
ScrollView {
LazyVStack(spacing: 12) {
ForEach(Array(viewStore.moments.enumerated()), id: \ .element.dynamicId) { index, moment in
OptimizedDynamicCardView(moment: moment, allMoments: viewStore.moments, currentIndex: index)
.padding(.horizontal, 12)
}
if viewStore.hasMore {
ProgressView()
.onAppear {
viewStore.send(.loadMore)
}
}
}
.padding(.top, 8)
}
.refreshable {
viewStore.send(.refresh)
}
}
)
}
//
if let errorMessage = errorMessage {
Text(errorMessage)
.font(.system(size: 14))
.foregroundColor(.red)
.multilineTextAlignment(.center)
} else {
Text(userInfo?.nick ?? userInfo?.nick ?? "用户昵称")
.font(.system(size: 18, weight: .medium))
.foregroundColor(.white)
}
// ID
HStack(spacing: 8) {
Text("ID: \(userInfo?.uid ?? 0)")
.font(.system(size: 14))
.foregroundColor(.white.opacity(0.7))
if userInfo?.userId != nil {
Button(action: {
// ID
if let userId = userInfo?.userId {
UIPasteboard.general.string = userId
}
}) {
Image(systemName: "doc.on.doc")
.font(.system(size: 12))
.foregroundColor(.white.opacity(0.6))
Spacer()
}
.frame(maxWidth: .infinity, alignment: .top)
}
}
.onAppear {
viewStore.send(.onAppear)
}
}
.padding(.horizontal, 20)
.padding(.bottom, 20)
}
}
//#Preview {
// MeView(onLogout: {})
//}