-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathBrowsingMenuSheetView.swift
More file actions
284 lines (250 loc) · 9.83 KB
/
Copy pathBrowsingMenuSheetView.swift
File metadata and controls
284 lines (250 loc) · 9.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//
// BrowsingMenuSheetView.swift
// DuckDuckGo
//
// Copyright © 2025 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
import UIKit
import DesignResourcesKit
import DesignResourcesKitIcons
typealias BrowsingMenuSheetViewController = UIHostingController<BrowsingMenuSheetView>
struct BrowsingMenuModel {
var headerItems: [BrowsingMenuModel.Entry]
var sections: [BrowsingMenuModel.Section]
var footerItems: [BrowsingMenuModel.Entry]
}
struct BrowsingMenuSheetView: View {
@Environment(\.presentationMode) var presentationMode
private let model: BrowsingMenuModel
private let onDismiss: () -> Void
@State private var highlightTag: BrowsingMenuModel.Entry.Tag?
@State private var actionToPerform: () -> Void = {}
init(model: BrowsingMenuModel, highlightRowWithTag: BrowsingMenuModel.Entry.Tag? = nil, onDismiss: @escaping () -> Void) {
self.model = model
self.onDismiss = onDismiss
_highlightTag = State(initialValue: highlightRowWithTag)
}
var body: some View {
NavigationView {
List {
Section {
if !model.headerItems.isEmpty {
HStack(spacing: 2) {
ForEach(model.headerItems) { headerItem in
MenuHeaderButton(entryData: headerItem) {
actionToPerform = { headerItem.action() }
presentationMode.wrappedValue.dismiss()
}
.frame(maxWidth: .infinity)
}
}
.background((Color(designSystemColor: .background)))
}
}
.listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
.listRowSeparatorTint(Color(designSystemColor: .lines))
ForEach(model.sections) { section in
Section {
ForEach(section.items) { item in
let isHighlighted = highlightTag != nil && item.tag == highlightTag
MenuRowButton(entryData: item, isHighlighted: isHighlighted) {
actionToPerform = { item.action() }
presentationMode.wrappedValue.dismiss()
}
.listRowBackground(Color(designSystemColor: .surface))
}
}
}
.listRowSeparatorTint(Color(designSystemColor: .lines))
}
.compactSectionSpacingIfAvailable()
.applyInsetGroupedListStyle()
.onDisappear(perform: {
actionToPerform()
onDismiss()
})
.floatingToolbar(
footerItems: model.footerItems,
actionToPerform: $actionToPerform,
presentationMode: presentationMode,
showsLabels: model.footerItems.count < 2
)
}
.tint(Color(designSystemColor: .textPrimary))
.background((Color(designSystemColor: .background)))
}
}
extension BrowsingMenuModel {
struct Section: Identifiable {
let id = UUID()
let items: [BrowsingMenuModel.Entry]
}
struct Entry: Identifiable, Equatable {
let id: UUID = UUID()
let name: String
let accessibilityLabel: String?
let image: UIImage
let showNotificationDot: Bool
let customDotColor: UIColor?
let action: () -> Void
let tag: Tag?
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
static func == (lhs: BrowsingMenuModel.Entry, rhs: BrowsingMenuModel.Entry) -> Bool {
lhs.id == rhs.id
}
enum Tag {
case favorite
}
}
}
extension BrowsingMenuModel.Entry {
init?(_ browsingMenuEntry: BrowsingMenuEntry?, tag: Tag? = nil) {
guard let browsingMenuEntry = browsingMenuEntry else { return nil }
switch browsingMenuEntry {
case .separator:
assertionFailure(#function + " should not be called for .separator")
self.init(name: "", accessibilityLabel: nil, image: UIImage(), showNotificationDot: false, customDotColor: nil, action: {}, tag: tag)
case .regular(let name, let accessibilityLabel, let image, let showNotificationDot, let customDotColor, let action):
self.init(
name: name,
accessibilityLabel: accessibilityLabel,
image: image,
showNotificationDot: showNotificationDot,
customDotColor: customDotColor,
action: action,
tag: tag
)
}
}
}
private struct MenuRowButton: View {
fileprivate let entryData: BrowsingMenuModel.Entry
let isHighlighted: Bool
let action: () -> Void
var body: some View {
Button(action: action) {
HStack(spacing: 8) {
Image(uiImage: entryData.image)
.padding(2)
.overlay {
if isHighlighted {
LottieView(lottieFile: "view_highlight", loopMode: .mode(.loop), isAnimating: .constant(true))
.scaledToFill()
.scaleEffect(1.3)
}
}
Text(entryData.name)
if entryData.showNotificationDot {
Circle().fill(entryData.customDotColor.map({ Color($0) }) ?? Color(designSystemColor: .accent))
.frame(width: 8, height: 8)
.padding(.leading, 6)
.padding(.trailing, 12)
Spacer()
}
}
}
.accessibilityLabel(entryData.accessibilityLabel ?? entryData.name)
}
}
private struct MenuHeaderButton: View {
fileprivate let entryData: BrowsingMenuModel.Entry
let action: () -> Void
var body: some View {
Button(action: action) {
VStack(spacing: 2) {
Image(uiImage: entryData.image)
.tint(Color(designSystemColor: .icons))
Text(entryData.name)
.daxFootnoteRegular()
.foregroundStyle(Color(designSystemColor: .textSecondary))
}
.padding(.vertical, 8)
.padding(.horizontal, 8)
.frame(maxWidth: .infinity)
.background(Color(designSystemColor: .surface))
.clipShape(RoundedRectangle(cornerRadius: Constant.cornerRadius))
.contentShape(RoundedRectangle(cornerRadius: Constant.cornerRadius))
}
.buttonStyle(.plain)
.accessibilityLabel(entryData.accessibilityLabel ?? entryData.name)
}
private enum Constant {
static let cornerRadius: CGFloat = 4
}
}
private extension View {
func floatingToolbar(
footerItems: [BrowsingMenuModel.Entry],
actionToPerform: Binding<() -> Void>,
presentationMode: Binding<PresentationMode>,
showsLabels: Bool
) -> some View {
modifier(FloatingToolbarModifier(
footerItems: footerItems,
actionToPerform: actionToPerform,
presentationMode: presentationMode,
showsLabels: showsLabels
))
}
}
private struct FloatingToolbarModifier: ViewModifier {
let footerItems: [BrowsingMenuModel.Entry]
@Binding var actionToPerform: () -> Void
let presentationMode: Binding<PresentationMode>
let showsLabels: Bool
func body(content: Content) -> some View {
if footerItems.isEmpty {
content
} else {
content
.safeAreaInset(edge: .bottom, content: {
createBottomToolbar(labels: showsLabels)
})
}
}
@ViewBuilder
private func createBottomToolbar(labels: Bool = false) -> some View {
HStack(spacing: 4) {
ForEach(footerItems) { footerItem in
Button(action: {
actionToPerform = { footerItem.action() }
presentationMode.wrappedValue.dismiss()
}) {
HStack(spacing: 4) {
Image(uiImage: footerItem.image)
.tint(Color(designSystemColor: .icons))
if labels {
Text(footerItem.name)
.daxBodyRegular()
.foregroundStyle(Color(designSystemColor: .textPrimary))
}
}
.padding(.vertical, 8)
.padding(.horizontal, 16)
}
.buttonStyle(.plain)
.accessibilityLabel(footerItem.accessibilityLabel ?? footerItem.name)
}
}
.background(Color(designSystemColor: .surfaceCanvas))
.clipShape(RoundedRectangle(cornerRadius: 12))
.shadow(color: Color(designSystemColor: .shadowSecondary), radius: 4, x: 0, y: 4)
.shadow(color: Color(designSystemColor: .shadowSecondary), radius: 2, x: 0, y: 1)
.fixedSize(horizontal: true, vertical: true)
}
}