Newer Older
71 lines | 2.732kb
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
    private var icloudGefaultsNotification: AnyCancellable?
Bogdan Timofte authored 2 weeks ago
15
    private var bluetoothManagerNotification: AnyCancellable?
Bogdan Timofte authored 2 weeks ago
16

            
Bogdan Timofte authored 2 weeks ago
17
    init() {
18
        icloudGefaultsNotification = NotificationCenter.default.publisher(for: NSUbiquitousKeyValueStore.didChangeExternallyNotification).sink(receiveValue: test)
Bogdan Timofte authored 2 weeks ago
19
        bluetoothManagerNotification = bluetoothManager.objectWillChange.sink { [weak self] _ in
Bogdan Timofte authored 2 weeks ago
20
            self?.scheduleObjectWillChange()
Bogdan Timofte authored 2 weeks ago
21
        }
Bogdan Timofte authored 2 weeks ago
22
        //NotificationCenter.default.publisher(for: NSUbiquitousKeyValueStore.didChangeExternallyNotification).sink(receiveValue: { notification in
23

            
24
    }
25

            
26
    let bluetoothManager = BluetoothManager()
27

            
28
    @Published var enableRecordFeature: Bool = true
29

            
Bogdan Timofte authored 2 weeks ago
30
    @Published var meters: [UUID:Meter] = [UUID:Meter]()
Bogdan Timofte authored 2 weeks ago
31

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

            
58
                }
59
            }
60
            if somethingChanged {
Bogdan Timofte authored 2 weeks ago
61
                scheduleObjectWillChange()
Bogdan Timofte authored 2 weeks ago
62
            }
63
        }
64
    }
Bogdan Timofte authored 2 weeks ago
65

            
66
    private func scheduleObjectWillChange() {
67
        DispatchQueue.main.async { [weak self] in
68
            self?.objectWillChange.send()
69
        }
70
    }
Bogdan Timofte authored 2 weeks ago
71
}