Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store complex preferences to Core Data #981

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions Library/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@ let package = Package(
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "AppUIMain",
targets: ["AppUIMainWrapper"]
targets: [
"AppDataPreferences",
"AppDataProfiles",
"AppDataProviders",
"AppUIMainWrapper"
]
),
.library(
name: "AppUITV",
targets: ["AppUITVWrapper"]
targets: [
"AppDataPreferences",
"AppDataProfiles",
"AppDataProviders",
"AppUITVWrapper"
]
),
.library(
name: "CommonIAP",
Expand All @@ -39,7 +49,10 @@ let package = Package(
),
.library(
name: "TunnelLibrary",
targets: ["CommonLibrary"]
targets: [
"AppDataPreferences",
"CommonLibrary"
]
),
.library(
name: "UILibrary",
Expand Down Expand Up @@ -69,6 +82,16 @@ let package = Package(
name: "AppData",
dependencies: []
),
.target(
name: "AppDataPreferences",
dependencies: [
"AppData",
"CommonLibrary"
],
resources: [
.process("Preferences.xcdatamodeld")
]
),
.target(
name: "AppDataProfiles",
dependencies: [
Expand Down Expand Up @@ -166,8 +189,6 @@ let package = Package(
.target(
name: "UILibrary",
dependencies: [
"AppDataProfiles",
"AppDataProviders",
"CommonAPI",
"CommonLibrary",
"UITesting"
Expand Down
39 changes: 39 additions & 0 deletions Library/Sources/AppDataPreferences/AppData+Preferences.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// AppData+Preferences.swift
// Passepartout
//
// Created by Davide De Rosa on 12/6/24.
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//

import AppData
import CoreData
import Foundation

extension AppData {

@MainActor
public static let cdPreferencesModel: NSManagedObjectModel = {
guard let model: NSManagedObjectModel = .mergedModel(from: [.module]) else {
fatalError("Unable to build Core Data model (Preferences v3)")
}
return model
}()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// CDModulePreferencesV3.swift
// Passepartout
//
// Created by Davide De Rosa on 12/5/24.
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//

import CoreData
import Foundation

@objc(CDModulePreferencesV3)
final class CDModulePreferencesV3: NSManagedObject {
@nonobjc static func fetchRequest() -> NSFetchRequest<CDModulePreferencesV3> {
NSFetchRequest<CDModulePreferencesV3>(entityName: "CDModulePreferencesV3")
}

@NSManaged var uuid: UUID?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// CDProviderPreferencesV3.swift
// Passepartout
//
// Created by Davide De Rosa on 12/5/24.
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//

import CoreData
import Foundation

@objc(CDProviderPreferencesV3)
final class CDProviderPreferencesV3: NSManagedObject {
@nonobjc static func fetchRequest() -> NSFetchRequest<CDProviderPreferencesV3> {
NSFetchRequest<CDProviderPreferencesV3>(entityName: "CDProviderPreferencesV3")
}

@NSManaged var providerId: String?
@NSManaged var favoriteServerIds: Data?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="23507" systemVersion="23H222" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithSwiftData="YES" userDefinedModelVersionIdentifier="">
<entity name="CDModulePreferencesV3" representedClassName="CDModulePreferencesV3" syncable="YES">
<attribute name="uuid" optional="YES" attributeType="UUID" usesScalarValueType="NO"/>
</entity>
<entity name="CDProviderPreferencesV3" representedClassName="CDProviderPreferencesV3" syncable="YES">
<attribute name="favoriteServerIds" optional="YES" attributeType="Binary"/>
<attribute name="providerId" optional="YES" attributeType="String"/>
</entity>
</model>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// CDModulePreferencesRepositoryV3.swift
// Passepartout
//
// Created by Davide De Rosa on 12/5/24.
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//

import AppData
import CommonLibrary
import CoreData
import Foundation
import PassepartoutKit

extension AppData {

@MainActor
public static func cdModulePreferencesRepositoryV3(context: NSManagedObjectContext) -> ModulePreferencesRepository {
CDModulePreferencesRepositoryV3(context: context)
}
}

// MARK: - Repository

private final class CDModulePreferencesRepositoryV3: ModulePreferencesRepository {
private nonisolated let context: NSManagedObjectContext

init(context: NSManagedObjectContext) {
self.context = context
}

func modulePreferencesProxy(in moduleId: UUID) throws -> ModulePreferencesProxy {
let entity = try context.performAndWait {
let request = CDModulePreferencesV3.fetchRequest()
request.predicate = NSPredicate(format: "uuid == %@", moduleId.uuidString)
do {
let entity = try request.execute().first ?? CDModulePreferencesV3(context: context)
entity.uuid = moduleId
return entity
} catch {
pp_log(.app, .error, "Unable to load preferences for module \(moduleId): \(error)")
throw error
}
}
return CDModulePreferencesProxy(context: context, entity: entity)
}
}

// MARK: - Preference

private final class CDModulePreferencesProxy: ModulePreferencesProxy {
private let context: NSManagedObjectContext

private let entity: CDModulePreferencesV3

init(context: NSManagedObjectContext, entity: CDModulePreferencesV3) {
self.context = context
self.entity = entity
}

func save() throws {
guard context.hasChanges else {
return
}
do {
try context.save()
} catch {
context.rollback()
throw error
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// CDProviderPreferencesRepositoryV3.swift
// Passepartout
//
// Created by Davide De Rosa on 12/5/24.
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//

import AppData
import CommonLibrary
import CoreData
import Foundation
import PassepartoutKit

extension AppData {

@MainActor
public static func cdProviderPreferencesRepositoryV3(context: NSManagedObjectContext) -> ProviderPreferencesRepository {
CDProviderPreferencesRepositoryV3(context: context)
}
}

// MARK: - Repository

private final class CDProviderPreferencesRepositoryV3: ProviderPreferencesRepository {
private nonisolated let context: NSManagedObjectContext

init(context: NSManagedObjectContext) {
self.context = context
}

func providerPreferencesProxy(in providerId: ProviderID) throws -> ProviderPreferencesProxy {
let entity = try context.performAndWait {
let request = CDProviderPreferencesV3.fetchRequest()
request.predicate = NSPredicate(format: "providerId == %@", providerId.rawValue)
do {
let entity = try request.execute().first ?? CDProviderPreferencesV3(context: context)
entity.providerId = providerId.rawValue
return entity
} catch {
pp_log(.app, .error, "Unable to load preferences for provider \(providerId): \(error)")
throw error
}
}
return CDProviderPreferencesProxy(context: context, entity: entity)
}
}

// MARK: - Preference

private final class CDProviderPreferencesProxy: ProviderPreferencesProxy {
private let context: NSManagedObjectContext

private let entity: CDProviderPreferencesV3

init(context: NSManagedObjectContext, entity: CDProviderPreferencesV3) {
self.context = context
self.entity = entity
}

var favoriteServers: Set<String> {
get {
do {
return try context.performAndWait {
guard let data = entity.favoriteServerIds else {
return []
}
return try JSONDecoder().decode(Set<String>.self, from: data)
}
} catch {
pp_log(.app, .error, "Unable to get favoriteServers: \(error)")
return []
}
}
set {
do {
try context.performAndWait {
entity.favoriteServerIds = try JSONEncoder().encode(newValue)
}
} catch {
pp_log(.app, .error, "Unable to set favoriteServers: \(error)")
}
}
}

func save() throws {
guard context.hasChanges else {
return
}
do {
try context.save()
} catch {
context.rollback()
throw error
}
}
}
Loading