Skip to content

Commit

Permalink
Extending menu activity type and using static properties with conveni…
Browse files Browse the repository at this point in the history
…ence initializer
  • Loading branch information
soner-yuksel committed Mar 4, 2024
1 parent 0ab5806 commit 6e78617
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ extension BrowserViewController {

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

Expand Down Expand Up @@ -94,11 +86,7 @@ extension BrowserViewController {
if tab.readerModeAvailableOrActive {
activities.append(
BasicMenuActivity(
activityType: MenuActivityType(
title: Strings.toggleReaderMode,
braveSystemImage: "leo.product.speedreader",
activityID: .toggleReaderMode
),
activityType: .toggleReaderMode,
callback: { [weak self] in
self?.toggleReaderMode()
}
Expand All @@ -111,11 +99,7 @@ extension BrowserViewController {
// Find In Page Activity
activities.append(
BasicMenuActivity(
activityType: MenuActivityType(
title: Strings.findInPage,
braveSystemImage: "leo.search",
activityID: .findInPage
),
activityType: .findInPage,
callback: { [weak self] in
guard let self = self else { return }

Expand All @@ -134,11 +118,7 @@ extension BrowserViewController {
// Page Zoom Activity
activities.append(
BasicMenuActivity(
activityType: MenuActivityType(
title: Strings.PageZoom.settingsTitle,
braveSystemImage: "leo.font.size",
activityID: .pageZoom
),
activityType: .pageZoom,
callback: { [weak self] in
guard let self = self else { return }

Expand All @@ -155,11 +135,7 @@ extension BrowserViewController {
if !FavoritesHelper.isAlreadyAdded(url) {
activities.append(
BasicMenuActivity(
activityType: MenuActivityType(
title: Strings.addToFavorites,
braveSystemImage: "leo.widget.generic",
activityID: .addFavourites
),
activityType: .addFavourites,
callback: { [weak self] in
guard let self = self else { return }

Expand All @@ -173,16 +149,9 @@ extension BrowserViewController {
}

// Request Desktop Site Activity
let requestSiteActivityType = MenuActivityType(
title: tab?.isDesktopSite == true
? Strings.appMenuViewMobileSiteTitleString : Strings.appMenuViewDesktopSiteTitleString,
braveSystemImage: tab?.isDesktopSite == true ? "leo.smartphone" : "leo.monitor",
activityID: tab?.isDesktopSite == true ? .requestMobileSite : .requestDesktopSite
)

activities.append(
BasicMenuActivity(
activityType: requestSiteActivityType,
activityType: tab?.isDesktopSite == true ? .requestMobileSite : .requestDesktopSite,
callback: {
tab?.switchUserAgent()
}
Expand All @@ -200,11 +169,7 @@ extension BrowserViewController {
if !feeds.isEmpty {
activities.append(
BasicMenuActivity(
activityType: MenuActivityType(
title: Strings.BraveNews.addSourceShareTitle,
braveSystemImage: "leo.rss",
activityID: .addSourceNews
),
activityType: .addSourceNews,
callback: { [weak self] in
guard let self = self else { return }
let controller = BraveNewsAddSourceResultsViewController(
Expand All @@ -227,11 +192,7 @@ extension BrowserViewController {
if let webView = tab?.webView, tab?.temporaryDocument == nil {
activities.append(
BasicMenuActivity(
activityType: MenuActivityType(
title: Strings.createPDF,
braveSystemImage: "leo.file",
activityID: .createPDF
),
activityType: .createPDF,
callback: {
webView.createPDF { [weak self] result in
dispatchPrecondition(condition: .onQueue(.main))
Expand Down Expand Up @@ -291,11 +252,7 @@ extension BrowserViewController {
if case .success(let feed) = parser.parse() {
activities.append(
BasicMenuActivity(
activityType: MenuActivityType(
title: Strings.BraveNews.addSourceShareTitle,
braveSystemImage: "leo.rss",
activityID: .copyCleanLink
),
activityType: .addSourceNews,
callback: { [weak self] in
guard let self = self else { return }
let controller = BraveNewsAddSourceResultsViewController(
Expand All @@ -322,11 +279,7 @@ extension BrowserViewController {
{
activities.append(
BasicMenuActivity(
activityType: MenuActivityType(
title: Strings.CustomSearchEngine.customEngineNavigationTitle,
braveSystemImage: "leo.search.zoom-in",
activityID: .addSearchEngine
),
activityType: .addSearchEngine,
callback: { [weak self] in
self?.addCustomSearchEngineForFocusedElement()
}
Expand All @@ -345,11 +298,7 @@ extension BrowserViewController {

activities.append(
BasicMenuActivity(
activityType: MenuActivityType(
title: Strings.displayCertificate,
braveSystemImage: "leo.lock.plain",
activityID: .displaySecurityCertificate
)
activityType: .displaySecurityCertificate
) { [weak self] in
self?.displayPageCertificateInfo()
}
Expand All @@ -359,11 +308,7 @@ extension BrowserViewController {
// Report Web-compat Issue Activity
activities.append(
BasicMenuActivity(
activityType: MenuActivityType(
title: Strings.Shields.reportABrokenSite,
braveSystemImage: "leo.warning.triangle-outline",
activityID: .reportBrokenSite
)
activityType: .reportBrokenSite
) { [weak self] in
self?.showSubmitReportView(for: url)
}
Expand Down
58 changes: 38 additions & 20 deletions ios/brave-ios/Sources/Brave/Frontend/Share/MenuActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,73 @@ protocol MenuActivity: UIActivity {
var menuImage: Image { get }
}

struct MenuActivityType {
var title: String
var braveSystemImage: String
var activityID: UIActivity.ActivityType
}

/// A standard activity that will appear in the apps menu and executes a callback when the user selects it
class BasicMenuActivity: UIActivity, MenuActivity {
struct ActivityType {
var id: String
var title: String
var braveSystemImage: String
}

private let menuActivityType: MenuActivityType
private let callback: () -> Void
private var id: String
private var title: String
private var braveSystemImage: String
private let callback: () -> Bool

init(
activityType: MenuActivityType,
callback: @escaping () -> Void
id: String,
title: String,
braveSystemImage: String,
callback: @escaping () -> Bool
) {
self.menuActivityType = activityType
self.id = id
self.title = title
self.braveSystemImage = braveSystemImage
self.callback = callback
}

convenience init(
activityType: ActivityType,
callback: @escaping () -> Void
) {
self.init(
id: activityType.id,
title: activityType.title,
braveSystemImage: activityType.braveSystemImage,
callback: {
callback()
return true
}
)
}

// MARK: - UIActivity

override var activityTitle: String? {
return menuActivityType.title
return title
}

override var activityImage: UIImage? {
return UIImage(braveSystemNamed: menuActivityType.braveSystemImage)?
.applyingSymbolConfiguration(
.init(scale: .large)
)
return UIImage(braveSystemNamed: braveSystemImage)?.applyingSymbolConfiguration(
.init(scale: .large)
)
}

override func perform() {
callback()
activityDidFinish(true)
activityDidFinish(callback())
}

override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
return true
}

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

// MARK: - MenuActivity

var menuImage: Image {
Image(braveSystemName: menuActivityType.braveSystemImage)
Image(braveSystemName: braveSystemImage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,70 @@ class ShareExtensionHelper {
}
}

extension UIActivity.ActivityType {
static let copyCleanLink: Self = .init("CopyCleanLink")
static let sendURL: Self = .init("SendURL")
static let toggleReaderMode: Self = .init("ToggleReaderMode")
static let findInPage: Self = .init("FindInPage")
static let pageZoom: Self = .init("PageZoom")
static let addFavourites: Self = .init("AddFavourites")
static let requestMobileSite: Self = .init("RequestMobileSite")
static let requestDesktopSite: Self = .init("RequestDesktopSite")
static let addSourceNews: Self = .init("AddSourceNews")
static let createPDF: Self = .init("CreatePDF")
static let addSearchEngine: Self = .init("AddSearchEngine")
static let displaySecurityCertificate: Self = .init("DisplaySecurityCertificate")
static let reportBrokenSite: Self = .init("ReportBrokenSite")
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 6e78617

Please sign in to comment.