Files
e-party-iOS/yana/Views/DetailView.swift
edwinQQQ 3d00e459e3 feat: 更新文档和视图以支持iOS 17及优化用户体验
- 更新Yana项目文档,调整适用版本至iOS 17,确保与最新开发环境兼容。
- 在多个视图中重构代码,优化状态管理和视图逻辑,提升用户体验。
- 添加默认初始化器以简化状态管理,确保各个Feature的状态一致性。
- 更新视图组件,移除不必要的硬编码,增强代码可读性和维护性。
- 修复多个视图中的逻辑错误,确保功能正常运行。
2025-07-29 17:57:42 +08:00

229 lines
8.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
import ComposableArchitecture
struct DetailView: View {
@State var store: StoreOf<DetailFeature>
let onLikeSuccess: ((Int, Bool) -> Void)?
init(store: StoreOf<DetailFeature>, onLikeSuccess: ((Int, Bool) -> Void)? = nil) {
self.store = store
self.onLikeSuccess = onLikeSuccess
}
var body: some View {
ZStack {
//
Image("bg")
.resizable()
.aspectRatio(contentMode: .fill)
.ignoresSafeArea()
VStack(spacing: 0) {
//
WithPerceptionTracking {
CustomNavigationBar(
title: NSLocalizedString("detail.title", comment: "Detail page title"),
showDeleteButton: isCurrentUserDynamic,
isDeleteLoading: store.isDeleteLoading,
onBack: {
// onDismiss?() 使 dismiss()
},
onDelete: {
store.send(.deleteDynamic)
}
)
}
.padding(.top, 24)
//
ScrollView {
VStack(spacing: 0) {
// 使OptimizedDynamicCardView
WithPerceptionTracking {
OptimizedDynamicCardView(
moment: store.moment,
allMoments: [store.moment], //
currentIndex: 0,
onImageTap: { images, index in
store.send(.showImagePreview(images, index))
},
onLikeTap: { dynamicId, uid, likedUid, worldId in
store.send(.likeDynamic(dynamicId, uid, likedUid, worldId))
},
onCardTap: nil, //
isDetailMode: true, //
isLikeLoading: store.isLikeLoading
)
.padding(.horizontal, 16)
.padding(.top, 16)
.padding(.bottom, 116)
}
}
}
}
}
.navigationBarHidden(true)
.onAppear {
debugInfoSync("🔍 DetailView: onAppear - moment.uid: \(store.moment.uid)")
store.send(.onAppear)
}
.onChange(of: store.shouldDismiss) { shouldDismiss in
if shouldDismiss {
debugInfoSync("🔍 DetailView: shouldDismiss = true, 调用onDismiss")
// onDismiss?() 使 dismiss
}
}
.fullScreenCover(isPresented: Binding(
get: { store.showImagePreview },
set: { _ in store.send(.hideImagePreview) }
)) {
WithPerceptionTracking {
ImagePreviewPager(
images: store.selectedImages,
currentIndex: Binding(
get: { store.selectedImageIndex },
set: { newIndex in
store.send(.showImagePreview(store.selectedImages, newIndex))
}
),
onClose: {
store.send(.imagePreviewDismissed)
}
)
}
}
}
//
private var isCurrentUserDynamic: Bool {
// 使storeID
guard let currentUserId = store.currentUserId,
let currentUserIdInt = Int(currentUserId) else {
debugInfoSync("🔍 DetailView: 无法获取当前用户ID - currentUserId: \(store.currentUserId ?? "nil")")
return false
}
let isCurrentUser = store.moment.uid == currentUserIdInt
debugInfoSync("🔍 DetailView: 动态用户判断 - moment.uid: \(store.moment.uid), currentUserId: \(currentUserIdInt), isCurrentUser: \(isCurrentUser)")
return isCurrentUser
}
}
// MARK: - CustomNavigationBar
struct CustomNavigationBar: View {
let title: String
let showDeleteButton: Bool
let isDeleteLoading: Bool
let onBack: () -> Void
let onDelete: () -> Void
init(title: String, showDeleteButton: Bool, isDeleteLoading: Bool, onBack: @escaping () -> Void, onDelete: @escaping () -> Void) {
self.title = title
self.showDeleteButton = showDeleteButton
self.isDeleteLoading = isDeleteLoading
self.onBack = onBack
self.onDelete = onDelete
}
@SwiftUI.Environment(\.dismiss) private var dismiss: SwiftUI.DismissAction
var body: some View {
HStack {
//
Button(action: {
onBack()
dismiss() // 使 dismiss
}) {
Image(systemName: "chevron.left")
.font(.system(size: 20, weight: .medium))
.foregroundColor(.white)
.frame(width: 44, height: 44)
.background(Color.black.opacity(0.3))
.clipShape(Circle())
}
Spacer()
//
Text(title)
.font(.system(size: 18, weight: .semibold))
.foregroundColor(.white)
.shadow(color: .black.opacity(0.5), radius: 2, x: 0, y: 1)
Spacer()
//
WithPerceptionTracking {
if showDeleteButton {
Button(action: onDelete) {
if isDeleteLoading {
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: .white))
.scaleEffect(0.8)
.frame(width: 44, height: 44)
.background(Color.red.opacity(0.8))
.clipShape(Circle())
} else {
Image(systemName: "trash")
.font(.system(size: 16, weight: .medium))
.foregroundColor(.white)
.frame(width: 44, height: 44)
.background(Color.red.opacity(0.8))
.clipShape(Circle())
}
}
.disabled(isDeleteLoading)
} else {
//
Color.clear
.frame(width: 44, height: 44)
}
}
}
.padding(.horizontal, 16)
.padding(.top, 8)
.padding(.bottom, 12)
.background(
LinearGradient(
gradient: Gradient(colors: [
Color.black.opacity(0.4),
Color.black.opacity(0.2),
Color.clear
]),
startPoint: .top,
endPoint: .bottom
)
)
}
}
//#Preview {
// DetailView(
// store: Store(
// initialState: DetailFeature.State(
// moment: MomentsInfo(
// dynamicId: 1,
// uid: 123,
// nick: "Test User",
// avatar: "https://example.com/avatar.jpg",
// type: 1,
// content: "This is a test dynamic content",
// publishTime: Int(Date().timeIntervalSince1970 * 1000),
// likeCount: 10,
// isLike: false,
// worldId: 1,
// dynamicResList: [
// MomentsPicture(
// id: 1,
// resUrl: "https://example.com/image1.jpg",
// format: "jpg",
// width: 800,
// height: 600,
// resDuration: nil
// )
// ]
// )
// )
// ) {
// DetailFeature()
// }
// )
//}