secretive/SecretKit/Common/SecretStoreList.swift

40 lines
1010 B
Swift
Raw Normal View History

2020-03-09 03:03:40 +00:00
import Foundation
import Combine
public class SecretStoreList: ObservableObject {
@Published public var stores: [AnySecretStore] = []
@Published public var modifiableStore: AnySecretStoreModifiable?
2020-03-09 03:44:15 +00:00
fileprivate var sinks: [AnyCancellable] = []
2020-03-09 03:03:40 +00:00
public init() {
}
public func add<SecretStoreType: SecretStore>(store: SecretStoreType) {
2020-03-09 03:44:15 +00:00
addInternal(store: AnySecretStore(store))
2020-03-09 03:03:40 +00:00
}
public func add<SecretStoreType: SecretStoreModifiable>(store: SecretStoreType) {
let modifiable = AnySecretStoreModifiable(modifiable: store)
modifiableStore = modifiable
2020-03-09 03:44:15 +00:00
addInternal(store: modifiable)
}
2020-03-21 03:20:20 +00:00
public var anyAvailable: Bool {
stores.reduce(false, { $0 || $1.isAvailable })
}
2020-03-09 04:11:59 +00:00
}
extension SecretStoreList {
fileprivate func addInternal(store: AnySecretStore) {
2020-03-09 03:44:15 +00:00
stores.append(store)
let sink = store.objectWillChange.sink {
self.objectWillChange.send()
}
sinks.append(sink)
2020-03-09 03:03:40 +00:00
}
}