-
Notifications
You must be signed in to change notification settings - Fork 423
/
CustomDaxDialog.swift
181 lines (159 loc) · 6.34 KB
/
CustomDaxDialog.swift
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
//
// CustomDaxDialog.swift
// DuckDuckGo
//
// Copyright © 2022 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 CustomDaxDialog: View {
@Environment(\.horizontalSizeClass) var horizontalSizeClass
@Environment(\.verticalSizeClass) var verticalSizeClass
@State var viewModel: CustomDaxDialogViewModel
var body: some View {
ZStack {
overlay
VStack(alignment: .leading, spacing: .zero) {
Spacer()
daxAndBubbleArrow
bubbleBody
}
.padding([.leading, .trailing], Constants.Padding.dialogHorizontal)
.if(verticalSizeClass == .regular) { view in
view.padding(.bottom, Constants.Padding.dialogBottom)
}
.if(verticalSizeClass == .compact) { view in
view.padding([.leading, .trailing], Constants.Padding.dialogHorizontalWide)
}
.if(horizontalSizeClass == .regular) { view in
view.frame(width: Constants.Size.fixedDialogWidth)
}
}
}
@ViewBuilder
private var overlay: some View {
Rectangle()
.foregroundColor(Constants.Colors.overlay)
}
@ViewBuilder
private var daxAndBubbleArrow: some View {
VStack(spacing: Constants.Spacing.daxLogoAndArrow) {
Image.daxLogo
.resizable()
.frame(width: Constants.Size.daxLogo.width, height: Constants.Size.daxLogo.height)
Triangle()
.frame(width: Constants.Size.bubbleArrow.width, height: Constants.Size.bubbleArrow.height)
.foregroundColor(Constants.Colors.bubbleBackground)
}
.padding(.leading, Constants.Padding.daxLogoAndArrow)
}
@ViewBuilder
private var bubbleBody: some View {
VStack {
ScrollView {
VStack(spacing: Constants.Spacing.bubbleElements) {
contentElements
buttons
}
}
.if(verticalSizeClass != .compact) { view in
view.simultaneousGesture(DragGesture(minimumDistance: 0))
}
.fixedSize(horizontal: false, vertical: true)
}
.padding(Constants.Padding.dialogInsets)
.background(
RoundedRectangle(cornerRadius: Constants.Size.dialogCornerRadius)
.foregroundColor(Constants.Colors.bubbleBackground)
)
}
@ViewBuilder
private var contentElements: some View {
ForEach(viewModel.content, id: \.self) { element in
switch element {
case .text(let text):
Text(text)
.font(Constants.Fonts.text)
.foregroundColor(Constants.Colors.text)
.lineSpacing(Constants.Spacing.textLineSpacing)
.frame(maxWidth: .infinity, alignment: .leading)
case .animation(let name, let delay):
LottieView(lottieFile: name, delay: delay)
.fixedSize()
}
}
}
@ViewBuilder
private var buttons: some View {
ForEach(viewModel.buttons, id: \.self) { button in
switch button {
case .bordered(let label, let action):
Button(action: action, label: {
Text(label)
.font(Constants.Fonts.button)
.frame(maxWidth: .infinity, maxHeight: .infinity)
})
.frame(height: Constants.Size.buttonHeight)
.foregroundColor(Constants.Colors.borderedButtonText)
.background(Capsule().foregroundColor(Constants.Colors.borderedButtonBackground))
case .borderless(let label, let action):
Button(action: action, label: {
Text(label)
.font(Constants.Fonts.button)
.frame(maxHeight: .infinity)
})
.frame(height: Constants.Size.buttonHeight)
.buttonStyle(.borderless)
.foregroundColor(Constants.Colors.borderlessButtonText)
.clipShape(Capsule())
}
}
}
}
private enum Constants {
enum Fonts {
static let text = Font(UIFont.appFont(ofSize: 17))
static let button = Font(UIFont.boldAppFont(ofSize: 16))
}
enum Colors {
static let overlay = Color("CustomDaxDialogOverlayColor")
static let bubbleBackground = Color("CustomDaxDialogBubbleBackgroundColor")
static let text = Color("CustomDaxDialogTextColor")
static let borderedButtonText = Color("CustomDaxDialogBorderedButtonTextColor")
static let borderlessButtonText = Color("CustomDaxDialogBorderlessButtonTextColor")
static let borderedButtonBackground = Color("CustomDaxDialogBorderedButtonBackgroundColor")
}
enum Spacing {
static let daxLogoAndArrow: CGFloat = 8
static let bubbleElements: CGFloat = 16
static let textLineSpacing: CGFloat = 5
}
enum Padding {
static let daxLogoAndArrow: CGFloat = 24
static let dialogInsets = EdgeInsets(top: 24, leading: 16, bottom: 24, trailing: 16)
static let dialogHorizontal: CGFloat = 8
static let dialogHorizontalWide: CGFloat = 70
static let dialogBottom: CGFloat = 92
}
enum Size {
static let daxLogo = CGSize(width: 54, height: 54)
static let bubbleArrow = CGSize(width: 15, height: 7)
static let buttonHeight: CGFloat = 44
static let fixedDialogWidth: CGFloat = 380
static let dialogCornerRadius: CGFloat = 16
}
}
private extension Image {
static let daxLogo = Image("Logo")
}