diff --git a/WooCommerce/Classes/ViewRelated/Products/Edit Product/ProductFormViewController.swift b/WooCommerce/Classes/ViewRelated/Products/Edit Product/ProductFormViewController.swift index ea217926e8a..4b7ab91ee9a 100644 --- a/WooCommerce/Classes/ViewRelated/Products/Edit Product/ProductFormViewController.swift +++ b/WooCommerce/Classes/ViewRelated/Products/Edit Product/ProductFormViewController.swift @@ -229,6 +229,20 @@ final class ProductFormViewController: } } + if viewModel.canSaveAsDraft() || viewModel.productModel.status == .draft { + actionSheet.addDefaultActionWithTitle(ActionSheetStrings.previewProduct) { [weak self] _ in + if self?.viewModel.canSaveAsDraft() == true || self?.viewModel.hasUnsavedChanges() == true { + self?.saveProduct(status: .draft) { result in + if result.isSuccess { + self?.displayProductPreview() + } + } + } else { + self?.displayProductPreview() + } + } + } + /// The "View product in store" action will be shown only if the product is published. if viewModel.canViewProductInStore() { actionSheet.addDefaultActionWithTitle(ActionSheetStrings.viewProduct) { [weak self] _ in @@ -716,14 +730,14 @@ private extension ProductFormViewController { // MARK: Navigation actions // private extension ProductFormViewController { - func saveProduct(status: ProductStatus? = nil) { + func saveProduct(status: ProductStatus? = nil, onCompletion: @escaping (Result) -> Void = { _ in }) { let productStatus = status ?? product.status let messageType = viewModel.saveMessageType(for: productStatus) showSavingProgress(messageType) - saveProductRemotely(status: status) + saveProductRemotely(status: status, onCompletion: onCompletion) } - func saveProductRemotely(status: ProductStatus?) { + func saveProductRemotely(status: ProductStatus?, onCompletion: @escaping (Result) -> Void = { _ in }) { viewModel.saveProductRemotely(status: status) { [weak self] result in switch result { case .failure(let error): @@ -732,6 +746,7 @@ private extension ProductFormViewController { // Dismisses the in-progress UI then presents the error alert. self?.navigationController?.dismiss(animated: true) { self?.displayError(error: error) + onCompletion(.failure(error)) } case .success: // Dismisses the in-progress UI, then presents the confirmation alert. @@ -741,6 +756,7 @@ private extension ProductFormViewController { // Show linked products promo banner after product save (self?.viewModel as? ProductFormViewModel)?.isLinkedProductsPromoEnabled = true self?.reloadLinkedPromoCellAnimated() + onCompletion(.success(())) } } } @@ -778,6 +794,18 @@ private extension ProductFormViewController { SharingHelper.shareURL(url: url, title: product.name, from: view, in: self) } + func displayProductPreview() { + var permalink = URLComponents(string: product.permalink) + var updatedQueryItems = permalink?.queryItems ?? [] + updatedQueryItems.append(.init(name: "preview", value: "true")) + permalink?.queryItems = updatedQueryItems + guard let url = permalink?.url else { + return + } + + WebviewHelper.launch(url, with: self) + } + func duplicateProduct() { showSavingProgress(.duplicate) viewModel.duplicateProduct(onCompletion: { [weak self] result in @@ -1555,6 +1583,8 @@ private enum Localization { private enum ActionSheetStrings { static let saveProductAsDraft = NSLocalizedString("Save as draft", comment: "Button title to save a product as draft in Product More Options Action Sheet") + static let previewProduct = NSLocalizedString("Preview", + comment: "Button title to open preview link for a product in Product More Options Action Sheet") static let viewProduct = NSLocalizedString("View Product in Store", comment: "Button title View product in store in Edit Product More Options Action Sheet") static let share = NSLocalizedString("Share", comment: "Button title Share in Edit Product More Options Action Sheet")