2023-04-25 17:56:57 -07:00
|
|
|
//
|
|
|
|
|
// UserDefaults.swift
|
|
|
|
|
// Meshtastic
|
|
|
|
|
//
|
|
|
|
|
// Copyright(c) Garth Vander Houwen 4/24/23.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
2024-03-26 16:45:32 -07:00
|
|
|
@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),
|
2024-03-26 16:45:32 -07:00
|
|
|
let value = (try? JSONDecoder().decode(T.self, from: data)) else { return defaultValue }
|
|
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
}
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2024-03-26 16:45:32 -07:00
|
|
|
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
|
2023-11-20 13:20:55 -08:00
|
|
|
case preferredPeripheralNum
|
2023-04-25 17:56:57 -07:00
|
|
|
case provideLocation
|
|
|
|
|
case provideLocationInterval
|
2023-05-07 08:01:16 -07:00
|
|
|
case mapLayer
|
2024-03-24 22:23:55 -07:00
|
|
|
case meshMapDistance
|
2024-03-25 18:43:03 -07:00
|
|
|
case enableMapWaypoints
|
2023-04-25 17:56:57 -07:00
|
|
|
case meshMapRecentering
|
|
|
|
|
case meshMapShowNodeHistory
|
|
|
|
|
case meshMapShowRouteLines
|
2023-09-17 19:42:03 -07:00
|
|
|
case enableMapConvexHull
|
2024-03-26 16:45:32 -07:00
|
|
|
case enableMapRecentering
|
|
|
|
|
case enableMapNodeHistoryPins
|
|
|
|
|
case enableMapRouteLines
|
2023-09-15 23:22:49 -07:00
|
|
|
case enableMapTraffic
|
|
|
|
|
case enableMapPointsOfInterest
|
2023-05-06 16:15:12 -07:00
|
|
|
case enableOfflineMaps
|
|
|
|
|
case mapTileServer
|
2024-03-26 16:45:32 -07:00
|
|
|
case enableOverlayServer
|
|
|
|
|
case mapOverlayServer
|
2023-05-07 08:01:16 -07:00
|
|
|
case mapTilesAboveLabels
|
2023-11-11 09:58:54 -08:00
|
|
|
case mapUseLegacy
|
2023-12-01 13:56:29 -08:00
|
|
|
case enableDetectionNotifications
|
|
|
|
|
case detectionSensorRole
|
2024-02-05 21:46:16 -08:00
|
|
|
case enableSmartPosition
|
2024-04-09 16:59:34 -07:00
|
|
|
case newNodeNotifications
|
2024-04-09 22:04:37 -07:00
|
|
|
case lowBatteryNotifications
|
2024-04-26 18:06:23 -07:00
|
|
|
case channelMessageNotifications
|
2024-03-09 23:37:00 -08:00
|
|
|
case modemPreset
|
2024-03-10 20:17:54 -07:00
|
|
|
case firmwareVersion
|
2024-07-11 23:52:35 -07:00
|
|
|
case environmentEnableWeatherKit
|
2024-08-18 17:15:55 -07:00
|
|
|
case enableAdministration
|
2025-04-28 19:59:32 -07:00
|
|
|
case mapReportingOptIn
|
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) }
|
|
|
|
|
}
|
2024-03-26 16:45:32 -07:00
|
|
|
|
|
|
|
|
@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)
|
2024-03-26 16:45:32 -07:00
|
|
|
static var provideLocationInterval: Int
|
|
|
|
|
|
|
|
|
|
@UserDefault(.mapLayer, defaultValue: .standard)
|
|
|
|
|
static var mapLayer: MapLayer
|
|
|
|
|
|
|
|
|
|
@UserDefault(.meshMapDistance, defaultValue: 800000)
|
|
|
|
|
static var meshMapDistance: Double
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2025-05-05 17:21:08 -07:00
|
|
|
@UserDefault(.enableMapWaypoints, defaultValue: true)
|
2024-03-26 16:45:32 -07:00
|
|
|
static var enableMapWaypoints: Bool
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2024-03-26 16:45:32 -07:00
|
|
|
@UserDefault(.enableMapRecentering, defaultValue: false)
|
|
|
|
|
static var enableMapRecentering: Bool
|
2024-03-25 18:43:03 -07:00
|
|
|
|
2024-03-26 16:45:32 -07:00
|
|
|
@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(.enableDetectionNotifications, defaultValue: false)
|
|
|
|
|
static var enableDetectionNotifications: Bool
|
|
|
|
|
|
|
|
|
|
@UserDefault(.detectionSensorRole, defaultValue: .sensor)
|
|
|
|
|
static var detectionSensorRole: DetectionSensorRole
|
|
|
|
|
|
|
|
|
|
@UserDefault(.enableSmartPosition, defaultValue: false)
|
|
|
|
|
static var enableSmartPosition: Bool
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2024-04-26 18:06:23 -07:00
|
|
|
@UserDefault(.channelMessageNotifications, defaultValue: true)
|
|
|
|
|
static var channelMessageNotifications: Bool
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2024-04-09 22:04:37 -07:00
|
|
|
@UserDefault(.newNodeNotifications, defaultValue: true)
|
2024-04-09 16:59:34 -07:00
|
|
|
static var newNodeNotifications: Bool
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2024-04-09 22:04:37 -07:00
|
|
|
@UserDefault(.lowBatteryNotifications, defaultValue: true)
|
|
|
|
|
static var lowBatteryNotifications: Bool
|
2024-03-26 16:45:32 -07:00
|
|
|
|
|
|
|
|
@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
|
|
|
|
2024-07-13 08:06:10 -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
|
|
|
|
2024-08-18 17:15:55 -07:00
|
|
|
@UserDefault(.enableAdministration, defaultValue: false)
|
|
|
|
|
static var enableAdministration: Bool
|
|
|
|
|
|
2025-04-28 19:59:32 -07:00
|
|
|
@UserDefault(.mapReportingOptIn, defaultValue: false)
|
|
|
|
|
static var mapReportingOptIn: 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
|
2024-03-26 16:45:32 -07:00
|
|
|
}
|