Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
main ▾

Modern-Apps.git / apple / FindFamily / FindFamilyiOS / UI / Cards.swift

Cards.swift206 lines · 7.0 KiBRaw
1import SwiftUI
2
3// MARK: - User picture
4
5struct UserPicture: View {
6 let user: User
7 let size: CGFloat
8
9 var body: some View {
10 ZStack {
11 if let photo = user.photo,
12 let data = photo.imageDataFromB64DataURI(),
13 let ui = UIImage(data: data) {
14 Image(uiImage: ui)
15 .resizable()
16 .scaledToFill()
17 } else {
18 Circle().fill(Color(.systemTeal))
19 Text(String(user.name.first ?? "?"))
20 .font(.system(size: size * 0.45, weight: .bold))
21 .foregroundStyle(.white)
22 }
23 }
24 .frame(width: size, height: size)
25 .clipShape(Circle())
26 .overlay(Circle().stroke(Color.accentColor, lineWidth: 2))
27 }
28}
29
30private extension String {
31 func imageDataFromB64DataURI() -> Data? {
32 guard let range = self.range(of: ";base64,") else { return nil }
33 let b64 = String(self[range.upperBound...])
34 return Data(base64Encoded: b64)
35 }
36}
37
38// MARK: - Battery bar
39
40struct BatteryBar: View {
41 let percent: Float
42 let width: CGFloat = 30
43 let height: CGFloat = 15
44
45 var fillColor: Color {
46 if percent > 50 { return .green }
47 if percent > 20 { return .yellow }
48 return .red
49 }
50
51 var body: some View {
52 HStack(spacing: 4) {
53 ZStack(alignment: .leading) {
54 RoundedRectangle(cornerRadius: 4)
55 .stroke(Color.accentColor, lineWidth: 2)
56 .frame(width: width, height: height)
57 RoundedRectangle(cornerRadius: 3)
58 .fill(fillColor)
59 .frame(width: max(0, width * CGFloat(percent / 100)), height: height)
60 }
61 Text(Strings.batteryPercentage(Int(percent)))
62 .font(.caption2)
63 }
64 }
65}
66
67// MARK: - User card
68
69struct UserCard: View {
70 let user: User
71 let latest: LocationValue?
72 let showSupporting: Bool
73 let onTap: () -> Void
74
75 var lastUpdatedString: String {
76 if let ts = latest?.timestamp { return TimeFormatting.timestring(ts, future: false) }
77 return Strings.lastUpdatedNever
78 }
79 var sinceString: String {
80 if user.locationName == "Unnamed Location" { return "" }
81 let since = Date().timeIntervalSince(user.lastLocationChangeTime)
82 if since < 60 { return Strings.sinceJustNow }
83 if since < 900 { return Strings.sinceMinutesAgo(Int64(since) / 60) }
84 let dayDiff = Calendar.current.dateComponents([.day], from: user.lastLocationChangeTime, to: Date()).day ?? 0
85 let datePart: String
86 switch dayDiff {
87 case 0: datePart = Strings.today
88 case 1: datePart = Strings.yesterday
89 default: datePart = TimeFormatting.dayMonth.string(from: user.lastLocationChangeTime)
90 }
91 return Strings.sinceTimeDate(TimeFormatting.amPmTime.string(from: user.lastLocationChangeTime), datePart)
92 }
93 var speedString: String {
94 TimeFormatting.formatSpeed(latest?.speed ?? 0)
95 }
96
97 var body: some View {
98 Button(action: showSupporting ? onTap : {}) {
99 HStack(alignment: .top, spacing: 12) {
100 VStack(spacing: 4) {
101 UserPicture(user: user, size: 56)
102 if let b = latest?.battery {
103 BatteryBar(percent: b)
104 }
105 }
106 VStack(alignment: .leading, spacing: 4) {
107 Text(user.name).font(.headline).lineLimit(1)
108 if showSupporting {
109 Text(Strings.userCardStatus(lastUpdatedString, user.locationName, sinceString))
110 .font(.caption)
111 .foregroundStyle(.secondary)
112 }
113 }
114 Spacer()
115 if showSupporting {
116 Text(speedString).font(.caption2).foregroundStyle(.secondary)
117 }
118 }
119 .padding(12)
120 .background(RoundedRectangle(cornerRadius: 12).fill(Color(.secondarySystemBackground)))
121 }
122 .buttonStyle(.plain)
123 .disabled(!showSupporting)
124 }
125}
126
127// MARK: - Awaiting request card
128
129struct AwaitingRequestCard: View {
130 let id: Int64
131 let onTap: () -> Void
132 var body: some View {
133 HStack {
134 Text(Strings.requestFrom(Base26.encode(id)))
135 Spacer()
136 Button(action: onTap) { Image(systemName: "plus.circle.fill").font(.title2) }
137 }
138 .padding(12)
139 .background(RoundedRectangle(cornerRadius: 12).fill(Color(.secondarySystemBackground)))
140 }
141}
142
143// MARK: - Temporary link card
144
145struct TemporaryLinkCard: View {
146 let link: TemporaryLink
147 var body: some View {
148 HStack {
149 VStack(alignment: .leading) {
150 Text(link.name).font(.subheadline.bold())
151 Text(Strings.expires(TimeFormatting.timestring(link.deleteAt, future: true)))
152 .font(.caption2)
153 .foregroundStyle(.secondary)
154 }
155 Spacer()
156 Button {
157 // The fragment carries the link's secret and never hits the server. New links
158 // send just the 32-byte ML-KEM seed (`#s=`) with a Base26 id, which fits in an
159 // SMS; links minted before that still carry their full private bundle.
160 if let seed = link.pqcSeed {
161 Platform.copy("https://api.maproj.org/view/\(Base26.encode(link.id))#s=\(seed)")
162 } else if let pqcKey = link.pqcKey {
163 Platform.copy("https://api.maproj.org/view/\(UInt64(bitPattern: link.id))#pqc_key=\(pqcKey)")
164 }
165 } label: { Image(systemName: "doc.on.doc") }
166 Button(role: .destructive) {
167 Database.shared.deleteTemporaryLink(link)
168 } label: { Image(systemName: "trash") }
169 }
170 .padding(12)
171 .background(RoundedRectangle(cornerRadius: 12).fill(Color(.secondarySystemBackground)))
172 }
173}
174
175// MARK: - Waypoint card
176
177struct WaypointCard: View {
178 let waypoint: Waypoint
179 let users: [User]
180 let onTap: () -> Void
181
182 var here: [User] { users.filter { $0.locationName == waypoint.name } }
183 var hereString: String {
184 switch here.count {
185 case 0: return Strings.nobodyHere
186 case 1: return Strings.userIsHere(here[0].name)
187 default: return Strings.usersAreHere(here.map { $0.name }.joined(separator: ", "))
188 }
189 }
190
191 var body: some View {
192 Button(action: onTap) {
193 HStack {
194 VStack(alignment: .leading) {
195 Text(waypoint.name).font(.subheadline.bold())
196 Text(hereString).font(.caption).foregroundStyle(.secondary)
197 }
198 Spacer()
199 Image(systemName: "square.and.pencil")
200 }
201 .padding(12)
202 .background(RoundedRectangle(cornerRadius: 12).fill(Color(.secondarySystemBackground)))
203 }
204 .buttonStyle(.plain)
205 }
206}