-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAddTodoListView.swift
57 lines (51 loc) · 1.57 KB
/
AddTodoListView.swift
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
import Foundation
import SwiftUI
struct AddTodoListView: View {
@Environment(SystemManager.self) private var system
@State private var isLoading = false
@Binding var newTodo: NewTodo
let listId: String
let completion: (Result<Bool, Error>) -> Void
var body: some View {
Section {
TextField("Description", text: $newTodo.description)
Button {
Task {
isLoading = true
defer { isLoading = false }
do {
try await system.insertTodo(newTodo, listId)
completion(.success(true))
} catch {
completion(.failure(error))
throw error
}
}
} label: {
HStack {
Text("Save")
if isLoading {
Spacer()
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
}
}
.frame(maxWidth: .infinity, alignment: .leading)
}
.disabled(isLoading)
}
}
}
#Preview {
AddTodoListView(
newTodo: .constant(
.init(
listId: UUID().uuidString.lowercased(),
isComplete: false,
description: ""
)
),
listId: UUID().uuidString.lowercased()
){ _ in
}.environment(SystemManager())
}