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

New Tab Page Shortcuts section #3104

Merged
merged 7 commits into from
Jul 19, 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
16 changes: 16 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@
6F40D15E2C34436500BF22F0 /* HomePageDisplayDailyPixelBucketTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F40D15C2C34436200BF22F0 /* HomePageDisplayDailyPixelBucketTests.swift */; };
6F5CC0812C2AFFE400AFC840 /* ToggleExpandButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F5CC0802C2AFFE400AFC840 /* ToggleExpandButtonView.swift */; };
6F64AA532C47E92600CF4489 /* FavoritesFaviconLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F64AA522C47E92600CF4489 /* FavoritesFaviconLoader.swift */; };
6F64AA592C4818D700CF4489 /* NewTabPageShortcut.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F64AA582C4818D700CF4489 /* NewTabPageShortcut.swift */; };
6F64AA5B2C481AAA00CF4489 /* Shortcuts.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6F64AA5A2C481AAA00CF4489 /* Shortcuts.xcassets */; };
6F64AA5D2C4920D200CF4489 /* ShortcutAccessoryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F64AA5C2C4920D200CF4489 /* ShortcutAccessoryView.swift */; };
6F64AA5F2C49463C00CF4489 /* ShortcutsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F64AA5E2C49463C00CF4489 /* ShortcutsModel.swift */; };
6F655BE22BAB289E00AC3597 /* DefaultTheme.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F655BE12BAB289E00AC3597 /* DefaultTheme.swift */; };
6F8496412BC3D8EE00ADA54E /* OnboardingButtonsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F8496402BC3D8EE00ADA54E /* OnboardingButtonsView.swift */; };
6F96FF102C2B128500162692 /* NewTabPageCustomizeButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F96FF0F2C2B128500162692 /* NewTabPageCustomizeButtonView.swift */; };
Expand Down Expand Up @@ -1405,6 +1409,10 @@
6F40D15C2C34436200BF22F0 /* HomePageDisplayDailyPixelBucketTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomePageDisplayDailyPixelBucketTests.swift; sourceTree = "<group>"; };
6F5CC0802C2AFFE400AFC840 /* ToggleExpandButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToggleExpandButtonView.swift; sourceTree = "<group>"; };
6F64AA522C47E92600CF4489 /* FavoritesFaviconLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoritesFaviconLoader.swift; sourceTree = "<group>"; };
6F64AA582C4818D700CF4489 /* NewTabPageShortcut.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageShortcut.swift; sourceTree = "<group>"; };
6F64AA5A2C481AAA00CF4489 /* Shortcuts.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Shortcuts.xcassets; sourceTree = "<group>"; };
6F64AA5C2C4920D200CF4489 /* ShortcutAccessoryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutAccessoryView.swift; sourceTree = "<group>"; };
6F64AA5E2C49463C00CF4489 /* ShortcutsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutsModel.swift; sourceTree = "<group>"; };
6F655BE12BAB289E00AC3597 /* DefaultTheme.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultTheme.swift; sourceTree = "<group>"; };
6F8496402BC3D8EE00ADA54E /* OnboardingButtonsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingButtonsView.swift; sourceTree = "<group>"; };
6F96FF0F2C2B128500162692 /* NewTabPageCustomizeButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageCustomizeButtonView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3569,6 +3577,10 @@
children = (
6FE1273F2C204D9B00EB5724 /* ShortcutsView.swift */,
6FE1274A2C20943500EB5724 /* ShortcutItemView.swift */,
6F64AA5C2C4920D200CF4489 /* ShortcutAccessoryView.swift */,
6F64AA582C4818D700CF4489 /* NewTabPageShortcut.swift */,
6F64AA5A2C481AAA00CF4489 /* Shortcuts.xcassets */,
6F64AA5E2C49463C00CF4489 /* ShortcutsModel.swift */,
);
name = Shortcuts;
sourceTree = "<group>";
Expand Down Expand Up @@ -6328,6 +6340,7 @@
F4F7F10C25813FE200045D62 /* 03_Airstream_divided_by_four.json in Resources */,
AAF2E28723E0498200962AF8 /* [email protected] in Resources */,
AA4D6AB923DE4D15007E8790 /* [email protected] in Resources */,
6F64AA5B2C481AAA00CF4489 /* Shortcuts.xcassets in Resources */,
984147B424F0264B00362052 /* Authentication.storyboard in Resources */,
1EE411FD2858B9300003FE64 /* dark-trackers-2.json in Resources */,
AA4D6ABC23DE4D15007E8790 /* [email protected] in Resources */,
Expand Down Expand Up @@ -6673,6 +6686,7 @@
6FE095D82BD90AFB00490FF8 /* UniversalOmniBarState.swift in Sources */,
1DEAADE82BA38AA500E25A97 /* SettingsGeneralView.swift in Sources */,
853C5F5B21BFF0AE001F7A05 /* HomeCollectionView.swift in Sources */,
6F64AA592C4818D700CF4489 /* NewTabPageShortcut.swift in Sources */,
3132FA2627A0784600DD7A12 /* FilePreviewHelper.swift in Sources */,
9820FF502244FECC008D4782 /* UIScrollViewExtension.swift in Sources */,
8540BD5423D8D5080057FDD2 /* PreserveLoginsAlert.swift in Sources */,
Expand Down Expand Up @@ -6898,6 +6912,7 @@
9FB027192C26BC29009EA190 /* BrowsersComparisonModel.swift in Sources */,
3151F0EE2735800800226F58 /* VoiceSearchFeedbackView.swift in Sources */,
37CF91642BB4A82A00BADCAE /* CrashCollectionOnboardingViewModel.swift in Sources */,
6F64AA5D2C4920D200CF4489 /* ShortcutAccessoryView.swift in Sources */,
857EEB752095FFAC008A005C /* HomeRowInstructionsViewController.swift in Sources */,
D63FF8952C1B67E9006DE24D /* YoutubePlayerUserScript.swift in Sources */,
4BF3E4AF2C06A85200ED5D57 /* VPNRedditSessionWorkaround.swift in Sources */,
Expand Down Expand Up @@ -6991,6 +7006,7 @@
85C861E628FF1B5F00189466 /* HomeViewSectionRenderersExtension.swift in Sources */,
CB825C922C071B1400BCC586 /* AlertView.swift in Sources */,
1DDF40292BA04FCD006850D9 /* SettingsPrivacyProtectionsView.swift in Sources */,
6F64AA5F2C49463C00CF4489 /* ShortcutsModel.swift in Sources */,
F1D477C61F2126CC0031ED49 /* OmniBarState.swift in Sources */,
85F2FFCD2211F615006BB258 /* MainViewController+KeyCommands.swift in Sources */,
6FD3F8192C41252900DA5797 /* NewTabPageControllerDelegate.swift in Sources */,
Expand Down
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"
Copy link
Contributor Author

