Repository navigation
Expand file tree
/
Copy pathArticleCardView.swift
More file actions
349 lines (328 loc) · 14 KB
/
Copy pathArticleCardView.swift
File metadata and controls
349 lines (328 loc) · 14 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
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
import SwiftUI
/// Rich preview card for a NIP-23 long-form article (kind 30023) referenced
/// inside note content via `nostr:naddr1…`. 1:1 port of Android's
/// `ArticleCard` (`RichContent.kt`): hero image, "ARTICLE" badge + title,
/// summary, author + published date footer. Tapping the card pushes an
/// `ArticleRoute`; tapping the author name routes to their profile.
///
/// State machine mirrors `QuotedNoteView`: loading → loaded / missing
/// (tap-to-retry with one silent auto-retry) plus blocked / safety-hidden
/// stubs so filtered authors never render.
struct ArticleCardView: View {
let dTag: String
let author: String
let relayHints: [String]
let profiles: [String: ProfileData]
var onProfileTap: ((String) -> Void)? = nil
@State private var event: NostrEvent?
@State private var loaded = false
@State private var blocked = false
@State private var safetyHidden = false
@State private var profile: ProfileData?
@State private var attempt: Int = 0
/// One silent redundancy retry on initial miss — broadens the relay set
/// without making the user tap. Matches `QuotedNoteView`.
private static let autoRetryAttempts = 1
var body: some View {
Group {
if blocked {
stubCard(icon: "nosign", text: "Article from a blocked user")
} else if safetyHidden {
stubCard(icon: "eye.slash", text: "Article hidden by your safety filters")
} else if let event {
articleCard(event)
} else if loaded {
missingCard
} else {
loadingCard
}
}
.task(id: TaskKey(dTag: dTag, author: author, attempt: attempt)) { await load() }
// Re-gate in place on safety-filter snapshot installs — same rationale
// as QuotedNoteView: a resolved article keeps rendering after the
// filter tightens (and a hidden one stays hidden after it relaxes)
// unless we re-evaluate here.
.onReceive(NotificationCenter.default.publisher(for: .safetyFilterChanged)) { _ in
if let event,
SafetyFilter.shared.shouldDrop(event: event, context: .feed) {
self.event = nil
safetyHidden = true
loaded = true
} else if safetyHidden {
safetyHidden = false
loaded = false
attempt += 1
}
}
}
/// Composite key so a retry (attempt bump) re-runs `.task` the same way a
/// coordinate change does.
private struct TaskKey: Hashable {
let dTag: String
let author: String
let attempt: Int
}
// MARK: - States
private var loadingCard: some View {
HStack(spacing: 8) {
ProgressView()
.controlSize(.small)
.tint(Color.wispOnSurfaceVariant)
Text("Loading article…")
.font(AppFont.bodyMedium)
.foregroundStyle(Color.wispOnSurfaceVariant)
}
.padding(14)
.frame(maxWidth: .infinity, alignment: .leading)
.background(Color.wispSurface)
.clipShape(RoundedRectangle(cornerRadius: 12))
.overlay(
RoundedRectangle(cornerRadius: 12)
.stroke(Color.wispOutline, lineWidth: 1)
)
.padding(.vertical, 6)
}
private func stubCard(icon: String, text: String) -> some View {
HStack(spacing: 8) {
Image(systemName: icon)
.foregroundStyle(.secondary)
Text(text)
.font(.caption)
.foregroundStyle(.secondary)
}
.padding(12)
.frame(maxWidth: .infinity, alignment: .leading)
.background(Color.wispSurfaceVariant.opacity(0.3))
.clipShape(RoundedRectangle(cornerRadius: 12))
.overlay(
RoundedRectangle(cornerRadius: 12)
.stroke(Color.wispSurfaceVariant, lineWidth: 1)
)
.padding(.vertical, 6)
.accessibilityLabel(text)
}
private var missingCard: some View {
Button {
attempt += 1
} label: {
HStack(spacing: 8) {
Image(systemName: "doc.text")
.foregroundStyle(.secondary)
Text("Article not found")
.font(.caption)
.foregroundStyle(.secondary)
Spacer(minLength: 8)
Image(systemName: "arrow.clockwise")
.font(.caption.weight(.semibold))
.foregroundStyle(Color.wispPrimary)
}
.padding(12)
.frame(maxWidth: .infinity, alignment: .leading)
.background(Color.wispSurfaceVariant.opacity(0.3))
.clipShape(RoundedRectangle(cornerRadius: 12))
.overlay(
RoundedRectangle(cornerRadius: 12)
.stroke(Color.wispSurfaceVariant, lineWidth: 1)
)
}
.buttonStyle(.plain)
.padding(.vertical, 6)
.accessibilityLabel("Article not found. Tap to retry.")
}
// MARK: - Loaded card
private func articleCard(_ event: NostrEvent) -> some View {
let title = firstTag(event, "title")
let summary = firstTag(event, "summary")
let image = firstTag(event, "image")
let publishedAt = firstTag(event, "published_at").flatMap { Int($0) }
return NavigationLink(value: ArticleRoute(author: author, dTag: dTag, relayHints: relayHints)) {
VStack(alignment: .leading, spacing: 0) {
if let image, let imageUrl = URL(string: image) {
heroImage(imageUrl, event: event, urlString: image, title: title)
}
VStack(alignment: .leading, spacing: 0) {
HStack(alignment: .center, spacing: 8) {
Text("ARTICLE")
.font(AppFont.labelSmall)
.foregroundStyle(Color.wispPrimary)
.padding(.horizontal, 6)
.padding(.vertical, 2)
.background(Color.wispPrimary.opacity(0.18), in: RoundedRectangle(cornerRadius: 4))
Text(title ?? "Untitled Article")
.font(AppFont.scaled(14, weight: .semibold))
.foregroundStyle(Color.wispOnSurface)
.lineLimit(2)
.multilineTextAlignment(.leading)
.frame(maxWidth: .infinity, alignment: .leading)
}
if let summary, !summary.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
Text(summary)
.font(AppFont.bodySmall)
.foregroundStyle(Color.wispOnSurfaceVariant)
.lineLimit(3)
.multilineTextAlignment(.leading)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.top, 4)
}
HStack(spacing: 0) {
// Author name + avatar route to the profile; the rest
// of the card opens the article via the surrounding
// NavigationLink.
Button {
onProfileTap?(author)
} label: {
HStack(spacing: 6) {
CachedAvatarView(url: profile?.picture, size: 20)
EmojiText(
profile?.displayString ?? Nip19.shortNpub(hex: author),
emojiMap: profile?.emojiMap ?? [:],
textStyle: .caption1,
weight: .medium,
color: UIColor(Color.wispOnSurfaceVariant)
)
}
}
.buttonStyle(.plain)
if let publishedAt {
Text(" · \(Self.formatQuotedTimestamp(publishedAt))")
.font(AppFont.scaled(12, weight: .medium))
.foregroundStyle(Color.wispOnSurfaceVariant.opacity(0.7))
}
Spacer(minLength: 0)
}
.padding(.top, 6)
}
.padding(.horizontal, 14)
.padding(.vertical, 10)
}
.frame(maxWidth: .infinity, alignment: .leading)
.background(Color.wispSurface)
.clipShape(RoundedRectangle(cornerRadius: 12))
.overlay(
RoundedRectangle(cornerRadius: 12)
.stroke(Color.wispOutline, lineWidth: 1)
)
}
.buttonStyle(.plain)
.padding(.vertical, 6)
}
/// Hero image: aspect ratio from the article's NIP-92 imeta tag when
/// present, else capped at 180pt — matches Android's
/// `aspectRatio(ratio) / heightIn(max = 180.dp)` + Crop scaling.
@ViewBuilder
private func heroImage(_ url: URL, event: NostrEvent, urlString: String, title: String?) -> some View {
let meta = ContentParser.parseImetaTags(event.tags)[urlString]
let ratio = ContentParser.parseAspectRatio(meta?.dimension)
// A malformed imeta dim ("0x100") parses to ratio 0 — treat any
// non-positive ratio as missing so the frame never degenerates.
let safeRatio = (ratio ?? 0) > 0 ? ratio! : heroFallbackRatio
Color.clear
.aspectRatio(safeRatio, contentMode: .fit)
.frame(maxWidth: .infinity)
.overlay(
RetryingAsyncImage(
url: url,
content: { image in
image
.resizable()
.scaledToFill()
},
loading: {
Rectangle().fill(Color.wispSurfaceVariant.opacity(0.4))
.overlay(ProgressView().controlSize(.small))
},
failure: {
Rectangle().fill(Color.wispSurfaceVariant.opacity(0.4))
}
)
)
.clipped()
.accessibilityLabel(title ?? "Article image")
}
/// Without imeta dimensions Android caps the hero at 180dp tall; a 2:1
/// frame approximates that for typical card widths.
private var heroFallbackRatio: CGFloat { 2.0 }
/// Port of Android's `formatQuotedTimestamp`: "Ns" / "Nm" / "Nh" /
/// "yesterday" / "MMM d" — different bucketing from the app-wide
/// `relativeTime(from:)`, kept for card parity.
static func formatQuotedTimestamp(_ epoch: Int) -> String {
let seconds = max(0, Int(Date().timeIntervalSince1970) - epoch)
let minutes = seconds / 60
let hours = minutes / 60
let days = hours / 24
if seconds < 60 { return "\(seconds)s" }
if minutes < 60 { return "\(minutes)m" }
if hours < 24 { return "\(hours)h" }
if days == 1 { return "yesterday" }
let formatter = DateFormatter()
formatter.dateFormat = "MMM d"
formatter.locale = Locale(identifier: "en_US_POSIX")
return formatter.string(from: Date(timeIntervalSince1970: TimeInterval(epoch)))
}
// MARK: - Loading
private func firstTag(_ event: NostrEvent, _ name: String) -> String? {
event.tags.first(where: { $0.count >= 2 && $0[0] == name })?[1]
}
private func load() async {
if SafetyFilter.shared.snapshot.blockedPubkeys.contains(author) {
blocked = true
loaded = true
return
}
if let cached = ArticleCache.shared.cached(author: author, dTag: dTag) {
if SafetyFilter.shared.shouldDrop(event: cached, context: .feed) {
self.safetyHidden = true
loaded = true
return
}
self.event = cached
self.profile = profiles[author] ?? ProfileRepository.shared.get(author)
loaded = true
return
}
// Re-enter the loading state so a tap-to-retry hides the missing
// card while the next attempt is in flight.
loaded = false
event = nil
let result: NostrEvent?
if attempt == 0 {
result = await ArticleCache.shared.fetch(author: author, dTag: dTag, relayHints: relayHints)
} else {
// Brief backoff before broader retries — same curve as QuotedNoteView.
let delay = min(3.0, 0.75 * Double(attempt))
try? await Task.sleep(for: .seconds(delay))
if Task.isCancelled { return }
result = await ArticleCache.shared.refetch(
author: author,
dTag: dTag,
relayHints: relayHints,
attempt: attempt
)
}
if Task.isCancelled { return }
if let result {
if SafetyFilter.shared.shouldDrop(event: result, context: .feed) {
self.safetyHidden = true
loaded = true
return
}
self.event = result
self.profile = profiles[author] ?? ProfileRepository.shared.get(author)
loaded = true
// The card may render before the author's profile has resolved —
// hydrate it in the background so the footer fills in.
if self.profile == nil {
Task {
let got = await ProfileRepository.shared.ensure([author])
if let p = got[author] { self.profile = p }
}
}
return
}
if attempt < Self.autoRetryAttempts {
attempt += 1
} else {
loaded = true
}
}
}