-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathElementHeaderFooter.swift
147 lines (130 loc) · 4.81 KB
/
ElementHeaderFooter.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
//
// ElementHeaderFooter.swift
// BlueprintUILists
//
// Created by Kyle Van Essen on 10/9/20.
//
import ListableUI
import BlueprintUI
///
/// ⚠️ This method is soft-deprecated! Consider using `myElement.headerFooter(...)` instead.
///
/// Provides a way to create a `HeaderFooter` for your Blueprint elements without
/// requiring the creation of a new `BlueprintHeaderFooterContent` struct.
///
/// Most arguments on this method are not required – you must only
/// provide an input and an element provider.
///
/// ### Note
/// This initializer is helpful if you have to nest an existing element that needs to be used in
/// a single place, without needing to define an entirely new type.
///
/// If your header or footer is to be used in more than one place, it is recommended that you
/// create a `BlueprintHeaderFooterContent` struct to share logic to avoid duplicate code.
///
/// ### Example
/// ```
/// section.header = ElementHeaderFooter(album) { lhs, rhs in
/// lhs.value != rhs.value
/// }, element: { album in
/// AlbumElement(...)
/// }, background: { album in
/// ...
/// }, pressedBackground: album in
/// ...
/// }, configure: { header in
/// ...
/// }
/// ```
public func ElementHeaderFooter<Represented>(
_ represented : Represented,
isEquivalent : @escaping (Represented, Represented) -> Bool,
element : @escaping (Represented) -> Element,
background : @escaping (Represented) -> Element? = { _ in nil },
pressedBackground : @escaping (Represented) -> Element? = { _ in nil },
configure : (inout HeaderFooter<ElementHeaderFooterContent<Represented>>) -> () = { _ in }
) -> HeaderFooter<ElementHeaderFooterContent<Represented>>
{
HeaderFooter(
ElementHeaderFooterContent<Represented>(
represented: represented,
isEquivalentProvider: isEquivalent,
elementProvider: element,
backgroundProvider: background,
pressedBackgroundProvider: pressedBackground
),
configure: configure
)
}
///
/// ⚠️ This method is soft-deprecated! Consider using `myElement.headerFooter(...)` instead.
///
/// Provides a way to create a `HeaderFooter` for your Blueprint elements without
/// requiring the creation of a new `BlueprintHeaderFooterContent` struct.
///
/// Most arguments on this method are not required – you must only
/// provide an input and an element provider.
///
/// Note
/// ----
/// This initializer is helpful if you have to nest an existing element that needs to be used in
/// a single place, without needing to define an entirely new type.
///
/// If your header or footer is to be used in more than one place, it is recommended that you
/// create a `BlueprintHeaderFooterContent` struct to share logic to avoid duplicate code.
///
/// Example
/// -------
/// ```
/// section.header = ElementHeaderFooter(album) { album in
/// AlbumElement(...)
/// }, background: { album in
/// ...
/// }, pressedBackground: album in
/// ...
/// }, configure: { header in
/// ...
/// }
/// ```
public func ElementHeaderFooter<Represented:Equatable>(
_ represented : Represented,
element : @escaping (Represented) -> Element,
background : @escaping (Represented) -> Element? = { _ in nil },
pressedBackground : @escaping (Represented) -> Element? = { _ in nil },
configure : (inout HeaderFooter<ElementHeaderFooterContent<Represented>>) -> () = { _ in }
) -> HeaderFooter<ElementHeaderFooterContent<Represented>>
{
HeaderFooter(
ElementHeaderFooterContent<Represented>(
represented: represented,
isEquivalentProvider: { $0 == $1 },
elementProvider: element,
backgroundProvider: background,
pressedBackgroundProvider: pressedBackground
),
configure: configure
)
}
/// The `BlueprintHeaderFooterContent` type that is used to provide
/// a lightweight way to present an `Element`, without needing to provide an entirely
/// new `BlueprintHeaderFooterContent` type.
public struct ElementHeaderFooterContent<Represented> : BlueprintHeaderFooterContent
{
public let represented : Represented
let isEquivalentProvider : (Represented, Represented) -> Bool
let elementProvider : (Represented) -> Element
let backgroundProvider : (Represented) -> Element?
let pressedBackgroundProvider : (Represented) -> Element?
public func isEquivalent(to other: Self) -> Bool {
self.isEquivalentProvider(self.represented, other.represented)
}
public var elementRepresentation : Element {
self.elementProvider(self.represented)
}
public var background : Element? {
self.backgroundProvider(self.represented)
}
public var pressedBackground : Element? {
self.pressedBackgroundProvider(self.represented)
}
}