|
| 1 | +// |
| 2 | +// BrowsingMenuVariantDBuilder.swift |
| 3 | +// DuckDuckGo |
| 4 | +// |
| 5 | +// Copyright © 2025 DuckDuckGo. All rights reserved. |
| 6 | +// |
| 7 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +// you may not use this file except in compliance with the License. |
| 9 | +// You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, software |
| 14 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +// See the License for the specific language governing permissions and |
| 17 | +// limitations under the License. |
| 18 | +// |
| 19 | + |
| 20 | +import Foundation |
| 21 | +import Bookmarks |
| 22 | +import Core |
| 23 | + |
| 24 | +/// Variant D Builder: Settings in header, share below favorites, no footer |
| 25 | +final class BrowsingMenuVariantDBuilder: BrowsingMenuVariantBuilder { |
| 26 | + weak var entryBuilder: BrowsingMenuEntryBuilding? |
| 27 | + |
| 28 | + init(entryBuilder: BrowsingMenuEntryBuilding) { |
| 29 | + self.entryBuilder = entryBuilder |
| 30 | + } |
| 31 | + |
| 32 | + func buildMenu( |
| 33 | + context: BrowsingMenuContext, |
| 34 | + bookmarksInterface: MenuBookmarksInteracting, |
| 35 | + mobileCustomization: MobileCustomization, |
| 36 | + clearTabsAndData: @escaping () -> Void |
| 37 | + ) -> BrowsingMenuModel? { |
| 38 | + |
| 39 | + switch context { |
| 40 | + case .newTabPage: |
| 41 | + return buildNewTabPageMenu(mobileCustomization: mobileCustomization, |
| 42 | + clearTabsAndData: clearTabsAndData) |
| 43 | + |
| 44 | + case .aiChatTab: |
| 45 | + return buildAIChatMenu() |
| 46 | + |
| 47 | + case .website: |
| 48 | + return buildWebsiteMenu( |
| 49 | + bookmarksInterface: bookmarksInterface, |
| 50 | + mobileCustomization: mobileCustomization, |
| 51 | + clearTabsAndData: clearTabsAndData |
| 52 | + ) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private func buildNewTabPageMenu(mobileCustomization: MobileCustomization, |
| 57 | + clearTabsAndData: @escaping () -> Void) -> BrowsingMenuModel? { |
| 58 | + guard let entryBuilder = entryBuilder else { return nil } |
| 59 | + |
| 60 | + let headerItems: [BrowsingMenuModel.Entry] = [ |
| 61 | + .init(entryBuilder.makeNewTabEntry()), |
| 62 | + .init(entryBuilder.makeChatEntry(withSmallIcon: false)), |
| 63 | + .init(entryBuilder.makeSettingsEntry(useSmallIcon: false)) |
| 64 | + ].compactMap { $0 } |
| 65 | + |
| 66 | + let shortcutsItems: [BrowsingMenuModel.Entry] = [ |
| 67 | + .init(entryBuilder.makeOpenBookmarksEntry()), |
| 68 | + .init(entryBuilder.makeAutoFillEntry()), |
| 69 | + .init(entryBuilder.makeDownloadsEntry()) |
| 70 | + ].compactMap { $0 } |
| 71 | + |
| 72 | + let privacyItems: [BrowsingMenuModel.Entry] = [ |
| 73 | + .init(entryBuilder.makeVPNEntry()), |
| 74 | + .init(entryBuilder.makeClearDataEntry(mobileCustomization: mobileCustomization, clearTabsAndData: clearTabsAndData)) |
| 75 | + ].compactMap { $0 } |
| 76 | + |
| 77 | + let sections = [ |
| 78 | + BrowsingMenuModel.Section(items: shortcutsItems), |
| 79 | + BrowsingMenuModel.Section(items: privacyItems) |
| 80 | + ] |
| 81 | + |
| 82 | + return BrowsingMenuModel( |
| 83 | + headerItems: headerItems, |
| 84 | + sections: sections, |
| 85 | + footerItems: [] |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + private func buildWebsiteMenu( |
| 90 | + bookmarksInterface: MenuBookmarksInteracting, |
| 91 | + mobileCustomization: MobileCustomization, |
| 92 | + clearTabsAndData: @escaping () -> Void |
| 93 | + ) -> BrowsingMenuModel? { |
| 94 | + guard let entryBuilder = entryBuilder else { return nil } |
| 95 | + |
| 96 | + // Header: new tab, duck.ai (conditional), settings |
| 97 | + let headerItems: [BrowsingMenuModel.Entry] = [ |
| 98 | + .init(entryBuilder.makeNewTabEntry()), |
| 99 | + .init(entryBuilder.makeChatEntry(withSmallIcon: false)), |
| 100 | + .init(entryBuilder.makeSettingsEntry(useSmallIcon: false)) |
| 101 | + ].compactMap { $0 } |
| 102 | + |
| 103 | + // Sections |
| 104 | + var sections = [BrowsingMenuModel.Section]() |
| 105 | + |
| 106 | + // Link section |
| 107 | + if let bookmarkEntries = entryBuilder.makeBookmarkEntries(with: bookmarksInterface) { |
| 108 | + let linkItems: [BrowsingMenuModel.Entry] = [ |
| 109 | + .init(bookmarkEntries.bookmark), |
| 110 | + .init(bookmarkEntries.favorite, tag: .favorite), |
| 111 | + .init(entryBuilder.makeShareEntry(useSmallIcon: true)) |
| 112 | + ].compactMap { $0 } |
| 113 | + sections.append(BrowsingMenuModel.Section(items: linkItems)) |
| 114 | + } |
| 115 | + |
| 116 | + // Tab actions section |
| 117 | + let tabActionItems: [BrowsingMenuModel.Entry] = [ |
| 118 | + .init(entryBuilder.makeFindInPageEntry()), |
| 119 | + .init(entryBuilder.makeZoomEntry()), |
| 120 | + .init(entryBuilder.makeDesktopSiteEntry()) |
| 121 | + ].compactMap { $0 } |
| 122 | + |
| 123 | + if !tabActionItems.isEmpty { |
| 124 | + sections.append(BrowsingMenuModel.Section(items: tabActionItems)) |
| 125 | + } |
| 126 | + |
| 127 | + // Privacy section |
| 128 | + let privacyItems: [BrowsingMenuModel.Entry] = [ |
| 129 | + .init(entryBuilder.makeVPNEntry()), |
| 130 | + .init(entryBuilder.makeUseNewDuckAddressEntry()), |
| 131 | + .init(entryBuilder.makeToggleProtectionEntry()), |
| 132 | + .init(entryBuilder.makeKeepSignInEntry()), |
| 133 | + .init(entryBuilder.makeClearDataEntry(mobileCustomization: mobileCustomization, clearTabsAndData: clearTabsAndData)) |
| 134 | + ].compactMap { $0 } |
| 135 | + |
| 136 | + if !privacyItems.isEmpty { |
| 137 | + sections.append(BrowsingMenuModel.Section(items: privacyItems)) |
| 138 | + } |
| 139 | + |
| 140 | + // Shortcuts section |
| 141 | + let shortcutItems: [BrowsingMenuModel.Entry] = [ |
| 142 | + .init(entryBuilder.makeOpenBookmarksEntry()), |
| 143 | + .init(entryBuilder.makeAutoFillEntry()), |
| 144 | + .init(entryBuilder.makeDownloadsEntry()) |
| 145 | + ].compactMap { $0 } |
| 146 | + |
| 147 | + if !shortcutItems.isEmpty { |
| 148 | + sections.append(BrowsingMenuModel.Section(items: shortcutItems)) |
| 149 | + } |
| 150 | + |
| 151 | + // Other section |
| 152 | + let otherItems: [BrowsingMenuModel.Entry] = [ |
| 153 | + .init(entryBuilder.makeReloadEntry()), |
| 154 | + .init(entryBuilder.makeReportBrokenSiteEntry()), |
| 155 | + .init(entryBuilder.makePrintEntry(withSmallIcon: true)) |
| 156 | + ].compactMap { $0 } |
| 157 | + |
| 158 | + if !otherItems.isEmpty { |
| 159 | + sections.append(BrowsingMenuModel.Section(items: otherItems)) |
| 160 | + } |
| 161 | + |
| 162 | + return BrowsingMenuModel( |
| 163 | + headerItems: headerItems, |
| 164 | + sections: sections, |
| 165 | + footerItems: [] |
| 166 | + ) |
| 167 | + } |
| 168 | +} |
0 commit comments