Skip to content

Commit 5bf3fba

Browse files
mbrandonwstephencelis
authored andcommitted
Better support for sharing in previews. (pointfreeco#367)
* wip * wip * wip * wip * wip * Better support for sharing in previews. * wip * wip * wip * clean up * wip * clean up * Trying out database pool for previews. * wip * wip * clean up * clean up * reformat * cancel timer when stopping sync engine * clean up * wip * wip * wip * clean up * wip * fixes * fix * wip * wip * Revert "fix" This reverts commit 090dd74. * Revert "wip" This reverts commit 43727d5. * Revert "wip" This reverts commit 6d5e42b. * modernize previews * wip * lock isolated * wip * wiup * clean up * Fix merge conflicts. * wip * format * wip * wip --------- Co-authored-by: Stephen Celis <stephen@stephencelis.com>
1 parent 0c2759c commit 5bf3fba

31 files changed

Lines changed: 885 additions & 532 deletions

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
name: macOS
1515
strategy:
1616
matrix:
17-
xcode: ['26.1']
17+
xcode: ['26.2']
1818
config: ['debug', 'release']
1919
runs-on: macos-26
2020
steps:
@@ -28,7 +28,7 @@ jobs:
2828
name: Examples
2929
strategy:
3030
matrix:
31-
xcode: ['26.1']
31+
xcode: ['26.2']
3232
config: ['debug']
3333
scheme: ['Reminders', 'CaseStudies', 'SyncUps']
3434
runs-on: macos-26

Examples/CloudKitDemo/CountersListFeature.swift

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@ import SQLiteData
33
import SwiftUI
44

55
struct CountersListView: View {
6-
@FetchAll var counters: [Counter]
6+
@FetchAll(
7+
Counter
8+
.leftJoin(SyncMetadata.all) { $0.syncMetadataID.eq($1.id) }
9+
.select {
10+
Row.Columns(counter: $0, isShared: $1.isShared.ifnull(false))
11+
}
12+
) var rows
713
@Dependency(\.defaultDatabase) var database
14+
@Dependency(\.defaultSyncEngine) var syncEngine
15+
16+
@Selection struct Row {
17+
let counter: Counter
18+
let isShared: Bool
19+
}
820

921
var body: some View {
1022
List {
11-
if !counters.isEmpty {
23+
if !rows.isEmpty {
1224
Section {
13-
ForEach(counters) { counter in
14-
CounterRow(counter: counter)
25+
ForEach(rows, id: \.counter.id) { row in
26+
CounterRow(row: row)
1527
.buttonStyle(.borderless)
1628
}
1729
.onDelete { indexSet in
@@ -24,10 +36,14 @@ struct CountersListView: View {
2436
.toolbar {
2537
ToolbarItem(placement: .primaryAction) {
2638
Button("Add") {
27-
withErrorReporting {
28-
try database.write { db in
29-
try Counter.insert { Counter.Draft() }
39+
Task {
40+
withErrorReporting {
41+
try database.write { db in
42+
try Counter.insert {
43+
Counter.Draft()
44+
}
3045
.execute(db)
46+
}
3147
}
3248
}
3349
}
@@ -39,7 +55,7 @@ struct CountersListView: View {
3955
withErrorReporting {
4056
try database.write { db in
4157
for index in indexSet {
42-
try Counter.find(counters[index].id).delete()
58+
try Counter.find(rows[index].counter.id).delete()
4359
.execute(db)
4460
}
4561
}
@@ -48,15 +64,18 @@ struct CountersListView: View {
4864
}
4965

5066
struct CounterRow: View {
51-
let counter: Counter
67+
let row: CountersListView.Row
5268
@State var sharedRecord: SharedRecord?
5369
@Dependency(\.defaultDatabase) var database
5470
@Dependency(\.defaultSyncEngine) var syncEngine
5571

5672
var body: some View {
5773
VStack {
5874
HStack {
59-
Text("\(counter.count)")
75+
if row.isShared {
76+
Image(systemName: "network")
77+
}
78+
Text("\(row.counter.count)")
6079
Button("-") {
6180
decrementButtonTapped()
6281
}
@@ -78,7 +97,7 @@ struct CounterRow: View {
7897

7998
func shareButtonTapped() {
8099
Task {
81-
sharedRecord = try await syncEngine.share(record: counter) { share in
100+
sharedRecord = try await syncEngine.share(record: row.counter) { share in
82101
share[CKShare.SystemFieldKey.title] = "Join my counter!"
83102
}
84103
}
@@ -87,7 +106,7 @@ struct CounterRow: View {
87106
func decrementButtonTapped() {
88107
withErrorReporting {
89108
try database.write { db in
90-
try Counter.find(counter.id).update {
109+
try Counter.find(row.counter.id).update {
91110
$0.count -= 1
92111
}
93112
.execute(db)
@@ -98,7 +117,7 @@ struct CounterRow: View {
98117
func incrementButtonTapped() {
99118
withErrorReporting {
100119
try database.write { db in
101-
try Counter.find(counter.id).update {
120+
try Counter.find(row.counter.id).update {
102121
$0.count += 1
103122
}
104123
.execute(db)
@@ -110,6 +129,7 @@ struct CounterRow: View {
110129
#Preview {
111130
let _ = try! prepareDependencies {
112131
try $0.bootstrapDatabase()
132+
try? $0.defaultDatabase.seedSampleData()
113133
}
114134
NavigationStack {
115135
CountersListView()

Examples/CloudKitDemo/Schema.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,16 @@ extension DependencyValues {
4747
}
4848

4949
private let logger = Logger(subsystem: "CloudKitDemo", category: "Database")
50+
51+
#if DEBUG
52+
extension DatabaseWriter {
53+
func seedSampleData() throws {
54+
try write { db in
55+
try db.seed {
56+
Counter.Draft(count: 24)
57+
Counter.Draft(count: 1729)
58+
}
59+
}
60+
}
61+
}
62+
#endif

Examples/Examples.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Examples/Reminders/RemindersDetail.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class RemindersDetailModel: HashableObject {
8484

8585
private func updateQuery() async {
8686
await withErrorReporting {
87-
try await $reminderRows.load(remindersQuery, animation: .default)
87+
_ = try await $reminderRows.load(remindersQuery, animation: .default)
8888
}
8989
}
9090

Examples/Reminders/RemindersLists.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,7 @@ class RemindersListsModel {
144144
#if DEBUG
145145
func seedDatabaseButtonTapped() {
146146
withErrorReporting {
147-
try database.write { db in
148-
try db.seedSampleData()
149-
}
147+
try database.seedSampleData()
150148
}
151149
}
152150
#endif
@@ -437,7 +435,8 @@ private struct ReminderGridCell: View {
437435

438436
#Preview {
439437
let _ = try! prepareDependencies {
440-
$0.defaultDatabase = try Reminders.appDatabase()
438+
try $0.bootstrapDatabase()
439+
try? $0.defaultDatabase.seedSampleData()
441440
}
442441
NavigationStack {
443442
RemindersListsView(model: RemindersListsModel())

0 commit comments

Comments
 (0)