Skip to content

Commit

Permalink
SwiftData - simplify modelContainer initialization (#1122)
Browse files Browse the repository at this point in the history
* feat(swiftData): move DataController to a value type that only contains methods for the initialization of a modelContainer

* fix(tests): replace date formats in fixtures

* fix(tests): replace date formats in fixtures
  • Loading branch information
Gio2018 authored Nov 26, 2024
1 parent d187483 commit 995fdae
Show file tree
Hide file tree
Showing 62 changed files with 233 additions and 254 deletions.
2 changes: 1 addition & 1 deletion PocketKit/Sources/PocketKit/Main/RootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private extension RootView {
model.handleSpotlight(userActivity)
})
// TODO: SWIFTUI - Once we move away from Services, these need to be handled with DI
.modelContainer(Services.shared.dataController)
.modelContainer(Services.shared.modelContainer)
.environmentObject(Services.shared.accessService)
}

Expand Down
5 changes: 2 additions & 3 deletions PocketKit/Sources/PocketKit/Services.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct Services {
let recentSavesWidgetUpdateService: RecentSavesWidgetUpdateService
let recommendationsWidgetUpdateService: RecommendationsWidgetUpdateService
let sharedWithYouStore: SharedWithYouStore
let dataController: ModelContainer
let modelContainer: ModelContainer

private let persistentContainer: PersistentContainer
private let sceneTracker: SceneTracker
Expand All @@ -65,8 +65,7 @@ struct Services {
notificationCenter = .default
Log.breadcrumb(category: "SWiftUIHome", level: .debug, message: "Initializing data controller.")
// Init the Group ID for Swift Data
DataController.appGroupContainerID = Keys.shared.groupID
dataController = DataController.sharedModelContainer
modelContainer = DataController.makeModelContainer(groupID: Keys.shared.groupID)
Log.breadcrumb(category: "SWiftUIHome", level: .debug, message: "Data controller initialized.")
persistentContainer = .init(storage: .shared, groupID: Keys.shared.groupID)
Log.breadcrumb(category: "SWiftUIHome", level: .debug, message: "Persistent container initialized.")
Expand Down
62 changes: 21 additions & 41 deletions PocketKit/Sources/Sync/SwiftData/DataController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,28 @@ import Foundation
import SwiftData
import SharedPocketKit

@MainActor
public class DataController {
public static var appGroupContainerID: String?
public struct DataController: Sendable {
public static func makeModelContainer(groupID: String) -> ModelContainer {
Log.breadcrumb(category: "SWiftUIHome", level: .debug, message: "Start initializing shared model container.")
ArticleTransformer.register()
Log.breadcrumb(category: "SWiftUIHome", level: .debug, message: "Article transformer registered.")
SyncTaskTransformer.register()
Log.breadcrumb(category: "SWiftUIHome", level: .debug, message: "SynkTask transformer registered.")

public static let schema = Schema([
guard let appGroupContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupID) else {
Log.capture(message: "Shared file container could not be created.")
fatalError("Shared file container could not be created.")
}
let url = appGroupContainer.appendingPathComponent("PocketModel.sqlite")
do {
return try ModelContainer(for: Self.schema, configurations: ModelConfiguration(url: url))
} catch {
Log.capture(message: "Could not create ModelContainer: \(error)")
fatalError("Could not create ModelContainer: \(error)")
}
}

private static let schema = Schema([
Author.self,
Sync.Collection.self,
CollectionAuthor.self,
Expand All @@ -32,41 +49,4 @@ public class DataController {
Tag.self,
UnresolvedSavedItem.self
])

public static let previewContainer: ModelContainer = {
ArticleTransformer.register()
SyncTaskTransformer.register()
do {
let config = ModelConfiguration(isStoredInMemoryOnly: true)
let container = try ModelContainer(for: schema, configurations: config)
MockData.insertFakeData(container: container)
return container
} catch {
fatalError("Failed to create model container for previewing: \(error.localizedDescription)")
}
}()

public static let sharedModelContainer: ModelContainer = {
Log.breadcrumb(category: "SWiftUIHome", level: .debug, message: "Start initializing shared model container.")
ArticleTransformer.register()
Log.breadcrumb(category: "SWiftUIHome", level: .debug, message: "Article transformer registered.")
SyncTaskTransformer.register()
Log.breadcrumb(category: "SWiftUIHome", level: .debug, message: "SynkTask transformer registered.")

guard let appGroupContainerID = appGroupContainerID else {
Log.capture(message: "appGroupContainerID must be set before accessing the sharedModelContainer.")
fatalError("appGroupContainerID must be set before accessing the sharedModelContainer.")
}
guard let appGroupContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupContainerID) else {
Log.capture(message: "Shared file container could not be created.")
fatalError("Shared file container could not be created.")
}
let url = appGroupContainer.appendingPathComponent("PocketModel.sqlite")
do {
return try ModelContainer(for: schema, configurations: ModelConfiguration(url: url))
} catch {
Log.capture(message: "Could not create ModelContainer: \(error)")
fatalError("Could not create ModelContainer: \(error)")
}
}()
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image" : {
"__typename": "[type-name-here]",
"url": "https://example.com/item-1/top-image.jpg"
Expand Down
2 changes: 1 addition & 1 deletion PocketKit/Tests/SyncTests/Fixtures/add-tags.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image" : {
"__typename": "[type-name-here]",
"url": "https://example.com/item-1/top-image.jpg"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"authors": [],
"excerpt": "Risus Aenean Ultricies Nullam Vehicula",
"title": "Archived Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image" : {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item-1/top-image.jpg"
Expand Down Expand Up @@ -93,7 +93,7 @@
"authors": [],
"excerpt": "Vehicula Nibh Ligula Porta Magna",
"title": "Archived Item 0",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image" : {
"__typename": "[type-name-here]",
"url": "https://example.com/items/archived-item-0/images/top-image.jpg"
Expand Down
4 changes: 2 additions & 2 deletions PocketKit/Tests/SyncTests/Fixtures/archived-items-page-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"authors": [],
"excerpt": "Risus Aenean Ultricies Nullam Vehicula",
"title": "Archived Item 0",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image" : {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item-0/top-image.jpg"
Expand Down Expand Up @@ -95,7 +95,7 @@
"authors": [],
"excerpt": "Risus Aenean Ultricies Nullam Vehicula",
"title": "Archived Item -1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image" : {
"__typename": "[type-name-here]",
"url": "https://example.com/items/archived-item--1/images/top-image.jpg"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"authors": [],
"excerpt": "Risus Aenean Ultricies Nullam Vehicula",
"title": "Archived Item -2",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down
4 changes: 2 additions & 2 deletions PocketKit/Tests/SyncTests/Fixtures/archived-items.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"authors": [],
"excerpt": "Vehicula Nibh Ligula Porta Magna",
"title": "Archived Item 2",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image" : {
"__typename": "[type-name-here]",
"url": "https://example.com/items/archived-item-2/images/top-image.jpg"
Expand Down Expand Up @@ -100,7 +100,7 @@
],
"excerpt": "Vehicula Nibh Ligula Porta Magna",
"title": "Archived Item 2",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image" : {
"__typename": "[type-name-here]",
"url": "https://example.com/items/archived-item-1/images/top-image.jpg"
Expand Down
6 changes: 3 additions & 3 deletions PocketKit/Tests/SyncTests/Fixtures/corpusSlates.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Slate 1, Recommendation 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image" : {
"__typename": "[type-name-here]",
"url": "http://example.com/slate-1-rec-1/top-image.png"
Expand Down Expand Up @@ -65,7 +65,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Slate 1, Recommendation 2",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image" : {
"__typename": "[type-name-here]",
"url": "http://example.com/slate-1-rec-2/top-image.png"
Expand Down Expand Up @@ -112,7 +112,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Slate 2, Recommendation 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image" : {
"__typename": "[type-name-here]",
"url": "http://example.com/slate-2-rec-1/top-image.png"
Expand Down
4 changes: 2 additions & 2 deletions PocketKit/Tests/SyncTests/Fixtures/duplicate-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"authors": [],
"excerpt": "Risus Aenean Ultricies Nullam Vehicula",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -113,7 +113,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item-1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down
30 changes: 15 additions & 15 deletions PocketKit/Tests/SyncTests/Fixtures/large-list-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -100,7 +100,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -158,7 +158,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -216,7 +216,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -274,7 +274,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -332,7 +332,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -390,7 +390,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -448,7 +448,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -506,7 +506,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -564,7 +564,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -622,7 +622,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -680,7 +680,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -738,7 +738,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -796,7 +796,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down Expand Up @@ -854,7 +854,7 @@
"authors": [],
"excerpt": "Cursus Aenean Elit",
"title": "Item 1",
"datePublished": "2021-01-01 12:01:01",
"datePublished": "2021-01-01T12:01:01.000Z",
"image": {
"__typename": "[type-name-here]",
"url": "https://example.com/archived-item--2/top-image.jpg"
Expand Down
Loading

0 comments on commit 995fdae

Please sign in to comment.