Back up profiles to Core Data (#762)

Closes #745
This commit is contained in:
Davide 2024-10-26 21:41:07 +02:00 committed by GitHub
parent baff2c58ca
commit ee3a40b597
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 66 additions and 32 deletions

View File

@ -37,6 +37,8 @@ public final class ProfileManager: ObservableObject {
private let repository: any ProfileRepository
private let backupRepository: (any ProfileRepository)?
private let remoteRepository: (any ProfileRepository)?
@Published
@ -59,6 +61,7 @@ public final class ProfileManager: ObservableObject {
// for testing/previews
public init(profiles: [Profile]) {
repository = InMemoryProfileRepository(profiles: profiles)
backupRepository = nil
remoteRepository = nil
self.profiles = []
allProfiles = profiles.reduce(into: [:]) {
@ -71,8 +74,13 @@ public final class ProfileManager: ObservableObject {
subscriptions = []
}
public init(repository: any ProfileRepository, remoteRepository: (any ProfileRepository)?) {
public init(
repository: any ProfileRepository,
backupRepository: (any ProfileRepository)? = nil,
remoteRepository: (any ProfileRepository)?
) {
self.repository = repository
self.backupRepository = backupRepository
self.remoteRepository = remoteRepository
profiles = []
allProfiles = [:]
@ -118,6 +126,12 @@ extension ProfileManager {
if existingProfile == nil || profile != existingProfile {
try await repository.saveProfile(profile)
if let backupRepository {
Task.detached {
try? await backupRepository.saveProfile(profile)
}
}
allProfiles[profile.id] = profile
didChange.send(.save(profile))
} else {

View File

@ -49,8 +49,6 @@ extension AppContext {
extension ProfileManager {
static let shared: ProfileManager = {
let repository = localProfileRepository
let remoteStore = CoreDataPersistentStore(
logger: .default,
containerName: Constants.shared.containers.remote,
@ -68,7 +66,11 @@ extension ProfileManager {
return .ignore
}
return ProfileManager(repository: repository, remoteRepository: remoteRepository)
return ProfileManager(
repository: mainProfileRepository,
backupRepository: backupProfileRepository,
remoteRepository: remoteRepository
)
}()
}
@ -158,7 +160,51 @@ extension Tunnel {
}
private extension ProfileManager {
static let localProfileRepository: ProfileRepository = {
static var mainProfileRepository: ProfileRepository {
coreDataProfileRepository
}
static var backupProfileRepository: ProfileRepository? {
nil
}
}
#else
extension Tunnel {
static let shared = Tunnel(
strategy: ProfileManager.neStrategy
)
}
private extension ProfileManager {
static var mainProfileRepository: ProfileRepository {
neProfileRepository
}
static var backupProfileRepository: ProfileRepository? {
coreDataProfileRepository
}
}
#endif
private extension ProfileManager {
static let neProfileRepository: ProfileRepository = {
NEProfileRepository(repository: neStrategy) {
ProfileManager.sharedTitle($0)
}
}()
static let neStrategy: NETunnelStrategy = {
NETunnelStrategy(
bundleIdentifier: BundleConfiguration.mainString(for: .tunnelId),
coder: Registry.sharedProtocolCoder,
environment: .shared
)
}()
static let coreDataProfileRepository: ProfileRepository = {
let store = CoreDataPersistentStore(
logger: .default,
containerName: Constants.shared.containers.local,
@ -178,32 +224,6 @@ private extension ProfileManager {
}()
}
#else
extension Tunnel {
static let shared = Tunnel(
strategy: ProfileManager.neStrategy
)
}
private extension ProfileManager {
static let localProfileRepository: ProfileRepository = {
NEProfileRepository(repository: neStrategy) {
ProfileManager.sharedTitle($0)
}
}()
static let neStrategy: NETunnelStrategy = {
NETunnelStrategy(
bundleIdentifier: BundleConfiguration.mainString(for: .tunnelId),
coder: Registry.sharedProtocolCoder,
environment: .shared
)
}()
}
#endif
// MARK: -
// FIXME: #705, store providers to Core Data