Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FXIOS-10139 [Menu] ⁃ Investigate Save as PDF option for Save Sub Menu #24150

Merged
merged 5 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions firefox-ios/Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
0A49784A2C53E63200B1E82A /* TrackingProtectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A4978492C53E63200B1E82A /* TrackingProtectionViewController.swift */; };
0A5E6F402CF88A7900C1DFC9 /* SwitchDetailedView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A5E6F3F2CF88A7900C1DFC9 /* SwitchDetailedView.swift */; };
0A686B3C2CDB70DC0090E146 /* MainMenuTelemetryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A686B3B2CDB70DC0090E146 /* MainMenuTelemetryTests.swift */; };
0A686B3E2CE253AB0090E146 /* PDFDocument+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A686B3D2CE253AB0090E146 /* PDFDocument+Extension.swift */; };
0A6875152C91886A00606F53 /* CertificatesHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A6875142C91886A00606F53 /* CertificatesHeaderView.swift */; };
0A7693612C7DD19600103A6D /* CertificatesViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A7693602C7DD19500103A6D /* CertificatesViewModelTests.swift */; };
0A7693692C81F2A300103A6D /* TrackingProtectionConnectionDetailsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A7693682C81F2A200103A6D /* TrackingProtectionConnectionDetailsView.swift */; };
Expand Down Expand Up @@ -2319,6 +2320,7 @@
0A5B4CE9B0996AE804491134 /* an */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = an; path = an.lproj/Shared.strings; sourceTree = "<group>"; };
0A5E6F3F2CF88A7900C1DFC9 /* SwitchDetailedView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwitchDetailedView.swift; sourceTree = "<group>"; };
0A686B3B2CDB70DC0090E146 /* MainMenuTelemetryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainMenuTelemetryTests.swift; sourceTree = "<group>"; };
0A686B3D2CE253AB0090E146 /* PDFDocument+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PDFDocument+Extension.swift"; sourceTree = "<group>"; };
0A6875142C91886A00606F53 /* CertificatesHeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CertificatesHeaderView.swift; sourceTree = "<group>"; };
0A734328A164466314ECE4BE /* ia */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ia; path = ia.lproj/Menu.strings; sourceTree = "<group>"; };
0A7693602C7DD19500103A6D /* CertificatesViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CertificatesViewModelTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -10552,6 +10554,7 @@
1BE7A48F2C636AC800460798 /* URLSession+sharedMPTCP.swift */,
1BE7A4912C636AEA00460798 /* URLSessionConfiguration+defaultMPTCP.swift */,
ED07C0E52CCACD7E006C0627 /* Locale+possibilitiesForLanguageIdentifier.swift */,
0A686B3D2CE253AB0090E146 /* PDFDocument+Extension.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -17080,6 +17083,7 @@
8CCCB08B2AE26B5C0073ADB9 /* ReportResponse.swift in Sources */,
21A43CDD291461C700B1206D /* ReaderModeFontTypeButton.swift in Sources */,
D3BE7B261B054D4400641031 /* main.swift in Sources */,
0A686B3E2CE253AB0090E146 /* PDFDocument+Extension.swift in Sources */,
810CD9C32BB3484F00E290C2 /* OnboardingMultipleChoiceCardViewController.swift in Sources */,
DFD104682B2341F900938418 /* ProductAdsCache.swift in Sources */,
C2D71B992A384F6A003DEC7A /* ThemedLeftAlignedTableViewCell.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ struct AccessibilityIdentifiers {
static let addToShortcuts = "MainMenu.AddToShortcuts"
static let bookmarkThisPage = "MainMenu.BookmarkThisPage"
static let share = "MainMenu.Share"
static let saveAsPDF = "MainMenu.SaveAsPDF"
static let reportBrokenSite = "MainMenu.ReportBrokenSite"
static let readerView = "MainMenu.ReaderViewOn"
static let nightMode = "MainMenu.NightModeOn"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Shared
import Storage
import Redux
import TabDataStore
import PDFKit

import enum MozillaAppServices.VisitType
import struct MozillaAppServices.CreditCard
Expand Down Expand Up @@ -598,6 +599,29 @@ class BrowserCoordinator: BaseCoordinator,
)
}

func presentSavePDFController() {
guard let webView = browserViewController.tabManager.selectedTab?.webView else { return }
webView.createPDF { [weak self] result in
switch result {
case .success(let data):
guard let pdf = PDFDocument(data: data),
let outputURL = pdf.createOutputURL(withFileName: webView.title ?? "") else {
return
}
pdf.write(to: outputURL)
if FileManager.default.fileExists(atPath: outputURL.path) {
let url = URL(fileURLWithPath: outputURL.path)
let controller = UIActivityViewController(activityItems: [url], applicationActivities: nil)
self?.present(controller)
}
case .failure(let error):
self?.logger.log("Failed to get a valid data URL result, with error: \(error.localizedDescription)",
level: .debug,
category: .webview)
}
}
}

private func makeMenuNavViewController() -> DismissableNavigationViewController? {
if let mainMenuCoordinator = childCoordinators.first(where: { $0 is MainMenuCoordinator }) as? MainMenuCoordinator {
mainMenuCoordinator.dismissMenuModal(animated: false)
Expand Down
17 changes: 17 additions & 0 deletions firefox-ios/Client/Extensions/PDFDocument+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/

import Foundation
import PDFKit

extension PDFDocument {
func createOutputURL(withFileName name: String) -> URL? {
try? FileManager.default.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
.appendingPathComponent(name)
.appendingPathExtension("pdf")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ struct MainMenuConfigurationUtility: Equatable, FeatureFlaggable {
description += ", \(Preview.SaveToReadingListSubtitle)"
}

description += ", \(Preview.SaveAsPDFSubtitle)"

return description
}

Expand Down Expand Up @@ -482,6 +484,7 @@ struct MainMenuConfigurationUtility: Equatable, FeatureFlaggable {
configureBookmarkItem(with: uuid, and: tabInfo),
configureShortcutsItem(with: uuid, and: tabInfo),
configureReadingListItem(with: uuid, and: tabInfo),
configureSaveAsPDFItem(with: uuid, and: tabInfo),
]
)]
}
Expand Down Expand Up @@ -586,6 +589,34 @@ struct MainMenuConfigurationUtility: Equatable, FeatureFlaggable {
)
}

private func configureSaveAsPDFItem(
with uuid: WindowUUID,
and tabInfo: MainMenuTabInfo
) -> MenuElement {
return MenuElement(
title: .MainMenu.Submenus.Save.SaveAsPDF,
iconName: Icons.saveAsPDF,
isEnabled: true,
isActive: false,
a11yLabel: .MainMenu.Submenus.Save.AccessibilityLabels.SaveAsPDF,
a11yHint: "",
a11yId: AccessibilityIdentifiers.MainMenu.saveAsPDF,
action: {
store.dispatch(
MainMenuAction(
windowUUID: uuid,
actionType: MainMenuActionType.tapNavigateToDestination,
navigationDestination: MenuNavigationDestination(
.saveAsPDF,
url: tabInfo.canonicalURL
),
telemetryInfo: TelemetryInfo(isHomepage: tabInfo.isHomepage)
)
)
}
)
}

// MARK: - Libraries Section
private func getLibrariesSection(with uuid: WindowUUID, tabInfo: MainMenuTabInfo) -> MenuSection {
return MenuSection(options: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ protocol MainMenuCoordinatorDelegate: AnyObject {
func showFindInPage()
func showSignInView(fxaParameters: FxASignInViewParameters?)
func updateZoomPageBarVisibility()
func presentSavePDFController()

/// Open the share sheet to share the currently selected `Tab`.
func showShareSheetForCurrentlySelectedTab()
Expand Down Expand Up @@ -114,6 +115,9 @@ class MainMenuCoordinator: BaseCoordinator, FeatureFlaggable {
case .shareSheet:
self.navigationHandler?.showShareSheetForCurrentlySelectedTab()

case .saveAsPDF:
self.navigationHandler?.presentSavePDFController()

case .zoom:
self.navigationHandler?.updateZoomPageBarVisibility()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final class MainMenuMiddleware {
static let tools = "tools"
static let save = "save"
static let share = "share"
static let saveAsPDF = "save_as_PDF"
static let switchToDesktopSite = "switch_to_desktop_site"
static let switchToMobileSite = "switch_to_mobile_site"
static let readerViewTurnOn = "reader_view_turn_on"
Expand Down Expand Up @@ -273,6 +274,9 @@ final class MainMenuMiddleware {
case .shareSheet:
telemetry.toolsSubmenuOptionTapped(with: isHomepage, and: TelemetryAction.share)

case .saveAsPDF:
telemetry.saveSubmenuOptionTapped(with: isHomepage, and: TelemetryAction.saveAsPDF)

case .syncSignIn:
telemetry.mainMenuOptionTapped(with: isHomepage, and: TelemetryAction.signInAccount)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum MainMenuNavigationDestination: Equatable, CaseIterable {
case settings
case syncSignIn
case shareSheet
case saveAsPDF
case zoom
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,54 +29,9 @@ struct DefaultBookmarksSaver: BookmarksSaver, BookmarksRefactorFeatureFlagProvid
let operation: Deferred<Maybe<GUID?>>? = {
switch bookmark.type {
case .bookmark:
guard let bookmark = bookmark as? BookmarkItemData else { return deferMaybe(nil) }

if bookmark.parentGUID == nil {
let position: UInt32? = parentFolderGUID == BookmarkRoots.MobileFolderGUID ? 0 : nil
return profile.places.createBookmark(parentGUID: parentFolderGUID,
url: bookmark.url,
title: bookmark.title,
position: position).bind { result in
return result.isFailure ? deferMaybe(BookmarkDetailPanelError())
: deferMaybe(result.successValue)
}
} else {
let position: UInt32? = parentFolderGUID == bookmark.parentGUID ? bookmark.position : nil
return profile.places.updateBookmarkNode(guid: bookmark.guid,
parentGUID: parentFolderGUID,
position: position,
title: bookmark.title,
url: bookmark.url).bind { result in
return result.isFailure ? deferMaybe(BookmarkDetailPanelError()) : deferMaybe(nil)
}
}

return saveBookmark(bookmark: bookmark, parentFolderGUID: parentFolderGUID)
case .folder:
guard let folder = bookmark as? BookmarkFolderData else { return deferMaybe(nil) }

if folder.parentGUID == nil {
TelemetryWrapper.recordEvent(category: .action,
method: .tap,
object: .bookmark,
value: .bookmarkAddFolder)

let position: UInt32? = parentFolderGUID == BookmarkRoots.MobileFolderGUID ? 0 : nil
return profile.places.createFolder(parentGUID: parentFolderGUID,
title: folder.title,
position: position).bind { result in
return result.isFailure ? deferMaybe(BookmarkDetailPanelError())
: deferMaybe(result.successValue)
}
} else {
let position: UInt32? = parentFolderGUID == folder.parentGUID ? folder.position : nil
return profile.places.updateBookmarkNode( guid: folder.guid,
parentGUID: parentFolderGUID,
position: position,
title: folder.title).bind { result in
return result.isFailure ? deferMaybe(BookmarkDetailPanelError()) : deferMaybe(nil)
}
}

return saveFolder(bookmark: bookmark, parentFolderGUID: parentFolderGUID)
default:
return nil
}
Expand Down