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

[iOS] Share Sheet Edit is broken for custom items #22438

Merged
merged 5 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ extension BrowserViewController {

activities.append(
BasicMenuActivity(
title: Strings.copyCleanLink,
braveSystemImage: "leo.broom",
activityType: .copyCleanLink,
callback: {
UIPasteboard.general.url = cleanedURL
}
Expand All @@ -46,8 +45,7 @@ extension BrowserViewController {
{
activities.append(
BasicMenuActivity(
title: Strings.OpenTabs.sendWebsiteShareActionTitle,
braveSystemImage: "leo.smartphone.laptop",
activityType: .sendURL,
callback: { [weak self] in
guard let self = self else { return }

Expand Down Expand Up @@ -88,8 +86,7 @@ extension BrowserViewController {
if tab.readerModeAvailableOrActive {
activities.append(
BasicMenuActivity(
title: Strings.toggleReaderMode,
braveSystemImage: "leo.product.speedreader",
activityType: .toggleReaderMode,
callback: { [weak self] in
self?.toggleReaderMode()
}
Expand All @@ -102,8 +99,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 }

Expand All @@ -122,8 +118,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 }

Expand All @@ -140,8 +135,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 }

Expand All @@ -157,9 +151,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()
}
Expand All @@ -177,8 +169,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(
Expand All @@ -201,8 +192,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))
Expand Down Expand Up @@ -262,8 +252,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(
Expand All @@ -288,11 +277,9 @@ extension BrowserViewController {
if let webView = tabManager.selectedTab?.webView,
evaluateWebsiteSupportOpenSearchEngine(webView)
{

activities.append(
BasicMenuActivity(
title: Strings.CustomSearchEngine.customEngineNavigationTitle,
braveSystemImage: "leo.search.zoom-in",
activityType: .addSearchEngine,
callback: { [weak self] in
self?.addCustomSearchEngineForFocusedElement()
}
Expand All @@ -311,8 +298,7 @@ extension BrowserViewController {

activities.append(
BasicMenuActivity(
title: Strings.displayCertificate,
braveSystemImage: "leo.lock.plain"
activityType: .displaySecurityCertificate
) { [weak self] in
self?.displayPageCertificateInfo()
}
Expand All @@ -322,8 +308,7 @@ extension BrowserViewController {
// Report Web-compat Issue Activity
activities.append(
BasicMenuActivity(
title: Strings.Shields.reportABrokenSite,
braveSystemImage: "leo.warning.triangle-outline"
activityType: .reportBrokenSite
) { [weak self] in
self?.showSubmitReportView(for: url)
}
Expand All @@ -339,6 +324,8 @@ extension BrowserViewController {
sourceRect: CGRect,
arrowDirection: UIPopoverArrowDirection
) {
let shareExtesionHelper = ShareExtensionHelper(url: url, tab: tab)

let activities: [UIActivity] = makeShareActivities(
for: url,
tab: tab,
Expand All @@ -347,10 +334,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
Expand Down
26 changes: 20 additions & 6 deletions ios/brave-ios/Sources/Brave/Frontend/Share/MenuActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

import Foundation
import Shared
import SwiftUI
import UIKit

Expand All @@ -14,28 +15,37 @@ 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,
id: activityType.id,
title: activityType.title,
braveSystemImage: activityType.braveSystemImage,
callback: {
callback()
return true
Expand Down Expand Up @@ -63,6 +73,10 @@ class BasicMenuActivity: UIActivity, MenuActivity {
return true
}

override var activityType: UIActivity.ActivityType {
return UIActivity.ActivityType(rawValue: id)
}

// MARK: - MenuActivity

var menuImage: Image {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@ 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
/// - selectedTab: The provided tab is used for additional info such as a print renderer and title
/// - 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)
Expand Down Expand Up @@ -55,3 +62,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"
)
}
Loading