Add 'didUpdate' subjects in managers

objectWillChange may not work outside SwiftUI.

- ProfileManager.didUpdateProfiles

- ProviderManager.didUpdateProviders
This commit is contained in:
Davide De Rosa 2022-07-15 08:41:05 +02:00
parent 7771b50b56
commit 7d5f3e63c8
4 changed files with 16 additions and 0 deletions

View File

@ -32,6 +32,8 @@ public protocol ProfileManager {
var activeProfileId: UUID? { get } var activeProfileId: UUID? { get }
var currentProfileId: UUID? { get set } var currentProfileId: UUID? { get set }
var didUpdateProfiles: PassthroughSubject<Void, Never> { get }
var didUpdateActiveProfile: PassthroughSubject<UUID?, Never> { get } var didUpdateActiveProfile: PassthroughSubject<UUID?, Never> { get }

View File

@ -27,6 +27,8 @@ import Foundation
import Combine import Combine
public protocol ProviderManager { public protocol ProviderManager {
var didUpdateProviders: PassthroughSubject<Void, Never> { get }
func allProviders() -> [ProviderMetadata] func allProviders() -> [ProviderMetadata]
func provider(withName name: ProviderName) -> ProviderMetadata? func provider(withName name: ProviderName) -> ProviderMetadata?

View File

@ -79,6 +79,8 @@ public class DefaultProfileManager: ProfileManagerWithCurrentProfile, Observable
public let currentProfile: ObservableProfile public let currentProfile: ObservableProfile
public let didUpdateProfiles = PassthroughSubject<Void, Never>()
public let didUpdateActiveProfile = PassthroughSubject<UUID?, Never>() public let didUpdateActiveProfile = PassthroughSubject<UUID?, Never>()
public let didCreateProfile = PassthroughSubject<Profile, Never>() public let didCreateProfile = PassthroughSubject<Profile, Never>()
@ -329,6 +331,8 @@ extension DefaultProfileManager {
pp_log.info("\tActive profile was deleted") pp_log.info("\tActive profile was deleted")
self.activeProfileId = nil self.activeProfileId = nil
} }
didUpdateProfiles.send()
// IMPORTANT: defer task to avoid recursive saves // IMPORTANT: defer task to avoid recursive saves
// FIXME: Core Data, not sure about this workaround // FIXME: Core Data, not sure about this workaround

View File

@ -44,6 +44,8 @@ public class DefaultProviderManager: ProviderManager, ObservableObject, RateLimi
private let serverRepository: ServerRepository private let serverRepository: ServerRepository
public let didUpdateProviders = PassthroughSubject<Void, Never>()
public init(appBuild: Int, bundleServices: WebServices, webServices: WebServices, persistence: Persistence) { public init(appBuild: Int, bundleServices: WebServices, webServices: WebServices, persistence: Persistence) {
self.appBuild = appBuild self.appBuild = appBuild
self.bundleServices = bundleServices self.bundleServices = bundleServices
@ -118,6 +120,8 @@ public class DefaultProviderManager: ProviderManager, ObservableObject, RateLimi
.tryMap { index in .tryMap { index in
self.saveLastAction(self.indexActionName) self.saveLastAction(self.indexActionName)
try self.providerRepository.mergeIndex(index) try self.providerRepository.mergeIndex(index)
self.didUpdateProviders.send()
}.eraseToAnyPublisher() }.eraseToAnyPublisher()
} }
@ -167,6 +171,8 @@ public class DefaultProviderManager: ProviderManager, ObservableObject, RateLimi
vpnProtocol: vpnProtocol, vpnProtocol: vpnProtocol,
lastUpdate: pub.lastModified ?? Date() lastUpdate: pub.lastModified ?? Date()
) )
self.didUpdateProviders.send()
} catch { } catch {
pp_log.error("Unable to persist \(providerName) infrastructure (\(vpnProtocol)): \(error)") pp_log.error("Unable to persist \(providerName) infrastructure (\(vpnProtocol)): \(error)")
} }
@ -178,6 +184,8 @@ public class DefaultProviderManager: ProviderManager, ObservableObject, RateLimi
public func reset() { public func reset() {
persistence.truncate() persistence.truncate()
didUpdateProviders.send()
} }
// MARK: RateLimited // MARK: RateLimited