/
ListPickerItem+View.swift
314 lines (284 loc) · 13.8 KB
/
ListPickerItem+View.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import Foundation
import SwiftUI
// FIXME: - Implement Fiori style definitions
extension Fiori {
enum ListPickerItem {
struct Key: ViewModifier {
func body(content: Content) -> some View {
content.font(.headline).foregroundColor(.preferredColor(.primaryLabel))
}
}
typealias KeyCumulative = EmptyModifier
typealias Value = EmptyModifier
typealias ValueCumulative = EmptyModifier
static let key = Key()
static let value = Value()
static let keyCumulative = KeyCumulative()
static let valueCumulative = ValueCumulative()
}
}
// FIXME: - Implement ListPickerItem View body
extension ListPickerItem: View {
public var body: some View {
NavigationLink(
destination: destinationView,
label: {
KeyValueItem {
key
} value: {
value
}
}
)
}
}
public extension ListPickerItem {
/// Returns a list picker item with given configuration.
/// - Parameters:
/// - key: The key view of the list.
/// - value: The value view of the list.
/// - configuration: The configuration for constructing the list picker.
init(
@ViewBuilder key: @escaping () -> Key,
@ViewBuilder value: @escaping () -> Value,
configuration: ListPickerItemConfiguration? = nil
) {
self.init(key: key, value: value)
if let configuration = configuration {
destinationView = configuration.destinationView
}
}
}
/// The configuration for constructing the list picker.
public struct ListPickerItemConfiguration {
let destinationView: AnyView
/// Creates a searchable configuration object from a collection of data which supports signle-level picker with the ability to select.
/// - Parameters:
/// - data: The data for constructing the list picker.
/// - id: The key path to the data model's unique identifier.
/// - children: The key path to the optional property of a data element whose value indicates the children of that element.
/// - selection: A binding to a set which stores the selected items.
/// - searchFilter: The closure to filter the `data` in searching process. Request a boolen by the element and the filter key.
/// - rowContent: The view builder which returns the content of each row in the list picker.
@available(iOS 15.0, macOS 12.0, *)
public init<Data, ID, RowContent>(_ data: Data,
id: KeyPath<Data.Element, ID>,
children: KeyPath<Data.Element, Data?>?,
selection: Binding<Set<ID>>?,
searchFilter: @escaping (Data.Element, String) -> Bool,
@ViewBuilder rowContent: @escaping (Data.Element) -> RowContent)
where Data: RandomAccessCollection, RowContent: View, ID: Hashable
{
self.init(data, id: id, children: children, selection: selection, isTopLevel: true, searchFilter: searchFilter, rowContent: rowContent)
}
/// Creates a configuration object from a collection of data which supports both signle-level and multi-level picker with the ability to select multiple items.
/// - Parameters:
/// - data: The data for constructing the list picker.
/// - id: The key path to the data model's unique identifier.
/// - children: The key path to the optional property of a data element whose value indicates the children of that element.
/// - selection: A binding to a set which stores the selected items.
/// - rowContent: The view builder which returns the content of each row in the list picker.
public init<Data, ID, RowContent>(_ data: Data,
id: KeyPath<Data.Element, ID>,
children: KeyPath<Data.Element, Data?>?,
selection: Binding<Set<ID>>?,
@ViewBuilder rowContent: @escaping (Data.Element) -> RowContent)
where Data: RandomAccessCollection, RowContent: View, ID: Hashable
{
self.destinationView = List {
ForEach(data, id: id) { element in
let row = rowContent(element)
let id_value = element[keyPath: id]
if let children = children, let childrenData = element[keyPath: children] {
ListPickerItem<RowContent, EmptyView>(key: {
row
}, value: {
EmptyView()
}, configuration: ListPickerItemConfiguration(childrenData, id: id, children: children, selection: selection, rowContent: rowContent))
} else {
ListPickerItem.Row(content: row, id: id_value, selection: selection)
}
}
}.typeErased
}
/// Creates a configuration object from a collection of data (conforms to `Identifiable`) which supports both single-level and multi-level picker with the ability to select multiple items.
/// - Parameters:
/// - data: The data for constructing the list picker.
/// - children: The key path to the optional property of a data element whose value indicates the children of that element.
/// - selection: A binding to a set which stores the selected items.
/// - rowContent: The view builder which returns the content of each row in the list picker.
public init<Data, ID, RowContent>(_ data: Data,
children: KeyPath<Data.Element, Data?>?,
selection: Binding<Set<ID>>?,
@ViewBuilder rowContent: @escaping (Data.Element) -> RowContent)
where Data: RandomAccessCollection, RowContent: View, Data.Element: Identifiable, ID == Data.Element.ID
{
let id = \Data.Element.id
self.init(data, id: id, children: children, selection: selection, rowContent: rowContent)
}
}
public extension ListPickerItemConfiguration {
/// Creates a configuration object from a collection of `String` which supports both single-level and multi-level picker with the ability to select multiple items.
/// - Parameters:
/// - data: An array of strings for constructing the list.
/// - selection: A binding to a set which stores the selected items.
init(_ data: [String], selection: Binding<Set<String>>?) {
self.init(data, id: \.self, children: nil, selection: selection) { str in
Text(str)
}
}
}
private extension ListPickerItemConfiguration {
@available(iOS 15.0, macOS 12.0, *)
init<Data, ID, RowContent>(_ data: Data,
id: KeyPath<Data.Element, ID>,
children: KeyPath<Data.Element, Data?>?,
selection: Binding<Set<ID>>?,
isTopLevel: Bool,
searchFilter: @escaping (Data.Element, String) -> Bool,
@ViewBuilder rowContent: @escaping (Data.Element) -> RowContent)
where Data: RandomAccessCollection, RowContent: View, ID: Hashable
{
self.destinationView = SearchListView(data: data,
id: id,
children: children,
selection: selection,
isTopLevel: isTopLevel,
searchFilter: searchFilter,
rowContent: rowContent).typeErased
}
}
extension ListPickerItem {
struct Row<ID: Hashable>: View where Value == EmptyView {
private let content: Key
private let id: ID
@Binding private var selection: Set<ID>
init(content: Key, id: ID, selection: Binding<Set<ID>>?) {
self.content = content
self.id = id
self._selection = selection ?? Binding.constant(Set<ID>())
}
var body: some View {
let isSelected = selection.contains(id)
HStack {
content
Spacer()
if isSelected {
Image(systemName: "checkmark")
.foregroundColor(.preferredColor(.tintColor))
}
}
.contentShape(Rectangle())
.onTapGesture {
if isSelected {
selection.remove(id)
} else {
selection.insert(id)
}
}
}
}
}
@available(iOS 15.0, macOS 12.0, *)
public struct SearchListView<Data: RandomAccessCollection,
ID: Hashable,
RowContent: View>: View
{
@Environment(\.presentationMode) var presentationMode
@State var searchText = ""
@State private var selectionBuffer: Set<ID>
let data: Data
let id: KeyPath<Data.Element, ID>
let children: KeyPath<Data.Element, Data?>?
let selection: Binding<Set<ID>>?
let searchFilter: (Data.Element, String) -> Bool
let rowContent: (Data.Element) -> RowContent
private let isTopLevel: Bool
public init(data: Data,
id: KeyPath<Data.Element, ID>,
children: KeyPath<Data.Element, Data?>?,
selection: Binding<Set<ID>>?,
isTopLevel: Bool = true,
searchFilter: @escaping (Data.Element, String) -> Bool,
@ViewBuilder rowContent: @escaping (Data.Element) -> RowContent)
{
self.data = data
self.id = id
self.selection = selection
self.children = children
self.isTopLevel = isTopLevel
self.searchFilter = searchFilter
self.rowContent = rowContent
self._selectionBuffer = State(wrappedValue: selection?.wrappedValue ?? Set<ID>())
}
public var body: some View {
#if swift(>=5.5)
List {
ForEach(data.filter { element in
searchFilter(element, searchText)
}, id: id) { element in
let row = rowContent(element)
let id_value = element[keyPath: id]
if let children = children, let childrenData = element[keyPath: children] {
let configuration = ListPickerItemConfiguration(childrenData,
id: id,
children: children,
selection: !isTopLevel ? selection : $selectionBuffer,
isTopLevel: false,
searchFilter: searchFilter,
rowContent: rowContent)
ListPickerItem<RowContent, EmptyView>(key: {
row
}, value: {
EmptyView()
}, configuration: configuration)
} else {
ListPickerItem.Row(content: row, id: id_value, selection: !isTopLevel ? selection : $selectionBuffer)
}
}
}
.searchable(text: $searchText)
.ifApply(isTopLevel) {
$0.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("Cancel") {
presentationMode.wrappedValue.dismiss()
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button("Confirm") {
selection?.wrappedValue = selectionBuffer
presentationMode.wrappedValue.dismiss()
}
}
}
.navigationBarBackButtonHidden(true)
}
#else
List {
ForEach(data.filter { element in
searchFilter(element, searchText)
}, id: id) { element in
let row = rowContent(element)
let id_value = element[keyPath: id]
if let children = children, let childrenData = element[keyPath: children] {
let configuration = ListPickerItemConfiguration(childrenData,
id: id,
children: children,
selection: !isTopLevel ? selection : $selectionBuffer,
isTopLevel: false,
searchFilter: searchFilter,
rowContent: rowContent)
ListPickerItem<RowContent, EmptyView>(key: {
row
}, value: {
EmptyView()
}, configuration: configuration)
} else {
ListPickerItem.Row(content: row, id: id_value, selection: !isTopLevel ? selection : $selectionBuffer)
}
}
}
#endif
}
}