@dus7 dus7 Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amddg44 additional source we've discussed. Used here.

}

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)
}
43 changes: 43 additions & 0 deletions DuckDuckGo/NewTabPageShortcut.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// NewTabPageShortcut.swift
// DuckDuckGo
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import UIKit

enum NewTabPageShortcut: CaseIterable, Equatable, Identifiable, Codable {
var id: String { storageIdentifier }

case bookmarks, aiChat, passwords, downloads, settings
}

extension NewTabPageShortcut {
var storageIdentifier: String {
switch self {
case .bookmarks:
"shortcut.storage.identifier.bookmarks"
case .aiChat:
"shortcut.storage.identifier.aichat"
case .passwords:
"shortcut.storage.identifier.passwords"
case .downloads:
"shortcut.storage.identifier.downloads"
case .settings:
"shortcut.storage.identifier.settings"
}
}
}
26 changes: 19 additions & 7 deletions DuckDuckGo/NewTabPageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ import RemoteMessaging
struct NewTabPageView<FavoritesModelType: FavoritesModel>: View {
@Environment(\.horizontalSizeClass) var horizontalSizeClass

@ObservedObject var messagesModel: NewTabPageMessagesModel
@ObservedObject var favoritesModel: FavoritesModelType
@ObservedObject private var messagesModel: NewTabPageMessagesModel
@ObservedObject private var favoritesModel: FavoritesModelType
@ObservedObject private var shortcutsModel: ShortcutsModel

init(messagesModel: NewTabPageMessagesModel, favoritesModel: FavoritesModelType) {
init(messagesModel: NewTabPageMessagesModel, favoritesModel: FavoritesModelType, shortcutsModel: ShortcutsModel) {
self.messagesModel = messagesModel
self.favoritesModel = favoritesModel
self.shortcutsModel = shortcutsModel

self.messagesModel.load()
}
Expand All @@ -55,8 +57,10 @@ struct NewTabPageView<FavoritesModelType: FavoritesModel>: View {
}

// MARK: Shortcuts
ShortcutsView()
.padding(Constant.sectionPadding)
if !shortcutsModel.enabledShortcuts.isEmpty {
ShortcutsView(model: shortcutsModel)
.padding(Constant.sectionPadding)
}

Spacer()

Expand Down Expand Up @@ -97,7 +101,8 @@ private struct Constant {
homeMessages: []
)
),
favoritesModel: FavoritesPreviewModel()
favoritesModel: FavoritesPreviewModel(),
shortcutsModel: ShortcutsModel()
)
}

