Repository navigation
Expand file tree
/
Copy pathObservableModelDemo.swift
More file actions
120 lines (109 loc) · 2.82 KB
/
Copy pathObservableModelDemo.swift
File metadata and controls
120 lines (109 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import SQLiteData
import SwiftUI
struct ObservableModelDemo: SwiftUICaseStudy {
let readMe = """
This demonstrates how to use the `@FetchAll` and `@FetchOne` tools in an `@Observable` model. \
In SwiftUI, the `@Query` macro only works when installed directly in a SwiftUI view, and \
cannot be used outside of views.
The tools provided with this library work basically anywhere, including in `@Observable` \
models and UIKit view controllers.
"""
let caseStudyTitle = "@Observable Model"
@State private var model = Model()
var body: some View {
List {
Section {
Text("Facts: \(model.factsCount)")
.font(.largeTitle)
.bold()
.contentTransition(.numericText(value: Double(model.factsCount)))
}
Section {
ForEach(model.facts) { fact in
Text(fact.body)
}
.onDelete { indices in
model.deleteFact(indices: indices)
}
}
}
.task {
do {
while true {
try await Task.sleep(for: .seconds(1))
await model.increment()
}
} catch {}
}
}
}
@Observable
private class Model {
@ObservationIgnored
@FetchAll(Fact.order { $0.id.desc() }, animation: .default)
var facts
@ObservationIgnored
@FetchOne(Fact.count(), animation: .default)
var factsCount = 0
var number = 0
@ObservationIgnored
@Dependency(\.defaultDatabase) var database
func increment() async {
number += 1
await withErrorReporting {
let fact = try await String(
decoding: URLSession.shared
.data(from: URL(string: "http://number-trivia.com/\(number)")!).0,
as: UTF8.self
)
try await database.write { db in
try Fact.insert {
Fact.Draft(body: fact)
}
.execute(db)
}
}
}
func deleteFact(indices: IndexSet) {
withErrorReporting {
try database.write { db in
let ids = indices.map { facts[$0].id }
try Fact
.where { $0.id.in(ids) }
.delete()
.execute(db)
}
}
}
}
@Table
nonisolated private struct Fact: Identifiable {
let id: Int
var body: String
}
extension DatabaseWriter where Self == DatabaseQueue {
static var observableModelDatabase: Self {
let databaseQueue = try! DatabaseQueue()
var migrator = DatabaseMigrator()
migrator.registerMigration("Create 'facts' table") { db in
try #sql(
"""
CREATE TABLE "facts" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"body" TEXT NOT NULL
) STRICT
"""
)
.execute(db)
}
try! migrator.migrate(databaseQueue)
return databaseQueue
}
}
#Preview(traits: .dependencies { $0.defaultDatabase = .observableModelDatabase }) {
NavigationStack {
CaseStudyView {
ObservableModelDemo()
}
}
}