Skip to content

Commit

Permalink
Define basic model with in-memory storage
Browse files Browse the repository at this point in the history
  • Loading branch information
dus7 committed Jul 18, 2024
1 parent 8d55272 commit 321eef5
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 15 deletions.
4 changes: 4 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@
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 @@ -1411,6 +1412,7 @@
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 @@ -3578,6 +3580,7 @@
6F64AA5C2C4920D200CF4489 /* ShortcutAccessoryView.swift */,
6F64AA582C4818D700CF4489 /* NewTabPageShortcut.swift */,
6F64AA5A2C481AAA00CF4489 /* Shortcuts.xcassets */,
6F64AA5E2C49463C00CF4489 /* ShortcutsModel.swift */,
);
name = Shortcuts;
sourceTree = "<group>";
Expand Down Expand Up @@ -7003,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
21 changes: 19 additions & 2 deletions DuckDuckGo/NewTabPageShortcut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,25 @@

import UIKit

enum NewTabPageShortcut: Int, CaseIterable, Equatable, Identifiable {
var id: Int { rawValue }
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())
}
}
3 changes: 2 additions & 1 deletion DuckDuckGo/NewTabPageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ final class NewTabPageViewController: UIHostingController<NewTabPageView<Favorit

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

super.init(rootView: newTabPageView)

Expand Down
13 changes: 13 additions & 0 deletions DuckDuckGo/ShortcutItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ struct ShortcutItemView: View {

#Preview {
VStack(spacing: 24) {

LazyVGrid(columns: [GridItem(.adaptive(minimum: 86))], content: {
ForEach(NewTabPageShortcut.allCases) { shortcut in
Button {

} label: {
ShortcutItemView(shortcut: shortcut, accessoryType: nil)
}
}
})

Divider()

LazyVGrid(columns: [GridItem(.adaptive(minimum: 86))], content: {
ForEach(NewTabPageShortcut.allCases) { shortcut in
ShortcutItemView(shortcut: shortcut, accessoryType: .add)
Expand Down
40 changes: 40 additions & 0 deletions DuckDuckGo/ShortcutsModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// ShortcutsModel.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 Foundation

protocol ShortcutsPreferencesStorage {
var enabledShortcuts: [NewTabPageShortcut] { get }
}

final class ShortcutsModel: ObservableObject {
@Published private(set) var enabledShortcuts: [NewTabPageShortcut] = []

private let shortcutsPreferencesStorage: ShortcutsPreferencesStorage

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

enabledShortcuts = shortcutsPreferencesStorage.enabledShortcuts
}
}

final class InMemoryShortcutsPreferencesStorage: ShortcutsPreferencesStorage {
private(set) var enabledShortcuts: [NewTabPageShortcut] = NewTabPageShortcut.allCases
}
9 changes: 4 additions & 5 deletions DuckDuckGo/ShortcutsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@
import SwiftUI

struct ShortcutsView: View {
@Environment(\.horizontalSizeClass) var horizontalSizeClass
@State var enabledShortcuts: [NewTabPageShortcut] = Array(NewTabPageShortcut.allCases)
@ObservedObject private(set) var model: ShortcutsModel

var body: some View {
NewTabPageGridView { _ in
ForEach(enabledShortcuts) { shortcut in
ShortcutItemView(shortcut: shortcut, accessoryType: .add)
ForEach(model.enabledShortcuts) { shortcut in
ShortcutItemView(shortcut: shortcut, accessoryType: nil)
}
}
}
}

#Preview {
ScrollView {
ShortcutsView()
ShortcutsView(model: ShortcutsModel(shortcutsPreferencesStorage: InMemoryShortcutsPreferencesStorage()))
}
.background(Color(designSystemColor: .background))
}

0 comments on commit 321eef5

Please sign in to comment.