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
|
import SwiftUI
struct ProjectDetailView: View {
let project: Project
@Environment(AppState.self) private var appState
@Environment(\.dismiss) private var dismiss
@Environment(\.openURL) private var openURL
@State private var detailProject: Project?
@State private var isLoading = false
@State private var error: String?
@State private var pinChangeCount = 0
private var displayedProject: Project {
detailProject ?? project
}
private var currentUserKey: String? {
appState.currentUser?.canonicalName
}
private var isPinnedToHome: Bool {
_ = pinChangeCount
guard let currentUserKey else { return false }
return ProjectPinStore.isPinned(projectID: displayedProject.id, for: currentUserKey, defaults: appState.accountDefaults)
}
var body: some View {
List {
headerSection
repositoriesSection
trackersSection
mailingListsSection
linksSection
emptyResourcesSection
}
.themedList()
.navigationTitle(displayedProject.displayName)
.navigationBarTitleDisplayMode(.inline)
.overlay {
if isLoading, detailProject == nil, !project.isFullyLoaded {
SRHTLoadingStateView(message: "Loading project…")
} else if let error, detailProject == nil, !project.isFullyLoaded {
SRHTErrorStateView(
title: "Couldn't Load Project",
message: error,
retryAction: { await loadProjectIfNeeded(forceRefresh: true) }
)
}
}
.toolbar {
if currentUserKey != nil {
ToolbarItem(placement: .topBarTrailing) {
Button {
togglePinnedState()
} label: {
Image(systemName: isPinnedToHome ? "pin.fill" : "pin")
}
.accessibilityLabel(isPinnedToHome ? "Unpin from Home" : "Pin to Home")
}
}
}
.task {
await loadProjectIfNeeded()
}
.refreshable {
await loadProjectIfNeeded(forceRefresh: true)
}
.srhtErrorBanner(error: $error)
}
@ViewBuilder
private var headerSection: some View {
Section {
VStack(alignment: .leading, spacing: 10) {
Text(displayedProject.displayName)
.font(.headline)
if let description = displayedProject.displayDescription {
Text(description)
.font(.subheadline)
.foregroundStyle(.secondary)
}
if !displayedProject.displayTags.isEmpty {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 8) {
ForEach(displayedProject.displayTags, id: \.self) { tag in
Text(tag)
.font(.caption.weight(.medium))
.padding(.horizontal, 10)
.padding(.vertical, 4)
.background(.quaternary, in: Capsule())
}
}
}
}
LabeledContent("Project", value: displayedProject.visibility.displayName)
LabeledContent("Updated", value: displayedProject.updated.relativeDescription)
if let summary = displayedProject.resourceSummary {
LabeledContent("Linked", value: summary)
}
}
.padding(.vertical, 4)
}
}
@ViewBuilder
private var linksSection: some View {
let links = projectLinks(for: displayedProject)
if !links.isEmpty {
Section("Links") {
ForEach(links) { link in
Button {
openURL(link.url)
} label: {
HStack(spacing: 12) {
Label(link.title, systemImage: link.systemImage)
.font(.subheadline)
.foregroundStyle(.primary)
Spacer()
Image(systemName: "arrow.up.right")
.font(.caption.weight(.semibold))
.foregroundStyle(.tertiary)
}
}
.buttonStyle(.plain)
}
}
}
}
@ViewBuilder
private var repositoriesSection: some View {
if !displayedProject.sources.isEmpty {
Section("Repositories") {
ForEach(displayedProject.sources) { source in
Button {
Task {
do {
try await appState.openProjectSource(source)
dismiss()
} catch {
self.error = "Couldn’t open repository. \(error.userFacingMessage)"
}
}
} label: {
ProjectResourceRow(
title: source.displayName,
subtitle: source.ownerDisplayName,
detail: source.displayDescription,
systemImage: "book.closed"
)
}
.buttonStyle(.plain)
}
}
}
}
@ViewBuilder
private var trackersSection: some View {
if !displayedProject.trackers.isEmpty {
Section("Trackers") {
ForEach(displayedProject.trackers) { tracker in
Button {
Task {
do {
try await appState.openProjectTracker(tracker)
dismiss()
} catch {
self.error = "Couldn’t open tracker. \(error.userFacingMessage)"
}
}
} label: {
ProjectResourceRow(
title: tracker.displayName,
subtitle: tracker.ownerDisplayName,
detail: tracker.displayDescription,
systemImage: "checklist"
)
}
.buttonStyle(.plain)
}
}
}
}
@ViewBuilder
private var mailingListsSection: some View {
if !displayedProject.mailingLists.isEmpty {
Section("Mailing Lists") {
ForEach(displayedProject.mailingLists) { mailingList in
Button {
appState.openMailingList(mailingList.inboxReference)
dismiss()
} label: {
ProjectResourceRow(
title: mailingList.displayName,
subtitle: mailingList.ownerDisplayName,
detail: mailingList.displayDescription,
systemImage: "list.bullet"
)
}
.buttonStyle(.plain)
}
}
}
}
@ViewBuilder
private var emptyResourcesSection: some View {
if !displayedProject.hasLinkedResources, displayedProject.websiteURL == nil {
Section {
ContentUnavailableView(
"No Linked Resources",
systemImage: "square.stack.3d.up.slash",
description: Text("This project doesn’t currently expose repositories, trackers, mailing lists, or external links.")
)
}
}
}
private func loadProjectIfNeeded(forceRefresh: Bool = false) async {
guard forceRefresh || !project.isFullyLoaded else {
detailProject = project
return
}
guard !isLoading else { return }
isLoading = true
error = nil
defer { isLoading = false }
do {
let service = ProjectService(client: appState.client)
detailProject = try await service.fetchProjectDetail(rid: project.id)
} catch {
self.error = "Couldn’t load project. \(error.userFacingMessage)"
}
}
private func togglePinnedState() {
guard let currentUserKey else { return }
ProjectPinStore.togglePin(projectID: displayedProject.id, for: currentUserKey, defaults: appState.accountDefaults)
pinChangeCount += 1
}
private func projectLinks(for project: Project) -> [ProjectLink] {
var links: [ProjectLink] = []
if let url = project.websiteURL {
links.append(ProjectLink(id: "website", title: project.website ?? url.absoluteString, systemImage: "globe", url: url))
}
if let source = project.sources.first,
let url = source.webURL {
links.append(ProjectLink(id: "primary-repo", title: "\(source.ownerUsername)/\(source.displayName)", systemImage: "book.closed", url: url))
}
if let tracker = project.trackers.first,
let url = tracker.webURL {
links.append(ProjectLink(id: "primary-tracker", title: "\(tracker.ownerUsername)/\(tracker.displayName)", systemImage: "checklist", url: url))
}
if let mailingList = project.mailingLists.first,
let url = SRHTWebURL.mailingList(ownerUsername: mailingList.ownerUsername, listName: mailingList.name) {
links.append(ProjectLink(id: "primary-list", title: "\(mailingList.ownerUsername)/\(mailingList.displayName)", systemImage: "list.bullet", url: url))
}
return links
}
}
private struct ProjectLink: Identifiable {
let id: String
let title: String
let systemImage: String
let url: URL
}
private struct ProjectResourceRow: View {
let title: String
let subtitle: String
let detail: String?
let systemImage: String
var body: some View {
HStack(alignment: .top, spacing: 12) {
Image(systemName: systemImage)
.frame(width: 18, alignment: .leading)
.foregroundStyle(.secondary)
VStack(alignment: .leading, spacing: 4) {
Text(title)
.font(.subheadline.weight(.medium))
.foregroundStyle(.primary)
Text(subtitle)
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(1)
if let detail, !detail.isEmpty {
Text(detail)
.font(.caption)
.foregroundStyle(.tertiary)
.lineLimit(2)
}
}
Spacer(minLength: 8)
Image(systemName: "chevron.right")
.font(.caption.weight(.semibold))
.foregroundStyle(.tertiary)
}
.contentShape(Rectangle())
.padding(.vertical, 4)
}
}
|