forked from cristianoalves92/react-native-live-activity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiveActivityDynamicIsland.swift
More file actions
96 lines (90 loc) · 2.61 KB
/
LiveActivityDynamicIsland.swift
File metadata and controls
96 lines (90 loc) · 2.61 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
//
// LiveActivityDynamicIsland.swift
// LiveActivityDynamicIsland
//
// Created by Cristiano Alves on 19/09/2022.
//
import WidgetKit
import SwiftUI
import ActivityKit
struct MyActivityAttributes: ActivityAttributes {
public struct ContentState: Codable, Hashable {
var data: String
}
}
struct Information: Codable {
let status: String
let driverName: String
let expectedDeliveryTime: String
}
// Data are sent as a string, so we need to convert it to a struct
func toJson(dataString: String) -> Information {
let decoder = JSONDecoder()
let stateData = Data(dataString.utf8);
let data = try? decoder.decode(Information.self, from: stateData)
if (data == nil) {
NSLog("Error: %@ %@", "Data is null");
return Information(status: "No status", driverName: "No Driver Name", expectedDeliveryTime: "00:00")
}
return data ?? Information(status: "No status", driverName: "No Driver Name", expectedDeliveryTime: "00:00");
}
@main
@available(iOS 16.1, *)
struct LiveActivityDynamicIsland: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: MyActivityAttributes.self) { context in
let data = toJson(dataString: context.state.data)
return HStack {
Image(systemName: "bicycle")
.foregroundColor(.blue)
.padding(8)
VStack(alignment: .leading) {
Text(data.status)
.font(.body)
Text(data.driverName)
}
Spacer()
VStack(alignment: .trailing) {
Text("Deliver at")
.font(.body)
Text(data.expectedDeliveryTime)
.font(.footnote)
}
.padding(8)
}
.padding(8)
} dynamicIsland: { context in
let data = toJson(dataString: context.state.data)
return DynamicIsland {
DynamicIslandExpandedRegion(.leading) {
Image(systemName: "bicycle")
.foregroundColor(.blue)
.padding(8)
}
DynamicIslandExpandedRegion(.trailing) {
VStack(alignment: .leading) {
Text("Deliver at")
.font(.body)
Text(data.expectedDeliveryTime)
.font(.footnote)
}
}
DynamicIslandExpandedRegion(.center) {
Text(data.status)
.font(.body)
}
DynamicIslandExpandedRegion(.bottom) {
Text(data.driverName)
}
} compactLeading: {
Image(systemName: "bicycle")
.foregroundColor(.blue)
} compactTrailing: {
Text(data.expectedDeliveryTime)
} minimal: {
Text(data.expectedDeliveryTime)
}
.keylineTint(.yellow)
}
}
}