Newer Older
69 lines | 2.558kb
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]
43
    func test(notification: NotificationCenter.Publisher.Output) -> Void {
44
        if let changedKeys = notification.userInfo?["NSUbiquitousKeyValueStoreChangedKeysKey"] as? [String] {
45
            var somethingChanged = false
46
            for changedKey in changedKeys {
47
                switch changedKey {
48
                case "MeterNames":
49
                    for meter in self.meters.values {
50
                        if let newName = self.meterNames[meter.btSerial.macAddress.description] {
51
                            if meter.name != newName {
52
                                meter.name = newName
53
                                somethingChanged = true
54
                            }
55
                        }
56
                    }
57
                default:
58
                    track("Unknown key: '\(changedKey)' changed in iCloud)")
59
                }
60
                if changedKey == "MeterNames" {
61

            
62
                }
63
            }
64
            if somethingChanged {
65
                self.objectWillChange.send()
66
            }
67
        }
68
    }
69
}