Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #8333: Add Open Browser History and Open Tabs from Other Devices …
Browse files Browse the repository at this point in the history
…Activity Based Shortcuts (#8387)
  • Loading branch information
soner-yuksel authored Nov 10, 2023
1 parent 3dff11e commit 3f7ce0f
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 21 deletions.
19 changes: 19 additions & 0 deletions App/iOS/Delegates/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,20 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
type: .newPrivateTab, using: browserViewController)
}

return
case ActivityType.openHistoryList.identifier:
if let browserViewController = scene.browserViewController {
ActivityShortcutManager.shared.performShortcutActivity(
type: .openHistoryList, using: browserViewController)
}

return
case ActivityType.openBookmarks.identifier:
if let browserViewController = scene.browserViewController {
ActivityShortcutManager.shared.performShortcutActivity(
type: .openBookmarks, using: browserViewController)
}

return
case ActivityType.clearBrowsingHistory.identifier:
if let browserViewController = scene.browserViewController {
Expand Down Expand Up @@ -319,6 +333,11 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
type: .openPlayList, using: browserViewController)
}

case ActivityType.openSyncedTabs.identifier:
if let browserViewController = scene.browserViewController {
ActivityShortcutManager.shared.performShortcutActivity(
type: .openSyncedTabs, using: browserViewController)
}
return
default:
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Playlist

