From 1541b73969846add1d8162eeada088a9de172245 Mon Sep 17 00:00:00 2001 From: kean Date: Fri, 26 Apr 2024 10:11:29 -0400 Subject: [PATCH] Add a context menu to the uploads screen --- .../SiteMediaPreviewViewController.swift | 13 +++++ .../Post/PostMediaUploadsView.swift | 51 ++++++++++++++----- .../Post/PostMediaUploadsViewModel.swift | 2 +- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/WordPress/Classes/ViewRelated/Media/SiteMedia/Controllers/SiteMediaPreviewViewController.swift b/WordPress/Classes/ViewRelated/Media/SiteMedia/Controllers/SiteMediaPreviewViewController.swift index 25cfa45103bc..77000d462092 100644 --- a/WordPress/Classes/ViewRelated/Media/SiteMedia/Controllers/SiteMediaPreviewViewController.swift +++ b/WordPress/Classes/ViewRelated/Media/SiteMedia/Controllers/SiteMediaPreviewViewController.swift @@ -1,6 +1,19 @@ import UIKit import Gifu import AVKit +import SwiftUI + +struct SiteMediaPreviewView: UIViewControllerRepresentable { + let media: Media + + func makeUIViewController(context: Context) -> SiteMediaPreviewViewController { + SiteMediaPreviewViewController(media: media) + } + + func updateUIViewController(_ vc: SiteMediaPreviewViewController, context: Context) { + // Do nothing + } +} final class SiteMediaPreviewViewController: UIViewController { private let imageView = GIFImageView() diff --git a/WordPress/Classes/ViewRelated/Post/PostMediaUploadsView.swift b/WordPress/Classes/ViewRelated/Post/PostMediaUploadsView.swift index 09b4e080b87b..a9e4f4a306e3 100644 --- a/WordPress/Classes/ViewRelated/Post/PostMediaUploadsView.swift +++ b/WordPress/Classes/ViewRelated/Post/PostMediaUploadsView.swift @@ -42,7 +42,7 @@ private struct PostMediaUploadItemView: View { @ObservedObject var viewModel: PostMediaUploadItemViewModel var body: some View { - HStack(alignment: .center, spacing: 0) { + let view = HStack(alignment: .center, spacing: 0) { MediaThubmnailImageView(image: viewModel.thumbnail) .aspectRatio(viewModel.thumbnailAspectRatio, contentMode: .fit) .frame(maxHeight: viewModel.thumbnailMaxHeight) @@ -67,26 +67,55 @@ private struct PostMediaUploadItemView: View { case .uploading: MediaUploadProgressView(progress: viewModel.fractionCompleted) .padding(.trailing, 4) // To align with the exlamation mark - makeMenu(for: viewModel) + menu case .failed: Image(systemName: "exclamationmark.circle.fill") .foregroundStyle(.red) - makeMenu(for: viewModel) + menu case .uploaded: Image(systemName: "checkmark.circle.fill") .foregroundStyle(.secondary.opacity(0.33)) } } } - .task { - await viewModel.loadThumbnail() + .task { + await viewModel.loadThumbnail() + } + if #available(iOS 16, *) { + view.contextMenu { + menuItems + } preview: { + SiteMediaPreviewView(media: viewModel.media) + } + } else { + view } } -} -@ViewBuilder -private func makeMenu(for viewModel: PostMediaUploadItemViewModel) -> some View { - Menu { + struct HostDetailsView: UIViewControllerRepresentable { + let media: Media + + func makeUIViewController(context: Context) -> MediaItemViewController { + MediaItemViewController(media: media) + } + + func updateUIViewController(_ uiViewController: MediaItemViewController, context: Context) { + // Do nothing + } + } + + private var menu: some View { + Menu { + menuItems + } label: { + Image(systemName: "ellipsis") + .font(.subheadline) + .tint(.secondary) + } + } + + @ViewBuilder + private var menuItems: some View { if viewModel.error != nil { Button(action: viewModel.buttonRetryTapped) { Label(Strings.retryUpload, systemImage: "arrow.clockwise") @@ -95,10 +124,6 @@ private func makeMenu(for viewModel: PostMediaUploadItemViewModel) -> some View Button(role: .destructive, action: viewModel.buttonCancelTapped) { Label(Strings.cancelUpload, systemImage: "trash") } - } label: { - Image(systemName: "ellipsis") - .font(.subheadline) - .tint(.secondary) } } diff --git a/WordPress/Classes/ViewRelated/Post/PostMediaUploadsViewModel.swift b/WordPress/Classes/ViewRelated/Post/PostMediaUploadsViewModel.swift index 49a9a95e8830..08daa79623ba 100644 --- a/WordPress/Classes/ViewRelated/Post/PostMediaUploadsViewModel.swift +++ b/WordPress/Classes/ViewRelated/Post/PostMediaUploadsViewModel.swift @@ -71,7 +71,7 @@ final class PostMediaUploadsViewModel: ObservableObject { final class PostMediaUploadItemViewModel: ObservableObject, Identifiable { @Published private(set) var state: State = .uploading - private let media: Media + let media: Media private let coordinator: MediaCoordinator private var completed: Int64 = 0