-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCollectionViewModel.swift
299 lines (250 loc) · 8.87 KB
/
CollectionViewModel.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
//
// Created by Jesse Squires
// https://www.jessesquires.com
//
// Documentation
// https://jessesquires.github.io/ReactiveCollectionsKit
//
// GitHub
// https://github.com/jessesquires/ReactiveCollectionsKit
//
// Copyright © 2019-present Jesse Squires
//
import Foundation
import UIKit
/// Represents a collection view with sections and items.
public struct CollectionViewModel: DiffableViewModel {
/// Returns the empty collection view model.
public static var empty: Self {
Self(id: "com.ReactiveCollectionsKit.CollectionViewModel.empty")
}
// MARK: DiffableViewModel
/// A unique id for this model.
public let id: UniqueIdentifier
// MARK: Properties
/// The sections in the collection.
public let sections: [SectionViewModel]
// MARK: Init
/// Initializes a new ``CollectionViewModel``.
///
/// - Parameters:
/// - id: A unique identifier for the collection.
/// - sections: The sections in the collection.
public init(id: UniqueIdentifier, sections: [SectionViewModel] = []) {
self.id = id
self.sections = sections.filter(\.isNotEmpty)
}
// MARK: Accessing Sections
/// Returns the section for the specified `id`.
/// - Parameter id: The identifier for the section.
/// - Returns: The section, if it exists.
public func sectionViewModel(for id: UniqueIdentifier) -> SectionViewModel? {
self.sections.first { $0.id == id }
}
/// Returns the section at the specified index.
///
/// - Parameter index: The index of the section.
/// - Returns: The section at `index`.
///
/// - Precondition: The specified `index` must be valid.
public func sectionViewModel(at index: Int) -> SectionViewModel {
precondition(index < self.count)
return self.sections[index]
}
// MARK: Accessing Cells
/// Returns the cell for the specified `id`.
///
/// - Parameter id: The identifier for the cell.
/// - Returns: The cell, if it exists.
public func cellViewModel(for id: UniqueIdentifier) -> AnyCellViewModel? {
for section in self.sections {
for cell in section.cells where cell.id == id {
return cell
}
}
return nil
}
/// Returns the cell at the specified index path.
///
/// - Parameter indexPath: The index path of the cell.
/// - Returns: The cell at `indexPath`.
///
/// - Precondition: The specified `indexPath` must be valid.
public func cellViewModel(at indexPath: IndexPath) -> AnyCellViewModel {
precondition(indexPath.section < self.count)
let section = self.sectionViewModel(at: indexPath.section)
let cells = section.cells
precondition(indexPath.item < cells.count)
return cells[indexPath.item]
}
// MARK: Accessing Supplementary Views
/// Returns the supplementary view for the specified `id`.
///
/// - Parameter id: The identifier for the supplementary view.
/// - Returns: The supplementary view, if it exists.
public func supplementaryViewModel(for id: UniqueIdentifier) -> AnySupplementaryViewModel? {
for section in self.sections {
for view in section.supplementaryViews where view.id == id {
return view
}
}
return nil
}
/// Returns the supplementary view for the specified kind and index path.
/// - Parameters:
/// - kind: The kind of the supplementary view.
/// - indexPath: The index path for the view.
/// - Returns: The supplementary view.
///
/// - Precondition: The specified `indexPath` must be valid.
public func supplementaryViewModel(ofKind kind: String, at indexPath: IndexPath) -> AnySupplementaryViewModel? {
precondition(indexPath.section < self.count)
let section = self.sectionViewModel(at: indexPath.section)
if kind == section.header?._kind {
return section.header
}
if kind == section.footer?._kind {
return section.footer
}
let supplementaryViews = section.supplementaryViews.filter { $0._kind == kind }
if indexPath.item < supplementaryViews.count {
return supplementaryViews[indexPath.item]
}
return nil
}
// MARK: Subscript
/// Returns the cell at the specified index path.
///
/// - Parameter indexPath: The index path of the cell.
/// - Returns: The cell at `indexPath`.
///
/// - Precondition: The specified `indexPath` must be valid.
public subscript(indexPath: IndexPath) -> AnyCellViewModel {
self.sections[indexPath.section][indexPath.item]
}
/// Returns the cell at the specified indexes.
///
/// - Parameter item: The item index of the cell.
/// - Parameter section: The section index of the cell.
/// - Returns: The cell at `item` and `section`.
///
/// - Precondition: The specified indexes must be valid.
public subscript(item item: Int, section section: Int) -> AnyCellViewModel {
self.sections[section][item]
}
// MARK: Internal
func _safeSectionViewModel(at index: Int) -> SectionViewModel? {
guard index < self.count else {
return nil
}
return self.sectionViewModel(at: index)
}
func _safeCellViewModel(at indexPath: IndexPath) -> AnyCellViewModel? {
guard let section = self._safeSectionViewModel(at: indexPath.section),
indexPath.item < section.cells.count else {
return nil
}
return self.cellViewModel(at: indexPath)
}
func _safeSupplementaryViewModel(ofKind kind: String, at indexPath: IndexPath) -> AnySupplementaryViewModel? {
guard let section = self._safeSectionViewModel(at: indexPath.section) else {
return nil
}
if kind == section.header?._kind {
return section.header
}
if kind == section.footer?._kind {
return section.footer
}
let supplementaryViews = section.supplementaryViews.filter { $0._kind == kind }
guard indexPath.item < supplementaryViews.count else {
return nil
}
return self.supplementaryViewModel(ofKind: kind, at: indexPath)
}
func allRegistrations() -> Set<ViewRegistration> {
var all = Set<ViewRegistration>()
self.sections.forEach {
all.formUnion($0.allRegistrations())
}
return all
}
func allCellRegistrations() -> Set<ViewRegistration> {
var all = Set<ViewRegistration>()
self.sections.forEach {
all.formUnion($0.cellRegistrations())
}
return all
}
func allHeaderFooterRegistrations() -> Set<ViewRegistration> {
var all = Set<ViewRegistration>()
self.sections.forEach {
all.formUnion($0.headerFooterRegistrations())
}
return all
}
func allSupplementaryViewRegistrations() -> Set<ViewRegistration> {
var all = Set<ViewRegistration>()
self.sections.forEach {
all.formUnion($0.supplementaryViewRegistrations())
}
return all
}
func allSectionsByIdentifier() -> [UniqueIdentifier: SectionViewModel] {
let tuples = self.sections.map { ($0.id, $0) }
return Dictionary(uniqueKeysWithValues: tuples)
}
func allCellsByIdentifier() -> [UniqueIdentifier: AnyCellViewModel] {
let allCells = self.flatMap(\.cells)
let tuples = allCells.map { ($0.id, $0) }
return Dictionary(uniqueKeysWithValues: tuples)
}
func allSupplementaryViewKinds() -> Set<String> {
var allKinds = Set<String>()
self.sections.forEach {
if let header = $0.header {
allKinds.insert(header.registration.viewType.kind)
}
if let footer = $0.footer {
allKinds.insert(footer.registration.viewType.kind)
}
$0.supplementaryViews.forEach {
allKinds.insert($0.registration.viewType.kind)
}
}
return allKinds
}
}
// MARK: Collection, RandomAccessCollection
extension CollectionViewModel: Collection, RandomAccessCollection {
/// :nodoc:
public var count: Int {
self.sections.count
}
/// :nodoc:
public var isEmpty: Bool {
self.sections.isEmpty
}
/// :nodoc:
public var startIndex: Int {
self.sections.startIndex
}
/// :nodoc:
public var endIndex: Int {
self.sections.endIndex
}
/// :nodoc:
public subscript(position: Int) -> SectionViewModel {
self.sections[position]
}
/// :nodoc:
public func index(after pos: Int) -> Int {
self.sections.index(after: pos)
}
}
extension CollectionViewModel: CustomDebugStringConvertible {
/// :nodoc:
public var debugDescription: String {
collectionDebugDescription(self)
}
}