-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCollectionViewModel.swift
183 lines (144 loc) · 5.23 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
//
// 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: Hashable, DiffableViewModel {
// MARK: DiffableViewModel
public var id: UniqueIdentifier
// MARK: Properties
public let sections: [SectionViewModel]
public var allRegistrations: Set<ViewRegistration> {
var all = Set<ViewRegistration>()
self.sections.forEach {
all.formUnion($0.allRegistrations)
}
return all
}
public var allCellsByIdentifier: [UniqueIdentifier: AnyCellViewModel] {
let allCells = self.flatMap { $0.cells }
let tuples = allCells.map { ($0.id, $0) }
return Dictionary(uniqueKeysWithValues: tuples)
}
public var allSectionsByIdentifier: [UniqueIdentifier: SectionViewModel] {
let tuples = self.sections.map { ($0.id, $0) }
return Dictionary(uniqueKeysWithValues: tuples)
}
// MARK: Init
public init(id: UniqueIdentifier = UUID(), sections: [SectionViewModel] = []) {
self.id = id
self.sections = sections.filter { $0.isNotEmpty }
}
// MARK: Accessing Sections
public func sectionViewModel(for id: UniqueIdentifier) -> SectionViewModel? {
self.allSectionsByIdentifier[id]
}
// MARK: Accessing Cells
public func cellViewModel(for id: UniqueIdentifier) -> AnyCellViewModel? {
self.allCellsByIdentifier[id]
}
public func cellViewModel(at indexPath: IndexPath) -> AnyCellViewModel {
precondition(indexPath.section < self.count)
let section = self[indexPath.section]
let cells = section.cells
precondition(indexPath.item < cells.count)
return cells[indexPath.item]
}
// MARK: Accessing Supplementary Views
public func supplementaryViewModel(ofKind kind: String, at indexPath: IndexPath) -> AnySupplementaryViewModel? {
precondition(indexPath.section < self.count)
let section = self[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: 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 i: Int) -> Int {
self.sections.index(after: i)
}
}
// TODO: implement CustomDebugStringConvertible for Section, AnyCell, AnySupplementary etc.
// If _viewModel conforms, use that implementation
extension CollectionViewModel: CustomDebugStringConvertible {
/// :nodoc:
public var debugDescription: String {
var text = "<CollectionViewModel:\n id: \(self.id)\n sections:\n"
for sectionIndex in 0..<self.count {
let section = self[sectionIndex]
text.append("\t[\(sectionIndex)]: \(section.id)\n")
text.append("\t isEmpty: \(section.isEmpty)\n")
if let header = section.header {
text.append("\t header: \(header.id) (\(header._kind))\n")
} else {
text.append("\t header: nil")
}
if let footer = section.footer {
text.append("\t footer: \(footer.id) (\(footer._kind))\n")
} else {
text.append("\t footer: nil")
}
text.append("\t cells: \n")
for cellIndex in 0..<section.cells.count {
let cell = section[cellIndex]
let cellId = String(describing: cell.id)
let reuseId = String(describing: cell.registration.reuseIdentifier)
text.append("\t\t[\(cellIndex)]: \(cellId) (\(reuseId)) \n")
}
text.append("\t supplementary views: \n")
for viewIndex in 0..<section.supplementaryViews.count {
let view = section.supplementaryViews[viewIndex]
let viewId = String(describing: view.id)
let kind = String(describing: view._kind)
text.append("\t\t[\(viewIndex)]: \(viewId) (\(kind)) \n")
}
}
text.append(" registrations: \n")
self.allRegistrations.forEach {
text.append("\t- \($0.reuseIdentifier)\n")
}
text.append(" isEmpty: \(self.isEmpty)\n")
text.append(">")
return text
}
}