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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
import SwiftUI
struct PasteListView: View {
@Environment(AppState.self) private var appState
@State private var viewModel: PasteListViewModel?
@State private var showCreatePasteSheet = false
@State private var createdPaste: Paste?
var body: some View {
Group {
if let viewModel {
content(viewModel)
} else {
SRHTLoadingStateView(message: "Loading pastes…")
}
}
.navigationTitle("Pastes")
.toolbar {
if viewModel != nil {
ToolbarItem(placement: .topBarTrailing) {
Button {
showCreatePasteSheet = true
} label: {
Image(systemName: "plus")
}
}
}
}
.sheet(isPresented: $showCreatePasteSheet) {
if let viewModel {
CreatePasteSheet(viewModel: viewModel) { paste in
showCreatePasteSheet = false
createdPaste = paste
}
}
}
.navigationDestination(isPresented: Binding(
get: { createdPaste != nil },
set: { isPresented in
if !isPresented {
createdPaste = nil
}
}
)) {
if let createdPaste {
PasteDetailView(
paste: createdPaste,
onUpdated: { updated in
viewModel?.upsertPaste(updated)
},
onDeleted: { id in
viewModel?.removePaste(id: id)
}
)
}
}
.task {
if viewModel == nil {
let vm = PasteListViewModel(service: PasteService(client: appState.client))
viewModel = vm
await vm.loadPastes()
}
}
}
@ViewBuilder
private func content(_ viewModel: PasteListViewModel) -> some View {
@Bindable var vm = viewModel
List {
ForEach(viewModel.pastes) { paste in
NavigationLink(value: paste) {
PasteRowView(paste: paste)
}
.task {
await viewModel.loadMoreIfNeeded(currentItem: paste)
}
}
if viewModel.isLoadingMore {
HStack {
Spacer()
ProgressView()
Spacer()
}
.listRowSeparator(.hidden)
}
}
.listStyle(.plain)
.overlay {
if viewModel.isLoading, viewModel.pastes.isEmpty {
SRHTLoadingStateView(message: "Loading pastes…")
} else if let error = viewModel.error, viewModel.pastes.isEmpty {
SRHTErrorStateView(
title: "Couldn't Load Pastes",
message: error,
retryAction: { await viewModel.loadPastes() }
)
} else if viewModel.pastes.isEmpty {
ContentUnavailableView(
"No Pastes",
systemImage: "doc.on.clipboard",
description: Text("Your pastes will appear here.")
)
}
}
.connectivityOverlay(hasContent: !viewModel.pastes.isEmpty) {
await viewModel.loadPastes()
}
.srhtErrorBanner(error: $vm.error)
.refreshable {
await viewModel.loadPastes()
}
.navigationDestination(for: Paste.self) { paste in
PasteDetailView(
paste: paste,
onUpdated: { updated in
viewModel.upsertPaste(updated)
},
onDeleted: { id in
viewModel.removePaste(id: id)
}
)
}
}
}
private struct PasteRowView: View {
let paste: Paste
var body: some View {
HStack(alignment: .top, spacing: 12) {
Image(systemName: "doc.text")
.foregroundStyle(.secondary)
.frame(width: 20)
VStack(alignment: .leading, spacing: 4) {
Text(primaryTitle)
.font(.subheadline.weight(.medium))
.lineLimit(1)
Text(secondaryLine)
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(2)
HStack(spacing: 8) {
VisibilityBadge(visibility: paste.visibility)
Text("•")
.foregroundStyle(.tertiary)
Text(paste.created.relativeDescription)
.foregroundStyle(.tertiary)
}
.font(.caption2)
}
}
.padding(.vertical, 2)
}
private var primaryTitle: String {
if let filename = paste.files.first?.filename, !filename.isEmpty {
return filename
}
return paste.files.count > 1 ? "Untitled Paste (\(paste.files.count) files)" : "Untitled Paste"
}
private var secondaryLine: String {
var parts: [String] = [paste.user.canonicalName]
if paste.files.count > 1 {
parts.append("\(paste.files.count) files")
} else {
parts.append("1 file")
}
if let firstHash = paste.files.first?.hash {
parts.append(String(firstHash.prefix(8)))
}
return parts.joined(separator: " • ")
}
}
private struct CreatePasteSheet: View {
let viewModel: PasteListViewModel
let onCreated: (Paste) -> Void
@Environment(\.dismiss) private var dismiss
@State private var files = [PasteUploadDraft()]
@State private var visibility: Visibility = .unlisted
var body: some View {
NavigationStack {
Form {
Section("Files") {
ForEach($files) { $file in
VStack(alignment: .leading, spacing: 8) {
TextField("Filename (optional)", text: $file.filename)
.autocorrectionDisabled()
.textInputAutocapitalization(.never)
ZStack(alignment: .topLeading) {
if file.contents.isEmpty {
Text("Paste contents")
.foregroundStyle(.tertiary)
.padding(.top, 8)
.padding(.leading, 5)
.allowsHitTesting(false)
}
TextEditor(text: $file.contents)
.font(.system(.body, design: .monospaced))
.frame(minHeight: 180)
}
}
.padding(.vertical, 4)
}
.onDelete { offsets in
files.remove(atOffsets: offsets)
if files.isEmpty {
files = [PasteUploadDraft()]
}
}
Button {
files.append(PasteUploadDraft())
} label: {
Label("Add File", systemImage: "plus")
}
}
Section("Visibility") {
Picker("Visibility", selection: $visibility) {
Text("Public").tag(Visibility.public)
Text("Unlisted").tag(Visibility.unlisted)
Text("Private").tag(Visibility.private)
}
}
Section {
Text("Paste contents are uploaded as UTF-8 text files. Hutch can change visibility later, but the API does not support editing file contents after creation.")
.font(.footnote)
.foregroundStyle(.secondary)
}
if let error = viewModel.error {
Section {
Label(error, systemImage: "exclamationmark.triangle.fill")
.foregroundStyle(.red)
}
}
}
.navigationTitle("New Paste")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") { dismiss() }
}
ToolbarItem(placement: .confirmationAction) {
Button {
Task {
if let paste = await viewModel.createPaste(files: files, visibility: visibility) {
onCreated(paste)
}
}
} label: {
if viewModel.isCreatingPaste {
ProgressView()
.controlSize(.small)
} else {
Text("Create Paste")
}
}
.disabled(!hasValidContent || viewModel.isCreatingPaste)
}
}
}
}
private var hasValidContent: Bool {
files.contains { !$0.contents.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }
}
}
|