Commit 9e886c16d7

9e886c16d7284deb2902554518d1ed8accedbe8d

parent: 886645fa08

Verified · cmc ci/accessibility: success

cmc <hello@cleberg.net> · 2026-10-23 06:22 UTC

SubmitToolbar and createFlow; Qlty filters for the remaining shapes

- SubmitToolbar for the paste, ticket, repository and tracker sheets.
- createFlow for the paste and repository lists' create-then-open flow.
- Qlty: leave the widget's own decodeDateString, the paging view
  models' loadMore...IfNeeded and calls to the shared load-state views
  out of duplication.

Layout: unified · split

.qlty/qlty.toml +10 −1
@@ -47,7 +47,16 @@ enabled = false
47 47
48# Exhaustive switch-over-enum lookup tables share shape but not content. 48# Exhaustive switch-over-enum lookup tables share shape but not content.
49[language.swift.smells.duplication] 49[language.swift.smells.duplication]
50filter_patterns = ["(import_declaration _)", "(switch_statement _)"] 50# The widget extension is its own target with its own decodeDateString; each paging view model
51# owns its cursor state in loadMore...IfNeeded; calls to the shared load-state views differ only in
52# their arguments.
53filter_patterns = [
54 "(import_declaration _)",
55 "(switch_statement _)",
56 "(function_declaration name: (simple_identifier) @_n (#eq? @_n \"decodeDateString\"))",
57 "(function_declaration name: (simple_identifier) @_n (#match? @_n \"^loadMore[A-Za-z]*IfNeeded$\"))",
58 "(call_expression (simple_identifier) @_n (#match? @_n \"^(SearchableListState|SRHTLoadStateView)$\"))",
59]
51 60
52# tree-sitter-swift nests each `else if` inside the previous branch, so a one-line `if` in the 61# tree-sitter-swift nests each `else if` inside the previous branch, so a one-line `if` in the
53# fourth branch of a flat chain reports level 5. 62# fourth branch of a flat chain reports level 5.
Hutch/Views/Pastes/PasteListView.swift +20 −43
@@ -11,33 +11,18 @@ struct PasteListView: View {
11 var body: some View { 11 var body: some View {
12 ModelContent(model: viewModel, loadingMessage: "Loading pastes…") { content($0) } 12 ModelContent(model: viewModel, loadingMessage: "Loading pastes…") { content($0) }
13 .navigationTitle("Pastes") 13 .navigationTitle("Pastes")
14 .toolbar { 14 .createFlow(viewModel, label: "Create paste", isPresented: $showCreatePasteSheet, created: $createdPaste) {
15 if viewModel != nil { 15 CreatePasteSheet(viewModel: $0, onCreated: $1)
16 AddToolbarItem(label: "Create paste") { 16 } destination: { createdPaste in
17 showCreatePasteSheet = true 17 PasteDetailView(
18 } 18 paste: createdPaste,
19 } 19 onUpdated: { updated in
20 } 20 viewModel?.upsertPaste(updated)
21 .sheet(isPresented: $showCreatePasteSheet) { 21 },
22 if let viewModel { 22 onDeleted: { id in
23 CreatePasteSheet(viewModel: viewModel) { paste in 23 viewModel?.removePaste(id: id)
24 showCreatePasteSheet = false
25 createdPaste = paste
26 } 24 }
27 } 25 )
28 }
29 .navigationDestination(isPresented: .isPresent($createdPaste)) {
30 if let createdPaste {
31 PasteDetailView(
32 paste: createdPaste,
33 onUpdated: { updated in
34 viewModel?.upsertPaste(updated)
35 },
36 onDeleted: { id in
37 viewModel?.removePaste(id: id)
38 }
39 )
40 }
41 } 26 }
42 .task { 27 .task {
43 if viewModel == nil { 28 if viewModel == nil {
@@ -312,25 +297,17 @@ private struct CreatePasteSheet: View {
312 .navigationTitle("New Paste") 297 .navigationTitle("New Paste")
313 .navigationBarTitleDisplayMode(.inline) 298 .navigationBarTitleDisplayMode(.inline)
314 .toolbar { 299 .toolbar {
315 ToolbarItem(placement: .cancellationAction) { 300 SubmitToolbar(
316 Button("Cancel") { dismiss() } 301 title: "Create Paste",
317 } 302 isWorking: viewModel.isCreatingPaste,
318 ToolbarItem(placement: .confirmationAction) { 303 isDisabled: !hasValidContent || viewModel.isCreatingPaste,
319 Button { 304 cancel: { dismiss() }
320 Task { 305 ) {
321 if let paste = await viewModel.createPaste(files: files, visibility: visibility) { 306 Task {
322 onCreated(paste) 307 if let paste = await viewModel.createPaste(files: files, visibility: visibility) {
323 } 308 onCreated(paste)
324 }
325 } label: {
326 if viewModel.isCreatingPaste {
327 ProgressView()
328 .controlSize(.small)
329 } else {
330 Text("Create Paste")
331 } 309 }
332 } 310 }
333 .disabled(!hasValidContent || viewModel.isCreatingPaste)
334 } 311 }
335 } 312 }
336 } 313 }
Hutch/Views/Repositories/RepositoryListView.swift +25 −48
@@ -11,32 +11,17 @@ struct RepositoryListView: View {
11 var body: some View { 11 var body: some View {
12 ModelContent(model: viewModel, loadingMessage: "Loading repositories…") { listContent($0) } 12 ModelContent(model: viewModel, loadingMessage: "Loading repositories…") { listContent($0) }
13 .navigationTitle("Repositories") 13 .navigationTitle("Repositories")
14 .toolbar { 14 .createFlow(viewModel, label: "Create repository", isPresented: $showCreateRepositorySheet, created: $createdRepository) {
15 if viewModel != nil { 15 CreateRepositorySheet(viewModel: $0, onCreated: $1)
16 AddToolbarItem(label: "Create repository") { 16 } destination: { createdRepository in
17 showCreateRepositorySheet = true 17 RepositoryDetailView(
18 } 18 repository: createdRepository,
19 } 19 onRepositoryUpdated: { updatedRepository in
20 } 20 self.createdRepository = updatedRepository
21 .sheet(isPresented: $showCreateRepositorySheet) { 21 viewModel?.updateRepository(updatedRepository)
22 if let viewModel {
23 CreateRepositorySheet(viewModel: viewModel) { repository in
24 showCreateRepositorySheet = false
25 createdRepository = repository
26 }
27 }
28 }
29 .navigationDestination(isPresented: .isPresent($createdRepository)) {
30 if let createdRepository {
31 RepositoryDetailView(
32 repository: createdRepository,
33 onRepositoryUpdated: { updatedRepository in
34 self.createdRepository = updatedRepository
35 viewModel?.updateRepository(updatedRepository)
36 }
37 ) {
38 viewModel?.removeRepository(id: createdRepository.id)
39 } 22 }
23 ) {
24 viewModel?.removeRepository(id: createdRepository.id)
40 } 25 }
41 } 26 }
42 .task { 27 .task {
@@ -243,31 +228,23 @@ private struct CreateRepositorySheet: View {
243 .navigationTitle(service == .git ? "New Git Repository" : "New Mercurial Repository") 228 .navigationTitle(service == .git ? "New Git Repository" : "New Mercurial Repository")
244 .navigationBarTitleDisplayMode(.inline) 229 .navigationBarTitleDisplayMode(.inline)
245 .toolbar { 230 .toolbar {
246 ToolbarItem(placement: .cancellationAction) { 231 SubmitToolbar(
247 Button("Cancel") { dismiss() } 232 title: "Create",
248 } 233 isWorking: viewModel.isCreatingRepository,
249 ToolbarItem(placement: .confirmationAction) { 234 isDisabled: name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || viewModel.isCreatingRepository,
250 Button { 235 cancel: { dismiss() }
251 Task { 236 ) {
252 if let repository = await viewModel.createRepository( 237 Task {
253 service: service, 238 if let repository = await viewModel.createRepository(
254 name: name, 239 service: service,
255 description: description, 240 name: name,
256 visibility: visibility, 241 description: description,
257 cloneURL: cloneURL 242 visibility: visibility,
258 ) { 243 cloneURL: cloneURL
259 onCreated(repository) 244 ) {
260 } 245 onCreated(repository)
261 }
262 } label: {
263 if viewModel.isCreatingRepository {
264 ProgressView()
265 .controlSize(.small)
266 } else {
267 Text("Create")
268 } 246 }
269 } 247 }
270 .disabled(name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || viewModel.isCreatingRepository)
271 } 248 }
272 } 249 }
273 } 250 }
Hutch/Views/Shared/ModelContent.swift +34
@@ -30,3 +30,37 @@ struct AddToolbarItem: ToolbarContent {
30 } 30 }
31 } 31 }
32} 32}
33
34extension View {
35 /// A + button, once `model` exists, that presents `sheet`. The sheet calls back with what it
36 /// created, which closes it and pushes `destination`.
37 func createFlow<Model, Item, Sheet: View, Destination: View>(
38 _ model: Model?,
39 label: LocalizedStringKey,
40 isPresented: Binding<Bool>,
41 created: Binding<Item?>,
42 @ViewBuilder sheet: @escaping (Model, @escaping (Item) -> Void) -> Sheet,
43 @ViewBuilder destination: @escaping (Item) -> Destination
44 ) -> some View {
45 toolbar {
46 if model != nil {
47 AddToolbarItem(label: label) {
48 isPresented.wrappedValue = true
49 }
50 }
51 }
52 .sheet(isPresented: isPresented) {
53 if let model {
54 sheet(model) { item in
55 isPresented.wrappedValue = false
56 created.wrappedValue = item
57 }
58 }
59 }
60 .navigationDestination(isPresented: .isPresent(created)) {
61 if let item = created.wrappedValue {
62 destination(item)
63 }
64 }
65 }
66}
Hutch/Views/Shared/SubmitToolbar.swift added +27
@@ -0,0 +1,27 @@
1import SwiftUI
2
3/// Cancel, and a confirm button that shows a spinner in place of its title while `isWorking`.
4struct SubmitToolbar: ToolbarContent {
5 let title: LocalizedStringKey
6 let isWorking: Bool
7 let isDisabled: Bool
8 let cancel: () -> Void
9 let submit: () -> Void
10
11 var body: some ToolbarContent {
12 ToolbarItem(placement: .cancellationAction) {
13 Button("Cancel", action: cancel)
14 }
15 ToolbarItem(placement: .confirmationAction) {
16 Button(action: submit) {
17 if isWorking {
18 ProgressView()
19 .controlSize(.small)
20 } else {
21 Text(title)
22 }
23 }
24 .disabled(isDisabled)
25 }
26 }
27}
Hutch/Views/Tickets/TicketListView.swift +9 −17
@@ -658,25 +658,17 @@ private struct CreateTicketSheet: View {
658 .navigationTitle("New Ticket") 658 .navigationTitle("New Ticket")
659 .navigationBarTitleDisplayMode(.inline) 659 .navigationBarTitleDisplayMode(.inline)
660 .toolbar { 660 .toolbar {
661 ToolbarItem(placement: .cancellationAction) { 661 SubmitToolbar(
662 Button("Cancel") { dismiss() } 662 title: "Create Ticket",
663 } 663 isWorking: viewModel.isCreatingTicket,
664 ToolbarItem(placement: .confirmationAction) { 664 isDisabled: subject.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || viewModel.isCreatingTicket,
665 Button { 665 cancel: { dismiss() }
666 Task { 666 ) {
667 if let ticket = await viewModel.createTicket(subject: subject, body: descriptionText) { 667 Task {
668 onCreated(ticket) 668 if let ticket = await viewModel.createTicket(subject: subject, body: descriptionText) {
669 } 669 onCreated(ticket)
670 }
671 } label: {
672 if viewModel.isCreatingTicket {
673 ProgressView()
674 .controlSize(.small)
675 } else {
676 Text("Create Ticket")
677 } 670 }
678 } 671 }
679 .disabled(subject.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || viewModel.isCreatingTicket)
680 } 672 }
681 } 673 }
682 } 674 }
Hutch/Views/Tickets/TrackerManagementView.swift +17 −25
@@ -1026,34 +1026,26 @@ private struct TrackerDefaultACLEditorSheet: View {
1026 .navigationTitle("Default ACL") 1026 .navigationTitle("Default ACL")
1027 .navigationBarTitleDisplayMode(.inline) 1027 .navigationBarTitleDisplayMode(.inline)
1028 .toolbar { 1028 .toolbar {
1029 ToolbarItem(placement: .cancellationAction) { 1029 SubmitToolbar(
1030 Button("Cancel") { dismiss() } 1030 title: "Save",
1031 } 1031 isWorking: isSaving,
1032 ToolbarItem(placement: .confirmationAction) { 1032 isDisabled: isSaving,
1033 Button { 1033 cancel: { dismiss() }
1034 Task { 1034 ) {
1035 let didSave = await onSave( 1035 Task {
1036 TrackerACLPermissions( 1036 let didSave = await onSave(
1037 browse: browse, 1037 TrackerACLPermissions(
1038 submit: submit, 1038 browse: browse,
1039 comment: comment, 1039 submit: submit,
1040 edit: edit, 1040 comment: comment,
1041 triage: triage 1041 edit: edit,
1042 ) 1042 triage: triage
1043 ) 1043 )
1044 if didSave { 1044 )
1045 dismiss() 1045 if didSave {
1046 } 1046 dismiss()
1047 }
1048 } label: {
1049 if isSaving {
1050 ProgressView()
1051 .controlSize(.small)
1052 } else {
1053 Text("Save")
1054 } 1047 }
1055 } 1048 }
1056 .disabled(isSaving)
1057 } 1049 }
1058 } 1050 }
1059 } 1051 }