Skip to content

Commit

Permalink
Merge pull request #22438 from brave/ios/bugfix/editsharemenu
Browse files Browse the repository at this point in the history
  • Loading branch information
soner-yuksel committed Mar 4, 2024
1 parent ae8df45 commit 5378ea1
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ extension BrowserViewController {

activities.append(
BasicMenuActivity(
title: Strings.copyCleanLink,
braveSystemImage: "leo.broom",
activityType: .copyCleanLink,
callback: {
UIPasteboard.general.url = cleanedURL
}
Expand All @@ -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 }

Expand Down Expand Up @@ -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()
}
Expand All @@ -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 }

Expand All @@ -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 }

Expand All @@ -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 }

Expand All @@ -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()
}
Expand All @@ -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(
Expand All @@ -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))
Expand Down Expand Up @@ -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(
Expand All @@ -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()
}
Expand All @@ -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,
Expand All @@ -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
Expand Down
38 changes: 28 additions & 10 deletions ios/brave-ios/Sources/Brave/Frontend/Share/MenuActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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 {
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 @@ -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"
)
}

0 comments on commit 5378ea1

Please sign in to comment.