forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharedDefaults.swift
More file actions
43 lines (39 loc) · 1.75 KB
/
Copy pathSharedDefaults.swift
File metadata and controls
43 lines (39 loc) · 1.75 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
import Foundation
/// App Group identifier shared between the main app and the widget extension.
let appGroupIdentifier = "group.com.friend-app-with-wearable.ios12"
/// Keys used to store device battery data in the shared UserDefaults.
enum BatteryWidgetKeys {
static let deviceName = "widget_device_name"
static let batteryLevel = "widget_battery_level"
static let deviceType = "widget_device_type"
static let isConnected = "widget_is_connected"
static let lastUpdated = "widget_last_updated"
static let isMuted = "widget_is_muted"
}
/// Model representing the device battery state shown in the widget.
struct DeviceBatteryInfo {
let deviceName: String
let batteryLevel: Int
let deviceType: String
let isConnected: Bool
let lastUpdated: Date
let isMuted: Bool
/// Reads the latest device battery info from the shared App Group UserDefaults.
static func fromSharedDefaults() -> DeviceBatteryInfo {
let defaults = UserDefaults(suiteName: appGroupIdentifier)
let name = defaults?.string(forKey: BatteryWidgetKeys.deviceName) ?? "Omi"
let battery = defaults?.integer(forKey: BatteryWidgetKeys.batteryLevel) ?? -1
let type = defaults?.string(forKey: BatteryWidgetKeys.deviceType) ?? "omi"
let connected = defaults?.bool(forKey: BatteryWidgetKeys.isConnected) ?? false
let updated = defaults?.object(forKey: BatteryWidgetKeys.lastUpdated) as? Date ?? Date.distantPast
let muted = defaults?.bool(forKey: BatteryWidgetKeys.isMuted) ?? false
return DeviceBatteryInfo(
deviceName: name,
batteryLevel: battery,
deviceType: type,
isConnected: connected,
lastUpdated: updated,
isMuted: muted
)
}
}