From 541c8216ea78c09785569e61dfc6dd32e3b184d1 Mon Sep 17 00:00:00 2001 From: Daniel Dervishi <58835213+DanielDervishi@users.noreply.github.com> Date: Tue, 25 Jun 2024 09:15:02 -0400 Subject: [PATCH] Add FXIOS-9309 - Download PDF Main Menu (#20754) Co-authored-by: Daniel Dervishi --- ...serViewController+TabToolbarDelegate.swift | 15 ++++++++++++- .../Browser/MainMenuActionHelper.swift | 22 +++++++++++++++++++ firefox-ios/Client/Frontend/Strings.swift | 10 +++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/firefox-ios/Client/Frontend/Browser/BrowserViewController/Extensions/BrowserViewController+TabToolbarDelegate.swift b/firefox-ios/Client/Frontend/Browser/BrowserViewController/Extensions/BrowserViewController+TabToolbarDelegate.swift index f81124edfdc7..98edf5dc88da 100644 --- a/firefox-ios/Client/Frontend/Browser/BrowserViewController/Extensions/BrowserViewController+TabToolbarDelegate.swift +++ b/firefox-ios/Client/Frontend/Browser/BrowserViewController/Extensions/BrowserViewController+TabToolbarDelegate.swift @@ -283,7 +283,7 @@ extension BrowserViewController: TabToolbarDelegate, PhotonActionSheetProtocol { } // MARK: - ToolbarActionMenuDelegate -extension BrowserViewController: ToolBarActionMenuDelegate { +extension BrowserViewController: ToolBarActionMenuDelegate, UIDocumentPickerDelegate { func updateToolbarState() { updateToolbarStateForTraitCollection(view.traitCollection) } @@ -361,4 +361,17 @@ extension BrowserViewController: ToolBarActionMenuDelegate { flowType: fxaParameters.flowType, referringPage: fxaParameters.referringPage) } + + func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { + if !urls.isEmpty { + showToast(message: .AppMenu.AppMenuDownloadPDFConfirmMessage, toastAction: .downloadPDF) + } + } + + func showFilePicker(fileURL: URL) { + let documentPicker = UIDocumentPickerViewController(forExporting: [fileURL], asCopy: true) + documentPicker.delegate = self + documentPicker.modalPresentationStyle = .formSheet + showViewController(viewController: documentPicker) + } } diff --git a/firefox-ios/Client/Frontend/Browser/MainMenuActionHelper.swift b/firefox-ios/Client/Frontend/Browser/MainMenuActionHelper.swift index 92111128d470..b336293a8134 100644 --- a/firefox-ios/Client/Frontend/Browser/MainMenuActionHelper.swift +++ b/firefox-ios/Client/Frontend/Browser/MainMenuActionHelper.swift @@ -26,6 +26,7 @@ protocol ToolBarActionMenuDelegate: AnyObject { func showZoomPage(tab: Tab) func showCreditCardSettings() func showSignInView(fxaParameters: FxASignInViewParameters) + func showFilePicker(fileURL: URL) } extension ToolBarActionMenuDelegate { @@ -44,6 +45,7 @@ enum MenuButtonToastAction { case pinPage case removePinPage case closeTab + case downloadPDF } /// MainMenuActionHelper handles the main menu (hamburger menu) in the toolbar. @@ -269,6 +271,13 @@ class MainMenuActionHelper: PhotonActionSheetProtocol, let sendToDeviceAction = getSendToDevice() append(to: §ion, action: sendToDeviceAction) + if let tab = self.selectedTab, + let url = tab.canonicalURL?.displayURL, + url.lastPathComponent.suffix(4) == ".pdf"{ + let downloadPDFAction = getDownloadPDFAction() + append(to: §ion, action: downloadPDFAction) + } + let shareAction = getShareAction() append(to: §ion, action: shareAction) } @@ -603,6 +612,19 @@ class MainMenuActionHelper: PhotonActionSheetProtocol, }.items } + private func getDownloadPDFAction() -> PhotonRowActions { + return SingleActionViewModel(title: .AppMenu.AppMenuDownloadPDF, + iconString: StandardImageIdentifiers.Large.folder) { _ in + guard let tab = self.selectedTab, let temporaryDocument = tab.temporaryDocument else { return } + temporaryDocument.getURL { fileURL in + DispatchQueue.main.async { + guard let fileURL = fileURL else {return} + self.delegate?.showFilePicker(fileURL: fileURL) + } + } + }.items + } + // Main menu option Share page with when opening a file private func share(fileURL: URL, buttonView: UIView) { TelemetryWrapper.recordEvent(category: .action, method: .tap, object: .sharePageWith) diff --git a/firefox-ios/Client/Frontend/Strings.swift b/firefox-ios/Client/Frontend/Strings.swift index 6957fd49186d..c4de05e7057b 100644 --- a/firefox-ios/Client/Frontend/Strings.swift +++ b/firefox-ios/Client/Frontend/Strings.swift @@ -3776,6 +3776,16 @@ extension String { tableName: nil, value: "Downloads", comment: "Label for the button, displayed in the menu, takes you to Downloads screen when pressed.") + public static let AppMenuDownloadPDF = MZLocalizedString( + key: "Menu.DownloadPDF.Label.v129", + tableName: "Menu", + value: "Download PDF", + comment: "Label for the button, displayed in the menu, downloads a pdf when pressed.") + public static let AppMenuDownloadPDFConfirmMessage = MZLocalizedString( + key: "Menu.DownloadPDF.Confirm.v129", + tableName: "Menu", + value: "Successfully Downloaded PDF", + comment: "Toast displayed to user after downlaod pdf was pressed.") public static let AppMenuPasswords = MZLocalizedString( key: "Menu.Passwords.Label", tableName: nil,