forked from pointfreeco/sqlite-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncUpForm.swift
More file actions
186 lines (169 loc) · 4.45 KB
/
SyncUpForm.swift
File metadata and controls
186 lines (169 loc) · 4.45 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import SQLiteData
import SwiftUI
import SwiftUINavigation
@Observable
final class SyncUpFormModel: Identifiable {
var attendees: [AttendeeDraft] = []
var focus: Field?
var isDismissed = false
var syncUp: SyncUp.Draft
@ObservationIgnored @Dependency(\.defaultDatabase) var database
@ObservationIgnored @Dependency(\.uuid) var uuid
struct AttendeeDraft: Identifiable {
let id: UUID
var name = ""
}
enum Field: Hashable {
case attendee(AttendeeDraft.ID)
case title
}
init(
syncUp: SyncUp.Draft,
focus: Field? = .title
) {
self.syncUp = syncUp
self.focus = focus
defer {
if attendees.isEmpty {
self.attendees.append(AttendeeDraft(id: uuid()))
}
}
guard let syncUpID = syncUp.id
else { return }
withErrorReporting {
self.attendees = try database.read { db in
try Attendee.all
.where { $0.syncUpID.eq(syncUpID) }
.fetchAll(db)
.map { (attendee: Attendee) in AttendeeDraft(id: uuid(), name: attendee.name) }
}
}
}
func deleteAttendees(atOffsets indices: IndexSet) {
attendees.remove(atOffsets: indices)
if attendees.isEmpty {
attendees.append(AttendeeDraft(id: uuid()))
}
guard let firstIndex = indices.first
else { return }
let index = min(firstIndex, attendees.count - 1)
focus = .attendee(attendees[index].id)
}
func addAttendeeButtonTapped() {
let attendee = AttendeeDraft(id: uuid())
attendees.append(attendee)
focus = .attendee(attendee.id)
}
func cancelButtonTapped() {
isDismissed = true
}
func saveButtonTapped() {
attendees.removeAll { attendee in
attendee.name.allSatisfy(\.isWhitespace)
}
if attendees.isEmpty {
attendees.append(SyncUpFormModel.AttendeeDraft(id: uuid()))
}
withErrorReporting {
try database.write { db in
let syncUpID = try SyncUp.upsert { syncUp }.returning(\.id).fetchOne(db)!
try Attendee.where { $0.syncUpID == syncUpID }.delete().execute(db)
try Attendee.insert {
for attendee in attendees {
Attendee.Draft(name: attendee.name, syncUpID: syncUpID)
}
}
.execute(db)
}
}
isDismissed = true
}
}
struct SyncUpFormView: View {
@Environment(\.dismiss) var dismiss
@FocusState var focus: SyncUpFormModel.Field?
@Bindable var model: SyncUpFormModel
var body: some View {
Form {
Section {
TextField("Title", text: $model.syncUp.title)
.focused($focus, equals: .title)
HStack {
Slider(value: $model.syncUp.seconds.toDouble, in: 5...30, step: 1) {
Text("Length")
}
Spacer()
Text(model.syncUp.seconds.duration.formatted(.units()))
}
ThemePicker(selection: $model.syncUp.theme)
} header: {
Text("Sync-up Info")
}
Section {
ForEach($model.attendees) { $attendee in
TextField("Name", text: $attendee.name)
.focused($focus, equals: .attendee(attendee.id))
}
.onDelete { indices in
model.deleteAttendees(atOffsets: indices)
}
Button("New attendee") {
model.addAttendeeButtonTapped()
}
} header: {
Text("Attendees")
}
}
.bind($model.focus, to: $focus)
.onChange(of: model.isDismissed) {
dismiss()
}
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
model.cancelButtonTapped()
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Save") {
model.saveButtonTapped()
}
}
}
}
}
extension Int {
fileprivate var toDouble: Double {
get { Double(self) }
set { self = Int(newValue) }
}
}
struct ThemePicker: View {
@Binding var selection: Theme
var body: some View {
Picker("Theme", selection: $selection) {
ForEach(Theme.allCases) { theme in
ZStack {
RoundedRectangle(cornerRadius: 4)
.fill(theme.mainColor)
Label(theme.name, systemImage: "paintpalette")
.padding(4)
}
.foregroundColor(theme.accentColor)
.fixedSize(horizontal: false, vertical: true)
.tag(theme)
}
}
}
}
struct SyncUpFormPreviews: PreviewProvider {
static var previews: some View {
NavigationStack {
SyncUpFormView(
model: SyncUpFormModel(
syncUp: SyncUp.Draft()
)
)
}
}
}