Commit 886645fa08

886645fa082a86877fb509f1b7a11e2bee3b3084

parent: fa473b2f8c

Verified · cmc ci/accessibility: success

cmc <hello@cleberg.net> · 2026-10-23 05:35 UTC

Share more list, settings and profile pieces

- SearchableListState, LoadingMoreRow, ProgressButton, SegmentedTabs,
  SwipeDeleteButton and SwipeEditButton.
- Settings key mutations, notification preference toggles and tracker
  deletes each go through one helper.
- One LabelToggleRow; UserProfileViewModel.reusing for both profiles.
- References sections and load states use the shared views.

Layout: unified · split

Hutch/Extensions/ErrorViews.swift +32
@@ -180,3 +180,35 @@ struct SRHTLoadStateView<Content: View>: View {
180180 }
181181 }
182182}
183
184/// A searchable list's loading and error states, then no matches for the search, or nothing at all.
185struct SearchableListState: View {
186 let isLoading: Bool
187 let error: String?
188 let isEmpty: Bool
189 let hasMatches: Bool
190 let searchText: String
191 let loadingMessage: String
192 let errorTitle: String
193 let emptyTitle: LocalizedStringKey
194 let emptySystemImage: String
195 let emptyDescription: LocalizedStringKey
196 let retryAction: () async -> Void
197
198 var body: some View {
199 SRHTLoadStateView(
200 isLoading: isLoading,
201 error: error,
202 isEmpty: isEmpty,
203 loadingMessage: loadingMessage,
204 errorTitle: errorTitle,
205 retryAction: retryAction
206 ) {
207 if !isEmpty, !hasMatches {
208 ContentUnavailableView.search(text: searchText)
209 } else if isEmpty {
210 ContentUnavailableView(emptyTitle, systemImage: emptySystemImage, description: Text(emptyDescription))
211 }
212 }
213 }
214}
Hutch/Views/Builds/BuildListView.swift +1 −6
@@ -155,12 +155,7 @@ struct BuildListView: View {
155155 .themedRow()
156156
157157 if viewModel.isLoadingMore {
158 HStack {
159 Spacer()
160 ProgressView()
161 Spacer()
162 }
163 .listRowSeparator(.hidden)
158 LoadingMoreRow()
164159 .themedRow()
165160 }
166161 }
Hutch/Views/Lists/MailingListListView.swift +7 −12
@@ -455,24 +455,19 @@ struct MailingListListView: View {
455455 Text("This permanently deletes the list and its entire archive, for everyone. This cannot be undone.")
456456 }
457457 .overlay {
458 SRHTLoadStateView(
458 SearchableListState(
459459 isLoading: viewModel.isLoading,
460460 error: viewModel.error,
461461 isEmpty: viewModel.mailingLists.isEmpty,
462 hasMatches: !viewModel.filteredMailingLists.isEmpty,
463 searchText: viewModel.searchText,
462464 loadingMessage: "Loading mailing lists…",
463465 errorTitle: "Couldn't Load Mailing Lists",
466 emptyTitle: "No Mailing Lists",
467 emptySystemImage: "list.bullet.rectangle",
468 emptyDescription: "Your subscribed mailing lists will appear here.",
464469 retryAction: { await viewModel.loadMailingLists() }
465 ) {
466 if !viewModel.mailingLists.isEmpty, viewModel.filteredMailingLists.isEmpty {
467 ContentUnavailableView.search(text: viewModel.searchText)
468 } else if viewModel.mailingLists.isEmpty {
469 ContentUnavailableView(
470 "No Mailing Lists",
471 systemImage: "list.bullet.rectangle",
472 description: Text("Your subscribed mailing lists will appear here.")
473 )
474 }
475 }
470 )
476471 }
477472 .srhtErrorBanner(error: $vm.error)
478473 .refreshable {
Hutch/Views/Lookup/UserProfileView.swift +3 −17
@@ -215,23 +215,9 @@ struct UserProfileView: View {
215215 : user.canonicalName
216216 let actor = user.canonicalName.hasPrefix("~") ? user.canonicalName : "~\(user.canonicalName)"
217217
218 let vm: UserProfileViewModel
219 if let existingViewModel = profileViewModel,
220 existingViewModel.actor == actor,
221 existingViewModel.ownerUsername == owner {
222 vm = existingViewModel
223 } else {
224 let newViewModel = UserProfileViewModel(
225 ownerUsername: owner,
226 actor: actor,
227 client: appState.client,
228 statsService: HutchStatsService(
229 configuration: appState.configuration,
230 currentActor: appState.currentUser?.canonicalName
231 )
232 )
233 profileViewModel = newViewModel
234 vm = newViewModel
218 let vm = UserProfileViewModel.reusing(profileViewModel, owner: owner, actor: actor, appState: appState)
219 if vm !== profileViewModel {
220 profileViewModel = vm
235221 }
236222
237223 async let repos: () = vm.loadRepositories()
Hutch/Views/Lookup/UserProfileViewModel.swift +18
@@ -210,3 +210,21 @@ final class UserProfileViewModel {
210210 }
211211
212212}
213
214extension UserProfileViewModel {
215 /// `existing` when it already shows `owner` to `actor`, else a new model for them.
216 static func reusing(_ existing: UserProfileViewModel?, owner: String, actor: String, appState: AppState) -> UserProfileViewModel {
217 if let existing, existing.actor == actor, existing.ownerUsername == owner {
218 return existing
219 }
220 return UserProfileViewModel(
221 ownerUsername: owner,
222 actor: actor,
223 client: appState.client,
224 statsService: HutchStatsService(
225 configuration: appState.configuration,
226 currentActor: appState.currentUser?.canonicalName
227 )
228 )
229 }
230}
Hutch/Views/More/ProfileView.swift +3 −17
@@ -30,23 +30,9 @@ struct ProfileView: View {
3030 : currentUser.canonicalName
3131 let actor = currentUser.canonicalName.hasPrefix("~") ? currentUser.canonicalName : "~\(currentUser.canonicalName)"
3232
33 let vm: UserProfileViewModel
34 if let existingViewModel = contributionViewModel,
35 existingViewModel.actor == actor,
36 existingViewModel.ownerUsername == owner {
37 vm = existingViewModel
38 } else {
39 let newViewModel = UserProfileViewModel(
40 ownerUsername: owner,
41 actor: actor,
42 client: appState.client,
43 statsService: HutchStatsService(
44 configuration: appState.configuration,
45 currentActor: appState.currentUser?.canonicalName
46 )
47 )
48 contributionViewModel = newViewModel
49 vm = newViewModel
33 let vm = UserProfileViewModel.reusing(contributionViewModel, owner: owner, actor: actor, appState: appState)
34 if vm !== contributionViewModel {
35 contributionViewModel = vm
5036 }
5137
5238 await vm.loadContributions()
Hutch/Views/Pastes/PasteListView.swift +9 −24
@@ -73,12 +73,7 @@ struct PasteListView: View {
7373 }
7474 .swipeActions(edge: .trailing, allowsFullSwipe: false) {
7575 if swipeActionsEnabled {
76 Button {
77 pasteToDelete = paste
78 } label: {
79 Label("Delete", systemImage: "trash")
80 }
81 .tint(.red)
76 SwipeDeleteButton { pasteToDelete = paste }
8277 }
8378 }
8479 .task {
@@ -88,12 +83,7 @@ struct PasteListView: View {
8883 .themedRow()
8984
9085 if viewModel.isLoadingMore {
91 HStack {
92 Spacer()
93 ProgressView()
94 Spacer()
95 }
96 .listRowSeparator(.hidden)
86 LoadingMoreRow()
9787 .themedRow()
9888 }
9989 }
@@ -105,24 +95,19 @@ struct PasteListView: View {
10595 prompt: "Search pastes"
10696 )
10797 .overlay {
108 SRHTLoadStateView(
98 SearchableListState(
10999 isLoading: viewModel.isLoading,
110100 error: viewModel.error,
111101 isEmpty: viewModel.pastes.isEmpty,
102 hasMatches: !viewModel.filteredPastes.isEmpty,
103 searchText: viewModel.searchText,
112104 loadingMessage: "Loading pastes…",
113105 errorTitle: "Couldn't Load Pastes",
106 emptyTitle: "No Pastes",
107 emptySystemImage: "doc.on.clipboard",
108 emptyDescription: "Your pastes will appear here.",
114109 retryAction: { await viewModel.loadPastes() }
115 ) {
116 if !viewModel.pastes.isEmpty, viewModel.filteredPastes.isEmpty {
117 ContentUnavailableView.search(text: viewModel.searchText)
118 } else if viewModel.pastes.isEmpty {
119 ContentUnavailableView(
120 "No Pastes",
121 systemImage: "doc.on.clipboard",
122 description: Text("Your pastes will appear here.")
123 )
124 }
125 }
110 )
126111 }
127112 .connectivityOverlay(hasContent: !viewModel.pastes.isEmpty) {
128113 await viewModel.loadPastes()
Hutch/Views/Projects/DiscoverProjectsView.swift +1 −6
@@ -95,12 +95,7 @@ struct DiscoverProjectsView: View {
9595 .themedRow()
9696
9797 if viewModel.isLoadingMore {
98 HStack {
99 Spacer()
100 ProgressView()
101 Spacer()
102 }
103 .listRowSeparator(.hidden)
98 LoadingMoreRow()
10499 .themedRow()
105100 }
106101 }
Hutch/Views/Projects/ProjectsListView.swift +7 −12
@@ -139,24 +139,19 @@ struct ProjectsListView: View {
139139 prompt: "Search projects"
140140 )
141141 .overlay {
142 SRHTLoadStateView(
142 SearchableListState(
143143 isLoading: viewModel.isLoading,
144144 error: viewModel.error,
145145 isEmpty: viewModel.projects.isEmpty,
146 hasMatches: !viewModel.filteredProjects.isEmpty,
147 searchText: viewModel.searchText,
146148 loadingMessage: "Loading projects…",
147149 errorTitle: "Couldn't Load Projects",
150 emptyTitle: "No Projects",
151 emptySystemImage: "square.stack.3d.up",
152 emptyDescription: "Projects from your SourceHut account will appear here when available.",
148153 retryAction: { await viewModel.loadProjects() }
149 ) {
150 if !viewModel.projects.isEmpty, viewModel.filteredProjects.isEmpty {
151 ContentUnavailableView.search(text: viewModel.searchText)
152 } else if viewModel.projects.isEmpty {
153 ContentUnavailableView(
154 "No Projects",
155 systemImage: "square.stack.3d.up",
156 description: Text("Projects from your SourceHut account will appear here when available.")
157 )
158 }
159 }
154 )
160155 }
161156 .srhtErrorBanner(
162157 error: Binding(
Hutch/Views/Repositories/ArtifactsView.swift +20 −19
@@ -142,25 +142,26 @@ struct ArtifactsView: View {
142142 }
143143 }
144144 .overlay {
145 if viewModel.isLoadingArtifacts, viewModel.referenceArtifacts.isEmpty {
146 SRHTLoadingStateView(message: "Loading artifacts…")
147 } else if let error = viewModel.error, viewModel.referenceArtifacts.isEmpty {
148 SRHTErrorStateView(
149 title: "Couldn't Load Artifacts",
150 message: error,
151 retryAction: { await viewModel.loadArtifacts() }
152 )
153 } else if viewModel.referenceArtifacts.isEmpty {
154 // The overlay covers the whole list, so the upload row above is
155 // hidden underneath it — and a repository with no artifacts is
156 // exactly the one that needs uploading. Offer it here too.
157 ContentUnavailableView {
158 SwiftUI.Label("No Artifacts", systemImage: "archivebox")
159 } description: {
160 Text("This repository has no release artifacts.")
161 } actions: {
162 if isOwnedByCurrentUser {
163 uploadMenu
145 SRHTLoadStateView(
146 isLoading: viewModel.isLoadingArtifacts,
147 error: viewModel.error,
148 isEmpty: viewModel.referenceArtifacts.isEmpty,
149 loadingMessage: "Loading artifacts…",
150 errorTitle: "Couldn't Load Artifacts",
151 retryAction: { await viewModel.loadArtifacts() }
152 ) {
153 if viewModel.referenceArtifacts.isEmpty {
154 // The overlay covers the whole list, so the upload row above is
155 // hidden underneath it — and a repository with no artifacts is
156 // exactly the one that needs uploading. Offer it here too.
157 ContentUnavailableView {
158 SwiftUI.Label("No Artifacts", systemImage: "archivebox")
159 } description: {
160 Text("This repository has no release artifacts.")
161 } actions: {
162 if isOwnedByCurrentUser {
163 uploadMenu
164 }
164165 }
165166 }
166167 }
Hutch/Views/Repositories/CommitLogView.swift +16 −20
@@ -16,32 +16,28 @@ struct CommitLogView: View {
1616 .themedRow()
1717
1818 if viewModel.isLoadingMoreCommits {
19 HStack {
20 Spacer()
21 ProgressView()
22 Spacer()
23 }
24 .listRowSeparator(.hidden)
19 LoadingMoreRow()
2520 .themedRow()
2621 }
2722 }
2823 .themedList()
2924 .listStyle(.plain)
3025 .overlay {
31 if viewModel.isLoadingCommits, viewModel.commits.isEmpty {
32 SRHTLoadingStateView(message: "Loading commits…")
33 } else if let error = viewModel.error, viewModel.commits.isEmpty {
34 SRHTErrorStateView(
35 title: "Couldn't Load Commits",
36 message: error,
37 retryAction: { await viewModel.loadCommits() }
38 )
39 } else if viewModel.commits.isEmpty {
40 ContentUnavailableView(
41 "No Commits",
42 systemImage: "clock.arrow.trianglehead.counterclockwise.rotate.90",
43 description: Text("This repository has no commit history.")
44 )
26 SRHTLoadStateView(
27 isLoading: viewModel.isLoadingCommits,
28 error: viewModel.error,
29 isEmpty: viewModel.commits.isEmpty,
30 loadingMessage: "Loading commits…",
31 errorTitle: "Couldn't Load Commits",
32 retryAction: { await viewModel.loadCommits() }
33 ) {
34 if viewModel.commits.isEmpty {
35 ContentUnavailableView(
36 "No Commits",
37 systemImage: "clock.arrow.trianglehead.counterclockwise.rotate.90",
38 description: Text("This repository has no commit history.")
39 )
40 }
4541 }
4642 }
4743 .task {
Hutch/Views/Repositories/HgRepositoryDetailView.swift +17 −30
@@ -182,16 +182,7 @@ struct HgRepositoryDetailView: View {
182182 @ViewBuilder
183183 private func content(_ viewModel: HgRepositoryDetailViewModel) -> some View {
184184 VStack(spacing: 0) {
185 Picker("Tab", selection: $selectedTab) {
186 ForEach(HgRepositoryDetailViewModel.Tab.allCases, id: \.self) { tab in
187 Text(tab.rawValue).tag(tab)
188 }
189 }
190 .pickerStyle(.segmented)
191 .padding(.horizontal)
192 .padding(.vertical, 8)
193
194 Divider()
185 SegmentedTabs(selection: $selectedTab)
195186
196187 switch selectedTab {
197188 case .summary:
@@ -453,32 +444,28 @@ struct HgRepositoryDetailView: View {
453444 .themedRow()
454445
455446 if viewModel.isLoadingMoreLog {
456 HStack {
457 Spacer()
458 ProgressView()
459 Spacer()
460 }
461 .listRowSeparator(.hidden)
447 LoadingMoreRow()
462448 .themedRow()
463449 }
464450 }
465451 .themedList()
466452 .listStyle(.plain)
467453 .overlay {
468 if viewModel.isLoadingLog, viewModel.log.isEmpty {
469 SRHTLoadingStateView(message: "Loading revisions…")
470 } else if let error = viewModel.error, viewModel.log.isEmpty {
471 SRHTErrorStateView(
472 title: "Couldn't Load Revisions",
473 message: error,
474 retryAction: { await viewModel.loadLog() }
475 )
476 } else if viewModel.log.isEmpty {
477 ContentUnavailableView(
478 "No Revisions",
479 systemImage: "clock.arrow.trianglehead.counterclockwise.rotate.90",
480 description: Text("This repository has no revision history.")
481 )
454 SRHTLoadStateView(
455 isLoading: viewModel.isLoadingLog,
456 error: viewModel.error,
457 isEmpty: viewModel.log.isEmpty,
458 loadingMessage: "Loading revisions…",
459 errorTitle: "Couldn't Load Revisions",
460 retryAction: { await viewModel.loadLog() }
461 ) {
462 if viewModel.log.isEmpty {
463 ContentUnavailableView(
464 "No Revisions",
465 systemImage: "clock.arrow.trianglehead.counterclockwise.rotate.90",
466 description: Text("This repository has no revision history.")
467 )
468 }
482469 }
483470 }
484471 .refreshable {
Hutch/Views/Repositories/HgRepositorySettingsView.swift +2 −18
@@ -24,18 +24,10 @@ struct HgRepositorySettingsView: View {
2424 }
2525
2626 private func saveInfoButton(_ viewModel: HgRepositorySettingsViewModel) -> some View {
27 Button {
27 ProgressButton("Save Changes", isWorking: viewModel.isSavingInfo) {
2828 Task {
2929 _ = await viewModel.saveInfo()
3030 }
31 } label: {
32 if viewModel.isSavingInfo {
33 ProgressView()
34 .frame(maxWidth: .infinity)
35 } else {
36 Text("Save Changes")
37 .frame(maxWidth: .infinity)
38 }
3931 }
4032 .disabled(viewModel.isSavingInfo || !viewModel.isInfoDirty)
4133 .themedRow()
@@ -232,16 +224,8 @@ struct HgRepositorySettingsView: View {
232224 @ViewBuilder
233225 private func deleteSection(_ viewModel: HgRepositorySettingsViewModel) -> some View {
234226 Section {
235 Button(role: .destructive) {
227 ProgressButton("Delete Repository", isWorking: viewModel.isDeleting, role: .destructive) {
236228 showDeleteConfirmation = true
237 } label: {
238 if viewModel.isDeleting {
239 ProgressView()
240 .frame(maxWidth: .infinity)
241 } else {
242 Text("Delete Repository")
243 .frame(maxWidth: .infinity)
244 }
245229 }
246230 .disabled(viewModel.isDeleting)
247231 .themedRow()
Hutch/Views/Repositories/ReferencesListView.swift +53 −44
@@ -6,58 +6,45 @@ struct ReferencesListView: View {
66 var body: some View {
77 List {
88 if let defaultBranch = viewModel.defaultBranchReference {
9 Section {
10 ReferenceRow(reference: defaultBranch, prefix: "refs/heads/")
11 .themedRow()
12
13 NavigationLink("See All Branches") {
14 ReferencesDetailListView(
15 title: "Branches",
16 references: viewModel.branches,
17 prefix: "refs/heads/"
18 )
19 }
20 .themedRow()
21 } header: {
22 sectionHeader(title: "Default Branch")
23 }
9 referenceSection(
10 defaultBranch,
11 header: "Default Branch",
12 seeAll: "See All Branches",
13 title: "Branches",
14 references: viewModel.branches,
15 prefix: "refs/heads/"
16 )
2417 }
2518
2619 if let latestTag = viewModel.latestTagReference {
27 Section {
28 ReferenceRow(reference: latestTag, prefix: "refs/tags/")
29 .themedRow()
30
31 NavigationLink("See All Tags") {
32 ReferencesDetailListView(
33 title: "Tags",
34 references: viewModel.tags,
35 prefix: "refs/tags/"
36 )
37 }
38 .themedRow()
39 } header: {
40 sectionHeader(title: "Latest Tag")
41 }
20 referenceSection(
21 latestTag,
22 header: "Latest Tag",
23 seeAll: "See All Tags",
24 title: "Tags",
25 references: viewModel.tags,
26 prefix: "refs/tags/"
27 )
4228 }
4329 }
4430 .themedList()
4531 .listStyle(.insetGrouped)
4632 .overlay {
47 if viewModel.isLoadingRefs, viewModel.branches.isEmpty, viewModel.tags.isEmpty {
48 SRHTLoadingStateView(message: "Loading references…")
49 } else if let error = viewModel.error, viewModel.branches.isEmpty, viewModel.tags.isEmpty {
50 SRHTErrorStateView(
51 title: "Couldn't Load References",
52 message: error,
53 retryAction: { await viewModel.loadReferences() }
54 )
55 } else if viewModel.branches.isEmpty, viewModel.tags.isEmpty {
56 ContentUnavailableView(
57 "No References",
58 systemImage: "arrow.triangle.branch",
59 description: Text("This repository has no branches or tags.")
60 )
33 SRHTLoadStateView(
34 isLoading: viewModel.isLoadingRefs,
35 error: viewModel.error,
36 isEmpty: viewModel.branches.isEmpty && viewModel.tags.isEmpty,
37 loadingMessage: "Loading references…",
38 errorTitle: "Couldn't Load References",
39 retryAction: { await viewModel.loadReferences() }
40 ) {
41 if viewModel.branches.isEmpty, viewModel.tags.isEmpty {
42 ContentUnavailableView(
43 "No References",
44 systemImage: "arrow.triangle.branch",
45 description: Text("This repository has no branches or tags.")
46 )
47 }
6148 }
6249 }
6350 .task {
@@ -71,6 +58,28 @@ struct ReferencesListView: View {
7158 }
7259
7360 @ViewBuilder
61 /// One reference with a link to all of its kind.
62 private func referenceSection(
63 _ reference: ReferenceDetail,
64 header: String,
65 seeAll: LocalizedStringKey,
66 title: String,
67 references: [ReferenceDetail],
68 prefix: String
69 ) -> some View {
70 Section {
71 ReferenceRow(reference: reference, prefix: prefix)
72 .themedRow()
73
74 NavigationLink(seeAll) {
75 ReferencesDetailListView(title: title, references: references, prefix: prefix)
76 }
77 .themedRow()
78 } header: {
79 sectionHeader(title: header)
80 }
81 }
82
7483 private func sectionHeader(title: String) -> some View {
7584 HStack {
7685 Text(title)
Hutch/Views/Repositories/RepositoryDetailView.swift +1 −10
@@ -101,16 +101,7 @@ struct RepositoryDetailView: View {
101101 @ViewBuilder
102102 private func detailContent(_ viewModel: RepositoryDetailViewModel) -> some View {
103103 VStack(spacing: 0) {
104 Picker("Tab", selection: $selectedTab) {
105 ForEach(RepositoryDetailViewModel.Tab.allCases, id: \.self) { tab in
106 Text(tab.rawValue).tag(tab)
107 }
108 }
109 .pickerStyle(.segmented)
110 .padding(.horizontal)
111 .padding(.vertical, 8)
112
113 Divider()
104 SegmentedTabs(selection: $selectedTab)
114105
115106 switch selectedTab {
116107 case .summary:
Hutch/Views/Repositories/RepositoryListView.swift +1 −6
@@ -75,12 +75,7 @@ struct RepositoryListView: View {
7575 .themedRow()
7676
7777 if viewModel.isLoadingMore {
78 HStack {
79 Spacer()
80 ProgressView()
81 Spacer()
82 }
83 .listRowSeparator(.hidden)
78 LoadingMoreRow()
8479 .themedRow()
8580 }
8681 }
Hutch/Views/Repositories/RepositorySettingsView.swift +4 −36
@@ -127,20 +127,12 @@ struct RepositorySettingsView: View {
127127 .themedRow()
128128 }
129129
130 Button {
130 ProgressButton("Save Details", isWorking: viewModel.isSavingMetadata) {
131131 Task {
132132 if let updatedRepository = await viewModel.saveMetadata() {
133133 onUpdated(updatedRepository)
134134 }
135135 }
136 } label: {
137 if viewModel.isSavingMetadata {
138 ProgressView()
139 .frame(maxWidth: .infinity)
140 } else {
141 Text("Save Details")
142 .frame(maxWidth: .infinity)
143 }
144136 }
145137 .disabled(
146138 viewModel.isMutating ||
@@ -181,20 +173,12 @@ struct RepositorySettingsView: View {
181173 .foregroundStyle(.secondary)
182174 .themedRow()
183175
184 Button {
176 ProgressButton("Set Default Branch", isWorking: viewModel.isSavingDefaultBranch) {
185177 Task {
186178 if let updatedRepository = await viewModel.saveDefaultBranch() {
187179 onUpdated(updatedRepository)
188180 }
189181 }
190 } label: {
191 if viewModel.isSavingDefaultBranch {
192 ProgressView()
193 .frame(maxWidth: .infinity)
194 } else {
195 Text("Set Default Branch")
196 .frame(maxWidth: .infinity)
197 }
198182 }
199183 .disabled(
200184 viewModel.isMutating ||
@@ -223,16 +207,8 @@ struct RepositorySettingsView: View {
223207 .foregroundStyle(.secondary)
224208 .themedRow()
225209
226 Button {
210 ProgressButton("Apply Visibility Change", isWorking: viewModel.isUpdatingVisibility) {
227211 showVisibilityConfirmation = true
228 } label: {
229 if viewModel.isUpdatingVisibility {
230 ProgressView()
231 .frame(maxWidth: .infinity)
232 } else {
233 Text("Apply Visibility Change")
234 .frame(maxWidth: .infinity)
235 }
236212 }
237213 .disabled(viewModel.isMutating || !viewModel.isVisibilityDirty)
238214 .themedRow()
@@ -244,16 +220,8 @@ struct RepositorySettingsView: View {
244220 @ViewBuilder
245221 private func deleteSection(_ viewModel: RepositorySettingsViewModel) -> some View {
246222 Section {
247 Button(role: .destructive) {
223 ProgressButton("Delete Repository", isWorking: viewModel.isDeleting, role: .destructive) {
248224 showDeleteConfirmation = true
249 } label: {
250 if viewModel.isDeleting {
251 ProgressView()
252 .frame(maxWidth: .infinity)
253 } else {
254 Text("Delete Repository")
255 .frame(maxWidth: .infinity)
256 }
257225 }
258226 .disabled(viewModel.isMutating)
259227 .themedRow()
Hutch/Views/Settings/NotificationPreferencesViewModel.swift +32 −29
@@ -126,48 +126,51 @@ final class NotificationPreferencesViewModel {
126126 }
127127
128128 func setNotifySelf(_ newValue: Bool) async {
129 guard !isSavingNotifySelf else { return }
130 isSavingNotifySelf = true
131 error = nil
132 defer { isSavingNotifySelf = false }
133
134 let previous = notifySelf
135 notifySelf = newValue
136
137 do {
138 let response = try await client.execute(
129 await setPreference(\.notifySelf, saving: \.isSavingNotifySelf, to: newValue,
130 failure: "Couldn't update ticket email preference.") {
131 try await self.client.execute(
139132 service: .todo,
140133 query: Self.updateNotifySelfMutation,
141134 variables: ["notifySelf": newValue],
142135 responseType: TodoPreferencesResponse.self
143 )
144 notifySelf = response.preferences.notifySelf
145 } catch {
146 notifySelf = previous
147 self.error = "Couldn't update ticket email preference. \(error.userFacingMessage)"
136 ).preferences.notifySelf
148137 }
149138 }
150139
151140 func setCopySelf(_ newValue: Bool) async {
152 guard !isSavingCopySelf else { return }
153 isSavingCopySelf = true
154 error = nil
155 defer { isSavingCopySelf = false }
156
157 let previous = copySelf
158 copySelf = newValue
159
160 do {
161 let response = try await client.execute(
141 await setPreference(\.copySelf, saving: \.isSavingCopySelf, to: newValue,
142 failure: "Couldn't update mailing list email preference.") {
143 try await self.client.execute(
162144 service: .lists,
163145 query: Self.updateCopySelfMutation,
164146 variables: ["copySelf": newValue],
165147 responseType: ListsPreferencesResponse.self
166 )
167 copySelf = response.preferences.copySelf
148 ).preferences.copySelf
149 }
150 }
151
152 /// Shows `newValue` at once and saves it with `save`, which answers what the server kept. On
153 /// failure the previous value comes back and the error says what `failure` couldn't do.
154 private func setPreference(
155 _ value: ReferenceWritableKeyPath<NotificationPreferencesViewModel, Bool>,
156 saving: ReferenceWritableKeyPath<NotificationPreferencesViewModel, Bool>,
157 to newValue: Bool,
158 failure: String,
159 save: () async throws -> Bool
160 ) async {
161 guard !self[keyPath: saving] else { return }
162 self[keyPath: saving] = true
163 error = nil
164 defer { self[keyPath: saving] = false }
165
166 let previous = self[keyPath: value]
167 self[keyPath: value] = newValue
168
169 do {
170 self[keyPath: value] = try await save()
168171 } catch {
169 copySelf = previous
170 self.error = "Couldn't update mailing list email preference. \(error.userFacingMessage)"
172 self[keyPath: value] = previous
173 self.error = "\(failure) \(error.userFacingMessage)"
171174 }
172175 }
173176}
Hutch/Views/Settings/SettingsViewModel.swift +26 −53
@@ -309,75 +309,48 @@ final class SettingsViewModel {
309309
310310 func addSSHKey() async {
311311 let key = newSSHKey.trimmingCharacters(in: .whitespacesAndNewlines)
312 guard !key.isEmpty else { return }
313 error = nil
314
315 do {
316 let result = try await client.execute(
317 service: .meta,
318 query: Self.createSSHKeyMutation,
319 variables: ["key": key],
320 responseType: CreateSSHKeyResponse.self
321 )
322 sshKeys.append(result.createSSHKey)
323 newSSHKey = ""
324 isAddingSSHKey = false
325 } catch {
326 self.error = error.userFacingMessage
327 }
312 guard !key.isEmpty,
313 let result = await metaMutation(Self.createSSHKeyMutation, ["key": key], as: CreateSSHKeyResponse.self)
314 else { return }
315 sshKeys.append(result.createSSHKey)
316 newSSHKey = ""
317 isAddingSSHKey = false
328318 }
329319
330320 func deleteSSHKey(_ key: SSHKey) async {
331 error = nil
332
333 do {
334 _ = try await client.execute(
335 service: .meta,
336 query: Self.deleteSSHKeyMutation,
337 variables: ["id": key.id],
338 responseType: DeleteSSHKeyResponse.self
339 )
340 sshKeys.removeAll { $0.id == key.id }
341 } catch {
342 self.error = error.userFacingMessage
343 }
321 guard await metaMutation(Self.deleteSSHKeyMutation, ["id": key.id], as: DeleteSSHKeyResponse.self) != nil else { return }
322 sshKeys.removeAll { $0.id == key.id }
344323 }
345324
346325 // MARK: - PGP Keys
347326
348327 func addPGPKey() async {
349328 let key = newPGPKey.trimmingCharacters(in: .whitespacesAndNewlines)
350 guard !key.isEmpty else { return }
351 error = nil
352
353 do {
354 let result = try await client.execute(
355 service: .meta,
356 query: Self.createPGPKeyMutation,
357 variables: ["key": key],
358 responseType: CreatePGPKeyResponse.self
359 )
360 pgpKeys.append(result.createPGPKey)
361 newPGPKey = ""
362 isAddingPGPKey = false
363 } catch {
364 self.error = error.userFacingMessage
365 }
329 guard !key.isEmpty,
330 let result = await metaMutation(Self.createPGPKeyMutation, ["key": key], as: CreatePGPKeyResponse.self)
331 else { return }
332 pgpKeys.append(result.createPGPKey)
333 newPGPKey = ""
334 isAddingPGPKey = false
366335 }
367336
368337 func deletePGPKey(_ key: PGPKey) async {
369 error = nil
338 guard await metaMutation(Self.deletePGPKeyMutation, ["id": key.id], as: DeletePGPKeyResponse.self) != nil else { return }
339 pgpKeys.removeAll { $0.id == key.id }
340 }
370341
342 /// Runs a meta.sr.ht mutation, clearing the last error first and keeping the new one on failure.
343 private func metaMutation<Response: Decodable>(
344 _ query: String,
345 _ variables: [String: any Sendable],
346 as responseType: Response.Type
347 ) async -> Response? {
348 error = nil
371349 do {
372 _ = try await client.execute(
373 service: .meta,
374 query: Self.deletePGPKeyMutation,
375 variables: ["id": key.id],
376 responseType: DeletePGPKeyResponse.self
377 )
378 pgpKeys.removeAll { $0.id == key.id }
350 return try await client.execute(service: .meta, query: query, variables: variables, responseType: responseType)
379351 } catch {
380352 self.error = error.userFacingMessage
353 return nil
381354 }
382355 }
383356
Hutch/Views/Shared/LoadingMoreRow.swift added +13
@@ -0,0 +1,13 @@
1import SwiftUI
2
3/// The spinner at the foot of a list while its next page loads.
4struct LoadingMoreRow: View {
5 var body: some View {
6 HStack {
7 Spacer()
8 ProgressView()
9 Spacer()
10 }
11 .listRowSeparator(.hidden)
12 }
13}
Hutch/Views/Shared/ProgressButton.swift added +28
@@ -0,0 +1,28 @@
1import SwiftUI
2
3/// A full-width button that shows a spinner in place of its title while `isWorking`.
4struct ProgressButton: View {
5 let title: LocalizedStringKey
6 let isWorking: Bool
7 var role: ButtonRole?
8 let action: () -> Void
9
10 init(_ title: LocalizedStringKey, isWorking: Bool, role: ButtonRole? = nil, action: @escaping () -> Void) {
11 self.title = title
12 self.isWorking = isWorking
13 self.role = role
14 self.action = action
15 }
16
17 var body: some View {
18 Button(role: role, action: action) {
19 if isWorking {
20 ProgressView()
21 .frame(maxWidth: .infinity)
22 } else {
23 Text(title)
24 .frame(maxWidth: .infinity)
25 }
26 }
27 }
28}
Hutch/Views/Shared/SegmentedTabs.swift added +19
@@ -0,0 +1,19 @@
1import SwiftUI
2
3/// The segmented tab bar across the top of a detail screen, with a divider under it.
4struct SegmentedTabs<Tab: CaseIterable & Hashable & RawRepresentable<String>>: View where Tab.AllCases: RandomAccessCollection {
5 @Binding var selection: Tab
6
7 var body: some View {
8 Picker("Tab", selection: $selection) {
9 ForEach(Tab.allCases, id: \.self) { tab in
10 Text(tab.rawValue).tag(tab)
11 }
12 }
13 .pickerStyle(.segmented)
14 .padding(.horizontal)
15 .padding(.vertical, 8)
16
17 Divider()
18 }
19}
Hutch/Views/Shared/SwipeButtons.swift added +25
@@ -0,0 +1,25 @@
1import SwiftUI
2
3/// The red Delete swipe action.
4struct SwipeDeleteButton: View {
5 let action: () -> Void
6
7 var body: some View {
8 Button(action: action) {
9 Label("Delete", systemImage: "trash")
10 }
11 .tint(.red)
12 }
13}
14
15/// The blue Edit swipe action.
16struct SwipeEditButton: View {
17 let action: () -> Void
18
19 var body: some View {
20 Button(action: action) {
21 Label("Edit", systemImage: "pencil")
22 }
23 .tint(.blue)
24 }
25}
Hutch/Views/Tickets/TicketDetailView.swift +2 −1
@@ -1139,7 +1139,8 @@ private struct CreateLabelSheet: View {
11391139 }
11401140}
11411141
1142private struct LabelToggleRow: View {
1142/// A label with a checkmark when it is applied; tapping asks for the opposite.
1143struct LabelToggleRow: View {
11431144 let label: TicketLabel
11441145 let isApplied: Bool
11451146 let isLoading: Bool
Hutch/Views/Tickets/TicketListView.swift +2 −31
@@ -275,12 +275,7 @@ struct TicketListView: View {
275275 .themedRow()
276276
277277 if viewModel.isLoadingMore {
278 HStack {
279 Spacer()
280 ProgressView()
281 Spacer()
282 }
283 .listRowSeparator(.hidden)
278 LoadingMoreRow()
284279 .themedRow()
285280 }
286281 }
@@ -711,7 +706,7 @@ private struct TicketLabelsSheet: View {
711706 } else {
712707 List {
713708 ForEach(viewModel.trackerLabels) { label in
714 TicketListLabelToggleRow(
709 LabelToggleRow(
715710 label: label,
716711 isApplied: ticket.labels.contains(where: { $0.id == label.id }),
717712 isLoading: viewModel.isPerformingAction
@@ -1035,30 +1030,6 @@ private struct SaveTicketFilterSheet: View {
10351030 }
10361031}
10371032
1038private struct TicketListLabelToggleRow: View {
1039 let label: TicketLabel
1040 let isApplied: Bool
1041 let isLoading: Bool
1042 let onToggle: (Bool) -> Void
1043
1044 var body: some View {
1045 Button {
1046 onToggle(!isApplied)
1047 } label: {
1048 HStack {
1049 LabelPill(label: label)
1050 Spacer()
1051 if isApplied {
1052 Image(systemName: "checkmark")
1053 .foregroundStyle(.blue)
1054 }
1055 }
1056 }
1057 .disabled(isLoading)
1058 .accessibilityAddTraits(isApplied ? [.isSelected] : [])
1059 }
1060}
1061
10621033private struct TicketBulkActionBar: View {
10631034 let selectedCount: Int
10641035 let isPerformingAction: Bool
Hutch/Views/Tickets/TrackerListView.swift +3 −18
@@ -121,19 +121,9 @@ struct TrackerListView: View {
121121 TrackerRowView(tracker: tracker)
122122 }
123123 .swipeActions(edge: .trailing, allowsFullSwipe: false) {
124 Button {
125 pendingDeletion = tracker
126 } label: {
127 Label("Delete", systemImage: "trash")
128 }
129 .tint(.red)
124 SwipeDeleteButton { pendingDeletion = tracker }
130125
131 Button {
132 editingTracker = tracker
133 } label: {
134 Label("Edit", systemImage: "pencil")
135 }
136 .tint(.blue)
126 SwipeEditButton { editingTracker = tracker }
137127 }
138128 .task {
139129 await viewModel.loadMoreIfNeeded(currentItem: tracker)
@@ -142,12 +132,7 @@ struct TrackerListView: View {
142132 .themedRow()
143133
144134 if viewModel.isLoadingMore {
145 HStack {
146 Spacer()
147 ProgressView()
148 Spacer()
149 }
150 .listRowSeparator(.hidden)
135 LoadingMoreRow()
151136 .themedRow()
152137 }
153138 }
Hutch/Views/Tickets/TrackerManagementView.swift +26 −50
@@ -438,22 +438,9 @@ final class TrackerManagementViewModel {
438438 }
439439
440440 func deleteACL(_ entry: TrackerACL) async {
441 guard !isDeletingACL else { return }
442 isDeletingACL = true
443 error = nil
444 defer { isDeletingACL = false }
445
446 do {
447 _ = try await client.execute(
448 service: .todo,
449 query: Self.deleteACLMutation,
450 variables: ["id": entry.id],
451 responseType: DeleteTrackerACLResponse.self
452 )
441 await todoMutation(Self.deleteACLMutation, ["id": entry.id], as: DeleteTrackerACLResponse.self, while: \.isDeletingACL) { _ in
453442 acls.removeAll { $0.id == entry.id }
454443 await loadACLs()
455 } catch {
456 self.error = error.userFacingMessage
457444 }
458445 }
459446
@@ -565,20 +552,29 @@ final class TrackerManagementViewModel {
565552 }
566553
567554 func deleteLabel(_ label: TicketLabel) async {
568 guard !isDeletingLabel else { return }
569 isDeletingLabel = true
555 await todoMutation(Self.deleteLabelMutation, ["id": label.id], as: DeleteTrackerLabelResponse.self, while: \.isDeletingLabel) { _ in
556 labels.removeAll { $0.id == label.id }
557 await loadLabels()
558 }
559 }
560
561 /// Runs a todo.sr.ht mutation and then `apply` while `busy` is set, clearing the last error
562 /// first and keeping the new one on failure. Does nothing while `busy` is already set.
563 private func todoMutation<Response: Decodable>(
564 _ query: String,
565 _ variables: [String: any Sendable],
566 as responseType: Response.Type,
567 while busy: ReferenceWritableKeyPath<TrackerManagementViewModel, Bool>,
568 apply: (Response) async -> Void
569 ) async {
570 guard !self[keyPath: busy] else { return }
571 self[keyPath: busy] = true
570572 error = nil
571 defer { isDeletingLabel = false }
573 defer { self[keyPath: busy] = false }
572574
573575 do {
574 _ = try await client.execute(
575 service: .todo,
576 query: Self.deleteLabelMutation,
577 variables: ["id": label.id],
578 responseType: DeleteTrackerLabelResponse.self
579 )
580 labels.removeAll { $0.id == label.id }
581 await loadLabels()
576 let response = try await client.execute(service: .todo, query: query, variables: variables, responseType: responseType)
577 await apply(response)
582578 } catch {
583579 self.error = error.userFacingMessage
584580 }
@@ -751,19 +747,9 @@ struct TrackerACLManagementSheet: View {
751747 TrackerPermissionSummary(permissions: entry.permissions)
752748 }
753749 .swipeActions(edge: .trailing, allowsFullSwipe: false) {
754 Button {
755 pendingDeletion = entry
756 } label: {
757 Label("Delete", systemImage: "trash")
758 }
759 .tint(.red)
760
761 Button {
762 editingACL = entry
763 } label: {
764 Label("Edit", systemImage: "pencil")
765 }
766 .tint(.blue)
750 SwipeDeleteButton { pendingDeletion = entry }
751
752 SwipeEditButton { editingACL = entry }
767753 }
768754 }
769755 .themedRow()
@@ -1120,19 +1106,9 @@ struct TrackerLabelManagementSheet: View {
11201106 }
11211107 .buttonStyle(.plain)
11221108 .swipeActions(edge: .trailing, allowsFullSwipe: false) {
1123 Button {
1124 editingLabel = label
1125 } label: {
1126 Label("Edit", systemImage: "pencil")
1127 }
1128 .tint(.blue)
1109 SwipeEditButton { editingLabel = label }
11291110
1130 Button {
1131 pendingDeletion = label
1132 } label: {
1133 Label("Delete", systemImage: "trash")
1134 }
1135 .tint(.red)
1111 SwipeDeleteButton { pendingDeletion = label }
11361112 }
11371113 }
11381114 .themedRow()