summaryrefslogtreecommitdiff
path: root/LearnMapKit/Managers/ConfigurationManager.swift
blob: 15ae274de1a8f107d6722c0dce03fcf3467b5d7a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//
//  ConfigurationManager.swift
//  LearnMapKit
//
//  Centralized configuration management
//

import Foundation
import Combine
import MapKit

// MARK: - App Configuration
struct AppConfiguration: Equatable {
    let dataSource: DataSourceConfiguration
    let displaySettings: DisplaySettings
    let mapSettings: MapSettings
    let networkSettings: NetworkSettings

    static let `default` = AppConfiguration(
        dataSource: NetworkDataSourceConfiguration(
            hostname: "64.226.23.78",
            port: 30003
        ),
        displaySettings: DisplaySettings(),
        mapSettings: MapSettings(),
        networkSettings: NetworkSettings()
    )
}

// MARK: - Display Settings
struct DisplaySettings: Equatable {
    var showAircraftLabels: Bool = true
    var showAltitude: Bool = true
    var showSpeed: Bool = false
    var aircraftHistoryLength: Int = 10
    var updateInterval: TimeInterval = 1.0
    var theme: AppTheme = .system

    enum AppTheme: String, CaseIterable {
        case light, dark, system
    }
}

// MARK: - Map Settings
struct MapSettings: Equatable {
    var defaultCenter: CLLocationCoordinate2D = CLLocationCoordinate2D(
        latitude: 55.90159,
        longitude: -3.53154
    )
    var defaultSpan: MKCoordinateSpan = MKCoordinateSpan(
        latitudeDelta: 0.1,
        longitudeDelta: 0.1
    )
    var mapStyle: MapStyle = .hybrid
    var showTerrain: Bool = true

    enum MapStyle: String, CaseIterable {
        case standard, hybrid, satellite

        var mkMapType: MKMapType {
            switch self {
            case .standard: return .standard
            case .hybrid: return .hybrid
            case .satellite: return .satellite
            }
        }
    }

    // Custom equality to handle CLLocationCoordinate2D
    static func == (lhs: MapSettings, rhs: MapSettings) -> Bool {
        lhs.defaultCenter.latitude == rhs.defaultCenter.latitude &&
        lhs.defaultCenter.longitude == rhs.defaultCenter.longitude &&
        lhs.defaultSpan.latitudeDelta == rhs.defaultSpan.latitudeDelta &&
        lhs.defaultSpan.longitudeDelta == rhs.defaultSpan.longitudeDelta &&
        lhs.mapStyle == rhs.mapStyle &&
        lhs.showTerrain == rhs.showTerrain
    }
}

// MARK: - Network Settings
struct NetworkSettings: Equatable {
    var connectionTimeout: TimeInterval = 10.0
    var reconnectAttempts: Int = 3
    var bufferSize: Int = 1024
    var enableHeartbeat: Bool = true
    var heartbeatInterval: TimeInterval = 30.0
}

// MARK: - Configuration Manager Protocol
protocol ConfigurationManager: AnyObject {
    var currentConfiguration: AnyPublisher<AppConfiguration, Never> { get }
    var hasUnsavedChanges: AnyPublisher<Bool, Never> { get }

    func getCurrentConfiguration() -> AppConfiguration
    func updateConfiguration(_ configuration: AppConfiguration) async
    func updateDataSource(_ dataSource: DataSourceConfiguration) async
    func updateDisplaySettings(_ settings: DisplaySettings) async
    func updateMapSettings(_ settings: MapSettings) async
    func updateNetworkSettings(_ settings: NetworkSettings) async
    func resetToDefaults() async
    func saveConfiguration() async throws
    func validateConfiguration(_ configuration: AppConfiguration) -> ConfigurationValidationResult
}

// MARK: - Validation
struct ConfigurationValidationResult {
    let isValid: Bool
    let errors: [String]
    let warnings: [String]

    static let valid = ConfigurationValidationResult(isValid: true, errors: [], warnings: [])
}

// MARK: - Default Implementation
@MainActor
class DefaultConfigurationManager: ConfigurationManager {

    // MARK: - Published Properties
    var currentConfiguration: AnyPublisher<AppConfiguration, Never> {
        configurationSubject.eraseToAnyPublisher()
    }

    var hasUnsavedChanges: AnyPublisher<Bool, Never> {
        hasChangesSubject.eraseToAnyPublisher()
    }

    // MARK: - Private Properties
    private let configurationSubject: CurrentValueSubject<AppConfiguration, Never>
    private let hasChangesSubject = CurrentValueSubject<Bool, Never>(false)
    private var savedConfiguration: AppConfiguration
    private let userDefaults = UserDefaults.standard

    // MARK: - Keys for UserDefaults
    private enum Keys {
        static let configuration = "app_configuration"
    }

    init() {
        // Load configuration from UserDefaults or use default
        if let data = userDefaults.data(forKey: Keys.configuration),
           let configuration = try? JSONDecoder().decode(AppConfiguration.self, from: data) {
            self.savedConfiguration = configuration
        } else {
            self.savedConfiguration = AppConfiguration.default
        }

        self.configurationSubject = CurrentValueSubject(savedConfiguration)

        // Monitor for changes
        setupChangeMonitoring()
    }

