-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync.swift
More file actions
200 lines (184 loc) · 6.33 KB
/
sync.swift
File metadata and controls
200 lines (184 loc) · 6.33 KB
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
import SwiftUI
import Observation
struct Repository: Identifiable, Hashable {
let id: String
var name: String
var url: String
var branch: String
var lastSynced: Date?
var fileCount: Int
var displayName: String {
url.split(separator: "/").last.map(String.init) ?? name
}
}
enum SyncStatus: Equatable {
case idle
case syncing(progress: Double)
case completed(filesChanged: Int)
case failed(message: String)
var isActive: Bool {
if case .syncing = self { return true }
return false
}
}
@Observable
class RepoListViewModel {
var repositories: [Repository] = []
var syncStatuses: [String: SyncStatus] = [:]
var searchText: String = ""
var showAddSheet: Bool = false
var errorMessage: String?
var filteredRepos: [Repository] {
guard !searchText.isEmpty else { return repositories }
return repositories.filter {
$0.name.localizedCaseInsensitiveContains(searchText) ||
$0.url.localizedCaseInsensitiveContains(searchText)
}
}
var activeSyncCount: Int {
syncStatuses.values.filter(\.isActive).count
}
func addRepository(name: String, url: String) {
let repo = Repository(
id: UUID().uuidString,
name: name,
url: url,
branch: "main",
lastSynced: nil,
fileCount: 0
)
repositories.append(repo)
}
func syncRepository(_ repo: Repository) async {
syncStatuses[repo.id] = .syncing(progress: 0)
do {
for i in 1...10 {
try await Task.sleep(for: .milliseconds(200))
syncStatuses[repo.id] = .syncing(progress: Double(i) / 10.0)
}
let changed = Int.random(in: 1...30)
syncStatuses[repo.id] = .completed(filesChanged: changed)
if let idx = repositories.firstIndex(where: { $0.id == repo.id }) {
repositories[idx].lastSynced = Date()
repositories[idx].fileCount += changed
}
} catch {
syncStatuses[repo.id] = .failed(message: error.localizedDescription)
}
}
func deleteRepository(_ repo: Repository) {
repositories.removeAll { $0.id == repo.id }
syncStatuses.removeValue(forKey: repo.id)
}
}
struct RepoListView: View {
@State private var viewModel = RepoListViewModel()
var body: some View {
NavigationStack {
List {
ForEach(viewModel.filteredRepos) { repo in
RepoRowView(repo: repo, status: viewModel.syncStatuses[repo.id] ?? .idle)
.swipeActions(edge: .trailing) {
Button(role: .destructive) {
viewModel.deleteRepository(repo)
} label: {
Label("Delete", systemImage: "trash")
}
}
.swipeActions(edge: .leading) {
Button {
Task { await viewModel.syncRepository(repo) }
} label: {
Label("Sync", systemImage: "arrow.triangle.2.circlepath")
}
.tint(.blue)
}
}
}
.searchable(text: $viewModel.searchText, prompt: "Search repositories")
.navigationTitle("Repositories")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button { viewModel.showAddSheet = true } label: {
Image(systemName: "plus")
}
}
ToolbarItem(placement: .status) {
if viewModel.activeSyncCount > 0 {
Text("Syncing \(viewModel.activeSyncCount)...")
.font(.caption)
.foregroundStyle(.secondary)
}
}
}
.sheet(isPresented: $viewModel.showAddSheet) {
AddRepoView { name, url in
viewModel.addRepository(name: name, url: url)
}
}
}
}
}
struct RepoRowView: View {
let repo: Repository
let status: SyncStatus
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text(repo.displayName)
.font(.headline)
Text(repo.url)
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(1)
HStack {
Label("\(repo.fileCount)", systemImage: "doc")
.font(.caption2)
if let date = repo.lastSynced {
Text("Synced \(date, style: .relative) ago")
.font(.caption2)
.foregroundStyle(.secondary)
}
Spacer()
switch status {
case .syncing(let progress):
ProgressView(value: progress)
.frame(width: 60)
case .failed(let msg):
Text(msg).font(.caption2).foregroundStyle(.red)
default:
EmptyView()
}
}
}
.padding(.vertical, 2)
}
}
struct AddRepoView: View {
@Environment(\.dismiss) private var dismiss
@State private var name = ""
@State private var url = ""
var onAdd: (String, String) -> Void
var body: some View {
NavigationStack {
Form {
TextField("Repository name", text: $name)
TextField("Git URL", text: $url)
.textInputAutocapitalization(.never)
.keyboardType(.URL)
}
.navigationTitle("Add Repository")
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") { dismiss() }
}
ToolbarItem(placement: .confirmationAction) {
Button("Add") {
onAdd(name, url)
dismiss()
}
.disabled(name.isEmpty || url.isEmpty)
}
}
}
}
}