Skip to content

Commit 2a1b357

Browse files
committed
Initial commit
1 parent d0dbf93 commit 2a1b357

File tree

31 files changed

+1244
-0
lines changed

31 files changed

+1244
-0
lines changed

DB/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>DB-Package.xcscheme_^#shared#^_</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>0</integer>
11+
</dict>
12+
<key>DB.xcscheme_^#shared#^_</key>
13+
<dict>
14+
<key>orderHint</key>
15+
<integer>1</integer>
16+
</dict>
17+
<key>DBSwiftData.xcscheme_^#shared#^_</key>
18+
<dict>
19+
<key>orderHint</key>
20+
<integer>2</integer>
21+
</dict>
22+
</dict>
23+
<key>SuppressBuildableAutocreation</key>
24+
<dict>
25+
<key>DB</key>
26+
<dict>
27+
<key>primary</key>
28+
<true/>
29+
</dict>
30+
</dict>
31+
</dict>
32+
</plist>

DB/Package.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// swift-tools-version: 5.9
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "DB",
8+
platforms: [.iOS(.v17)],
9+
products: [
10+
.library(name: "DB", targets: [
11+
"DB"
12+
]),
13+
.library(name: "DBSwiftData", targets: [
14+
"DBSwiftData"
15+
])
16+
],
17+
targets: [
18+
.target(name: "DB"),
19+
.target(name: "DBSwiftData", dependencies: [
20+
"DB"
21+
])
22+
]
23+
)

DB/Sources/DB/Entry.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Foundation
2+
3+
public protocol Entry: AnyObject, Identifiable, Equatable, Observable {
4+
var date: Date { get }
5+
var isEnabled: Bool { get set }
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Foundation
2+
3+
public protocol EntryRepository: AnyObject, Observable {
4+
associatedtype EntryType: Entry
5+
var models: [EntryType] { get }
6+
func addEntry()
7+
func deleteEntry(_ entry: EntryType)
8+
func fetchModels() throws
9+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import CoreData
2+
import Foundation
3+
import SwiftData
4+
5+
@Observable
6+
final class FetchedResultsController<T: PersistentModel> {
7+
private(set) var models: [T] = []
8+
9+
private let modelContext: ModelContext
10+
private let predicate: Predicate<T>?
11+
private let sortDescriptors: [SortDescriptor<T>]
12+
13+
init(
14+
modelContext: ModelContext,
15+
predicate: Predicate<T>? = nil,
16+
sortDescriptors: [SortDescriptor<T>] = []
17+
) {
18+
self.modelContext = modelContext
19+
self.predicate = predicate
20+
self.sortDescriptors = sortDescriptors
21+
setupNotification()
22+
}
23+
24+
func fetch() throws {
25+
let fetchDesciptor = FetchDescriptor<T>(predicate: predicate, sortBy: sortDescriptors)
26+
models = try modelContext.fetch(fetchDesciptor)
27+
}
28+
29+
private func setupNotification() {
30+
// Ideally we'd use the ModelContext.didSave notification but this doesn't seem to be sent.
31+
// Last tested with iOS 17 beta 8 on September 4th, 2023.
32+
NotificationCenter.default.addObserver(
33+
self,
34+
selector: #selector(didSave),
35+
name: .NSPersistentStoreRemoteChange,
36+
object: nil
37+
)
38+
}
39+
40+
@objc private func didSave(_ notification: Notification) {
41+
do {
42+
try fetch()
43+
} catch {}
44+
}
45+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import SwiftData
2+
3+
public struct SwiftDataDB {
4+
public let modelContainer: ModelContainer
5+
6+
public init(isStoredInMemoryOnly: Bool) {
7+
let modelConfiguration = ModelConfiguration(
8+
isStoredInMemoryOnly: isStoredInMemoryOnly
9+
)
10+
modelContainer = try! ModelContainer(
11+
for: SwiftDataEntry.self,
12+
configurations: modelConfiguration
13+
)
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import DB
2+
import Foundation
3+
import SwiftData
4+
5+
@Model
6+
public final class SwiftDataEntry: Entry {
7+
public let date: Date
8+
public var isEnabled = false
9+
10+
public init() {
11+
self.date = Date()
12+
}
13+
}

0 commit comments

Comments
 (0)