-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutlineGroupExperimentsView.swift
235 lines (214 loc) · 7.15 KB
/
OutlineGroupExperimentsView.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
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
225
226
227
228
229
230
231
232
233
234
235
//
// OutlineGroupExperimentsView.swift
// Prototype House
//
// Created by Felipe Espinoza on 30/05/2022.
//
import SwiftUI
struct OutlineGroupExperimentsView: View {
@State var data: [Item] = Self.sampleHierarchy
var body: some View {
specializedSOApproach
}
var nestedList: some View {
List(data, children: \.children) { item in
if item.children == nil {
leafContent(for: item)
} else {
nodeContent(for: item)
}
}
.navigationBarTitle("Hierarchies - List")
}
// var customList: some View {
// List {
// levelContent(for: $data)
// }
// .navigationTitle("Hierachies - Custom")
// }
// @ViewBuilder func levelContent(for items: Binding<[Item]>, level: Int = 0) -> some View {
// ForEach(items) { $item in
// DisclosureGroup(isExpanded: $item.isExpended) {
// nestedContent(for: $item, level: level)
// } label: {
// nodeContent(for: item, level: level)
// }
// }
// }
//
// @ViewBuilder func nestedContent(for item: Binding<Item>, level: Int) -> some View {
// if item.wrappedValue.children == nil {
// leafContent(for: item.wrappedValue)
// } else {
// levelContent(
// for: .init(
// get: { item.wrappedValue.children ?? [] },
// set: { newValue in item.wrappedValue.children = newValue }
// ),
// level: level + 1
// )
// }
// }
private func leafContent(for item: Item) -> some View {
Label(item.name, systemImage: "mappin.circle.fill")
.accentColor(.blue)
}
private func nodeContent(for item: Item, level: Int = 0) -> some View {
let nesting = String(repeating: "\t", count: level)
return Label(nesting + item.name, systemImage: "map.fill")
.font(.body.bold())
}
var stackOverflowApproach: some View {
List {
ForEach(data) { item in
NodeOutlineGroup(node: item, childKeyPath: \.children, isExpanded: true)
}
}
.navigationBarTitle("Hierarchies - StackOverflow")
}
var specializedSOApproach: some View {
List {
ForEach(data) { item in
ItemOutlineGroup(item: item, childKeyPath: \.children, isExpanded: true)
}
}
.navigationBarTitle("Hierarchies - Spec")
}
}
// from: https://stackoverflow.com/questions/62832809/list-or-outlinegroup-expanded-by-default-in-swiftui
struct NodeOutlineGroup<Node>: View where Node: Hashable, Node: Identifiable, Node: CustomStringConvertible {
let node: Node
let childKeyPath: KeyPath<Node, [Node]?>
@State var isExpanded: Bool = true
var body: some View {
if node[keyPath: childKeyPath] != nil {
DisclosureGroup(
isExpanded: $isExpanded,
content: {
if isExpanded {
ForEach(node[keyPath: childKeyPath]!) { childNode in
NodeOutlineGroup(node: childNode, childKeyPath: childKeyPath, isExpanded: isExpanded)
}
}
},
label: { Text(node.description) })
} else {
Text(node.description)
}
}
}
struct ItemOutlineGroup: View {
typealias Item = OutlineGroupExperimentsView.Item
let item: Item
let childKeyPath: KeyPath<Item, [Item]?>
@State var isExpanded: Bool = true
var body: some View {
if item[keyPath: childKeyPath] != nil {
DisclosureGroup(
isExpanded: $isExpanded,
content: {
if isExpanded {
ForEach(item[keyPath: childKeyPath]!) { childNode in
ItemOutlineGroup(item: childNode, childKeyPath: childKeyPath, isExpanded: isExpanded)
}
}
},
label: {
nodeContent(for: item)
}
)
} else {
leafContent(for: item)
}
}
private func leafContent(for item: Item) -> some View {
Label(item.name, systemImage: "mappin.circle.fill")
.accentColor(.blue)
}
private func nodeContent(for item: Item, level: Int = 0) -> some View {
let nesting = String(repeating: "\t", count: level)
return Label(nesting + item.name, systemImage: "map.fill")
.font(.body.bold())
}
}
extension OutlineGroupExperimentsView {
struct Item: Identifiable, CustomStringConvertible, Hashable {
var description: String { name }
let id: UUID = .init()
let name: String
var isExpended: Bool = false
var children: [Item]?
}
static let sampleHierarchy: [Item] = [
.init(
name: "Europe",
children: [
.init(
name: "Norway",
children: [
.init(name: "Oslo"),
.init(name: "Drammen"),
.init(name: "Begen"),
.init(name: "Tromsø"),
.init(name: "Sandefjord"),
]
),
.init(
name: "Sweden",
children: [
.init(name: "Stockholm"),
.init(name: "Malmö"),
.init(name: "Göteborg"),
]
),
.init(
name: "Germany",
children: [
.init(name: "Berlin"),
.init(name: "Stuttgart"),
.init(name: "Cologne"),
.init(name: "Munich"),
]
),
]
),
.init(
name: "Latin America",
children: [
.init(
name: "Chile",
children: [
.init(name: "Santiago"),
.init(name: "Concepción"),
.init(name: "Temuco"),
.init(name: "Arica"),
]
),
.init(
name: "Argentina",
children: [
.init(name: "Buenos Aires"),
.init(name: "Mendoza"),
]
),
.init(
name: "Brazil",
children: [
.init(name: "Brasilia"),
.init(name: "São Paulo"),
.init(name: "Rio de Janeiro"),
.init(name: "Fortaleza"),
.init(name: "Bahía"),
]
),
]
)
]
}
struct OutlineGroupExperimentsView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
OutlineGroupExperimentsView()
}
}
}