Skip to content

Commit

Permalink
Add shortcut redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
dus7 committed Jul 18, 2024
1 parent 321eef5 commit b5d9331
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions DuckDuckGo/AutofillLoginSettingsListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum AutofillSettingsSource: String {
case appIconShortcut = "app_icon_shortcut"
case homeScreenWidget = "home_screen_widget"
case lockScreenWidget = "lock_screen_widget"
case newTabPageShortcut = "new_tab_page_shortcut"
}

protocol AutofillLoginSettingsListViewControllerDelegate: AnyObject {
Expand Down
27 changes: 27 additions & 0 deletions DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ class MainViewController: UIViewController {
homePageMessagesConfiguration: homePageConfiguration)

controller.delegate = self
controller.shortcutsDelegate = self

newTabPageViewController = controller
addToContentContainer(controller: controller)
Expand Down Expand Up @@ -2030,6 +2031,32 @@ extension MainViewController: NewTabPageControllerDelegate {
}
}

extension MainViewController: NewTabPageControllerShortcutsDelegate {
func newTabPageDidRequestDownloads(_ controller: NewTabPageViewController) {
segueToDownloads()
}

func newTabPageDidRequestBookmarks(_ controller: NewTabPageViewController) {
segueToBookmarks()
}

func newTabPageDidRequestPasswords(_ controller: NewTabPageViewController) {
launchAutofillLogins(source: .newTabPageShortcut)
}

func newTabPageDidRequestAIChat(_ controller: NewTabPageViewController) {
loadUrl(Constant.duckAIURL)
}

func newTabPageDidRequestSettings(_ controller: NewTabPageViewController) {
segueToSettings()
}

private enum Constant {
static let duckAIURL = URL(string: "https://duckduckgo.com/?q=DuckDuckGo+AI+Chat&ia=chat&duckai=1")!
}
}

extension MainViewController: TabDelegate {

func tab(_ tab: TabViewController,
Expand Down
8 changes: 8 additions & 0 deletions DuckDuckGo/NewTabPageControllerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ protocol NewTabPageControllerDelegate: AnyObject {
func newTabPageDidDeleteFavorite(_ controller: NewTabPageViewController, favorite: BookmarkEntity)
func newTabPageDidEditFavorite(_ controller: NewTabPageViewController, favorite: BookmarkEntity)
}

protocol NewTabPageControllerShortcutsDelegate: AnyObject {
func newTabPageDidRequestDownloads(_ controller: NewTabPageViewController)
func newTabPageDidRequestBookmarks(_ controller: NewTabPageViewController)
func newTabPageDidRequestPasswords(_ controller: NewTabPageViewController)
func newTabPageDidRequestAIChat(_ controller: NewTabPageViewController)
func newTabPageDidRequestSettings(_ controller: NewTabPageViewController)
}
25 changes: 24 additions & 1 deletion DuckDuckGo/NewTabPageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ final class NewTabPageViewController: UIHostingController<NewTabPageView<Favorit
private(set) lazy var faviconsFetcherOnboarding = FaviconsFetcherOnboarding(syncService: syncService, syncBookmarksAdapter: syncBookmarksAdapter)

private let favoritesModel: FavoritesDefaultModel
private let shortcutsModel: ShortcutsModel

init(interactionModel: FavoritesListInteracting,
syncService: DDGSyncing,
Expand All @@ -40,13 +41,15 @@ final class NewTabPageViewController: UIHostingController<NewTabPageView<Favorit
self.syncBookmarksAdapter = syncBookmarksAdapter

self.favoritesModel = FavoritesDefaultModel(interactionModel: interactionModel)
self.shortcutsModel = ShortcutsModel(shortcutsPreferencesStorage: InMemoryShortcutsPreferencesStorage())
let newTabPageView = NewTabPageView(messagesModel: NewTabPageMessagesModel(homePageMessagesConfiguration: homePageMessagesConfiguration),
favoritesModel: favoritesModel,
shortcutsModel: ShortcutsModel(shortcutsPreferencesStorage: InMemoryShortcutsPreferencesStorage()))
shortcutsModel: shortcutsModel)

super.init(rootView: newTabPageView)

assignFavoriteModelActions()
assignShorcutsModelActions()
}

// MARK: - Private
Expand Down Expand Up @@ -76,12 +79,32 @@ final class NewTabPageViewController: UIHostingController<NewTabPageView<Favorit
}
}

private func assignShorcutsModelActions() {
shortcutsModel.onShortcutOpened = { [weak self] shortcut in
guard let self else { return }

switch shortcut {
case .aiChat:
shortcutsDelegate?.newTabPageDidRequestAIChat(self)
case .bookmarks:
shortcutsDelegate?.newTabPageDidRequestBookmarks(self)
case .downloads:
shortcutsDelegate?.newTabPageDidRequestDownloads(self)
case .passwords:
shortcutsDelegate?.newTabPageDidRequestPasswords(self)
case .settings:
shortcutsDelegate?.newTabPageDidRequestSettings(self)
}
}
}

// MARK: - NewTabPage

let isDragging: Bool = false

weak var chromeDelegate: BrowserChromeDelegate?
weak var delegate: NewTabPageControllerDelegate?
weak var shortcutsDelegate: NewTabPageControllerShortcutsDelegate?

func launchNewSearch() {

Expand Down
6 changes: 6 additions & 0 deletions DuckDuckGo/ShortcutsModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ final class ShortcutsModel: ObservableObject {

private let shortcutsPreferencesStorage: ShortcutsPreferencesStorage

var onShortcutOpened: ((NewTabPageShortcut) -> Void)?

init(shortcutsPreferencesStorage: ShortcutsPreferencesStorage) {
self.shortcutsPreferencesStorage = shortcutsPreferencesStorage

enabledShortcuts = shortcutsPreferencesStorage.enabledShortcuts
}

func openShortcut(_ shortcut: NewTabPageShortcut) {
onShortcutOpened?(shortcut)
}
}

final class InMemoryShortcutsPreferencesStorage: ShortcutsPreferencesStorage {
Expand Down
6 changes: 5 additions & 1 deletion DuckDuckGo/ShortcutsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ struct ShortcutsView: View {
var body: some View {
NewTabPageGridView { _ in
ForEach(model.enabledShortcuts) { shortcut in
ShortcutItemView(shortcut: shortcut, accessoryType: nil)
Button {
model.openShortcut(shortcut)
} label: {
ShortcutItemView(shortcut: shortcut, accessoryType: nil)
}
}
}
}
Expand Down

0 comments on commit b5d9331

Please sign in to comment.