From d2c05f06085de999e00433d81cdb124799857257 Mon Sep 17 00:00:00 2001 From: kean Date: Wed, 8 Jan 2025 14:59:18 -0500 Subject: [PATCH] Remove unused media upload code from PostSettingsViewController --- ...omeSiteHeaderViewController+SiteIcon.swift | 2 +- .../Cells/PostFeaturedImageCell.swift | 27 --- .../SiteMediaAddMediaMenuController.swift | 2 +- ...gsViewController+FeaturedImageUpload.swift | 226 ------------------ .../Post/PostSettingsViewController.m | 63 +---- .../PostSettingsViewController_Internal.h | 8 - .../Views/PostSettingsFeaturedImageCell.swift | 3 + 7 files changed, 7 insertions(+), 324 deletions(-) delete mode 100644 WordPress/Classes/ViewRelated/Cells/PostFeaturedImageCell.swift delete mode 100644 WordPress/Classes/ViewRelated/Post/PostSettingsViewController+FeaturedImageUpload.swift diff --git a/WordPress/Classes/ViewRelated/Blog/My Site/Header/HomeSiteHeaderViewController+SiteIcon.swift b/WordPress/Classes/ViewRelated/Blog/My Site/Header/HomeSiteHeaderViewController+SiteIcon.swift index fa108934eb09..7e05c14d1013 100644 --- a/WordPress/Classes/ViewRelated/Blog/My Site/Header/HomeSiteHeaderViewController+SiteIcon.swift +++ b/WordPress/Classes/ViewRelated/Blog/My Site/Header/HomeSiteHeaderViewController+SiteIcon.swift @@ -28,7 +28,7 @@ extension HomeSiteHeaderViewController { mediaMenu.makeCameraAction(delegate: presenter), mediaMenu.makeImagePlaygroundAction(delegate: presenter), mediaMenu.makeSiteMediaAction(blog: blog, delegate: presenter) - ] + ].compactMap { $0 } if FeatureFlag.siteIconCreator.enabled { actions.append(UIAction( title: SiteIconAlertStrings.Actions.createWithEmoji, diff --git a/WordPress/Classes/ViewRelated/Cells/PostFeaturedImageCell.swift b/WordPress/Classes/ViewRelated/Cells/PostFeaturedImageCell.swift deleted file mode 100644 index 41cedf53d84f..000000000000 --- a/WordPress/Classes/ViewRelated/Cells/PostFeaturedImageCell.swift +++ /dev/null @@ -1,27 +0,0 @@ -import UIKit -import WordPressUI -import AsyncImageKit - -final class PostFeaturedImageCell: UITableViewCell { - let featuredImageView = AsyncImageView() - - override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { - super.init(style: style, reuseIdentifier: reuseIdentifier) - - featuredImageView.configuration.loadingStyle = .spinner - - contentView.addSubview(featuredImageView) - featuredImageView.pinEdges() - NSLayoutConstraint.activate([ - featuredImageView.heightAnchor.constraint(equalTo: featuredImageView.widthAnchor, multiplier: ReaderPostCell.coverAspectRatio) - ]) - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - @objc func setImage(withURL url: URL, post: AbstractPost) { - featuredImageView.setImage(with: url, host: MediaHost(post)) - } -} diff --git a/WordPress/Classes/ViewRelated/Media/SiteMedia/Controllers/SiteMediaAddMediaMenuController.swift b/WordPress/Classes/ViewRelated/Media/SiteMedia/Controllers/SiteMediaAddMediaMenuController.swift index ebe6a25659a7..66c0434f51cd 100644 --- a/WordPress/Classes/ViewRelated/Media/SiteMedia/Controllers/SiteMediaAddMediaMenuController.swift +++ b/WordPress/Classes/ViewRelated/Media/SiteMedia/Controllers/SiteMediaAddMediaMenuController.swift @@ -21,7 +21,7 @@ final class SiteMediaAddMediaMenuController: NSObject, PHPickerViewControllerDel menu.makeCameraAction(delegate: self), menu.makeImagePlaygroundAction(delegate: self), makeDocumentPickerAction(from: viewController) - ]) + ].compactMap { $0 }) ] let freeMediaActions: [UIAction] = [ menu.makeStockPhotos(blog: blog, delegate: self), diff --git a/WordPress/Classes/ViewRelated/Post/PostSettingsViewController+FeaturedImageUpload.swift b/WordPress/Classes/ViewRelated/Post/PostSettingsViewController+FeaturedImageUpload.swift deleted file mode 100644 index 1afa0a8f9193..000000000000 --- a/WordPress/Classes/ViewRelated/Post/PostSettingsViewController+FeaturedImageUpload.swift +++ /dev/null @@ -1,226 +0,0 @@ -import Foundation -import Photos -import PhotosUI -import WordPressFlux - -// MARK: - PostSettingsViewController (Featured Image Menu) - -extension PostSettingsViewController: PHPickerViewControllerDelegate, ImagePickerControllerDelegate { - @objc func makeSetFeaturedImageCell() -> UITableViewCell { - let cell = UITableViewCell(style: .default, reuseIdentifier: nil) - cell.selectionStyle = .none - - let button = UIButton() - var configuration = UIButton.Configuration.plain() - configuration.title = Strings.buttonSetFeaturedImage - configuration.baseForegroundColor = UIAppColor.primary - button.configuration = configuration - button.menu = makeSetFeaturedImageMenu() - button.showsMenuAsPrimaryAction = true - - cell.contentView.addSubview(button) - button.translatesAutoresizingMaskIntoConstraints = false - cell.contentView.pinSubviewToAllEdges(button) - - cell.accessibilityIdentifier = "SetFeaturedImage" - return cell - } - - private func makeSetFeaturedImageMenu() -> UIMenu { - let menu = MediaPickerMenu(viewController: self, filter: .images) - return UIMenu(children: [ - menu.makePhotosAction(delegate: self), - menu.makeCameraAction(delegate: self), - menu.makeImagePlaygroundAction(delegate: self), - menu.makeSiteMediaAction(blog: self.apost.blog, delegate: self) - ]) - } - - // MARK: PHPickerViewControllerDelegate - - public func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { - self.dismiss(animated: true) { - if let result = results.first { - self.setFeaturedImage(with: result.itemProvider) - } - } - } - - // MARK: ImagePickerControllerDelegate - - func imagePicker(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { - self.dismiss(animated: true) { - if let image = info[.originalImage] as? UIImage { - self.setFeaturedImage(with: image) - } - } - } -} - -extension PostSettingsViewController: SiteMediaPickerViewControllerDelegate { - func siteMediaPickerViewController(_ viewController: SiteMediaPickerViewController, didFinishWithSelection selection: [Media]) { - dismiss(animated: true) - - guard let media = selection.first else { return } - - WPAnalytics.track(.editorPostFeaturedImageChanged, properties: ["via": "settings", "action": "added"]) - setFeaturedImage(media: media) - reloadFeaturedImageCell() - } -} - -extension PostSettingsViewController: ImagePlaygroundPickerDelegate { - func imagePlaygroundViewController(_ viewController: UIViewController, didCreateImageAt imageURL: URL) { - dismiss(animated: true) - - if let data = try? Data(contentsOf: imageURL), let image = UIImage(data: data) { - setFeaturedImage(with: image) - } else { - wpAssertionFailure("failed to read the image created by ImagePlayground") - } - } -} - -// MARK: - PostSettingsViewController (Featured Image Upload) - -extension PostSettingsViewController { - func setFeaturedImage(with asset: ExportableAsset) { - guard let media = MediaCoordinator.shared.addMedia(from: asset, to: apost) else { - return - } - self.apost.featuredImage = media - self.setupObservingOf(media: media) - } - - @objc func setFeaturedImage(media: Media) { - apost.featuredImage = media - if !media.hasRemote { - MediaCoordinator.shared.retryMedia(media) - setupObservingOf(media: media) - } - - if let mediaIdentifier = apost.featuredImage?.mediaID { - featuredImageDelegate?.gutenbergDidRequestFeaturedImageId(mediaIdentifier) - } - } - - @objc func removeMediaObserver() { - if let receipt = mediaObserverReceipt { - MediaCoordinator.shared.removeObserver(withUUID: receipt) - mediaObserverReceipt = nil - } - } - - @objc func setupObservingOf(media: Media) { - removeMediaObserver() - isUploadingMedia = true - mediaObserverReceipt = MediaCoordinator.shared.addObserver({ [weak self](media, state) in - self?.mediaObserver(media: media, state: state) - }) - let progress = MediaCoordinator.shared.progress(for: media) - if let url = media.absoluteThumbnailLocalURL, let data = try? Data(contentsOf: url) { - progress?.setUserInfoObject(UIImage(data: data), forKey: .WPProgressImageThumbnailKey) - } - featuredImageProgress = progress - } - - func mediaObserver(media: Media, state: MediaCoordinator.MediaState) { - switch state { - case .processing: - featuredImageProgress?.localizedDescription = NSLocalizedString("Preparing...", comment: "Label to show while converting and/or resizing media to send to server") - case .thumbnailReady: - if let url = media.absoluteThumbnailLocalURL, let data = try? Data(contentsOf: url) { - featuredImageProgress?.setUserInfoObject(UIImage(data: data), forKey: .WPProgressImageThumbnailKey) - } - case .uploading(let progress): - featuredImageProgress = progress - featuredImageProgress?.kind = .file - featuredImageProgress?.setUserInfoObject(Progress.FileOperationKind.copying, forKey: ProgressUserInfoKey.fileOperationKindKey) - featuredImageProgress?.localizedDescription = NSLocalizedString("Uploading...", comment: "Label to show while uploading media to server") - progressCell?.setProgress(progress) - tableView.reloadData() - case .ended: - isUploadingMedia = false - tableView.reloadData() - case .failed(let error): - DDLogError("Couldn't upload the featured image: \(error.localizedDescription)") - isUploadingMedia = false - tableView.reloadData() - if error.domain == NSURLErrorDomain && error.code == NSURLErrorCancelled { - apost.featuredImage = nil - apost.removeMediaObject(media) - break - } - - let errorTitle = NSLocalizedString("Couldn't upload the featured image", comment: "The title for an alert that says to the user that the featured image he selected couldn't be uploaded.") - let notice = Notice(title: errorTitle, message: error.localizedDescription) - - ActionDispatcher.dispatch(NoticeAction.clearWithTag(MediaProgressCoordinatorNoticeViewModel.uploadErrorNoticeTag)) - // The Media coordinator shows its own notice about a failed upload. We have a better, more explanatory message for users here - // so we want to supress the one coming from the coordinator and show ours. - ActionDispatcher.dispatch(NoticeAction.post(notice)) - case .progress, .cancelled: - break - } - } - - @objc func showFeaturedImageRemoveOrRetryAction(atIndexPath indexPath: IndexPath) { - guard let media = apost.featuredImage else { - return - } - - let alertController = UIAlertController(title: FeaturedImageActionSheet.title, message: nil, preferredStyle: .actionSheet) - alertController.addActionWithTitle(FeaturedImageActionSheet.dismissActionTitle, - style: .cancel, - handler: nil) - - alertController.addActionWithTitle(FeaturedImageActionSheet.retryUploadActionTitle, - style: .default, - handler: { (action) in - self.setFeaturedImage(media: media) - }) - - alertController.addActionWithTitle(FeaturedImageActionSheet.removeActionTitle, - style: .destructive, - handler: { (action) in - self.apost.featuredImage = nil - self.apost.removeMediaObject(media) - }) - if let error = media.error { - alertController.message = error.localizedDescription - } - if let anchorView = self.tableView.cellForRow(at: indexPath) ?? self.view { - alertController.popoverPresentationController?.sourceView = anchorView - alertController.popoverPresentationController?.sourceRect = CGRect(origin: CGPoint(x: anchorView.bounds.midX, y: anchorView.bounds.midY), size: CGSize(width: 1, height: 1)) - alertController.popoverPresentationController?.permittedArrowDirections = .any - } - present(alertController, animated: true) - } - - struct FeaturedImageActionSheet { - static let title = NSLocalizedString( - "postSettings.featuredImageUploadActionSheet.title", - value: "Featured Image Options", - comment: "Title for action sheet with featured media options." - ) - static let dismissActionTitle = NSLocalizedString( - "postSettings.featuredImageUploadActionSheet.dismiss", - value: "Dismiss", - comment: "User action to dismiss featured media options." - ) - static let retryUploadActionTitle = NSLocalizedString( - "postSettings.featuredImageUploadActionSheet.retryUpload", - value: "Retry", - comment: "User action to retry featured media upload." - ) - static let removeActionTitle = NSLocalizedString( - "postSettings.featuredImageUploadActionSheet.remove", - value: "Remove", - comment: "User action to remove featured media." - ) - } -} - -private enum Strings { - static let buttonSetFeaturedImage = NSLocalizedString("postSettings.setFeaturedImageButton", value: "Set Featured Image", comment: "Button in Post Settings") -} diff --git a/WordPress/Classes/ViewRelated/Post/PostSettingsViewController.m b/WordPress/Classes/ViewRelated/Post/PostSettingsViewController.m index 24494ed5bc86..6845007efd77 100644 --- a/WordPress/Classes/ViewRelated/Post/PostSettingsViewController.m +++ b/WordPress/Classes/ViewRelated/Post/PostSettingsViewController.m @@ -26,9 +26,6 @@ typedef NS_ENUM(NSInteger, PostSettingsRow) { PostSettingsRowVisibility, PostSettingsRowFormat, PostSettingsRowFeaturedImage, - PostSettingsRowFeaturedImageAdd, - PostSettingsRowFeaturedImageRemove, - PostSettingsRowFeaturedLoading, PostSettingsRowShareConnection, PostSettingsRowShareMessage, PostSettingsRowSlug, @@ -39,8 +36,6 @@ typedef NS_ENUM(NSInteger, PostSettingsRow) { }; static NSString *const PostSettingsAnalyticsTrackingSource = @"post_settings"; -static NSString *const TableViewActivityCellIdentifier = @"TableViewActivityCellIdentifier"; -static NSString *const TableViewProgressCellIdentifier = @"TableViewProgressCellIdentifier"; static NSString *const TableViewFeaturedImageCellIdentifier = @"TableViewFeaturedImageCellIdentifier"; static NSString *const TableViewToggleCellIdentifier = @"TableViewToggleCellIdentifier"; static NSString *const TableViewGenericCellIdentifier = @"TableViewGenericCellIdentifier"; @@ -83,8 +78,6 @@ @implementation PostSettingsViewController - (void)dealloc { [self.internetReachability stopNotifier]; - - [self removeMediaObserver]; } - (instancetype)initWithPost:(AbstractPost *)aPost @@ -119,8 +112,6 @@ - (void)viewDidLoad [self setupFormatsList]; [self setupPublicizeConnections]; - [self.tableView registerNib:[UINib nibWithNibName:@"WPTableViewActivityCell" bundle:nil] forCellReuseIdentifier:TableViewActivityCellIdentifier]; - [self.tableView registerClass:[WPProgressTableViewCell class] forCellReuseIdentifier:TableViewProgressCellIdentifier]; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:TableViewFeaturedImageCellIdentifier]; [self.tableView registerClass:[SwitchTableViewCell class] forCellReuseIdentifier:TableViewToggleCellIdentifier]; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:TableViewGenericCellIdentifier]; @@ -131,7 +122,6 @@ - (void)viewDidLoad // Compensate for the first section's height of 1.0f self.tableView.contentInset = UIEdgeInsetsMake(-1.0f, 0, 0, 0); self.tableView.accessibilityIdentifier = @"SettingsTable"; - self.isUploadingMedia = NO; self.featuredImageViewModel.tableView = self.tableView; @@ -490,10 +480,6 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath [self showPostFormatSelector]; } else if (cell.tag == PostSettingsRowFeaturedImage) { [self showFeaturedImageSelector]; - } else if (cell.tag == PostSettingsRowFeaturedImageAdd) { - [self showFeaturedImageSelector]; - } else if (cell.tag == PostSettingsRowFeaturedImageRemove) { - [self showFeaturedImageRemoveOrRetryActionAtIndexPath:indexPath]; } else if (sec == PostSettingsSectionDisabledTwitter) { [self showShareDetailForIndexPath:indexPath]; } else if (cell.tag == PostSettingsRowShareConnection) { @@ -644,7 +630,7 @@ - (UITableViewCell *)makeFeaturedImageCellForIndexPath:(NSIndexPath *)indexPath cell.tag = PostSettingsRowFeaturedImage; return cell; - // TODO: remove unused code + // TODO: (kean) remove unused code // if (!self.apost.featuredImage && !self.isUploadingMedia) { // return [self cellForSetFeaturedImage]; // @@ -684,41 +670,7 @@ - (UITableViewCell *)configureStickyPostCellForIndexPath:(NSIndexPath *)indexPat return cell; } -- (UITableViewCell *)cellForSetFeaturedImage -{ - UITableViewCell *cell = [self makeSetFeaturedImageCell]; - cell.tag = PostSettingsRowFeaturedImageAdd; - return cell; -} - -- (UITableViewCell *)cellForFeaturedImageError -{ - WPTableViewActivityCell *activityCell = [self getWPTableViewActivityCell]; - activityCell.textLabel.text = NSLocalizedString(@"Upload failed. Tap for options.", @"Description to show on post setting for a featured image that failed to upload."); - activityCell.tag = PostSettingsRowFeaturedImageRemove; - return activityCell; -} - -- (UITableViewCell *)cellForFeaturedImageUploadProgressAtIndexPath:(NSIndexPath *)indexPath -{ - self.progressCell = [self.tableView dequeueReusableCellWithIdentifier:TableViewProgressCellIdentifier forIndexPath:indexPath]; - [WPStyleGuide configureTableViewCell:self.progressCell]; - [self.progressCell setProgress:self.featuredImageProgress]; - self.progressCell.tag = PostSettingsRowFeaturedLoading; - return self.progressCell; -} - -- (UITableViewCell *)cellForFeaturedImageWithURL:(nonnull NSURL *)featuredURL atIndexPath:(NSIndexPath *)indexPath -{ - // TODO: remove - return [UITableViewCell new]; - -// PostFeaturedImageCell *featuredImageCell = [self.tableView dequeueReusableCellWithIdentifier:TableViewFeaturedImageCellIdentifier forIndexPath:indexPath]; -// [featuredImageCell setImageWithURL:featuredURL post:self.apost]; -// featuredImageCell.tag = PostSettingsRowFeaturedImage; -// return featuredImageCell; -} - +// TODO: (kean) remove - (nullable NSURL *)urlForFeaturedImage { NSURL *featuredURL = self.apost.featuredImage.absoluteLocalURL; @@ -862,17 +814,6 @@ - (WPTableViewCell *)getWPTableViewImageAndAccessoryCell return cell; } -- (WPTableViewActivityCell *)getWPTableViewActivityCell -{ - WPTableViewActivityCell *cell = [self.tableView dequeueReusableCellWithIdentifier:TableViewActivityCellIdentifier]; - cell.accessoryType = UITableViewCellAccessoryNone; - cell.selectionStyle = UITableViewCellSelectionStyleBlue; - [WPStyleGuide configureTableViewActionCell:cell]; - - cell.tag = 0; - return cell; -} - - (void)showPostAuthorSelector { PostAuthorSelectorViewController *vc = [[PostAuthorSelectorViewController alloc] init:self.apost]; diff --git a/WordPress/Classes/ViewRelated/Post/PostSettingsViewController_Internal.h b/WordPress/Classes/ViewRelated/Post/PostSettingsViewController_Internal.h index d6ed3545b9cc..b801650a2bbe 100644 --- a/WordPress/Classes/ViewRelated/Post/PostSettingsViewController_Internal.h +++ b/WordPress/Classes/ViewRelated/Post/PostSettingsViewController_Internal.h @@ -20,12 +20,4 @@ typedef enum { @property (nonnull, nonatomic, strong) NSArray *sections; -@property (nullable, nonatomic, strong) NSProgress *featuredImageProgress; - -@property (nonatomic, assign) BOOL isUploadingMedia; - -@property (nullable, nonatomic, strong) NSUUID *mediaObserverReceipt; - -@property (nullable, nonatomic, strong) WPProgressTableViewCell *progressCell; - @end diff --git a/WordPress/Classes/ViewRelated/Post/Views/PostSettingsFeaturedImageCell.swift b/WordPress/Classes/ViewRelated/Post/Views/PostSettingsFeaturedImageCell.swift index 9af4481ecfa1..8fcbfc0f97f3 100644 --- a/WordPress/Classes/ViewRelated/Post/Views/PostSettingsFeaturedImageCell.swift +++ b/WordPress/Classes/ViewRelated/Post/Views/PostSettingsFeaturedImageCell.swift @@ -31,6 +31,7 @@ struct PostSettingsFeaturedImageCell: View { Label(Strings.buttonSetFeaturedImage, systemImage: "photo.badge.plus") .frame(maxWidth: .infinity) .contentShape(Rectangle()) // Make the whole cell tappable + .accessibilityIdentifier("SetFeaturedImage") } } } @@ -130,6 +131,7 @@ final class PostSettingsFeaturedImageViewModel: NSObject, ObservableObject { private func didProcessMedia(_ media: Media) { wpAssert(media.remoteURL != nil) + // TODO: (kean) fix animations UIView.performWithoutAnimation { upload = nil post.featuredImage = media @@ -145,6 +147,7 @@ final class PostSettingsFeaturedImageViewModel: NSObject, ObservableObject { func buttonRemoveTapped() { WPAnalytics.track(.editorPostFeaturedImageChanged, properties: ["via": "settings", "action": "removed"]) + // TODO: (kean) do we need to call removeMediaObject? post.featuredImage = nil tableView?.reloadData() }