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
import SwiftUI
private enum DraftRetryAction {
case load, open(SavedPostDraft), delete(SavedPostDraft)
}
struct SavedDraftsView: View {
@Environment(AppModel.self) private var model
@Environment(\.dismiss) private var dismiss
@Environment(\.accessibilityReduceMotion) private var reduceMotion
let openDraft: (SavedPostDraft) async throws -> Void
@State private var drafts: [SavedPostDraft] = []
@State private var cursor: String?
@State private var isLoading = false
@State private var showsLoadingIndicator = false
@State private var hasLoaded = false
@State private var errorMessage: String?
@State private var activeDraftID: String?
@State private var retryAction = DraftRetryAction.load
private var showsEmptyState: Bool {
hasLoaded && drafts.isEmpty && cursor == nil && errorMessage == nil && !isLoading
}
var body: some View {
NavigationStack {
List {
ForEach(drafts) { draft in
Button {
Task { await open(draft) }
} label: {
row(draft)
}
.disabled(activeDraftID != nil || draft.unavailableReason != nil)
.accessibilityIdentifier("savedDraft-\(draft.id)")
.swipeActions {
Button("Delete", role: .destructive) {
Task { await delete(draft) }
}
.disabled(activeDraftID != nil)
}
}
if isLoading && !drafts.isEmpty {
ProgressView().frame(maxWidth: .infinity)
} else if !isLoading && !drafts.isEmpty && cursor != nil {
Button("Load more") { Task { await loadMore() } }
}
if let errorMessage {
Text(errorMessage).foregroundStyle(.secondary)
Button("Try again") { Task { await retry() } }
.disabled(isLoading || activeDraftID != nil)
}
}
.overlay {
ZStack {
if showsEmptyState {
ContentUnavailableView(
"No drafts", systemImage: "doc",
description: Text("Saved posts appear here."))
.transition(.asymmetric(insertion: .opacity, removal: .identity))
} else if isLoading && drafts.isEmpty && showsLoadingIndicator {
ProgressView("Loading drafts")
.transition(.identity)
}
}
.animation(reduceMotion ? nil : .easeIn(duration: 0.2), value: showsEmptyState)
}
.appNavigationTitle("Drafts")
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Done") { dismiss() }
.disabled(activeDraftID != nil)
}
}
.interactiveDismissDisabled(activeDraftID != nil)
.task { if !hasLoaded { await loadMore() } }
.task(id: isLoading) {
showsLoadingIndicator = false
guard isLoading else { return }
do { try await Task.sleep(for: .milliseconds(300)) } catch { return }
guard !Task.isCancelled else { return }
showsLoadingIndicator = true
}
}
}
private func row(_ draft: SavedPostDraft) -> some View {
VStack(alignment: .leading, spacing: 4) {
Text(draft.text.isEmpty ? "Post with attachments" : draft.text)
.foregroundStyle(.primary)
.lineLimit(3)
HStack {
if draft.replyTo != nil { Label("Reply", systemImage: "arrow.turn.down.right") }
if draft.quotePostID != nil { Label("Quote", systemImage: "quote.bubble") }
if draft.imageCount > 0 { Label("\(draft.imageCount)", systemImage: "photo") }
if draft.hasVideo { Label("Video", systemImage: "video") }
Text(draft.updatedAt, style: .date)
if activeDraftID == draft.id { ProgressView() }
}
.appFont(.caption)
.foregroundStyle(.secondary)
if let reason = draft.unavailableReason {
Text(reason).appFont(.caption).foregroundStyle(.secondary)
}
}
}
private func loadMore() async {
guard !isLoading else { return }
isLoading = true
errorMessage = nil
defer { isLoading = false }
do {
var requestedCursors: Set<String> = []
repeat {
if let cursor, !requestedCursors.insert(cursor).inserted { throw ServiceError.invalidResponse }
let page = try await model.loadSavedDrafts(cursor: cursor)
let existing = Set(drafts.map(\.id))
drafts.append(contentsOf: page.drafts.filter { !existing.contains($0.id) })
cursor = page.cursor.flatMap { $0.isEmpty ? nil : $0 }
hasLoaded = true
} while drafts.isEmpty && cursor != nil
} catch {
errorMessage = NetworkFailure.message(for: error)
retryAction = .load
}
}
private func open(_ draft: SavedPostDraft) async {
activeDraftID = draft.id
errorMessage = nil
defer { activeDraftID = nil }
do {
try await openDraft(draft)
} catch {
errorMessage = NetworkFailure.message(for: error)
retryAction = .open(draft)
}
}
private func delete(_ draft: SavedPostDraft) async {
activeDraftID = draft.id
errorMessage = nil
defer { activeDraftID = nil }
do {
try await model.deleteSavedDraft(id: draft.id)
drafts.removeAll { $0.id == draft.id }
if drafts.isEmpty && cursor != nil { await loadMore() }
} catch {
errorMessage = NetworkFailure.message(for: error)
retryAction = .delete(draft)
}
}
private func retry() async {
switch retryAction {
case .load: await loadMore()
case .open(let draft): await open(draft)
case .delete(let draft): await delete(draft)
}
}
}