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
|
import SwiftUI
@Observable
@MainActor
final class ProjectsListViewModel {
private(set) var projects: [Project] = []
private(set) var isLoading = false
private(set) var isSaving = false
var error: String?
var saveError: String?
var searchText = ""
let service: ProjectService
init(service: ProjectService) {
self.service = service
}
func createProject(_ values: ProjectFormValues) async -> Bool {
guard !isSaving else { return false }
isSaving = true
saveError = nil
defer { isSaving = false }
do {
_ = try await service.createProject(
name: values.name,
visibility: values.visibility,
description: values.description.isEmpty ? nil : values.description,
tags: values.tags
)
await loadProjects(forceRefresh: true)
return true
} catch {
saveError = error.userFacingMessage
return false
}
}
var filteredProjects: [Project] {
let query = searchText.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
guard !query.isEmpty else { return projects }
return projects.filter {
$0.name.lowercased().contains(query) ||
($0.description?.lowercased().contains(query) ?? false) ||
$0.tags.contains(where: { $0.lowercased().contains(query) })
}
}
func loadProjects(forceRefresh: Bool = false) async {
guard !isLoading else { return }
isLoading = true
error = nil
defer { isLoading = false }
do {
projects = try await service.fetchProjects(forceRefresh: forceRefresh)
} catch {
if projects.isEmpty {
self.error = error.userFacingMessage
} else {
self.error = "Couldn’t refresh projects. \(error.userFacingMessage)"
}
}
}
}
struct ProjectsListView: View {
@Environment(AppState.self) private var appState
@State private var viewModel: ProjectsListViewModel?
@State private var isPresentingCreate = false
var body: some View {
Group {
if let viewModel {
content(viewModel)
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
NavigationLink {
DiscoverProjectsView()
} label: {
Image(systemName: "sparkle.magnifyingglass")
}
.accessibilityLabel("Discover public projects")
}
if appState.currentUser != nil {
ToolbarItem(placement: .topBarTrailing) {
Button {
isPresentingCreate = true
} label: {
Image(systemName: "plus")
}
.accessibilityLabel("Create project")
}
}
}
.sheet(isPresented: $isPresentingCreate) {
ProjectFormSheet(
title: "New Project",
confirmationTitle: "Create",
isSaving: viewModel.isSaving,
error: viewModel.saveError,
includeWebsite: false,
onSave: { await viewModel.createProject($0) }
)
}
} else {
SRHTLoadingStateView(message: "Loading projects…")
}
}
.navigationTitle("Projects")
.task {
if viewModel == nil {
let vm = ProjectsListViewModel(service: ProjectService(client: appState.client))
viewModel = vm
await vm.loadProjects()
}
}
}
@ViewBuilder
private func content(_ viewModel: ProjectsListViewModel) -> some View {
List {
ForEach(viewModel.filteredProjects) { project in
NavigationLink {
ProjectDetailView(project: project, canManage: true)
} label: {
ProjectListRow(project: project)
}
.buttonStyle(.plain)
.alignmentGuide(.listRowSeparatorLeading) { _ in 0 }
}
.themedRow()
}
.themedList()
.listStyle(.plain)
.searchable(
text: Binding(
get: { viewModel.searchText },
set: { viewModel.searchText = $0 }
),
placement: .navigationBarDrawer(displayMode: .always),
prompt: "Search projects"
)
.overlay {
if viewModel.isLoading, viewModel.projects.isEmpty {
SRHTLoadingStateView(message: "Loading projects…")
} else if let error = viewModel.error, viewModel.projects.isEmpty {
SRHTErrorStateView(
title: "Couldn't Load Projects",
message: error,
retryAction: { await viewModel.loadProjects() }
)
} else if !viewModel.projects.isEmpty, viewModel.filteredProjects.isEmpty {
ContentUnavailableView.search(text: viewModel.searchText)
} else if viewModel.projects.isEmpty {
ContentUnavailableView(
"No Projects",
systemImage: "square.stack.3d.up",
description: Text("Projects from your SourceHut account will appear here when available.")
)
}
}
.srhtErrorBanner(
error: Binding(
get: { viewModel.error },
set: { viewModel.error = $0 }
)
)
.refreshable {
await viewModel.loadProjects(forceRefresh: true)
}
.connectivityOverlay(hasContent: !viewModel.projects.isEmpty) {
await viewModel.loadProjects()
}
}
}
private struct ProjectListRow: View {
let project: Project
var body: some View {
VStack(alignment: .leading, spacing: 6) {
HStack(alignment: .top, spacing: 10) {
VStack(alignment: .leading, spacing: 4) {
Text(project.displayName)
.font(.subheadline.weight(.medium))
.foregroundStyle(.primary)
.lineLimit(1)
if let description = project.displayDescription {
Text(description)
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(2)
}
}
Spacer(minLength: 8)
VisibilityBadge(visibility: project.visibility)
}
Text(project.metadataLine)
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(1)
if !project.displayTags.isEmpty {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 6) {
ForEach(project.displayTags.prefix(4), id: \.self) { tag in
Text(tag)
.font(.caption2.weight(.medium))
.foregroundStyle(.secondary)
.padding(.horizontal, 8)
.padding(.vertical, 3)
.background(.quaternary, in: Capsule())
}
}
}
.scrollDisabled(true)
}
}
.contentShape(Rectangle())
.padding(.vertical, 4)
}
}
|