-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListBootcamp.swift
98 lines (86 loc) · 2.98 KB
/
ListBootcamp.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
//
// ListBootcamp.swift
// SwiftfulThinkingBootcamp
//
// Created by enesozmus on 11.05.2024.
//
import SwiftUI
struct ListBootcamp: View {
// ... ⬛️
@State var fruits: [String] = [
"apple", "orange", "banana", "peach", "tangerine"
]
@State var veggies: [String] = [
"tomato", "potato", "carrot"
]
// ... ⬛️
var body: some View {
NavigationStack {
List {
Section(
header:
HStack {
Text("Fruits")
Image(systemName: "flame.fill")
}
.font(.headline)
.foregroundColor(.orange)
) {
ForEach(fruits, id: \.self) { fruit in
Text(fruit)
.font(.caption)
.foregroundStyle(.white)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.pink)
}
// .onDelete(perform: { indexSet in
// //fruits.remove(atOffsets: indexSet)
// 🟢 excract the function logic from the view
// delete(indexSet: indexSet)
// })
.onDelete(perform: delete)
// .onMove(perform: { indices, newOffset in
// //fruits.move(fromOffsets: indices, toOffset: newOffset)
// move(indices: indices, newOffset: newOffset)
// })
.onMove(perform: move)
.listRowBackground(Color.black)
}
Section(header: Text("Veggies")) {
ForEach(veggies, id: \.self) { veggies in
Text(veggies.capitalized)
}
}
}
.listStyle(SidebarListStyle())
.navigationTitle("Grocery List")
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
EditButton()
}
ToolbarItem(placement: .navigationBarTrailing) {
addButton
}
}
}
.tint(.green)
}
// ... ⬛️
var addButton: some View {
Button("Add", action: {
add()
})
}
func delete(indexSet: IndexSet) {
fruits.remove(atOffsets: indexSet)
}
func move(indices: IndexSet, newOffset: Int) {
fruits.move(fromOffsets: indices, toOffset: newOffset)
}
func add() {
fruits.append("Coconut")
}
}
#Preview {
ListBootcamp()
}