SPB Git

Repository Public BSD-3-Clause

AppSettings.swift 8.6 KB 169 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Maps9// File             : Apps/ORIXMaps/Sources/App/AppSettings.swift10// Module           : App11// Repository       : https://git.spboucher.ai/orix-maps12// Created          : 2026-11-2513// Last Modified    : 2026-11-2514//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation89import Observation90import ORIXDesignSystem91import ORIXGeoCore92import ORIXMapRenderer9394/// User preferences, stored in UserDefaults on the device only.95@Observable96final class AppSettings {97    enum Appearance: String, CaseIterable, Identifiable { case system, light, dark; var id: String { rawValue } }98    enum LabelLanguage: String, CaseIterable, Identifiable { case automatic, local, english, french; var id: String { rawValue } }99100    @ObservationIgnored private let defaults: UserDefaults101102    var themeID: String { didSet { defaults.set(themeID, forKey: "themeID") } }103    /// Theme used when the interface is dark and `nightThemeAutomatic` is on.104    var nightThemeAutomatic: Bool { didSet { defaults.set(nightThemeAutomatic, forKey: "nightThemeAutomatic") } }105    var appearance: Appearance { didSet { defaults.set(appearance.rawValue, forKey: "appearance") } }106    var units: MeasureFormatter.System { didSet { defaults.set(units.rawValue, forKey: "units") } }107    var coordinateStyle: CoordinateText.Style { didSet { defaults.set(coordinateStyle.rawValue, forKey: "coordinateStyle") } }108    var labelLanguage: LabelLanguage { didSet { defaults.set(labelLanguage.rawValue, forKey: "labelLanguage") } }109    /// Keep searches and visited places on the device to offer them again. Off = nothing is recorded.110    var historyEnabled: Bool { didSet { defaults.set(historyEnabled, forKey: "historyEnabled") } }111    var voiceGuidance: Bool { didSet { defaults.set(voiceGuidance, forKey: "voiceGuidance") } }112    var voiceIdentifier: String? { didSet { defaults.set(voiceIdentifier, forKey: "voiceIdentifier") } }113    var voiceVolume: Double { didSet { defaults.set(voiceVolume, forKey: "voiceVolume") } }114    var keepScreenOnDuringNavigation: Bool { didSet { defaults.set(keepScreenOnDuringNavigation, forKey: "keepScreenOn") } }115    var highFrameRate: Bool { didSet { defaults.set(highFrameRate, forKey: "highFrameRate") } }116    var hasCompletedOnboarding: Bool { didSet { defaults.set(hasCompletedOnboarding, forKey: "hasCompletedOnboarding") } }117    var avoidTolls: Bool { didSet { defaults.set(avoidTolls, forKey: "avoidTolls") } }118    var avoidFerries: Bool { didSet { defaults.set(avoidFerries, forKey: "avoidFerries") } }119    var avoidMotorways: Bool { didSet { defaults.set(avoidMotorways, forKey: "avoidMotorways") } }120    var avoidUnpaved: Bool { didSet { defaults.set(avoidUnpaved, forKey: "avoidUnpaved") } }121122    init(defaults: UserDefaults = .standard) {123        self.defaults = defaults124        themeID = defaults.string(forKey: "themeID") ?? "orix.day"125        nightThemeAutomatic = defaults.object(forKey: "nightThemeAutomatic") as? Bool ?? true126        appearance = Appearance(rawValue: defaults.string(forKey: "appearance") ?? "") ?? .system127        let metricDefault: MeasureFormatter.System = Locale.current.measurementSystem == .metric ? .metric : .imperial128        units = MeasureFormatter.System(rawValue: defaults.string(forKey: "units") ?? "") ?? metricDefault129        coordinateStyle = CoordinateText.Style(rawValue: defaults.string(forKey: "coordinateStyle") ?? "") ?? .decimalDegrees130        labelLanguage = LabelLanguage(rawValue: defaults.string(forKey: "labelLanguage") ?? "") ?? .automatic131        historyEnabled = defaults.object(forKey: "historyEnabled") as? Bool ?? true132        voiceGuidance = defaults.object(forKey: "voiceGuidance") as? Bool ?? true133        voiceIdentifier = defaults.string(forKey: "voiceIdentifier")134        voiceVolume = defaults.object(forKey: "voiceVolume") as? Double ?? 1135        keepScreenOnDuringNavigation = defaults.object(forKey: "keepScreenOn") as? Bool ?? true136        highFrameRate = defaults.object(forKey: "highFrameRate") as? Bool ?? false137        hasCompletedOnboarding = defaults.bool(forKey: "hasCompletedOnboarding")138        avoidTolls = defaults.bool(forKey: "avoidTolls")139        avoidFerries = defaults.bool(forKey: "avoidFerries")140        avoidMotorways = defaults.bool(forKey: "avoidMotorways")141        avoidUnpaved = defaults.bool(forKey: "avoidUnpaved")142    }143144    var formatter: MeasureFormatter { MeasureFormatter(system: units) }145146    /// Language code for map labels ("" = each place's local name).147    var labelLanguageCode: String {148        switch labelLanguage {149        case .automatic:150            let code = Locale.current.language.languageCode?.identifier ?? "en"151            return ["fr", "en"].contains(code) ? code : "en"152        case .local: return ""153        case .english: return "en"154        case .french: return "fr"155        }156    }157158    /// Language of generated guidance and interface texts handled outside the string catalog.159    var interfaceLanguageCode: String { (Locale.current.language.languageCode?.identifier ?? "en") == "fr" ? "fr" : "en" }160161    // The last viewpoint is restored at launch. It stays in UserDefaults on the device and is erased with the history.162    var lastCamera: MapCamera? {163        get { (defaults.data(forKey: "lastCamera")).flatMap { try? JSONDecoder().decode(MapCamera.self, from: $0) } }164        set {165            if let newValue, let data = try? JSONEncoder().encode(newValue) { defaults.set(data, forKey: "lastCamera") } else { defaults.removeObject(forKey: "lastCamera") }166        }167    }168}169
Files main
Menu

Theme