extension BrowserViewController: TopToolbarDelegate {

func showTabTray() {
func showTabTray(isExternallyPresented: Bool = false) {
if tabManager.tabsForCurrentMode.isEmpty {
return
}
Expand All @@ -42,6 +42,7 @@ extension BrowserViewController: TopToolbarDelegate {
isTabTrayActive = true

let tabTrayController = TabTrayController(
isExternallyPresented: isExternallyPresented,
tabManager: tabManager,
braveCore: braveCore,
windowProtection: windowProtection).then {
Expand All @@ -51,10 +52,12 @@ extension BrowserViewController: TopToolbarDelegate {
let container = UINavigationController(rootViewController: tabTrayController)

if !UIAccessibility.isReduceMotionEnabled {
container.transitioningDelegate = tabTrayController
if !isExternallyPresented {
container.transitioningDelegate = tabTrayController
}
container.modalPresentationStyle = .fullScreen
}
present(container, animated: true)
present(container, animated: !isExternallyPresented)
}

func topToolbarDidPressLockImageView(_ urlBar: TopToolbarView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ class BrowserNavigationHelper {
FileManager.default.openBraveDownloadsFolder(completion)
}

func openHistory() {
func openHistory(isModal: Bool = false) {
guard let bvc = bvc else { return }
let vc = HistoryViewController(
isPrivateBrowsing: bvc.privateBrowsingManager.isPrivateBrowsing,
isModallyPresented: isModal,
historyAPI: bvc.braveCore.historyAPI,
tabManager: bvc.tabManager)
vc.toolbarUrlActionsDelegate = bvc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class TabTrayController: AuthenticationController {
var isTabTrayBeingSearched = false
var tabTraySearchQuery: String?
var tabTrayMode: TabTrayMode = .local
private var isExternallyPresented: Bool // The tab tray is presented by an action outside the application like shortcuts
private var privateModeCancellable: AnyCancellable?
private var initialScrollCompleted = false
private var localAuthObservers = Set<AnyCancellable>()
Expand Down Expand Up @@ -180,7 +181,8 @@ class TabTrayController: AuthenticationController {

// MARK: Lifecycle

init(tabManager: TabManager, braveCore: BraveCoreMain, windowProtection: WindowProtection?) {
init(isExternallyPresented: Bool = false, tabManager: TabManager, braveCore: BraveCoreMain, windowProtection: WindowProtection?) {
self.isExternallyPresented = isExternallyPresented
self.tabManager = tabManager
self.braveCore = braveCore

Expand Down Expand Up @@ -319,6 +321,16 @@ class TabTrayController: AuthenticationController {
becomeFirstResponder()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

// Navigate tabs from other devices
if isExternallyPresented {
tabTypeSelector.selectedSegmentIndex = 1
tabTypeSelector.sendActions(for: UIControl.Event.valueChanged)
}
}

override func loadView() {
createTypeSelectorItems()
layoutTabTray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ import UniformTypeIdentifiers

class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtocol {

private var bookmarksFRC: BookmarksV2FetchResultsController?
private let bookmarkManager: BookmarkManager
/// Called when the bookmarks are updated via some user input (i.e. Delete, edit, etc.)
private var bookmarksDidChange: (() -> Void)?
weak var toolbarUrlActionsDelegate: ToolbarUrlActionsDelegate?
private weak var addBookmarksFolderOkAction: UIAlertAction?

private lazy var editBookmarksButton: UIBarButtonItem? = UIBarButtonItem().then {
$0.image = UIImage(braveSystemNamed: "leo.edit.pencil")
Expand Down Expand Up @@ -59,15 +56,16 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
return items
}

private weak var addBookmarksFolderOkAction: UIAlertAction?

private var isEditingIndividualBookmark = false

private var bookmarksFRC: BookmarksV2FetchResultsController?
private let bookmarkManager: BookmarkManager
/// Called when the bookmarks are updated via some user input (i.e. Delete, edit, etc.)
private var bookmarksDidChange: (() -> Void)?

private var currentFolder: Bookmarkv2?

/// Certain bookmark actions are different in private browsing mode.
private let isPrivateBrowsing: Bool

private var isEditingIndividualBookmark = false
private var isAtBookmarkRootLevel: Bool {
return self.currentFolder == nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,20 @@ class HistoryViewController: SiteTableViewController, ToolbarUrlActionsProtocol

private let historyAPI: BraveHistoryAPI
private let tabManager: TabManager
private var historyFRC: HistoryV2FetchResultsController?

var historyFRC: HistoryV2FetchResultsController?

/// Certain bookmark actions are different in private browsing mode.
let isPrivateBrowsing: Bool

var isHistoryRefreshing = false
private let isPrivateBrowsing: Bool /// Certain bookmark actions are different in private browsing mode.
private let isModallyPresented: Bool
private var isHistoryRefreshing = false

private var searchHistoryTimer: Timer?
private var isHistoryBeingSearched = false
private let searchController = UISearchController(searchResultsController: nil)
private var searchQuery = ""

init(isPrivateBrowsing: Bool, historyAPI: BraveHistoryAPI, tabManager: TabManager) {
init(isPrivateBrowsing: Bool, isModallyPresented: Bool = false, historyAPI: BraveHistoryAPI, tabManager: TabManager) {
self.isPrivateBrowsing = isPrivateBrowsing
self.isModallyPresented = isModallyPresented
self.historyAPI = historyAPI
self.tabManager = tabManager
super.init(nibName: nil, bundle: nil)
Expand Down Expand Up @@ -72,6 +71,10 @@ class HistoryViewController: SiteTableViewController, ToolbarUrlActionsProtocol
$0.rightBarButtonItem =
UIBarButtonItem(image: UIImage(braveSystemNamed: "leo.trash")!.template, style: .done, target: self, action: #selector(performDeleteAll))
}

if isModallyPresented {
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(performDone))
}
}

definesPresentationContext = true
Expand Down Expand Up @@ -178,6 +181,8 @@ class HistoryViewController: SiteTableViewController, ToolbarUrlActionsProtocol
searchHistoryTimer = nil
}
}

// MARK: Actions

@objc private func performDeleteAll() {
let style: UIAlertController.Style = UIDevice.current.userInterfaceIdiom == .pad ? .alert : .actionSheet
Expand Down Expand Up @@ -212,6 +217,12 @@ class HistoryViewController: SiteTableViewController, ToolbarUrlActionsProtocol

present(alert, animated: true, completion: nil)
}

@objc private func performDone() {
dismiss(animated: true)
}

// MARK: UITableViewDelegate - UITableViewDataSource

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAt: indexPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ class ShortcutSettingsViewController: TableViewController {
],
footer: .title(Strings.Shortcuts.shortcutSettingsOpenNewPrivateTabDescription))
)

dataSource.sections.append(
Section(
rows: [
Row(
text: Strings.Shortcuts.shortcutSettingsOpenBookmarksTitle,
selection: { [unowned self] in
manageShortcutActivity(for: .openBookmarks)
}, accessory: .disclosureIndicator, cellClass: MultilineValue1Cell.self)
],
footer: .title(Strings.Shortcuts.shortcutSettingsOpenBookmarksDescription))
)

dataSource.sections.append(
Section(
rows: [
Row(
text: Strings.Shortcuts.shortcutSettingsOpenHistoryListTitle,
selection: { [unowned self] in
manageShortcutActivity(for: .openHistoryList)
}, accessory: .disclosureIndicator, cellClass: MultilineValue1Cell.self)
],
footer: .title(Strings.Shortcuts.shortcutSettingsOpenHistoryListDescription))
)

dataSource.sections.append(
Section(
Expand Down Expand Up @@ -102,6 +126,18 @@ class ShortcutSettingsViewController: TableViewController {
footer: .title(Strings.Shortcuts.shortcutSettingsOpenPlaylistDescription))
)

dataSource.sections.append(
Section(
rows: [
Row(
text: Strings.Shortcuts.shortcutSettingsOpenSyncedTabsTitle,
selection: { [unowned self] in
manageShortcutActivity(for: .openSyncedTabs)
}, accessory: .disclosureIndicator, cellClass: MultilineValue1Cell.self)
],
footer: .title(Strings.Shortcuts.shortcutSettingsOpenSyncedTabsDescription))
)

dataSource.sections.append(
Section(
rows: [
Expand Down
30 changes: 30 additions & 0 deletions Sources/Brave/Shortcuts/ActivityShortcutManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ import UniformTypeIdentifiers
public enum ActivityType: String {
case newTab = "NewTab"
case newPrivateTab = "NewPrivateTab"
case openBookmarks = "OpenBookmarks"
case openHistoryList = "OpenHistoryList"
case clearBrowsingHistory = "ClearBrowsingHistory"
case enableBraveVPN = "EnableBraveVPN"
case openBraveNews = "OpenBraveNews"
case openPlayList = "OpenPlayList"
case openSyncedTabs = "OpenSyncedTabs"

public var identifier: String {
return "\(Bundle.main.bundleIdentifier ?? "").\(self.rawValue)"
Expand All @@ -38,6 +41,10 @@ public enum ActivityType: String {
return Strings.Shortcuts.activityTypeNewTabTitle
case .newPrivateTab:
return Strings.Shortcuts.activityTypeNewPrivateTabTitle
case .openBookmarks:
return Strings.Shortcuts.activityTypeOpenBookmarksTitle
case .openHistoryList:
return Strings.Shortcuts.activityTypeOpenHistoryListTitle
case .clearBrowsingHistory:
return Strings.Shortcuts.activityTypeClearHistoryTitle
case .enableBraveVPN:
Expand All @@ -46,6 +53,8 @@ public enum ActivityType: String {
return Strings.Shortcuts.activityTypeOpenBraveNewsTitle
case .openPlayList:
return Strings.Shortcuts.activityTypeOpenPlaylistTitle
case .openSyncedTabs:
return Strings.Shortcuts.activityTypeOpenSyncedTabsTitle
}
}

Expand All @@ -54,6 +63,10 @@ public enum ActivityType: String {
switch self {
case .newTab, .newPrivateTab:
return Strings.Shortcuts.activityTypeTabDescription
case .openHistoryList:
return Strings.Shortcuts.activityTypeOpenHistoryListDescription
case .openBookmarks:
return Strings.Shortcuts.activityTypeOpenBookmarksDescription
case .clearBrowsingHistory:
return Strings.Shortcuts.activityTypeClearHistoryDescription
case .enableBraveVPN:
Expand All @@ -62,6 +75,8 @@ public enum ActivityType: String {
return Strings.Shortcuts.activityTypeBraveNewsDescription
case .openPlayList:
return Strings.Shortcuts.activityTypeOpenPlaylistDescription
case .openSyncedTabs:
return Strings.Shortcuts.activityTypeOpenSyncedTabsDescription
}
}

Expand All @@ -72,6 +87,10 @@ public enum ActivityType: String {
return Strings.Shortcuts.activityTypeNewTabSuggestedPhrase
case .newPrivateTab:
return Strings.Shortcuts.activityTypeNewPrivateTabSuggestedPhrase
case .openBookmarks:
return Strings.Shortcuts.activityTypeOpenBookmarksSuggestedPhrase
case .openHistoryList:
return Strings.Shortcuts.activityTypeOpenHistoryListSuggestedPhrase
case .clearBrowsingHistory:
return Strings.Shortcuts.activityTypeClearHistorySuggestedPhrase
case .enableBraveVPN:
Expand All @@ -80,6 +99,8 @@ public enum ActivityType: String {
return Strings.Shortcuts.activityTypeOpenBraveNewsSuggestedPhrase
case .openPlayList:
return Strings.Shortcuts.activityTypeOpenPlaylistSuggestedPhrase
case .openSyncedTabs:
return Strings.Shortcuts.activityTypeOpenSyncedTabsSuggestedPhrase
}
}
}
Expand Down Expand Up @@ -134,6 +155,12 @@ public class ActivityShortcutManager: NSObject {
case .newPrivateTab:
bvc.openBlankNewTab(attemptLocationFieldFocus: false, isPrivate: true, isExternal: true)
bvc.popToBVC()
case .openBookmarks:
bvc.popToBVC()
bvc.navigationHelper.openBookmarks()
case .openHistoryList:
bvc.popToBVC()
bvc.navigationHelper.openHistory(isModal: true)
case .clearBrowsingHistory:
bvc.clearHistoryAndOpenNewTab()
case .enableBraveVPN:
Expand Down Expand Up @@ -185,6 +212,9 @@ public class ActivityShortcutManager: NSObject {
PlaylistP3A.recordUsage()
bvc.present(playlistController, animated: true)
}
case .openSyncedTabs:
bvc.popToBVC()
bvc.showTabTray(isExternallyPresented: true)
}
}

Expand Down
Loading

0 comments on commit 3f7ce0f

Please sign in to comment.