Newer Older
75 lines | 2.951kb
Bogdan Timofte authored 2 weeks ago
1
//
2
//  DataStore.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 03/03/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8

            
9
import SwiftUI
10
import Combine
11
import CoreBluetooth
12

            
13
final class AppData : ObservableObject {
14

            
15

            
16
    let objectWillChange = ObservableObjectPublisher()
17
    private var userDefaultsNotification: AnyCancellable?
18
    private var icloudGefaultsNotification: AnyCancellable?
Bogdan Timofte authored 2 weeks ago
19
    private var bluetoothManagerNotification: AnyCancellable?
Bogdan Timofte authored 2 weeks ago
20
    init() {
21
        userDefaultsNotification = NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification).sink { _ in
22
            self.objectWillChange.send()
23
        }
24
        icloudGefaultsNotification = NotificationCenter.default.publisher(for: NSUbiquitousKeyValueStore.didChangeExternallyNotification).sink(receiveValue: test)
Bogdan Timofte authored 2 weeks ago
25
        bluetoothManagerNotification = bluetoothManager.objectWillChange.sink { [weak self] _ in
26
            self?.objectWillChange.send()
27
        }
Bogdan Timofte authored 2 weeks ago
28
        //NotificationCenter.default.publisher(for: NSUbiquitousKeyValueStore.didChangeExternallyNotification).sink(receiveValue: { notification in
29

            
30
    }
31

            
32
    let bluetoothManager = BluetoothManager()
33

            
34
    @Published var enableRecordFeature: Bool = true
35

            
36
    @Published var meters: [UUID:Meter] = [UUID:Meter]() {
37
        willSet {
38
            self.objectWillChange.send()
39
        }
40
    }
41

            
42
    @ICloudDefault(key: "MeterNames", defaultValue: [:]) var meterNames: [String:String]
Bogdan Timofte authored 2 weeks ago
43
    @ICloudDefault(key: "TC66TemperatureUnits", defaultValue: [:]) var tc66TemperatureUnits: [String:String]
Bogdan Timofte authored 2 weeks ago
44
    func test(notification: NotificationCenter.Publisher.Output) -> Void {
45
        if let changedKeys = notification.userInfo?["NSUbiquitousKeyValueStoreChangedKeysKey"] as? [String] {
46
            var somethingChanged = false
47
            for changedKey in changedKeys {
48
                switch changedKey {
49
                case "MeterNames":
50
                    for meter in self.meters.values {
51
                        if let newName = self.meterNames[meter.btSerial.macAddress.description] {
52
                            if meter.name != newName {
53
                                meter.name = newName
54
                                somethingChanged = true
55
                            }
56
                        }
57
                    }
Bogdan Timofte authored 2 weeks ago
58
                case "TC66TemperatureUnits":
59
                    for meter in self.meters.values where meter.supportsManualTemperatureUnitSelection {
60
                        meter.reloadTemperatureUnitPreference()
61
                        somethingChanged = true
62
                    }
Bogdan Timofte authored 2 weeks ago
63
                default:
64
                    track("Unknown key: '\(changedKey)' changed in iCloud)")
65
                }
66
                if changedKey == "MeterNames" {
67

            
68
                }
69
            }
70
            if somethingChanged {
71
                self.objectWillChange.send()
72
            }
73
        }
74
    }
75
}