-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathExamplesScreen.swift
More file actions
400 lines (353 loc) · 14.8 KB
/
ExamplesScreen.swift
File metadata and controls
400 lines (353 loc) · 14.8 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//
//
import Foundation
import JDStatusBarNotification
import SwiftUI
public class ExamplesScreenFactory: NSObject {
@objc public static func createExamplesScreen(title: String) -> UIViewController {
let text = "👋 Hello World!"
NotificationPresenter.shared.present(text, includedStyle: .matrix, duration: 2.5)
return UIHostingController(rootView:
NavigationView {
ExamplesScreen(title: title)
}
)
}
}
struct ExamplesScreen: View {
let title: String
@State var progress = 0.0
@State var showActivity = false
@State var showSubtitle = false
@State var backgroundType: StatusBarNotificationBackgroundType = .pill
@State var queueContent: [StatusBarNotification]? = nil // for queue counter
func showDefaultNotification(_ text: String, completion: @escaping (NotificationPresenter) -> ()) {
let styleName = NotificationPresenter.shared.addStyle(named: "default_sample") { style in
style.backgroundStyle.backgroundType = backgroundType
return style
}
NotificationPresenter.shared.present(text,
subtitle: showSubtitle ? "{subtitle}" : nil,
styleName: styleName,
completion: completion)
if showActivity {
NotificationPresenter.shared.displayActivityIndicator(true)
}
if progress > 0.0 {
NotificationPresenter.shared.displayProgressBar(at: progress)
}
}
func showIncludedStyle(_ text: String, style: IncludedStatusBarNotificationStyle) {
let styleName = NotificationPresenter.shared.addStyle(named: "incl_style", usingStyle: style) { style in
style.backgroundStyle.backgroundType = backgroundType
return style
}
NotificationPresenter.shared.present(text, subtitle: showSubtitle ? "{subtitle}" : nil, styleName: styleName)
NotificationPresenter.shared.dismiss(after: 3.0)
if showActivity {
NotificationPresenter.shared.displayActivityIndicator(true)
}
if progress > 0.0 {
NotificationPresenter.shared.displayProgressBar(at: progress)
}
}
var body: some View {
List {
Section {
NavigationLink {
StyleEditorScreen()
} label: {
VStack(alignment: .leading) {
Text("Style Editor")
.font(.subheadline)
.foregroundColor(.accentColor)
Text("Get creative & create your own style!")
.font(.caption2)
.foregroundColor(.secondary)
}
}.foregroundColor(.accentColor)
}
Section("Settings") {
Text("These settings affect the presentation of all below examples.")
.font(.caption)
.foregroundStyle(.secondary)
Toggle("Show subtitle", isOn: $showSubtitle)
.onChange(of: showSubtitle) { on in
if on, !NotificationPresenter.shared.isVisible {
showDefaultNotification("Look!") { _ in }
NotificationPresenter.shared.dismiss(after: 2.0)
}
NotificationPresenter.shared.updateSubtitle(on ? "I am a subtitle" : nil)
}.font(.subheadline)
Toggle("Activity Indicator", isOn: $showActivity)
.onChange(of: showActivity) { _ in
if !NotificationPresenter.shared.isVisible {
if showActivity {
let styleName = NotificationPresenter.shared.addStyle(named: "activity") { style in
style.backgroundStyle.backgroundType = backgroundType
style.backgroundStyle.pillStyle.minimumWidth = 0.0
return style
}
NotificationPresenter.shared.present("", styleName: styleName, duration: 2.0)
NotificationPresenter.shared.displayActivityIndicator(true)
}
} else {
NotificationPresenter.shared.displayActivityIndicator(showActivity)
}
}.font(.subheadline)
HStack {
Text("Progress Bar (\(Int(round(progress * 100)))%)")
Spacer()
Slider(value: $progress)
.frame(width: 150)
}
.onChange(of: progress) { _ in
if !NotificationPresenter.shared.isVisible {
if progress > 0.0 {
showDefaultNotification("Making progress…") { _ in }
NotificationPresenter.shared.dismiss(after: 2.0)
}
} else {
NotificationPresenter.shared.displayProgressBar(at: progress)
}
}.font(.subheadline)
VStack(alignment: .leading, spacing: 6.0) {
Text("Background Type")
.font(.subheadline)
Picker("", selection: $backgroundType) {
EnumPickerOptionView(StatusBarNotificationBackgroundType.pill)
EnumPickerOptionView(StatusBarNotificationBackgroundType.fullWidth)
}.font(.subheadline).pickerStyle(.segmented)
}
.onChange(of: backgroundType) { _ in
showDefaultNotification(backgroundType == .pill ? "Ohhh so shiny!" : "I prefer classic…") { _ in }
NotificationPresenter.shared.dismiss(after: 2.0)
}
}
Section("Included Styles") {
includedStyleCell("Uh huh.", style: .defaultStyle)
includedStyleCell("It's time.", style: .light)
includedStyleCell("Don't mess with me!", style: .dark)
includedStyleCell("That's how we roll!", style: .success)
includedStyleCell("You know who I am!", style: .warning)
includedStyleCell("Uh oh, that didn't work..", style: .error)
includedStyleCell("Wake up Neo…", style: .matrix)
}
Section("SwiftUI Examples") {
Text("These SwiftUI examples ignore above subtitle, activity and progress bar settings.")
.font(.caption)
.foregroundStyle(.secondary)
cell(title: "Simple text", subtitle: "Display a SwiftUI text, 2.5s") {
let styleName = NotificationPresenter.shared.addStyle(named: "swiftui") { style in
style.backgroundStyle.backgroundType = backgroundType
style.backgroundStyle.backgroundColor = .systemMint
style.backgroundStyle.pillStyle.minimumWidth = 20.0
return style
}
NotificationPresenter.shared.presentSwiftView(styleName: styleName) {
Text("👋 This is SwiftUI!")
.font(.caption.bold())
.padding([.leading, .trailing], 20.0)
.fixedSize()
}
NotificationPresenter.shared.dismiss(after: 2.5)
}
cell(title: "Two row layout + Icon", subtitle: "Display SwiftUI text & image, 2.5s") {
let styleName = NotificationPresenter.shared.addStyle(named: "swiftui") { style in
style.backgroundStyle.backgroundType = backgroundType
style.backgroundStyle.backgroundColor = .systemIndigo
style.backgroundStyle.pillStyle.minimumWidth = 20.0
return style
}
NotificationPresenter.shared.presentSwiftView(styleName: styleName) {
HStack {
Spacer()
Image(systemName: "swift")
.foregroundColor(.white)
Spacer()
.frame(width: 10.0)
VStack(alignment: .leading, spacing: 0.0) {
Text("Swift Views!")
.font(.caption.bold())
.foregroundColor(.white)
Text("Easy custom layouts")
.font(.caption)
.foregroundColor(.white.opacity(0.5))
.lineLimit(1)
}
Spacer()
}
.padding([.leading, .trailing], 20.0)
.fixedSize()
}
NotificationPresenter.shared.dismiss(after: 2.5)
}
if #available(iOS 16.0, *) { // Gradient is iOS 16+
cell(title: "Gradient & Icon", subtitle: "A custom SwiftUI background, 2.5s") {
let styleName = NotificationPresenter.shared.addStyle(named: "gradient") { style in
style.backgroundStyle.backgroundType = backgroundType
style.backgroundStyle.backgroundColor = UIColor(.orange)
style.backgroundStyle.pillStyle.minimumWidth = 20.0
return style
}
NotificationPresenter.shared.presentSwiftView(styleName: styleName) {
let inner = VStack {
Spacer()
Image(systemName: "swift")
.foregroundStyle(.white)
Spacer()
}.padding([.leading, .trailing], 14.0)
Group {
if backgroundType == .fullWidth {
HStack {
Spacer()
inner
Spacer()
}
} else {
inner
}
}
.background(Gradient(colors: [.orange, .red]))
}
NotificationPresenter.shared.dismiss(after: 2.5)
}
}
}
Section("Custom Style Examples") {
Text("These examples showcase a progress bar animation and multiple animation types.")
.font(.caption)
.foregroundStyle(.secondary)
customStyleCell("Love it!", subtitle: "AnimationType.fade + Progress", style: .loveIt)
customStyleCell("Level Up!", subtitle: "AnimationType.bounce + Progress", style: .levelUp)
customStyleCell("Looks good", subtitle: "Subtitle + Activity", style: .looksGood)
customStyleCell("Small Pill", subtitle: "Modified pill size + Progress", style: .smallPill)
customStyleCell("Style Editor Style", subtitle: "Subtitle + Progress", style: .editor)
}
Section("Custom View Examples") {
cell(title: "Present a button", subtitle: "A custom notification view") {
// create button
let button = UIButton(type: .system, primaryAction: UIAction { _ in
NotificationPresenter.shared.dismiss()
})
button.setTitle("Dismiss!", for: .normal)
// present
let styleName = NotificationPresenter.shared.addStyle(named: "button") { style in
style.backgroundStyle.backgroundType = backgroundType
style.canTapToHold = false // this ensure the button can receive touches
return style
}
NotificationPresenter.shared.presentCustomView(button, styleName: styleName)
}
cell(title: "Present a custom left icon", subtitle: "A custom left view, 2.5s") {
// create icon
let image = UIImageView(image: UIImage(systemName: "gamecontroller.fill"))
// present
ExampleStyle.iconLeftView.register(for: backgroundType)
NotificationPresenter.shared.present("Player II", subtitle: "Connected", styleName: ExampleStyle.iconLeftView.rawValue)
NotificationPresenter.shared.displayLeftView(image)
NotificationPresenter.shared.dismiss(after: 2.5)
}
}
Section("Sequencing Example") {
cell(title: "2 notifications in sequence", subtitle: "Utilizing the completion block, 2 x 1s") {
showIncludedStyle("This is 1/2!", style: .dark)
NotificationPresenter.shared.displayActivityIndicator(true)
NotificationPresenter.shared.displayProgressBar(at: 0.0)
NotificationPresenter.shared.dismiss(after: 1.0) { presenter in
showIncludedStyle("✅ This is 2/2!", style: .dark)
NotificationPresenter.shared.displayActivityIndicator(false)
NotificationPresenter.shared.displayProgressBar(at: 0.0)
presenter.dismiss(after: 1.0)
}
}
}
Section("Queue") {
Button("Clear queue") {
NotificationPresenter.shared.clearQueue()
queueContent = NotificationPresenter.shared.queue
}
Button("Add to queue") {
NotificationPresenter.shared.addToQueue(notification: StatusBarNotification(title: "From queue", styleName: "default_sample"))
queueContent = NotificationPresenter.shared.queue
}
Button("Present first from queue") {
NotificationPresenter.shared.presentFromQueue(duration: 5.0)
if NotificationPresenter.shared.queue.count == 0 {
NotificationPresenter.shared.present("Queue is empty", styleName: "default_sample", duration: 5.0)
}
queueContent = NotificationPresenter.shared.queue
}
Button("Present queue") {
NotificationPresenter.shared.presentQueue()
queueContent = NotificationPresenter.shared.queue
}
if queueContent != nil {
Text("\(queueContent!.count) notification\(queueContent!.count == 1 ? "" : "s") in queue")
}
}
}
.navigationTitle(title)
.navigationBarTitleDisplayMode(.inline)
}
func cell(title: String, subtitle: String? = nil, useAccentColor: Bool = false, action: @escaping () -> ()) -> some View {
Button(action: action, label: {
HStack {
VStack(alignment: .leading) {
Text(title)
.font(.subheadline)
.foregroundColor(useAccentColor ? .accentColor : .primary)
if let subtitle = subtitle {
Text(subtitle)
.font(.caption2)
.foregroundColor(.secondary)
}
}
Spacer()
// a hack to get disclosure icons on these table rows
NavigationLink.empty
.frame(width: 30.0)
.foregroundColor(useAccentColor ? .accentColor : .secondary)
}
})
}
func includedStyleCell(_ text: String, style: IncludedStatusBarNotificationStyle) -> some View {
cell(title: "Present \(style.stringValue)", subtitle: "Duration: 3s") {
showIncludedStyle(text, style: style)
}
}
func customStyleCell(_ title: String, subtitle: String? = nil, style: ExampleStyle) -> some View {
let content = style.exampleContent
return cell(title: "Present: \(title)", subtitle: subtitle) {
style.register(for: backgroundType)
NotificationPresenter.shared.present(content.title, subtitle: content.subtitle, styleName: style.rawValue) { presenter in
presenter.animateProgressBar(to: 1.0, duration: animationDurationForCurrentStyle()) { presenter in
presenter.dismiss()
}
}
NotificationPresenter.shared.displayActivityIndicator(showActivity)
}
}
func animationDurationForCurrentStyle() -> Double {
switch backgroundType {
case .pill:
return 0.75
case .fullWidth:
fallthrough
default:
return 1.25
}
}
}
extension NavigationLink where Label == EmptyView, Destination == EmptyView {
static var empty: NavigationLink {
self.init(destination: EmptyView(), label: { EmptyView() })
}
}
struct ExamplesScreen_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
ExamplesScreen(title: "ExampleScreen")
}
}
}