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
|
import SwiftUI
struct RepositoryListView: View {
@Environment(AppState.self) private var appState
@State private var viewModel: RepositoryListViewModel?
@State private var searchTask: Task<Void, Never>?
@State private var showCreateRepositorySheet = false
@State private var createdRepository: RepositorySummary?
var body: some View {
Group {
if let viewModel {
listContent(viewModel)
} else {
SRHTLoadingStateView(message: "Loading repositories…")
}
}
.navigationTitle("Repositories")
.toolbar {
if viewModel != nil {
ToolbarItem(placement: .topBarTrailing) {
Button {
showCreateRepositorySheet = true
} label: {
Image(systemName: "plus")
}
}
}
}
.sheet(isPresented: $showCreateRepositorySheet) {
if let viewModel {
CreateRepositorySheet(viewModel: viewModel) { repository in
showCreateRepositorySheet = false
createdRepository = repository
}
}
}
.navigationDestination(isPresented: Binding(
get: { createdRepository != nil },
set: { isPresented in
if !isPresented {
createdRepository = nil
}
}
)) {
if let createdRepository {
RepositoryDetailView(repository: createdRepository) {
viewModel?.removeRepository(id: createdRepository.id)
}
}
}
.task {
if viewModel == nil {
viewModel = RepositoryListViewModel(client: appState.client)
}
}
}
@ViewBuilder
private func listContent(_ viewModel: RepositoryListViewModel) -> some View {
@Bindable var vm = viewModel
List {
ForEach(viewModel.repositories) { repo in
NavigationLink(value: repo) {
RepositoryRowView(repository: repo)
}
.alignmentGuide(.listRowSeparatorLeading) { _ in 0 }
.task {
await viewModel.loadMoreIfNeeded(currentItem: repo)
}
}
if viewModel.isLoadingMore {
HStack {
Spacer()
ProgressView()
Spacer()
}
.listRowSeparator(.hidden)
}
}
.listStyle(.plain)
.searchable(text: $vm.searchText, placement: .navigationBarDrawer(displayMode: .always), prompt: "Search repositories")
.overlay {
if viewModel.isLoading, viewModel.repositories.isEmpty {
SRHTLoadingStateView(message: "Loading repositories…")
} else if let error = viewModel.error, viewModel.repositories.isEmpty {
SRHTErrorStateView(
title: "Couldn't Load Repositories",
message: error,
retryAction: { await viewModel.loadRepositories() }
)
} else if viewModel.repositories.isEmpty, viewModel.error == nil {
if viewModel.searchText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
ContentUnavailableView(
"No Repositories",
systemImage: "book.closed",
description: Text("You don't have any repositories yet.")
)
} else {
ContentUnavailableView.search
}
}
}
.connectivityOverlay(hasContent: !viewModel.repositories.isEmpty) {
await viewModel.loadRepositories()
}
.srhtErrorBanner(error: $vm.error)
.refreshable {
await viewModel.loadRepositories()
}
.task {
await viewModel.loadRepositories()
}
.onChange(of: viewModel.searchText) { oldValue, newValue in
// Cancel previous search task
searchTask?.cancel()
// Clear results immediately when search text is cleared
if newValue.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
viewModel.resetSearch()
Task {
await viewModel.loadRepositories()
}
return
}
// Debounce search to avoid excessive API calls
searchTask = Task {
try? await Task.sleep(for: .milliseconds(350))
guard !Task.isCancelled else { return }
await viewModel.loadRepositories(search: newValue)
}
}
.navigationDestination(for: RepositorySummary.self) { repo in
RepositoryDetailView(repository: repo) {
viewModel.removeRepository(id: repo.id)
}
}
}
}
private struct CreateRepositorySheet: View {
let viewModel: RepositoryListViewModel
let onCreated: (RepositorySummary) -> Void
@Environment(\.dismiss) private var dismiss
@State private var name = ""
@State private var description = ""
@State private var cloneURL = ""
@State private var visibility: Visibility = .public
@State private var service: RepositoryCreationService = .git
var body: some View {
NavigationStack {
Form {
Section("Repository Details") {
Picker("Version Control", selection: $service) {
ForEach(RepositoryCreationService.allCases) { service in
Text(service.displayName).tag(service)
}
}
TextField("Repository name", text: $name)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
TextField("Short description (optional)", text: $description, axis: .vertical)
.lineLimit(2...4)
Picker("Visibility", selection: $visibility) {
Text("Public").tag(Visibility.public)
Text("Unlisted").tag(Visibility.unlisted)
Text("Private").tag(Visibility.private)
}
}
Section("Import Existing Repository") {
if service == .git {
TextField("Remote URL (optional)", text: $cloneURL)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.keyboardType(.URL)
Text("Import an existing Git repository from a remote URL.")
.font(.footnote)
.foregroundStyle(.secondary)
} else {
Text("Importing a Mercurial repository from a remote URL is not available through the public API.")
.font(.footnote)
.foregroundStyle(.secondary)
}
}
}
.navigationTitle(service == .git ? "New Git Repository" : "New Mercurial Repository")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") { dismiss() }
}
ToolbarItem(placement: .confirmationAction) {
Button {
Task {
if let repository = await viewModel.createRepository(
service: service,
name: name,
description: description,
visibility: visibility,
cloneURL: cloneURL
) {
onCreated(repository)
}
}
} label: {
if viewModel.isCreatingRepository {
ProgressView()
.controlSize(.small)
} else {
Text("Create Repository")
}
}
.disabled(name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || viewModel.isCreatingRepository)
}
}
}
}
}
|