Expand All @@ -117,7 +122,8 @@ private struct Constant {
]
)
),
favoritesModel: FavoritesPreviewModel()
favoritesModel: FavoritesPreviewModel(),
shortcutsModel: ShortcutsModel()
)
}

Expand All @@ -140,3 +146,9 @@ private final class PreviewMessagesConfiguration: HomePageMessagesConfiguration
homeMessages = homeMessages.dropLast()
}
}

private extension ShortcutsModel {
convenience init() {
self.init(shortcutsPreferencesStorage: InMemoryShortcutsPreferencesStorage())
}
}
26 changes: 25 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,12 +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)
favoritesModel: favoritesModel,
shortcutsModel: shortcutsModel)

super.init(rootView: newTabPageView)

assignFavoriteModelActions()
assignShorcutsModelActions()
}

// MARK: - Private
Expand Down Expand Up @@ -75,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
79 changes: 79 additions & 0 deletions DuckDuckGo/ShortcutAccessoryView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// ShortcutAccessoryView.swift
// DuckDuckGo
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import SwiftUI

struct ShortcutAccessoryView: View {

let accessoryType: ShortcutAccessoryType

var body: some View {
ZStack {
Circle()
.foregroundStyle(accessoryType.backgroundColor)
Image(accessoryType.iconResource)
.resizable()
.padding(4)
.foregroundColor(accessoryType.foregroundColor)
.aspectRatio(contentMode: .fit)
}
.shadow(color: .shade(0.15), radius: 1, y: 1)
}
}

enum ShortcutAccessoryType {
case selected
case add
}

private extension ShortcutAccessoryType {
var iconResource: ImageResource {
switch self {
case .selected:
return .check24
case .add:
return .add24
}
}

var backgroundColor: Color {
switch self {
case .selected:
Color(designSystemColor: .accent)
case .add:
Color(designSystemColor: .surface)
}
}

var foregroundColor: Color {
switch self {
case .selected:
Color(designSystemColor: .surface)
case .add:
Color(designSystemColor: .accent)
}
}
}

#Preview {
VStack {
ShortcutAccessoryView(accessoryType: .add).frame(width: 24)
ShortcutAccessoryView(accessoryType: .selected).frame(width: 24)
}
}
Loading
Loading