Repository navigation
Expand file tree
/
Copy pathWidgetDemo.swift
More file actions
470 lines (452 loc) · 21.6 KB
/
Copy pathWidgetDemo.swift
File metadata and controls
470 lines (452 loc) · 21.6 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//
// WidgetDemo.swift
// WidgetDemo
//
// Created by Ming on 28/7/2022.
//
import ActivityKit
import WidgetKit
import SwiftUI
@main
struct Widgets: WidgetBundle {
var body: some Widget {
PizzaDeliveryActivityWidget()
PizzaAdActivityWidget()
}
}
/// Pure function of `state` + wall-clock time. This is the key:
/// because the widget body is re-evaluated when `staleDate` passes,
/// this check will flip from `false` → `true` on its own — no app code,
/// no push — and the widget swaps to its "done" layout.
private func isDelivered(_ context: ActivityViewContext<PizzaDeliveryAttributes>) -> Bool {
context.isStale || Date() >= context.state.estimatedDeliveryTime.upperBound
}
/// Warehouse (orange) before the fill reaches it, ✓ (green) after.
/// Driven by the `activity.update()` push that the app's keep-alive
/// timer fires at the delivery midpoint — TimelineView alone is not
/// reliable for Live Activity re-renders once the phone is locked.
@ViewBuilder
private func midpointIcon(for range: ClosedRange<Date>) -> some View {
let midpoint = range.lowerBound.addingTimeInterval(
range.upperBound.timeIntervalSince(range.lowerBound) / 2
)
if Date() >= midpoint {
Image(systemName: "checkmark.circle.fill")
.resizable()
.scaledToFit()
.foregroundColor(.green)
.background(Circle().fill(.white))
.frame(width: 28, height: 28)
} else {
Image(systemName: "building.2.crop.circle.fill")
.resizable()
.scaledToFit()
.foregroundColor(.orange)
.background(Circle().fill(.white))
.frame(width: 28, height: 28)
}
}
// Note on the progress bar:
// In Live Activities, `ProgressView(timerInterval:)` only auto-updates when
// rendered with a *built-in* style (`.linear`). Custom `ProgressViewStyle`s
// receive `fractionCompleted` once at body evaluation and never update again,
// which is why the previous custom style stayed frozen at its starting width.
// We use the built-in linear style and compose the rounded track + icon
// overlay around it.
/// Historical wrapper used across all Dynamic Island regions. The stale/timeline/
/// both experiments are gone, so it's now a passthrough — left in place to keep
/// the widget body diff small. Safe to inline away later.
@ViewBuilder
private func timelineWrapped<Content: View>(
context: ActivityViewContext<PizzaDeliveryAttributes>,
@ViewBuilder content: @escaping () -> Content
) -> some View {
content()
}
struct PizzaDeliveryActivityWidget: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: PizzaDeliveryAttributes.self) { context in
timelineWrapped(context: context) {
// MARK: - For devices that don't support the Dynamic Island.
if isDelivered(context) {
// End-state: driver message is the headline. Package has
// met home → they collapse into a single ✓ at the right
// end of the (now fully blue) bar. Paid-footer text is
// kept to mirror the in-progress layout so the transition
// doesn't shrink the widget.
VStack(alignment: .leading) {
Text("\(context.state.driverName) dropped off your order! 📦")
.font(.headline)
.padding(.horizontal, 5)
HStack {
ZStack {
Capsule()
.fill(
LinearGradient(
colors: [.blue, .green],
startPoint: .leading,
endPoint: .trailing
)
)
.frame(height: 28)
// Delivered: single ✓ at the right edge.
HStack {
Spacer()
Image(systemName: "checkmark.circle.fill")
.resizable()
.scaledToFit()
.foregroundColor(.green)
.background(Circle().fill(.white))
.frame(width: 28, height: 28)
}
}
.frame(height: 32)
Spacer()
Image(context.state.driverName)
.resizable()
.scaledToFill()
.frame(width: 44, height: 44)
.clipShape(Circle())
.overlay(Circle().stroke(.white, lineWidth: 2))
}.padding(5)
Text("You've already paid: \(context.attributes.totalAmount) + $9.9 Delivery Fee 💸")
.font(.caption)
.foregroundColor(.secondary)
.padding(.horizontal, 5)
}.padding(15)
} else {
VStack(alignment: .leading) {
if context.state.driverName == "Tim" {
Text("Your \(context.state.driverName) is on the way!")
.font(.headline)
.padding(.horizontal, 5)
} else {
Text("Apple reassigned \(context.state.driverName) to deliver your order")
.font(.headline)
.padding(.horizontal, 5)
}
HStack {
VStack(alignment: .leading) {
// Bar stack:
// - Gray track (full width, height 28pt to fit icons)
// - Blue fill: `ProgressView(timerInterval:)` + `.linear`
// is the ONLY widget primitive iOS interpolates
// frame-to-frame — stays buttery smooth.
// - Package (left) + home (right) sit ON the bar;
// both are 28pt so they match the bar height
// exactly (no icons-taller-than-bar gap).
// - Package is static: Live Activity widgets are
// snapshot-rendered with strict re-render budgets,
// so nothing except the ProgressView fill can
// animate smoothly. The blue fill sweeping across
// the package → home IS the progress visual.
// At endDate the delivered layout renders a
// single ✓ where the two icons "meet".
ZStack {
Capsule()
.fill(Color.secondary.opacity(0.25))
.frame(height: 28)
ProgressView(timerInterval: context.state.estimatedDeliveryTime, countsDown: false) {
EmptyView()
} currentValueLabel: {
EmptyView()
}
.tint(
LinearGradient(
colors: [.blue, .green],
startPoint: .leading,
endPoint: .trailing
)
)
.progressViewStyle(.linear)
.scaleEffect(x: 1, y: 7, anchor: .center)
.frame(height: 28)
.clipShape(Capsule())
// Dead-center warehouse trick: split the bar
// into two equal halves via `maxWidth: .infinity`
// with the warehouse icon sitting in the seam.
// Each half gets (barWidth - 28)/2, so the
// warehouse's center lands exactly on barWidth/2
// regardless of how wide the timer text is.
HStack(spacing: 0) {
// Left half: package pinned left, timer pinned right
HStack {
Image(systemName: "shippingbox.circle.fill")
.resizable()
.scaledToFit()
.foregroundColor(.white)
.frame(width: 28, height: 28)
Spacer()
Text(timerInterval: context.state.estimatedDeliveryTime, countsDown: true)
.bold()
.font(.caption)
.foregroundColor(.white)
.monospacedDigit()
.padding(.trailing, 4)
}
.frame(maxWidth: .infinity)
// Warehouse → ✓ swap at midpoint. Driven by
// the app's keep-alive push; see helper.
midpointIcon(for: context.state.estimatedDeliveryTime)
// Right half: home pinned right
HStack {
Spacer()
Image(systemName: "house.circle.fill")
.resizable()
.scaledToFit()
.foregroundColor(.green)
.background(Circle().fill(.white))
.frame(width: 28, height: 28)
}
.frame(maxWidth: .infinity)
}
}
.frame(height: 32)
}
Spacer()
VStack {
Image(context.state.driverName)
.resizable()
.scaledToFill()
.frame(width: 44, height: 44)
.clipShape(Circle())
.overlay(Circle().stroke(.white, lineWidth: 2))
Spacer()
}
}.padding(5)
Text("You've already paid: \(context.attributes.totalAmount) + $9.9 Delivery Fee 💸")
.font(.caption)
.foregroundColor(.secondary)
.padding(.horizontal, 5)
}.padding(15)
}
} // end timelineWrapped
// MARK: - For Dynamic Island
} dynamicIsland: { context in
// NOTE: the `delivered` flag is NOT captured once at the top of the
// closure — each region re-evaluates it inside its own TimelineView
// so the DI regions flip the instant `endDate` is reached even when
// the app is suspended and `staleDate` hasn't fired yet.
DynamicIsland {
DynamicIslandExpandedRegion(.leading) {
timelineWrapped(context: context) {
if isDelivered(context) {
Label("Delivered", systemImage: "checkmark.circle.fill")
.font(.title3)
.foregroundColor(.green)
} else {
Label("\(context.attributes.numberOfPizzas) Pizza", systemImage: "bag")
.font(.title3)
}
}
}
DynamicIslandExpandedRegion(.trailing) {
timelineWrapped(context: context) {
if isDelivered(context) {
Label("Done", systemImage: "checkmark.circle.fill")
.font(.title2)
.foregroundColor(.green)
} else {
Label {
Text(timerInterval: context.state.estimatedDeliveryTime, countsDown: true)
.multilineTextAlignment(.trailing)
.frame(width: 50)
.monospacedDigit()
.font(.caption2)
} icon: {
Image(systemName: "timer")
}
.font(.title2)
}
}
}
DynamicIslandExpandedRegion(.center) {
timelineWrapped(context: context) {
if isDelivered(context) {
Text("Enjoy your pizza! 🍕")
.lineLimit(1)
.font(.caption)
.foregroundColor(.secondary)
} else {
VStack(spacing: 5) {
Text("\(context.state.driverName) is on the way!")
.lineLimit(1)
.font(.caption)
.foregroundColor(.secondary)
ProgressView(timerInterval: context.state.estimatedDeliveryTime, countsDown: false)
.tint(.blue)
.progressViewStyle(.linear)
}
}
}
}
DynamicIslandExpandedRegion(.bottom) {
timelineWrapped(context: context) {
if isDelivered(context) {
HStack {
Spacer()
Label("Rate your order", systemImage: "star.fill")
.font(.caption)
.padding()
.background(Color.yellow.opacity(0.3))
.clipShape(RoundedRectangle(cornerRadius: 15))
Spacer()
}
} else {
// Deep Linking
HStack {
Link(destination: URL(string: "pizza://contact+TIM")!) {
Label("Contact driver", systemImage: "phone.circle.fill")
.font(.caption)
.padding()
}.background(Color.accentColor)
.clipShape(RoundedRectangle(cornerRadius: 15))
Spacer()
Link(destination: URL(string: "pizza://cancelOrder")!) {
Label("Cancel Order", systemImage: "xmark.circle.fill")
.font(.caption)
.padding()
}.background(Color.red)
.clipShape(RoundedRectangle(cornerRadius: 15))
}
}
}
}
} compactLeading: {
timelineWrapped(context: context) {
if isDelivered(context) {
Image(systemName: "checkmark.circle.fill").foregroundColor(.green)
} else {
Label {
Text("\(context.attributes.numberOfPizzas) Pizza")
} icon: {
Image(systemName: "bag")
}
.font(.caption2)
}
}
} compactTrailing: {
timelineWrapped(context: context) {
if isDelivered(context) {
Text("Done").font(.caption2).foregroundColor(.green)
} else {
Text(timerInterval: context.state.estimatedDeliveryTime, countsDown: true)
.multilineTextAlignment(.center)
.frame(width: 40)
.font(.caption2)
}
}
} minimal: {
timelineWrapped(context: context) {
if isDelivered(context) {
Image(systemName: "checkmark.circle.fill").foregroundColor(.green)
} else {
VStack(alignment: .center) {
Image(systemName: "timer")
Text(timerInterval: context.state.estimatedDeliveryTime, countsDown: true)
.multilineTextAlignment(.center)
.monospacedDigit()
.font(.caption2)
}
}
}
}
.keylineTint(.accentColor)
}
}
}
struct PizzaAdActivityWidget: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: PizzaAdAttributes.self) { context in
HStack {
let logo = UserDefaults(suiteName: "group.io.startway.iOS16-Live-Activities")?.data(forKey: "pizzaLogo")
if (logo != nil) {
Image(uiImage: UIImage(data: logo!)!)
.resizable()
.scaledToFit()
.frame(width: 64, height: 64)
.cornerRadius(15)
}
Spacer()
VStack(alignment: .leading) {
Text(context.state.adName).font(.caption).foregroundColor(.secondary)
Text("Get \(Text(context.attributes.discount).fontWeight(.black).foregroundColor(.blue)) OFF").bold().font(.system(size: 25)).foregroundColor(.secondary)
Text("when purchase 🍕 every $500").font(.callout).italic().lineLimit(1)
}.padding(.trailing)
}.padding()
} dynamicIsland: { context in
DynamicIsland {
DynamicIslandExpandedRegion(.leading) {
Label(context.attributes.discount, systemImage: "dollarsign.arrow.circlepath")
.font(.title2)
}
DynamicIslandExpandedRegion(.trailing) {
Label {
Text("Ads")
.multilineTextAlignment(.trailing)
.frame(width: 50)
.monospacedDigit()
.font(.caption2)
} icon: {
Image(systemName: "dollarsign.circle.fill")
}
.font(.title2)
}
DynamicIslandExpandedRegion(.center) {
Text(context.state.adName)
.lineLimit(1)
.font(.caption)
}
DynamicIslandExpandedRegion(.bottom) {
Button {
// Deep link into the app.
} label: {
Label("Pay now", systemImage: "creditcard")
}
}
} compactLeading: {
Label {
Text(context.attributes.discount)
} icon: {
Image(systemName: "dollarsign.circle.fill")
}
.font(.caption2)
.foregroundColor(.red)
} compactTrailing: {
Text("Due")
.multilineTextAlignment(.center)
.frame(width: 40)
.font(.caption2)
} minimal: {
VStack(alignment: .center) {
Image(systemName: "dollarsign.circle.fill")
Text(context.attributes.discount)
.multilineTextAlignment(.center)
.monospacedDigit()
.font(.caption2)
}
}
.keylineTint(.accentColor)
}
}
}
// Preview available on iOS 16.2 or above
@available(iOSApplicationExtension 16.2, *)
struct PizzaDeliveryActivityWidget_Previews: PreviewProvider {
static let activityAttributes = PizzaDeliveryAttributes(numberOfPizzas: 2, totalAmount: "1000")
static let activityState = PizzaDeliveryAttributes.ContentState(driverName: "Tim", estimatedDeliveryTime: Date()...Date().addingTimeInterval(15 * 60))
static var previews: some View {
activityAttributes
.previewContext(activityState, viewKind: .content)
.previewDisplayName("Notification")
activityAttributes
.previewContext(activityState, viewKind: .dynamicIsland(.compact))
.previewDisplayName("Compact")
activityAttributes
.previewContext(activityState, viewKind: .dynamicIsland(.expanded))
.previewDisplayName("Expanded")
activityAttributes
.previewContext(activityState, viewKind: .dynamicIsland(.minimal))
.previewDisplayName("Minimal")
}
}