Meshtastic-Apple/Meshtastic/Extensions/UserDefaults.swift

174 lines
4.8 KiB
Swift
Raw Normal View History

2023-04-25 17:56:57 -07:00
//
// UserDefaults.swift
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 4/24/23.
//
import Foundation
@propertyWrapper
struct UserDefault<T: Decodable> {
let key: UserDefaults.Keys
let defaultValue: T
init(_ key: UserDefaults.Keys, defaultValue: T) {
self.key = key
self.defaultValue = defaultValue
}
var wrappedValue: T {
get {
if defaultValue as? any RawRepresentable != nil {
let storedValue = UserDefaults.standard.object(forKey: key.rawValue)
guard let storedValue,
2024-03-26 19:40:48 -07:00
let jsonString = (storedValue as? String != nil) ? "\"\(storedValue)\"" : "\(storedValue)",
let data = jsonString.data(using: .utf8),
let value = (try? JSONDecoder().decode(T.self, from: data)) else { return defaultValue }
return value
}
return UserDefaults.standard.object(forKey: key.rawValue) as? T ?? defaultValue
}
set {
UserDefaults.standard.set((newValue as? any RawRepresentable)?.rawValue ?? newValue, forKey: key.rawValue)
}
}
}
2023-04-25 17:56:57 -07:00
extension UserDefaults {
enum Keys: String, CaseIterable {
case preferredPeripheralId
case preferredPeripheralNum
2023-04-25 17:56:57 -07:00
case provideLocation
case provideLocationInterval
case mapLayer
2024-03-24 22:23:55 -07:00
case meshMapDistance
case enableMapWaypoints
2023-04-25 17:56:57 -07:00
case meshMapRecentering
case meshMapShowNodeHistory
case meshMapShowRouteLines
case enableMapConvexHull
case enableMapRecentering
case enableMapNodeHistoryPins
case enableMapRouteLines
case enableMapTraffic
case enableMapPointsOfInterest
2023-05-06 16:15:12 -07:00
case enableOfflineMaps
case mapTileServer
case enableOverlayServer
case mapOverlayServer
case mapTilesAboveLabels
2023-11-11 09:58:54 -08:00
case mapUseLegacy
2023-12-01 13:56:29 -08:00
case enableDetectionNotifications
case detectionSensorRole
case enableSmartPosition
case newNodeNotifications
case lowBatteryNotifications
2024-04-26 18:06:23 -07:00
case channelMessageNotifications
2024-03-09 23:37:00 -08:00
case modemPreset
case firmwareVersion
2024-07-11 23:52:35 -07:00
case environmentEnableWeatherKit
2024-03-26 19:40:48 -07:00
case testIntEnum
2023-04-25 17:56:57 -07:00
}
func reset() {
Keys.allCases.forEach { removeObject(forKey: $0.rawValue) }
}
@UserDefault(.preferredPeripheralId, defaultValue: "")
static var preferredPeripheralId: String
@UserDefault(.preferredPeripheralNum, defaultValue: 0)
static var preferredPeripheralNum: Int
@UserDefault(.provideLocation, defaultValue: false)
static var provideLocation: Bool
2024-04-22 13:22:16 -07:00
@UserDefault(.provideLocationInterval, defaultValue: 30)
static var provideLocationInterval: Int
@UserDefault(.mapLayer, defaultValue: .standard)
static var mapLayer: MapLayer
@UserDefault(.meshMapDistance, defaultValue: 800000)
static var meshMapDistance: Double
@UserDefault(.enableMapWaypoints, defaultValue: false)
static var enableMapWaypoints: Bool
@UserDefault(.enableMapRecentering, defaultValue: false)
static var enableMapRecentering: Bool
@UserDefault(.enableMapNodeHistoryPins, defaultValue: false)
static var enableMapNodeHistoryPins: Bool
@UserDefault(.enableMapRouteLines, defaultValue: false)
static var enableMapRouteLines: Bool
@UserDefault(.enableMapConvexHull, defaultValue: false)
static var enableMapConvexHull: Bool
@UserDefault(.enableMapTraffic, defaultValue: false)
static var enableMapTraffic: Bool
@UserDefault(.enableMapPointsOfInterest, defaultValue: false)
static var enableMapPointsOfInterest: Bool
@UserDefault(.enableOfflineMaps, defaultValue: false)
static var enableOfflineMaps: Bool
@UserDefault(.mapTileServer, defaultValue: .openStreetMap)
static var mapTileServer: MapTileServer
@UserDefault(.enableOverlayServer, defaultValue: false)
static var enableOverlayServer: Bool
@UserDefault(.mapOverlayServer, defaultValue: .baseReReflectivityCurrent)
static var mapOverlayServer: MapOverlayServer
@UserDefault(.mapTilesAboveLabels, defaultValue: false)
static var mapTilesAboveLabels: Bool
@UserDefault(.mapUseLegacy, defaultValue: false)
static var mapUseLegacy: Bool
@UserDefault(.enableDetectionNotifications, defaultValue: false)
static var enableDetectionNotifications: Bool
@UserDefault(.detectionSensorRole, defaultValue: .sensor)
static var detectionSensorRole: DetectionSensorRole
@UserDefault(.enableSmartPosition, defaultValue: false)
static var enableSmartPosition: Bool
2024-04-26 18:06:23 -07:00
@UserDefault(.channelMessageNotifications, defaultValue: true)
static var channelMessageNotifications: Bool
@UserDefault(.newNodeNotifications, defaultValue: true)
static var newNodeNotifications: Bool
@UserDefault(.lowBatteryNotifications, defaultValue: true)
static var lowBatteryNotifications: Bool
@UserDefault(.modemPreset, defaultValue: 0)
static var modemPreset: Int
@UserDefault(.firmwareVersion, defaultValue: "0.0.0")
static var firmwareVersion: String
2024-08-04 21:42:02 -07:00
@UserDefault(.environmentEnableWeatherKit, defaultValue: true)
2024-07-11 23:52:35 -07:00
static var environmentEnableWeatherKit: Bool
2024-03-26 19:40:48 -07:00
@UserDefault(.testIntEnum, defaultValue: .one)
static var testIntEnum: TestIntEnum
}
enum TestIntEnum: Int, Decodable {
case one = 1
case two
case three
}