    // MARK: - ConfigurationManager Implementation
    func getCurrentConfiguration() -> AppConfiguration {
        configurationSubject.value
    }

    func updateConfiguration(_ configuration: AppConfiguration) async {
        configurationSubject.send(configuration)
        checkForChanges()
    }

    func updateDataSource(_ dataSource: DataSourceConfiguration) async {
        var current = getCurrentConfiguration()
        current = AppConfiguration(
            dataSource: dataSource,
            displaySettings: current.displaySettings,
            mapSettings: current.mapSettings,
            networkSettings: current.networkSettings
        )
        await updateConfiguration(current)
    }

    func updateDisplaySettings(_ settings: DisplaySettings) async {
        var current = getCurrentConfiguration()
        current = AppConfiguration(
            dataSource: current.dataSource,
            displaySettings: settings,
            mapSettings: current.mapSettings,
            networkSettings: current.networkSettings
        )
        await updateConfiguration(current)
    }

    func updateMapSettings(_ settings: MapSettings) async {
        var current = getCurrentConfiguration()
        current = AppConfiguration(
            dataSource: current.dataSource,
            displaySettings: current.displaySettings,
            mapSettings: settings,
            networkSettings: current.networkSettings
        )
        await updateConfiguration(current)
    }

    func updateNetworkSettings(_ settings: NetworkSettings) async {
        var current = getCurrentConfiguration()
        current = AppConfiguration(
            dataSource: current.dataSource,
            displaySettings: current.displaySettings,
            mapSettings: current.mapSettings,
            networkSettings: settings
        )
        await updateConfiguration(current)
    }

    func resetToDefaults() async {
        await updateConfiguration(AppConfiguration.default)
    }

    func saveConfiguration() async throws {
        let current = getCurrentConfiguration()
        let data = try JSONEncoder().encode(current)
        userDefaults.set(data, forKey: Keys.configuration)
        savedConfiguration = current
        hasChangesSubject.send(false)
    }

    func validateConfiguration(_ configuration: AppConfiguration) -> ConfigurationValidationResult {
        var errors: [String] = []
        var warnings: [String] = []

        // Validate data source
        switch configuration.dataSource.sourceType {
        case .file(let path):
            if !FileManager.default.fileExists(atPath: path) {
                errors.append("File does not exist at path: \(path)")
            }
        case .network(let host, let port):
            if host.isEmpty {
                errors.append("Network hostname cannot be empty")
            }
            if port <= 0 || port > 65535 {
                errors.append("Invalid port number: \(port)")
            }
        case .test:
            warnings.append("Using test data source")
        }

        // Validate display settings
        if configuration.displaySettings.updateInterval < 0.1 {
            errors.append("Update interval must be at least 0.1 seconds")
        }
        if configuration.displaySettings.aircraftHistoryLength < 1 {
            errors.append("Aircraft history length must be at least 1")
        }

        // Validate network settings
        if configuration.networkSettings.connectionTimeout < 1.0 {
            errors.append("Connection timeout must be at least 1 second")
        }
        if configuration.networkSettings.reconnectAttempts < 0 {
            errors.append("Reconnect attempts cannot be negative")
        }

        return ConfigurationValidationResult(
            isValid: errors.isEmpty,
            errors: errors,
            warnings: warnings
        )
    }

    // MARK: - Private Methods
    private func setupChangeMonitoring() {
        configurationSubject
            .dropFirst() // Skip initial value
            .sink { [weak self] _ in
                self?.checkForChanges()
            }
            .store(in: &cancellables)
    }

    private func checkForChanges() {
        let hasChanges = getCurrentConfiguration() != savedConfiguration
        hasChangesSubject.send(hasChanges)
    }

    private var cancellables = Set<AnyCancellable>()
}

// MARK: - Extensions for Codable Support
extension AppConfiguration: Codable {
    // Custom coding keys and implementation would be needed here
    // for proper serialization of the configuration
}

extension DisplaySettings: Codable {}
extension NetworkSettings: Codable {}

extension MapSettings: Codable {
    enum CodingKeys: String, CodingKey {
        case defaultCenterLatitude, defaultCenterLongitude
        case defaultSpanLatitudeDelta, defaultSpanLongitudeDelta
        case mapStyle, showTerrain
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        let latitude = try container.decode(Double.self, forKey: .defaultCenterLatitude)
        let longitude = try container.decode(Double.self, forKey: .defaultCenterLongitude)
        defaultCenter = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

        let latDelta = try container.decode(Double.self, forKey: .defaultSpanLatitudeDelta)
        let lonDelta = try container.decode(Double.self, forKey: .defaultSpanLongitudeDelta)
        defaultSpan = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)

        mapStyle = try container.decode(MapStyle.self, forKey: .mapStyle)
        showTerrain = try container.decode(Bool.self, forKey: .showTerrain)
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)

        try container.encode(defaultCenter.latitude, forKey: .defaultCenterLatitude)
        try container.encode(defaultCenter.longitude, forKey: .defaultCenterLongitude)
        try container.encode(defaultSpan.latitudeDelta, forKey: .defaultSpanLatitudeDelta)
        try container.encode(defaultSpan.longitudeDelta, forKey: .defaultSpanLongitudeDelta)
        try container.encode(mapStyle, forKey: .mapStyle)
        try container.encode(showTerrain, forKey: .showTerrain)
    }
}