From 286e2dc5582a241f2d6820563c5eab7c5acb278e Mon Sep 17 00:00:00 2001 From: Max Goedjen Date: Sun, 8 Mar 2020 20:44:15 -0700 Subject: [PATCH] Combine stuff --- SecretKit/Common/Erasers/AnySecretStore.swift | 5 +++++ SecretKit/Common/SecretStoreList.swift | 13 +++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/SecretKit/Common/Erasers/AnySecretStore.swift b/SecretKit/Common/Erasers/AnySecretStore.swift index 6bf9809..9e71d2c 100644 --- a/SecretKit/Common/Erasers/AnySecretStore.swift +++ b/SecretKit/Common/Erasers/AnySecretStore.swift @@ -1,4 +1,5 @@ import Foundation +import Combine public class AnySecretStore: SecretStore { @@ -8,6 +9,7 @@ public class AnySecretStore: SecretStore { fileprivate let _name: () -> String fileprivate let _secrets: () -> [AnySecret] fileprivate let _sign: (Data, AnySecret) throws -> Data + fileprivate var sink: AnyCancellable? public init(_ secretStore: SecretStoreType) where SecretStoreType: SecretStore { base = secretStore @@ -16,6 +18,9 @@ public class AnySecretStore: SecretStore { _id = { secretStore.id } _secrets = { secretStore.secrets.map { AnySecret($0) } } _sign = { try secretStore.sign(data: $0, with: $1 as! SecretStoreType.SecretType) } + sink = secretStore.objectWillChange.sink { _ in + self.objectWillChange.send() + } } public var isAvailable: Bool { diff --git a/SecretKit/Common/SecretStoreList.swift b/SecretKit/Common/SecretStoreList.swift index 5810b97..7d7003d 100644 --- a/SecretKit/Common/SecretStoreList.swift +++ b/SecretKit/Common/SecretStoreList.swift @@ -5,18 +5,27 @@ public class SecretStoreList: ObservableObject { @Published public var stores: [AnySecretStore] = [] @Published public var modifiableStore: AnySecretStoreModifiable? + fileprivate var sinks: [AnyCancellable] = [] public init() { } public func add(store: SecretStoreType) { - stores.append(AnySecretStore(store)) + addInternal(store: AnySecretStore(store)) } public func add(store: SecretStoreType) { let modifiable = AnySecretStoreModifiable(modifiable: store) modifiableStore = modifiable - stores.append(modifiable) + addInternal(store: modifiable) + } + + public func addInternal(store: AnySecretStore) { + stores.append(store) + let sink = store.objectWillChange.sink { + self.objectWillChange.send() + } + sinks.append(sink) } }