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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
import SwiftUI
struct PasteListView: View {
@AppStorage(AppStorageKeys.swipeActionsEnabled, store: .standard) private var swipeActionsEnabled = true
@Environment(AppState.self) private var appState
@State private var viewModel: PasteListViewModel?
@State private var showCreatePasteSheet = false
@State private var createdPaste: Paste?
@State private var pasteToDelete: 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.filteredPastes) { paste in
NavigationLink(value: paste) {
PasteRowView(paste: paste)
.equatable()
}
.swipeActions(edge: .leading, allowsFullSwipe: true) {
if swipeActionsEnabled {
Button {
Task { await viewModel.cycleVisibility(for: paste) }
} label: {
Label(
nextVisibilityLabel(for: paste.visibility),
systemImage: nextVisibilityIcon(for: paste.visibility)
)
}
.tint(nextVisibilityColor(for: paste.visibility))
}
}
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
if swipeActionsEnabled {
Button(role: .destructive) {
pasteToDelete = paste
} label: {
Label("Delete", systemImage: "trash")
}
}
}
.task {
await viewModel.loadMoreIfNeeded(currentItem: paste)
}
}
.themedRow()
if viewModel.isLoadingMore {
HStack {
Spacer()
ProgressView()
Spacer()
}
.listRowSeparator(.hidden)
.themedRow()
}
}
.themedList()
.listStyle(.plain)
.searchable(
text: $vm.searchText,
placement: .navigationBarDrawer(displayMode: .always),
prompt: "Search pastes"
)
.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, viewModel.filteredPastes.isEmpty {
ContentUnavailableView.search(text: viewModel.searchText)
} 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)
.alert("Delete Paste?", isPresented: Binding(
get: { pasteToDelete != nil },
set: { if !$0 { pasteToDelete = nil } }
)) {
Button("Cancel", role: .cancel) {
pasteToDelete = nil
}
Button("Delete", role: .destructive) {
if let paste = pasteToDelete {
pasteToDelete = nil
Task {
await viewModel.deletePaste(paste)
}
}
}
} message: {
Text("This paste will be permanently deleted from SourceHut.")
}
.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 func nextVisibilityLabel(for visibility: Visibility) -> String {
switch visibility {
case .publicVisibility:
return "Make Unlisted"
case .unlisted:
return "Make Private"
case .privateVisibility:
return "Make Public"
}
}
private func nextVisibilityIcon(for visibility: Visibility) -> String {
switch visibility {
case .publicVisibility:
return "eye.slash"
case .unlisted:
return "lock"
case .privateVisibility:
return "globe"
}
}
private func nextVisibilityColor(for visibility: Visibility) -> Color {
switch visibility {
case .publicVisibility:
return .orange
case .unlisted:
return .red
case .privateVisibility:
return .green
}
}
}
private struct PasteRowView: View, Equatable {
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()]
}
}
.themedRow()
Button {
files.append(PasteUploadDraft())
} label: {
Label("Add File", systemImage: "plus")
}
.themedRow()
}
Section("Visibility") {
Picker("Visibility", selection: $visibility) {
Text("Public").tag(Visibility.publicVisibility)
Text("Unlisted").tag(Visibility.unlisted)
Text("Private").tag(Visibility.privateVisibility)
}
.themedRow()
}
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)
.themedRow()
}
if let error = viewModel.error {
Section {
Label(error, systemImage: "exclamationmark.triangle.fill")
.foregroundStyle(.red)
.themedRow()
}
}
}
.themedList()
.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 }
}
}
|