/
ListPickerItem+View.swift
256 lines (230 loc) · 11 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
import FioriThemeManager
import Foundation
import SwiftUI
// FIXME: - Implement Fiori style definitions
extension Fiori {
enum ListPickerItem {
struct Key: ViewModifier {
func body(content: Content) -> some View {
content.font(.fiori(forTextStyle: .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?.listStyle(listpickerListStyle).listPickerListStyle(listpickerListStyle).listBackground(listBackground).typeErased,
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 {
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 single-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.
/// - allowsMultipleSelection: A boolean value to indicate to allow multiple selections or not.
/// - 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.
/// - rowBackground: `listRowBackground` for each row.
@available(iOS 15.0, macOS 12.0, *)
public init<Data, ID>(
_ data: Data,
id: KeyPath<Data.Element, ID>,
children: KeyPath<Data.Element, Data?>?,
selection: Binding<Set<ID>>?,
allowsMultipleSelection: Bool = true,
searchFilter: ((Data.Element, String) -> Bool)?,
@ViewBuilder rowContent: @escaping (Data.Element) -> some View,
rowBackground: ((Data.Element) -> some View)? = nil
)
where Data: RandomAccessCollection, ID: Hashable
{
self.init(data, id: id, children: children, selection: selection, isTopLevel: true, allowsMultipleSelection: allowsMultipleSelection, searchFilter: searchFilter, rowContent: rowContent, rowBackground: rowBackground)
}
@available(iOS 15.0, macOS 12.0, *)
public init<Data, ID>(_ data: Data,
id: KeyPath<Data.Element, ID>,
children: KeyPath<Data.Element, Data?>?,
selection: Binding<Set<ID>>?,
allowsMultipleSelection: Bool = true,
searchFilter: ((Data.Element, String) -> Bool)?,
@ViewBuilder rowContent: @escaping (Data.Element) -> some View)
where Data: RandomAccessCollection, ID: Hashable
{
self.init(data, id: id, children: children, selection: selection, isTopLevel: true, allowsMultipleSelection: allowsMultipleSelection, searchFilter: searchFilter, rowContent: rowContent, rowBackground: { _ in Color.preferredColor(.primaryBackground) })
}
/// Creates a configuration object from a collection of data 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.
/// - 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, 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>(_ data: Data,
children: KeyPath<Data.Element, Data?>?,
selection: Binding<Set<ID>>?,
@ViewBuilder rowContent: @escaping (Data.Element) -> some View)
where Data: RandomAccessCollection, 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)
}
}
}
extension ListPickerItemConfiguration {
@available(iOS 15.0, macOS 12.0, *)
init<Data, ID>(
_ data: Data,
id: KeyPath<Data.Element, ID>,
children: KeyPath<Data.Element, Data?>?,
selection: Binding<Set<ID>>?,
isTopLevel: Bool,
allowsMultipleSelection: Bool,
searchFilter: ((Data.Element, String) -> Bool)?,
@ViewBuilder rowContent: @escaping (Data.Element) -> some View,
rowBackground: ((Data.Element) -> some View)? = nil
)
where Data: RandomAccessCollection, ID: Hashable
{
self.destinationView = SearchableListView(data: data,
id: id,
children: children,
selection: selection,
isTopLevel: isTopLevel,
allowsMultipleSelection: allowsMultipleSelection,
searchFilter: searchFilter,
rowContent: rowContent,
rowBackground: rowBackground)
.typeErased
}
}
extension ListPickerItem {
struct Row<ID: Hashable>: View where Value == EmptyView {
private let content: Key
private let id: ID
private let allowsMultipleSelection: Bool
@Binding private var selection: Set<ID>
init(content: Key, id: ID, selection: Binding<Set<ID>>?, allowsMultipleSelection: Bool = true) {
self.content = content
self.id = id
self.allowsMultipleSelection = allowsMultipleSelection
self._selection = selection ?? Binding.constant(Set<ID>())
}
var body: some View {
let isSelected = self.selection.contains(self.id)
HStack {
self.content
Spacer()
if isSelected {
Image(systemName: "checkmark")
.foregroundColor(.preferredColor(.tintColor))
}
}
.contentShape(Rectangle())
.onTapGesture {
if isSelected {
self.selection.remove(self.id)
} else {
if !self.allowsMultipleSelection {
self.selection.removeAll()
}
self.selection.insert(self.id)
}
}
}
}
}
struct ListpickerListStyleKey: EnvironmentKey {
static let defaultValue: any ListStyle = .automatic
}
extension EnvironmentValues {
/// listpickerListStyle environment value.
var listpickerListStyle: any ListStyle {
get { self[ListpickerListStyleKey.self] }
set { self[ListpickerListStyleKey.self] = newValue }
}
}
public extension View {
/// List style for destination list in list picker.
/// - Parameter style: some `ListStyle`.
/// - Returns: New destination list style for list picker.
func listPickerListStyle(_ style: some ListStyle) -> some View {
self.environment(\.listpickerListStyle, style)
}
}