From 5378ea18e1ec1cc63f260a7955ca08bd9f36396f Mon Sep 17 00:00:00 2001 From: Soner YUKSEL Date: Mon, 4 Mar 2024 16:38:24 -0500 Subject: [PATCH] Merge pull request #22438 from brave/ios/bugfix/editsharemenu --- .../BVC+ShareActivity.swift | 71 ++++++++-------- .../Brave/Frontend/Share/MenuActivity.swift | 38 ++++++--- .../Frontend/Share/ShareExtensionHelper.swift | 81 ++++++++++++++++++- 3 files changed, 140 insertions(+), 50 deletions(-) diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ShareActivity.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ShareActivity.swift index 1958b0bd6658..a854cfdacfcf 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ShareActivity.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ShareActivity.swift @@ -23,8 +23,7 @@ extension BrowserViewController { activities.append( BasicMenuActivity( - title: Strings.copyCleanLink, - braveSystemImage: "leo.broom", + activityType: .copyCleanLink, callback: { UIPasteboard.general.url = cleanedURL } @@ -37,8 +36,7 @@ extension BrowserViewController { !url.isLocal, !InternalURL.isValid(url: url), !url.isReaderModeURL { activities.append( BasicMenuActivity( - title: Strings.OpenTabs.sendWebsiteShareActionTitle, - braveSystemImage: "leo.smartphone.laptop", + activityType: .sendURL, callback: { [weak self] in guard let self = self else { return } @@ -70,8 +68,7 @@ extension BrowserViewController { if tab.readerModeAvailableOrActive { activities.append( BasicMenuActivity( - title: Strings.toggleReaderMode, - braveSystemImage: "leo.product.speedreader", + activityType: .toggleReaderMode, callback: { [weak self] in self?.toggleReaderMode() } @@ -84,8 +81,7 @@ extension BrowserViewController { // Find In Page Activity activities.append( BasicMenuActivity( - title: Strings.findInPage, - braveSystemImage: "leo.search", + activityType: .findInPage, callback: { [weak self] in guard let self = self else { return } @@ -102,8 +98,7 @@ extension BrowserViewController { // Page Zoom Activity activities.append( BasicMenuActivity( - title: Strings.PageZoom.settingsTitle, - braveSystemImage: "leo.font.size", + activityType: .pageZoom, callback: { [weak self] in guard let self = self else { return } @@ -120,8 +115,7 @@ extension BrowserViewController { if !FavoritesHelper.isAlreadyAdded(url) { activities.append( BasicMenuActivity( - title: Strings.addToFavorites, - braveSystemImage: "leo.widget.generic", + activityType: .addFavourites, callback: { [weak self] in guard let self = self else { return } @@ -137,8 +131,7 @@ extension BrowserViewController { // Request Desktop Site Activity activities.append( BasicMenuActivity( - title: tab?.isDesktopSite == true ? Strings.appMenuViewMobileSiteTitleString : Strings.appMenuViewDesktopSiteTitleString, - braveSystemImage: tab?.isDesktopSite == true ? "leo.smartphone" : "leo.monitor", + activityType: tab?.isDesktopSite == true ? .requestMobileSite : .requestDesktopSite, callback: { tab?.switchUserAgent() } @@ -155,8 +148,7 @@ extension BrowserViewController { if !feeds.isEmpty { activities.append( BasicMenuActivity( - title: Strings.BraveNews.addSourceShareTitle, - braveSystemImage: "leo.rss", + activityType: .addSourceNews, callback: { [weak self] in guard let self = self else { return } let controller = BraveNewsAddSourceResultsViewController( @@ -179,8 +171,7 @@ extension BrowserViewController { if let webView = tab?.webView, tab?.temporaryDocument == nil { activities.append( BasicMenuActivity( - title: Strings.createPDF, - braveSystemImage: "leo.file", + activityType: .createPDF, callback: { webView.createPDF { [weak self] result in dispatchPrecondition(condition: .onQueue(.main)) @@ -230,8 +221,7 @@ extension BrowserViewController { if case .success(let feed) = parser.parse() { activities.append( BasicMenuActivity( - title: Strings.BraveNews.addSourceShareTitle, - braveSystemImage: "leo.rss", + activityType: .addSourceNews, callback: { [weak self] in guard let self = self else { return } let controller = BraveNewsAddSourceResultsViewController( @@ -254,12 +244,11 @@ extension BrowserViewController { // Add Search Engine Activity if let webView = tabManager.selectedTab?.webView, - evaluateWebsiteSupportOpenSearchEngine(webView) { - + evaluateWebsiteSupportOpenSearchEngine(webView) + { activities.append( BasicMenuActivity( - title: Strings.CustomSearchEngine.customEngineNavigationTitle, - braveSystemImage: "leo.search.zoom-in", + activityType: .addSearchEngine, callback: { [weak self] in self?.addCustomSearchEngineForFocusedElement() } @@ -274,26 +263,35 @@ extension BrowserViewController { { activities.append( BasicMenuActivity( - title: Strings.displayCertificate, - braveSystemImage: "leo.lock.plain") { [weak self] in - self?.displayPageCertificateInfo() - } + activityType: .displaySecurityCertificate + ) { [weak self] in + self?.displayPageCertificateInfo() + } ) } // Report Web-compat Issue Activity activities.append( BasicMenuActivity( - title: Strings.Shields.reportABrokenSite, - braveSystemImage: "leo.warning.triangle-outline") { [weak self] in - self?.showSubmitReportView(for: url) - } + activityType: .reportBrokenSite + ) { [weak self] in + self?.showSubmitReportView(for: url) + } ) return activities } - func presentActivityViewController(_ url: URL, tab: Tab? = nil, sourceView: UIView?, sourceRect: CGRect, arrowDirection: UIPopoverArrowDirection) { + + func presentActivityViewController( + _ url: URL, + tab: Tab? = nil, + sourceView: UIView?, + sourceRect: CGRect, + arrowDirection: UIPopoverArrowDirection + ) { + let shareExtesionHelper = ShareExtensionHelper(url: url, tab: tab) + let activities: [UIActivity] = makeShareActivities( for: url, tab: tab, @@ -302,10 +300,9 @@ extension BrowserViewController { arrowDirection: arrowDirection ) - let controller = ShareExtensionHelper.makeActivityViewController( - selectedURL: url, - selectedTab: tab, - applicationActivities: activities + let controller = shareExtesionHelper.createActivityViewController( + applicationActivities: + activities ) controller.completionWithItemsHandler = { [weak self] _, _, _, _ in diff --git a/ios/brave-ios/Sources/Brave/Frontend/Share/MenuActivity.swift b/ios/brave-ios/Sources/Brave/Frontend/Share/MenuActivity.swift index 1462e0700c5d..c4a023a5546a 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Share/MenuActivity.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Share/MenuActivity.swift @@ -4,8 +4,9 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. import Foundation -import UIKit +import Shared import SwiftUI +import UIKit protocol MenuActivity: UIActivity { /// The image to use when shown on the menu. @@ -14,29 +15,42 @@ protocol MenuActivity: UIActivity { /// A standard activity that will appear in the apps menu and executes a callback when the user selects it class BasicMenuActivity: UIActivity, MenuActivity { - private let title: String - private let braveSystemImage: String + struct ActivityType { + var id: String + var title: String + var braveSystemImage: String + } + + private var id: String + private var title: String + private var braveSystemImage: String private let callback: () -> Bool init( + id: String, title: String, braveSystemImage: String, callback: @escaping () -> Bool ) { + self.id = id self.title = title self.braveSystemImage = braveSystemImage self.callback = callback } convenience init( - title: String, - braveSystemImage: String, + activityType: ActivityType, callback: @escaping () -> Void ) { - self.init(title: title, braveSystemImage: braveSystemImage, callback: { - callback() - return true - }) + self.init( + id: activityType.id, + title: activityType.title, + braveSystemImage: activityType.braveSystemImage, + callback: { + callback() + return true + } + ) } // MARK: - UIActivity @@ -56,7 +70,11 @@ class BasicMenuActivity: UIActivity, MenuActivity { override func canPerform(withActivityItems activityItems: [Any]) -> Bool { return true } - + + override var activityType: UIActivity.ActivityType { + return UIActivity.ActivityType(rawValue: id) + } + // MARK: - MenuActivity var menuImage: Image { diff --git a/ios/brave-ios/Sources/Brave/Frontend/Share/ShareExtensionHelper.swift b/ios/brave-ios/Sources/Brave/Frontend/Share/ShareExtensionHelper.swift index 8f7f8938af54..eea1b67f0c38 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Share/ShareExtensionHelper.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Share/ShareExtensionHelper.swift @@ -8,6 +8,15 @@ import UIKit /// A helper class that aids in the creation of share sheets class ShareExtensionHelper { + /// URL and Tab to be shared + private let selectedURL: URL + private weak var selectedTab: Tab? + + init(url: URL, tab: Tab?) { + self.selectedURL = url + self.selectedTab = tab + } + /// Create a activity view controller with the given elements. /// - Parameters: /// - selectedURL: The url or url content to share. May include an internal file or a link @@ -15,9 +24,7 @@ class ShareExtensionHelper { /// - applicationActivities: The application activities to include in this share sheet. /// - completionHandler: This will be triggered once the share sheet is dismissed and can be used to cleanup any lingering data /// - Returns: An `UIActivityViewController` prepped and ready to present. - static func makeActivityViewController( - selectedURL: URL, - selectedTab: Tab? = nil, + func createActivityViewController( applicationActivities: [UIActivity] = [] ) -> UIActivityViewController { let printInfo = UIPrintInfo(dictionary: nil) @@ -52,3 +59,71 @@ class ShareExtensionHelper { return activityViewController } } + +extension BasicMenuActivity.ActivityType { + static let copyCleanLink: Self = .init( + id: "CopyCleanLink", + title: Strings.copyCleanLink, + braveSystemImage: "leo.broom" + ) + static let sendURL: Self = .init( + id: "SendURL", + title: Strings.OpenTabs.sendWebsiteShareActionTitle, + braveSystemImage: "leo.smartphone.laptop" + ) + static let toggleReaderMode: Self = .init( + id: "ToggleReaderMode", + title: Strings.toggleReaderMode, + braveSystemImage: "leo.product.speedreader" + ) + static let findInPage: Self = .init( + id: "FindInPage", + title: Strings.findInPage, + braveSystemImage: "leo.search" + ) + static let pageZoom: Self = .init( + id: "PageZoom", + title: Strings.PageZoom.settingsTitle, + braveSystemImage: "leo.font.size" + ) + static let addFavourites: Self = .init( + id: "CopyCleanLink", + title: Strings.addToFavorites, + braveSystemImage: "leo.widget.generic" + ) + static let requestMobileSite: Self = .init( + id: "RequestMobileSite", + title: Strings.appMenuViewMobileSiteTitleString, + braveSystemImage: "leo.smartphone" + ) + static let requestDesktopSite: Self = .init( + id: "RequestDesktopSite", + title: Strings.appMenuViewDesktopSiteTitleString, + braveSystemImage: "leo.monitor" + ) + static let addSourceNews: Self = .init( + id: "AddSourceNews", + title: Strings.BraveNews.addSourceShareTitle, + braveSystemImage: "leo.rss" + ) + static let createPDF: Self = .init( + id: "CreatePDF", + title: Strings.createPDF, + braveSystemImage: "leo.broom" + ) + static let addSearchEngine: Self = .init( + id: "AddSearchEngine", + title: Strings.CustomSearchEngine.customEngineNavigationTitle, + braveSystemImage: "leo.search.zoom-in" + ) + static let displaySecurityCertificate: Self = .init( + id: "DisplaySecurityCertificate", + title: Strings.displayCertificate, + braveSystemImage: "leo.lock.plain" + ) + static let reportBrokenSite: Self = .init( + id: "ReportBrokenSite", + title: Strings.Shields.reportABrokenSite, + braveSystemImage: "leo.warning.triangle-outline" + ) +}