forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryEditorSheets.swift
More file actions
159 lines (140 loc) · 4.82 KB
/
Copy pathMemoryEditorSheets.swift
File metadata and controls
159 lines (140 loc) · 4.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
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
//
// MemoryEditorSheets.swift — the two small text sheets that create and edit a memory.
//
// A memory is one string, so both sheets are the same form twice: a title, a `TextEditor` bound to
// the view model's draft, and Cancel/Save. They are presented from `MemoriesPage` and from the
// memory detail panel, and own no state of their own beyond the dismiss they were handed.
//
import OmiTheme
import SwiftUI
// MARK: - Add Memory Sheet
struct AddMemorySheet: View {
@ObservedObject var viewModel: MemoriesViewModel
var onDismiss: (() -> Void)? = nil
@Environment(\.dismiss) private var environmentDismiss
private func dismissSheet() {
viewModel.newMemoryText = ""
if let onDismiss = onDismiss {
onDismiss()
} else {
environmentDismiss()
}
}
var body: some View {
VStack(spacing: OmiSpacing.xl) {
// Header with close button
HStack {
Text("Add Memory")
.scaledFont(size: OmiType.heading, weight: .semibold)
.foregroundColor(Ink.primary)
Spacer()
DismissButton(action: dismissSheet)
}
TextEditor(text: $viewModel.newMemoryText)
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.primary)
.scrollContentBackground(.hidden)
.padding(OmiSpacing.md)
.background(Ink.rowFillHover)
.cornerRadius(OmiChrome.elementRadius)
.frame(height: 150)
HStack(spacing: OmiSpacing.md) {
// Cancel button
Button(action: dismissSheet) {
Text("Cancel")
.foregroundColor(Ink.secondary)
}
Spacer()
Button {
Task { await viewModel.createMemory() }
} label: {
Text("Save")
.scaledFont(size: OmiType.body, weight: .medium)
.foregroundColor(viewModel.newMemoryText.isEmpty ? Ink.secondary : Ink.surface)
.padding(.horizontal, OmiSpacing.xl)
.padding(.vertical, OmiSpacing.sm)
.background(
// Ink.surface is this label's own colour, so filling with it painted white on
// white and the button read as blank the moment the field had text. The edit
// sheet's identical button already fills with Ink.primary.
viewModel.newMemoryText.isEmpty ? Ink.rowFillHover : Ink.primary
)
.cornerRadius(OmiChrome.elementRadius)
.overlay(
RoundedRectangle(cornerRadius: OmiChrome.elementRadius)
.stroke(
viewModel.newMemoryText.isEmpty ? Color.clear : Ink.separator, lineWidth: 1)
)
}
.buttonStyle(.plain)
.disabled(viewModel.newMemoryText.isEmpty)
}
}
.padding(OmiSpacing.xxl)
.frame(width: 400)
.background(Ink.rowFill)
}
}
// MARK: - Edit Memory Sheet
struct EditMemorySheet: View {
let memory: ServerMemory
@ObservedObject var viewModel: MemoriesViewModel
var onDismiss: (() -> Void)? = nil
@Environment(\.dismiss) private var environmentDismiss
private func dismissSheet() {
viewModel.editText = ""
if let onDismiss = onDismiss {
onDismiss()
} else {
environmentDismiss()
}
}
var body: some View {
VStack(spacing: OmiSpacing.xl) {
// Header with close button
HStack {
Text("Edit Memory")
.scaledFont(size: OmiType.heading, weight: .semibold)
.foregroundColor(Ink.primary)
Spacer()
DismissButton(action: dismissSheet)
}
TextEditor(text: $viewModel.editText)
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.primary)
.scrollContentBackground(.hidden)
.padding(OmiSpacing.md)
.background(Ink.rowFillHover)
.cornerRadius(OmiChrome.elementRadius)
.frame(height: 150)
HStack(spacing: OmiSpacing.md) {
// Cancel button
Button(action: dismissSheet) {
Text("Cancel")
.foregroundColor(Ink.secondary)
}
Spacer()
Button {
Task { await viewModel.saveEditedMemory(memory) }
} label: {
Text("Save")
.scaledFont(size: OmiType.body, weight: .medium)
.foregroundColor(viewModel.editText.isEmpty ? Ink.secondary : Ink.surface)
.padding(.horizontal, OmiSpacing.xl)
.padding(.vertical, OmiSpacing.sm)
.background(viewModel.editText.isEmpty ? Ink.rowFillHover : Ink.primary)
.cornerRadius(OmiChrome.elementRadius)
.overlay(
RoundedRectangle(cornerRadius: OmiChrome.elementRadius)
.stroke(viewModel.editText.isEmpty ? Color.clear : Ink.separator, lineWidth: 1)
)
}
.buttonStyle(.plain)
.disabled(viewModel.editText.isEmpty)
}
}
.padding(OmiSpacing.xxl)
.frame(width: 400)
.background(Ink.rowFill)
}
}