LocationBasedServices/mowesta/pipeline/head There was a failure building this commit
110 lines
3.8 KiB
Swift
110 lines
3.8 KiB
Swift
//
|
|
// AppState.swift
|
|
// App
|
|
//
|
|
// Created by Marcus Handte on 25.05.22.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftUI
|
|
import MoWeStaApi
|
|
|
|
/// The global application state that can be accessed all throughout the user interface
|
|
/// to access the user and ui state and settings.
|
|
public class AppState: ObservableObject {
|
|
|
|
// The current version of the license terms.
|
|
static let TERMS_VERSION = 1
|
|
|
|
// The shared application state.
|
|
public static let shared = AppState()
|
|
|
|
// The version of the license terms that the user has accepted.
|
|
@UserDefault("terms", defaultValue: 0) var terms: Int { willSet { objectWillChange.send(); }}
|
|
|
|
// A boolean to indicate whether the app is in development mode.
|
|
@UserDefault("development", defaultValue: false ) var development: Bool { willSet { objectWillChange.send() }}
|
|
|
|
// The device token used to upload data
|
|
@UserDefault("token", defaultValue: "") var token: String
|
|
|
|
@UserDefault("precipitation", defaultValue: true) var precipitation: Bool { willSet { objectWillChange.send() }}
|
|
@UserDefault("windSpeed", defaultValue: true) var windSpeed: Bool { willSet { objectWillChange.send() }}
|
|
@UserDefault("cloudCoverage", defaultValue: true) var cloudCoverage: Bool { willSet { objectWillChange.send() }}
|
|
@UserDefault("humidity", defaultValue: true) var humidity: Bool { willSet { objectWillChange.send() }}
|
|
@UserDefault("snowDepth", defaultValue: true) var snowDepth: Bool { willSet { objectWillChange.send() }}
|
|
@UserDefault("pressure", defaultValue: true) var pressure: Bool { willSet { objectWillChange.send() }}
|
|
@UserDefault("unitSystem", defaultValue: .si) var unitSystem: UnitSystem { willSet { objectWillChange.send() }}
|
|
@UserDefault("temperatureUnit", defaultValue: .both) var temperatureUnit: TemperatureUnit { willSet { objectWillChange.send() }}
|
|
|
|
var credentialFailure = false { willSet { objectWillChange.send() }}
|
|
|
|
private var loadedDevice: Device? = nil
|
|
|
|
var device: Device? {
|
|
get {
|
|
return loadedDevice
|
|
}
|
|
set(value) {
|
|
objectWillChange.send()
|
|
loadedDevice = value
|
|
if (value != nil) {
|
|
if let encoded = try? JSONEncoder().encode(value) {
|
|
UserDefaults.standard.set(encoded, forKey: "device")
|
|
}
|
|
} else {
|
|
UserDefaults.standard.removeObject(forKey: "device")
|
|
}
|
|
}
|
|
}
|
|
|
|
private var keyChain: KeyChain
|
|
|
|
var credentials: UserCredentials? {
|
|
get {
|
|
if let result = try? keyChain.get() {
|
|
return result
|
|
}
|
|
return nil
|
|
}
|
|
set(value) {
|
|
try? keyChain.clear()
|
|
if let v = value {
|
|
try? keyChain.add(credentials: v)
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// Sets the base url of the mowesta service
|
|
public func setUrl(_ url: String) {
|
|
UserDefaults.standard.set(url, forKey: "accountUrl")
|
|
updateUrl()
|
|
}
|
|
|
|
/// Updates the url of the api client
|
|
private func updateUrl() {
|
|
if let u = UserDefaults.standard.string(forKey: "accountUrl") {
|
|
OpenAPIClientAPI.basePath = u
|
|
}
|
|
}
|
|
|
|
// Resets the device completely.
|
|
public func reset() {
|
|
device = nil
|
|
terms = 0
|
|
development = false
|
|
token = ""
|
|
}
|
|
|
|
private init() {
|
|
self.keyChain = KeyChain(server: "www.mowesta.com")
|
|
UserDefaults.standard.register(defaults: ["accountUrl": "https://www.mowesta.com/api/"])
|
|
updateUrl()
|
|
if let data = UserDefaults.standard.object(forKey: "device") as? Data,
|
|
let value = try? JSONDecoder().decode(Device.self, from: data) {
|
|
loadedDevice = value
|
|
}
|
|
}
|
|
}
|