-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTodoListView.swift
192 lines (172 loc) · 6.52 KB
/
TodoListView.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
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
//
// TodoListView.swift
// SwiftPlaygrounds
//
// Created by A. Zheng (github.com/aheze) on 1/10/22.
// Copyright © 2022 A. Zheng. All rights reserved.
//
import SwiftUI
struct TodoListItem: Identifiable, Codable {
var id = UUID()
var name = ""
var completed = false
}
struct PieChart: Shape {
var startAngle: CGFloat
var endAngle: CGFloat
public var animatableData: AnimatablePair<CGFloat, CGFloat> {
get { AnimatablePair(startAngle, endAngle) }
set { (startAngle, endAngle) = (newValue.first, newValue.second) }
}
func path(in rect: CGRect) -> Path {
var path = Path()
let center = CGPoint(x: rect.midX, y: rect.midY)
path.move(to: center)
path.addArc(
center: center,
radius: rect.height / 2,
startAngle: .radians(startAngle),
endAngle: .radians(endAngle),
clockwise: false
)
return path
}
}
struct TodoListView: View {
@State var items = [
TodoListItem(name: "Learn Swift", completed: true),
TodoListItem(name: "Learn SwiftUI", completed: false),
TodoListItem(name: "Bake a cookie", completed: false),
TodoListItem(name: "Do HW", completed: false),
]
@State var editMode = EditMode.inactive
@State var showingAlert = false
var body: some View {
NavigationView {
List {
Section("Chart") {
ZStack {
PieChart(startAngle: 0, endAngle: angleCompleted())
.fill(Color.green)
PieChart(startAngle: angleCompleted(), endAngle: 2 * .pi)
.fill(Color.blue)
}
.frame(height: 150)
.overlay(
Circle()
.fill(Color(uiColor: .systemBackground))
.overlay {
VStack {
let itemsCompletedCount = itemsCompletedCount()
if itemsCompletedCount < items.count {
Text("\(itemsCompletedCount) / \(items.count)")
.font(.system(.largeTitle, design: .rounded).bold())
} else {
Image(systemName: "checkmark")
.font(.system(.largeTitle, design: .rounded).bold())
}
}
.foregroundColor(.green)
}
.padding(24)
)
.padding()
}
Section("Actions") {
Button {
let newItem = TodoListItem()
withAnimation(.spring()) {
items.insert(newItem, at: 0)
}
} label: {
Text("Add Item")
.foregroundColor(.green)
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.opacity(editMode.isEditing ? 0.5 : 1)
Button {
showingAlert = true
} label: {
Text("Print Items")
.foregroundColor(.green)
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
}
.alert("Here are your current items", isPresented: $showingAlert) {
Button("Ok", role: .cancel) {}
} message: {
let string = items.map { "Name: \($0.name), completed: \($0.completed)" }.joined(separator: "\n")
Text(verbatim: string)
}
.buttonStyle(.plain)
.opacity(editMode.isEditing ? 0.5 : 1)
}
Section("Items") {
ForEach($items) { $item in
HStack {
TextField("Item Name", text: $item.name)
Spacer()
Button {
withAnimation(.easeOut(duration: 0.5)) {
item.completed.toggle()
}
} label: {
Image(systemName: "checkmark.circle")
.symbolVariant(item.completed ? .fill : .none)
.foregroundColor(.green)
}
.buttonStyle(.plain)
}
}
.onDelete(perform: delete)
.onMove(perform: move)
}
}
.listStyle(.insetGrouped)
.navigationTitle("Todo List")
.toolbar {
EditButton()
}
.environment(\.editMode, $editMode)
}
.navigationViewStyle(.stack)
.tint(.green)
}
func delete(at offsets: IndexSet) {
withAnimation(.spring()) {
items.remove(atOffsets: offsets)
}
}
func move(from source: IndexSet, to destination: Int) {
items.move(fromOffsets: source, toOffset: destination)
}
func itemsCompletedCount() -> Int {
let itemsCompleted = items.filter { $0.completed }
return itemsCompleted.count
}
func angleCompleted() -> CGFloat {
let percentageCompleted = CGFloat(itemsCompletedCount()) / CGFloat(items.count)
let angle = (2 * CGFloat.pi) * percentageCompleted
return angle
}
}
extension Array: RawRepresentable where Element: Codable {
public init?(rawValue: String) {
guard let data = rawValue.data(using: .utf8),
let result = try? JSONDecoder().decode([Element].self, from: data)
else {
return nil
}
self = result
}
public var rawValue: String {
guard let data = try? JSONEncoder().encode(self),
let result = String(data: data, encoding: .utf8)
else {
return "[]"
}
return result
}
}