13
13
14
14
import Foundation
15
15
16
- /// Represents a section of items in a collection or list .
16
+ /// Represents a section of items in a collection.
17
17
@MainActor
18
18
public struct SectionViewModel : DiffableViewModel {
19
+ // MARK: DiffableViewModel
19
20
21
+ /// A unique id for this model.
20
22
public let id : UniqueIdentifier
21
23
24
+ // MARK: Properties
25
+
26
+ /// The cells in the section.
22
27
public let cells : [ AnyCellViewModel ]
23
28
29
+ /// The header for the section.
24
30
public let header : AnySupplementaryViewModel ?
25
31
32
+ /// The footer for the section.
26
33
public let footer : AnySupplementaryViewModel ?
27
34
35
+ /// The supplementary views in the section.
28
36
public let supplementaryViews : [ AnySupplementaryViewModel ]
29
37
30
- public var allSupplementaryViewsByIdentifier : [ UniqueIdentifier : AnySupplementaryViewModel ] {
31
- let tuples = self . supplementaryViews. map { ( $0. id, $0) }
32
- return Dictionary ( uniqueKeysWithValues: tuples)
33
- }
34
-
38
+ /// Returns `true` if the section has supplementary views, `false` otherwise.
35
39
public var hasSupplementaryViews : Bool {
36
40
self . header != nil
37
41
|| self . footer != nil
38
42
|| self . supplementaryViews. isNotEmpty
39
43
}
40
44
41
- public var cellRegistrations : Set < ViewRegistration > {
42
- Set ( self . cells. map { $0. registration } )
43
- }
44
-
45
- public var headerFooterRegistrations : Set < ViewRegistration > {
46
- Set ( [ self . header, self . footer] . compactMap { $0? . registration } )
47
- }
48
-
49
- public var supplementaryViewRegistrations : Set < ViewRegistration > {
50
- Set ( self . supplementaryViews. map { $0. registration } )
51
- }
52
-
53
- public var allRegistrations : Set < ViewRegistration > {
54
- let cells = self . cellRegistrations
55
- let headerFooter = self . headerFooterRegistrations
56
- let views = self . supplementaryViewRegistrations
57
- return cells. union ( views) . union ( headerFooter)
58
- }
45
+ // MARK: Init
59
46
47
+ /// Initializes a section.
48
+ ///
49
+ /// - Parameters:
50
+ /// - id: A unique identifier for the section.
51
+ /// - cells: The cells in the section.
60
52
public init ( id: UniqueIdentifier , cells: [ AnyCellViewModel ] = [ ] ) {
61
53
self . init (
62
54
id: id,
@@ -67,6 +59,11 @@ public struct SectionViewModel: DiffableViewModel {
67
59
)
68
60
}
69
61
62
+ /// Initializes a section.
63
+ ///
64
+ /// - Parameters:
65
+ /// - id: A unique identifier for the section.
66
+ /// - cells: The cells in the section.
70
67
public init < Cell: CellViewModel > (
71
68
id: UniqueIdentifier ,
72
69
cells: [ Cell ]
@@ -80,6 +77,14 @@ public struct SectionViewModel: DiffableViewModel {
80
77
)
81
78
}
82
79
80
+ /// Initializes a section.
81
+ ///
82
+ /// - Parameters:
83
+ /// - id: A unique identifier for the section.
84
+ /// - cells: The cells in the section.
85
+ /// - header: The header for the section.
86
+ /// - footer: The footer for the section.
87
+ /// - supplementaryViews: The supplementary views in the section.
83
88
public init < Header: SupplementaryHeaderViewModel , Footer: SupplementaryFooterViewModel > (
84
89
id: UniqueIdentifier ,
85
90
cells: [ AnyCellViewModel ] = [ ] ,
@@ -96,6 +101,14 @@ public struct SectionViewModel: DiffableViewModel {
96
101
)
97
102
}
98
103
104
+ /// Initializes a section.
105
+ ///
106
+ /// - Parameters:
107
+ /// - id: A unique identifier for the section.
108
+ /// - cells: The cells in the section.
109
+ /// - header: The header for the section.
110
+ /// - footer: The footer for the section.
111
+ /// - supplementaryViews: The supplementary views in the section.
99
112
public init < Cell: CellViewModel , Header: SupplementaryHeaderViewModel , Footer: SupplementaryFooterViewModel > (
100
113
id: UniqueIdentifier ,
101
114
cells: [ Cell ] ,
@@ -112,24 +125,34 @@ public struct SectionViewModel: DiffableViewModel {
112
125
)
113
126
}
114
127
115
- public init < Cell: CellViewModel ,
116
- Header: SupplementaryHeaderViewModel ,
117
- Footer: SupplementaryFooterViewModel ,
118
- View: SupplementaryViewModel > (
119
- id: UniqueIdentifier ,
120
- cells: [ Cell ] ,
121
- header: Header ? ,
122
- footer: Footer ? ,
123
- supplementaryViews: [ View ]
124
- ) {
125
- self . init (
126
- id: id,
127
- anyCells: cells. map { $0. eraseToAnyViewModel ( ) } ,
128
- anyHeader: header? . eraseToAnyViewModel ( ) ,
129
- anyFooter: footer? . eraseToAnyViewModel ( ) ,
130
- anySupplementaryViews: supplementaryViews. map { $0. eraseToAnyViewModel ( ) }
131
- )
132
- }
128
+ /// Initializes a section.
129
+ ///
130
+ /// - Parameters:
131
+ /// - id: A unique identifier for the section.
132
+ /// - cells: The cells in the section.
133
+ /// - header: The header for the section.
134
+ /// - footer: The footer for the section.
135
+ /// - supplementaryViews: The supplementary views in the section.
136
+ public init <
137
+ Cell: CellViewModel ,
138
+ Header: SupplementaryHeaderViewModel ,
139
+ Footer: SupplementaryFooterViewModel ,
140
+ View: SupplementaryViewModel >
141
+ (
142
+ id: UniqueIdentifier ,
143
+ cells: [ Cell ] ,
144
+ header: Header ? ,
145
+ footer: Footer ? ,
146
+ supplementaryViews: [ View ]
147
+ ) {
148
+ self . init (
149
+ id: id,
150
+ anyCells: cells. map { $0. eraseToAnyViewModel ( ) } ,
151
+ anyHeader: header? . eraseToAnyViewModel ( ) ,
152
+ anyFooter: footer? . eraseToAnyViewModel ( ) ,
153
+ anySupplementaryViews: supplementaryViews. map { $0. eraseToAnyViewModel ( ) }
154
+ )
155
+ }
133
156
134
157
private init (
135
158
id: UniqueIdentifier ,
@@ -148,10 +171,30 @@ public struct SectionViewModel: DiffableViewModel {
148
171
self . supplementaryViews = anySupplementaryViews
149
172
}
150
173
151
- func supplementaryViewsEqualTo( _ otherSection: Self ) -> Bool {
152
- self . header == otherSection. header
153
- && self . footer == otherSection. footer
154
- && self . supplementaryViews == otherSection. supplementaryViews
174
+ // MARK: Internal
175
+
176
+ func cellRegistrations( ) -> Set < ViewRegistration > {
177
+ Set ( self . cells. map { $0. registration } )
178
+ }
179
+
180
+ func headerFooterRegistrations( ) -> Set < ViewRegistration > {
181
+ Set ( [ self . header, self . footer] . compactMap { $0? . registration } )
182
+ }
183
+
184
+ func supplementaryViewRegistrations( ) -> Set < ViewRegistration > {
185
+ Set ( self . supplementaryViews. map { $0. registration } )
186
+ }
187
+
188
+ func allRegistrations( ) -> Set < ViewRegistration > {
189
+ let cells = self . cellRegistrations ( )
190
+ let headerFooter = self . headerFooterRegistrations ( )
191
+ let views = self . supplementaryViewRegistrations ( )
192
+ return cells. union ( views) . union ( headerFooter)
193
+ }
194
+
195
+ func allSupplementaryViewsByIdentifier( ) -> [ UniqueIdentifier : AnySupplementaryViewModel ] {
196
+ let tuples = self . supplementaryViews. map { ( $0. id, $0) }
197
+ return Dictionary ( uniqueKeysWithValues: tuples)
155
198
}
156
199
}
157
200
@@ -241,7 +284,7 @@ extension SectionViewModel: CustomDebugStringConvertible {
241
284
}
242
285
243
286
text. append ( " registrations: \n " )
244
- self . allRegistrations. forEach {
287
+ self . allRegistrations ( ) . forEach {
245
288
text. append ( " \t - \( $0. reuseIdentifier) ( \( $0. viewType. kind) ) \n " )
246
289
}
247
290
text. append ( " isEmpty: \( self . isEmpty) \n " )
0 commit comments