Meshtastic-Apple/Meshtastic/Extensions/UserDefaults.swift
Blake McAnally 4d547e48db This change fixes several lint errors throughout the project, and moves the SwiftLint build phase to before compilation.
After this change, a developer can now clone the project and run without the build failing due to lint errors! 😃

* I ran `swiftlint --fix` to resolve many auto-correctable issues (mostly whitespace)
* Excluded the `Meshtastic/Protobufs` directory from lint, since that code is automatically generated.
* Converted some single letter method parameters to lowercase.
* Converted several instances `force_cast` to instead use `guard` or `if let` to unwrap optional values. During this change, some of the SwiftUI views became "too complex to be solved in a reasonable time", so I broke up the views into distinct sub-expressions.

I was able to build and run the app on an iOS simulator.
2024-05-31 21:48:50 -05:00

173 lines
4.8 KiB
Swift

//
// 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,
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)
}
}
}
extension UserDefaults {
enum Keys: String, CaseIterable {
case preferredPeripheralId
case preferredPeripheralNum
case provideLocation
case provideLocationInterval
case mapLayer
case meshMapDistance
case enableMapWaypoints
case meshMapRecentering
case meshMapShowNodeHistory
case meshMapShowRouteLines
case enableMapConvexHull
case enableMapRecentering
case enableMapNodeHistoryPins
case enableMapRouteLines
case enableMapTraffic
case enableMapPointsOfInterest
case enableOfflineMaps
case enableOfflineMapsMBTiles
case mapTileServer
case enableOverlayServer
case mapOverlayServer
case mapTilesAboveLabels
case mapUseLegacy
case enableDetectionNotifications
case detectionSensorRole
case enableSmartPosition
case newNodeNotifications
case lowBatteryNotifications
case channelMessageNotifications
case modemPreset
case firmwareVersion
case testIntEnum
}
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
@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(.enableOfflineMapsMBTiles, defaultValue: false)
static var enableOfflineMapsMBTiles: 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
@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
@UserDefault(.testIntEnum, defaultValue: .one)
static var testIntEnum: TestIntEnum
}
enum TestIntEnum: Int, Decodable {
case one = 1
case two
case three
}