feat: 暂提

This commit is contained in:
jinyaqiu 2025-08-28 16:18:50 +08:00
parent 2cc7e5fb01
commit 45b8d211af

View File

@ -302,7 +302,7 @@ struct MainUploadArea: View {
.frame(height: 50) .frame(height: 50)
// //
if let mediaToDisplay = selectedMedia ?? uploadManager.selectedMedia.first { if let mediaToDisplay = selectedMedia ?? uploadManager.selectedMedia.first {
MediaPreview(media: mediaToDisplay, uploadManager: uploadManager) MediaPreview(media: mediaToDisplay)
.id(mediaToDisplay.id) .id(mediaToDisplay.id)
.frame(width: 225, height: 225) .frame(width: 225, height: 225)
.overlay( .overlay(
@ -374,15 +374,12 @@ struct MainUploadArea: View {
/// - Returns: /// - Returns:
private func mediaItemView(for media: MediaType, at index: Int) -> some View { private func mediaItemView(for media: MediaType, at index: Int) -> some View {
ZStack(alignment: .topTrailing) { ZStack(alignment: .topTrailing) {
VStack(spacing: 4) { // - 使
// MediaPreview(media: media)
MediaPreview(media: media, uploadManager: uploadManager)
.frame(width: 58, height: 58) .frame(width: 58, height: 58)
.cornerRadius(8) .cornerRadius(8)
.shadow(radius: 1) .shadow(radius: 1)
.overlay( .overlay(
//
ZStack(alignment: .topLeading) {
// //
ZStack(alignment: .topLeading) { ZStack(alignment: .topLeading) {
Path { path in Path { path in
@ -412,14 +409,15 @@ struct MainUploadArea: View {
path.closeSubpath() path.closeSubpath()
} }
.fill(Color(hex: "BEBEBE").opacity(0.6)) .fill(Color(hex: "BEBEBE").opacity(0.6))
.frame(width: 14, height: 10)
.overlay(
Text("\(index + 1)") Text("\(index + 1)")
.font(.system(size: 8, weight: .bold)) .font(.system(size: 8, weight: .bold))
.foregroundColor(.black) .foregroundColor(.black)
.frame(width: 14, height: 10) .frame(width: 14, height: 10)
.offset(y: -1) .offset(y: -1),
} alignment: .topLeading
.frame(width: 14, height: 10, alignment: .topLeading) )
.padding([.top, .leading], 2) .padding([.top, .leading], 2)
// //
@ -439,11 +437,12 @@ struct MainUploadArea: View {
.padding([.trailing, .bottom], 0) .padding([.trailing, .bottom], 0)
} }
}else{ }else{
//
VStack { VStack {
Spacer() Spacer()
HStack { HStack {
Spacer() Spacer()
Text("") Text("占位")
.font(.system(size: 8, weight: .bold)) .font(.system(size: 8, weight: .bold))
.foregroundColor(.black) .foregroundColor(.black)
.padding(.horizontal, 4) .padding(.horizontal, 4)
@ -455,39 +454,26 @@ struct MainUploadArea: View {
} }
.opacity(0) .opacity(0)
} }
} },
alignment: .topLeading
) )
// mediaItemView onTapGesture
// .onTapGesture {
// print("Tapped media at index: \(index)") //
// withAnimation {
// selectedIndices = [index]
// selectedMedia = media
// }
// }
// .contentShape(Rectangle()) //
// mediaItemView onTapGesture
.onTapGesture { .onTapGesture {
print("点击了媒体项,索引: \(index)") print("点击了媒体项,索引: \(index)")
withAnimation { withAnimation {
selectedMedia = media // selectedMedia = media
} }
} }
.contentShape(Rectangle()) // .contentShape(Rectangle())
}
// //
Button(action: { Button(action: {
// 使API
uploadManager.removeMedia(id: media.id) uploadManager.removeMedia(id: media.id)
//
if selectedMedia == media { if selectedMedia == media {
selectedMedia = nil selectedMedia = nil
} }
}) { }) {
Image(systemName: "xmark") Image(systemName: "xmark")
.font(.system(size: 10, weight: .bold)) .font(.system(size: 8, weight: .bold))
.foregroundColor(.black) .foregroundColor(.black)
.frame(width: 12, height: 12) .frame(width: 12, height: 12)
.background( .background(
@ -496,51 +482,12 @@ struct MainUploadArea: View {
.frame(width: 12, height: 12) .frame(width: 12, height: 12)
) )
} }
.offset(x: 6, y: -6) // .offset(x: 6, y: -6)
} }
.padding(.horizontal, 4) .padding(.horizontal, 4)
.contentShape(Rectangle()) .contentShape(Rectangle())
} }
///
/// - Parameter index:
/// - Returns:
@ViewBuilder
private func uploadStatusView(for index: Int) -> some View {
if let status = uploadManager.uploadStatus["\(index)"] {
switch status {
case .uploading(let progress):
//
VStack(alignment: .center, spacing: 2) {
Text("\(Int(progress * 100))%")
.font(.caption2)
.foregroundColor(.gray)
ProgressView(value: progress, total: 1.0)
.progressViewStyle(LinearProgressViewStyle())
.frame(height: 3)
.padding(.horizontal, 4)
.padding(.bottom, 2)
}
.frame(width: 60)
case .completed:
//
Image(systemName: "checkmark.circle.fill")
.font(.caption)
.foregroundColor(.green)
case .failed:
//
Image(systemName: "exclamationmark.triangle.fill")
.font(.caption)
.foregroundColor(.red)
default:
EmptyView()
}
}
}
/// ///
private var addMoreButton: some View { private var addMoreButton: some View {
Button(action: { showMediaPicker = true }) { Button(action: { showMediaPicker = true }) {
@ -599,102 +546,23 @@ struct UploadPromptView: View {
// MARK: - // MARK: -
/// ///
/// /// 使
struct MediaPreview: View { struct MediaPreview: View {
// MARK: - // MARK: -
///
private let imageProcessingQueue = DispatchQueue(
label: "com.yourapp.imageprocessing",
qos: .userInitiated,
attributes: .concurrent
)
/// ///
let media: MediaType let media: MediaType
///
@ObservedObject var uploadManager: MediaUploadManager
// MARK: -
///
@State private var image: UIImage?
///
enum LoadState {
///
case success(UIImage)
///
case failure(Error)
}
///
@State private var loadState: LoadState?
// MARK: -
///
private struct ImageCache {
static let shared = NSCache<NSString, UIImage>()
private static var cacheKeys = Set<String>()
static func setImage(_ image: UIImage, forKey key: String) {
shared.setObject(image, forKey: key as NSString)
cacheKeys.insert(key)
}
static func getImage(forKey key: String) -> UIImage? {
return shared.object(forKey: key as NSString)
}
static func clearCache() {
cacheKeys.forEach { key in
shared.removeObject(forKey: key as NSString)
}
cacheKeys.removeAll()
}
}
// MARK: -
init(media: MediaType, uploadManager: MediaUploadManager) {
self.media = media
self.uploadManager = uploadManager
//
let cacheKey = "\(media.id)" as NSString
if let cachedImage = ImageCache.shared.object(forKey: cacheKey) {
self._image = State(initialValue: cachedImage)
self._loadState = State(initialValue: .success(cachedImage))
}
}
// MARK: - // MARK: -
/// ///
private var uploadProgress: Double { private var displayImage: UIImage? {
guard let index = uploadManager.selectedMedia.firstIndex(where: { $0.id == media.id }) else { switch media {
return 0 case .image(let uiImage):
return uiImage
case .video(_, let thumbnail):
return thumbnail
} }
if case .uploading(let progress) = uploadManager.uploadStatus["\(index)"] {
return progress
} else if case .completed = uploadManager.uploadStatus["\(index)"] {
return 1.0
}
return 0
}
///
private var isUploading: Bool {
guard let index = uploadManager.selectedMedia.firstIndex(where: { $0.id == media.id }) else {
return false
}
if case .uploading = uploadManager.uploadStatus["\(index)"] {
return true
}
return false
} }
// MARK: - // MARK: -
@ -702,33 +570,14 @@ struct MediaPreview: View {
var body: some View { var body: some View {
ZStack { ZStack {
// 1. // 1.
if let image = image { if let image = displayImage {
ZStack(alignment: .bottomTrailing) { Image(uiImage: image)
loadedImageView(image) .resizable()
} .scaledToFill()
.transition(.opacity.animation(.easeInOut(duration: 0.2)))
// 100%
if isUploading && uploadProgress < 1.0 {
VStack {
Spacer()
ProgressView(value: uploadProgress, total: 1.0)
.progressViewStyle(LinearProgressViewStyle(tint: .white))
.frame(height: 2)
.padding(.horizontal, 4)
.padding(.bottom, 2)
}
}
} else { } else {
// 2. // 2.
placeholderView Color.gray.opacity(0.1)
.onAppear {
loadImage()
}
}
// 3.
if case .failure(let error) = loadState, image == nil {
errorView(error: error)
} }
} }
.aspectRatio(1, contentMode: .fill) .aspectRatio(1, contentMode: .fill)
@ -739,100 +588,6 @@ struct MediaPreview: View {
.stroke(Color.themePrimary.opacity(0.3), lineWidth: 1) .stroke(Color.themePrimary.opacity(0.3), lineWidth: 1)
) )
} }
//
private var placeholderView: some View {
Color.gray.opacity(0.1)
}
//
private func loadedImageView(_ image: UIImage) -> some View {
Image(uiImage: image)
.resizable()
.scaledToFill()
.transition(.opacity.animation(.easeInOut(duration: 0.2)))
}
///
private func errorView(error: Error) -> some View {
VStack(spacing: 8) {
Image(systemName: "exclamationmark.triangle")
.font(.system(size: 20))
.foregroundColor(.orange)
Text("加载失败")
.font(.caption2)
.foregroundColor(.secondary)
Button(action: {
loadState = nil
loadImage()
}) {
Image(systemName: "arrow.clockwise")
.font(.caption)
.padding(4)
.background(Color.gray.opacity(0.2))
.clipShape(Circle())
}
.padding(.top, 4)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.gray.opacity(0.1))
.cornerRadius(8)
}
// MARK: -
///
private func loadImage() {
let cacheKey = "\(media.id)" as NSString
//
if let cachedImage = ImageCache.getImage(forKey: cacheKey as String) {
image = cachedImage
loadState = .success(cachedImage)
return
}
// 使
imageProcessingQueue.async {
do {
let imageToCache: UIImage
switch media {
case .image(let uiImage):
imageToCache = uiImage
case .video(_, let thumbnail):
guard let thumbnail = thumbnail else {
throw NSError(
domain: "com.yourapp.media",
code: -1,
userInfo: [NSLocalizedDescriptionKey: "视频缩略图加载失败"]
)
}
imageToCache = thumbnail
}
//
ImageCache.setImage(imageToCache, forKey: cacheKey as String)
// UI
DispatchQueue.main.async {
withAnimation(.easeInOut(duration: 0.2)) {
image = imageToCache
loadState = .success(imageToCache)
}
}
} catch {
print("图片加载失败: \(error.localizedDescription)")
DispatchQueue.main.async {
loadState = .failure(error)
}
}
}
}
} }
private func getVideoDuration(url: URL) -> String { private func getVideoDuration(url: URL) -> String {