Repository navigation
Expand file tree
/
Copy pathControllersWidget.swift
More file actions
384 lines (349 loc) · 13.6 KB
/
Copy pathControllersWidget.swift
File metadata and controls
384 lines (349 loc) · 13.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
//
// ControllersWidget.swift
// ControllersWidget
//
// Created by 강동영 on 2023/06/07.
//
import WidgetKit
import SwiftUI
import ControllerKit
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> ControllerEntry {
.sample
}
func getSnapshot(in context: Context, completion: @escaping (ControllerEntry) -> ()) {
// 위젯 갤러리에서는 실제 상태 대신 샘플을 보여 준다.
completion(context.isPreview ? .sample : currentEntry())
}
func getTimeline(in context: Context, completion: @escaping (Timeline<ControllerEntry>) -> ()) {
// 상태가 바뀌면 앱이 타임라인을 다시 읽게 하므로(WidgetSharedState) 여기서는 30분마다 다시 확인만 한다.
let entry = currentEntry()
completion(Timeline(entries: [entry], policy: .after(entry.date.addingTimeInterval(30 * 60))))
}
/// 위젯 프로세스에서 컨트롤러를 직접 읽을 수 있으면 그 값을, 아니면 앱이 App Group 에 남긴 값을 쓴다.
private func currentEntry() -> ControllerEntry {
let defaults = UserDefaults.shared
let updated = defaults.double(forKey: StringKey.LAST_UPDATED)
var entry = ControllerEntry(date: Date(),
isConnected: defaults.bool(forKey: StringKey.CONTROLLER_CONNECTED),
batteryLevel: defaults.float(forKey: StringKey.BATTERY_LEVEL),
isCharging: defaults.integer(forKey: StringKey.BATTERY_STATE) == BatteryState.charging.rawValue,
hasBatteryInfo: defaults.integer(forKey: StringKey.BATTERY_STATE) != BatteryState.unknown.rawValue,
vendorName: defaults.string(forKey: StringKey.VENDOR_NAME),
lastUpdated: updated > 0 ? Date(timeIntervalSince1970: updated) : nil)
if let info = GameControllerManager.shared.getBatteryInfo(), info.state != BatteryState.unknown.rawValue {
entry.isConnected = true
entry.batteryLevel = info.level
entry.isCharging = info.state == BatteryState.charging.rawValue
entry.hasBatteryInfo = true
entry.lastUpdated = entry.date
}
return entry
}
}
struct ControllerEntry: TimelineEntry {
let date: Date
var isConnected: Bool
var batteryLevel: Float
var isCharging: Bool
/// 배터리 상태가 unknown(-1)인 컨트롤러(가상 게임패드, 일부 비공식 컨트롤러)는 잔량을 믿을 수 없다. 0%라도 state 가 있으면 진짜 값이다.
var hasBatteryInfo = true
var vendorName: String?
var lastUpdated: Date?
static var sample: ControllerEntry {
ControllerEntry(date: Date(), isConnected: true, batteryLevel: 0.76, isCharging: false,
vendorName: "DualSense Wireless Controller", lastUpdated: Date())
}
/// 잔량을 그려도 되는 상태
var showsLevel: Bool { isConnected && hasBatteryInfo }
var percentText: String {
"\(Int(batteryLevel * 100))%"
}
var nameText: Text {
if let vendorName, !vendorName.isEmpty { return Text(verbatim: vendorName) }
return Text("widget.controller")
}
/// 좁은 곳(잠금 화면 직사각형)용 이름. "DualSense Wireless Controller" → "DualSense"
var shortNameText: Text {
guard let vendorName else { return nameText }
let short = [" Wireless Controller", " Controller"].reduce(vendorName) { name, suffix in
name.hasSuffix(suffix) ? String(name.dropLast(suffix.count)) : name
}
return short.isEmpty ? nameText : Text(verbatim: short)
}
/// "5분 전 갱신"
var updatedText: Text? {
lastUpdated.map { Text("widget.updated \(Text($0, style: .relative))") }
}
/// 미연결일 때 마지막으로 알던 잔량 "마지막: 42%"
var lastLevelText: Text? {
lastUpdated == nil ? nil : Text("widget.last \(percentText)")
}
}
// MARK: - Views
struct ControllersWidgetEntryView: View {
@Environment(\.widgetFamily) private var family
let entry: ControllerEntry
var body: some View {
if #available(iOS 17.0, *) {
content.containerBackground(for: .widget) { Color(UIColor.systemBackground) }
} else if family.isSystem {
content.padding()
} else {
content
}
}
@ViewBuilder private var content: some View {
switch family {
case .systemSmall:
SmallWidgetView(entry: entry)
case .systemMedium:
MediumWidgetView(entry: entry)
case .systemLarge:
LargeWidgetView(entry: entry)
default:
if #available(iOS 15.0, *), family == .systemExtraLarge {
LargeWidgetView(entry: entry)
} else
if #available(iOS 16.0, *) {
AccessoryWidgetView(entry: entry)
}
}
}
}
private extension WidgetFamily {
var isSystem: Bool {
if #available(iOS 15.0, *), self == .systemExtraLarge { return true }
return self == .systemSmall || self == .systemMedium || self == .systemLarge
}
}
/// 잔량 링과 가운데 퍼센트. 미연결이면 흐린 컨트롤러 아이콘만 그린다.
private struct BatteryRingView: View {
let entry: ControllerEntry
var lineWidth: CGFloat = 8
var iconFont: Font = .caption
var percentFont: Font = .title2.bold()
var body: some View {
ZStack {
ProgressBar(progress: entry.showsLevel ? entry.batteryLevel : 0, lineWidth: lineWidth, color: color)
if entry.showsLevel {
VStack(spacing: 0) {
Image(systemName: entry.isCharging ? "bolt.fill" : "gamecontroller.fill")
.font(iconFont)
Text(verbatim: entry.percentText)
.font(percentFont)
.lineLimit(1)
.minimumScaleFactor(0.6)
}
} else {
Image(systemName: entry.isConnected ? "gamecontroller.fill" : "gamecontroller")
.font(.title)
.foregroundColor(.secondary)
}
}
}
/// 충전 알림 기준값 이하면 빨간 링으로 보여 준다.
private var color: Color {
guard entry.showsLevel else { return .gray }
return entry.batteryLevel * 100 <= Float(UserDefaults.shared.batteryNotificationThreshold) ? .red : .green
}
}
private struct SmallWidgetView: View {
let entry: ControllerEntry
var body: some View {
VStack(spacing: 6) {
BatteryRingView(entry: entry)
.frame(width: 84, height: 84)
if entry.showsLevel {
entry.updatedText?
.font(.caption2)
.foregroundColor(.secondary)
} else if entry.isConnected {
Text("widget.noBatteryInfo")
.font(.caption.bold())
} else {
Text("widget.notConnected")
.font(.caption.bold())
entry.lastLevelText?
.font(.caption2)
.foregroundColor(.secondary)
}
}
.lineLimit(1)
.minimumScaleFactor(0.8)
}
}
private struct MediumWidgetView: View {
let entry: ControllerEntry
var body: some View {
HStack(spacing: 16) {
BatteryRingView(entry: entry)
.frame(width: 100, height: 100)
VStack(alignment: .leading, spacing: 4) {
entry.nameText
.font(.headline)
if entry.showsLevel {
Text(verbatim: entry.percentText)
.font(.largeTitle.bold())
Group {
if entry.isCharging {
Label("widget.charging", systemImage: "bolt.fill")
} else {
Text("widget.notCharging")
}
}
.font(.subheadline)
} else if entry.isConnected {
Text("widget.noBatteryInfo")
.font(.title3.bold())
} else {
Text("widget.notConnected")
.font(.title3.bold())
entry.lastLevelText?
.font(.subheadline)
.foregroundColor(.secondary)
}
entry.updatedText?
.font(.caption2)
.foregroundColor(.secondary)
}
.lineLimit(1)
Spacer(minLength: 0)
}
}
}
/// large·extraLarge 공용. 큰 링 아래에 이름·상태·갱신 시각을 쌓는다.
private struct LargeWidgetView: View {
let entry: ControllerEntry
var body: some View {
VStack(spacing: 12) {
entry.nameText
.font(.title3.bold())
BatteryRingView(entry: entry, lineWidth: 16, iconFont: .title3, percentFont: .system(size: 44, weight: .bold))
.frame(width: 190, height: 190)
Group {
if entry.showsLevel {
if entry.isCharging {
Label("widget.charging", systemImage: "bolt.fill")
} else {
Text("widget.notCharging")
}
} else if entry.isConnected {
Text("widget.noBatteryInfo")
} else {
Text("widget.notConnected")
entry.lastLevelText?
.foregroundColor(.secondary)
}
}
.font(.headline)
entry.updatedText?
.font(.footnote)
.foregroundColor(.secondary)
}
.lineLimit(1)
.minimumScaleFactor(0.8)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
@available(iOS 16.0, *)
private struct AccessoryWidgetView: View {
@Environment(\.widgetFamily) private var family
let entry: ControllerEntry
var body: some View {
switch family {
case .accessoryCircular:
circular
case .accessoryRectangular:
rectangular
default:
inline
}
}
@ViewBuilder private var circular: some View {
if entry.showsLevel {
Gauge(value: Double(entry.batteryLevel)) {
Image(systemName: "gamecontroller.fill")
} currentValueLabel: {
VStack(spacing: 0) {
if entry.isCharging {
Image(systemName: "bolt.fill")
.font(.system(size: 9))
}
Text(verbatim: entry.percentText)
.minimumScaleFactor(0.5)
}
}
.gaugeStyle(.accessoryCircularCapacity)
} else {
ZStack {
AccessoryWidgetBackground()
VStack(spacing: 0) {
Image(systemName: "gamecontroller")
Text(verbatim: "—")
}
}
}
}
private var rectangular: some View {
VStack(alignment: .leading, spacing: 2) {
Label { entry.shortNameText } icon: { Image(systemName: "gamecontroller.fill") }
.font(.headline)
if entry.showsLevel {
HStack(spacing: 2) {
Text(verbatim: entry.percentText)
if entry.isCharging {
Image(systemName: "bolt.fill")
}
}
Gauge(value: Double(entry.batteryLevel)) { EmptyView() }
.gaugeStyle(.accessoryLinearCapacity)
} else if entry.isConnected {
Text("widget.noBatteryInfo")
} else {
Text("widget.notConnected")
entry.lastLevelText?
.foregroundColor(.secondary)
}
}
.lineLimit(1)
.frame(maxWidth: .infinity, alignment: .leading)
}
@ViewBuilder private var inline: some View {
if entry.showsLevel {
Label {
entry.isCharging
? Text(verbatim: entry.percentText) + Text(Image(systemName: "bolt.fill"))
: Text(verbatim: entry.percentText)
} icon: {
Image(systemName: "gamecontroller.fill")
}
} else if entry.isConnected {
Label("widget.noBatteryInfo", systemImage: "gamecontroller.fill")
} else {
Label("widget.notConnected", systemImage: "gamecontroller")
}
}
}
struct ControllersWidget: Widget {
var body: some WidgetConfiguration {
StaticConfiguration(kind: StringKey.WIDGET_KIND, provider: Provider()) { entry in
ControllersWidgetEntryView(entry: entry)
}
.configurationDisplayName("widget.name")
.description("widget.description")
.supportedFamilies(supportedFamilies)
}
private var supportedFamilies: [WidgetFamily] {
var families: [WidgetFamily] = [.systemSmall, .systemMedium, .systemLarge]
if #available(iOS 15.0, *) { families.append(.systemExtraLarge) }
if #available(iOS 16.0, *) { families += [.accessoryCircular, .accessoryRectangular, .accessoryInline] }
return families
}
}
struct ControllersWidget_Previews: PreviewProvider {
static var previews: some View {
ControllersWidgetEntryView(entry: .sample)
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}