-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNotifier.swift
235 lines (209 loc) · 8.88 KB
/
Notifier.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
//
// Notifier.swift
// FastAdapter
//
// Created by Fabian Terhorst on 23.07.18.
// Copyright © 2018 everHome. All rights reserved.
//
open class Notifier<Itm: Item> {
weak var fastAdapter: FastAdapter<Itm>?
public init() {
}
open func reloadData(listView: ListView) {
listView.reloadData()
}
open func reloadSection(section: Int) {
fastAdapter?.listView?.reloadSections(IndexSet(integer: section), with: .automatic)
}
open func reloadItems(at indexPaths: [IndexPath]) {
fastAdapter?.listView?.reloadItems(at: indexPaths, with: .automatic)
}
open func insertItems(at indexPaths: [IndexPath]) {
fastAdapter?.listView?.insertItems(at: indexPaths, with: .automatic)
}
open func insertSections(_ sections: IndexSet) {
fastAdapter?.listView?.insertSections(sections, with: .automatic)
}
open func insert(_ listView: ListView, _ itemList: ItemList<Itm>, section: Section<Itm>, at sectionIndex: Int) {
itemList.sections.insert(Section<Itm>(header: nil, items: [Itm](), footer: nil), at: sectionIndex)
listView.insertSections(IndexSet(integer: sectionIndex), with: .automatic)
}
open func delete(_ listView: ListView, _ itemList: ItemList<Itm>, sectionIndex: Int) {
if itemList.sections.count <= sectionIndex {
return
}
itemList.sections.remove(at: sectionIndex)
listView.deleteSections(IndexSet(integer: sectionIndex), with: .automatic)
}
open func insert(_ listView: ListView, _ itemList: ItemList<Itm>, items: [Itm], at index: Int, in section: Int) {
_insert(listView, itemList, items: items, at: index, in: section, with: .automatic)
}
private func _insert(_ listView: ListView, _ itemList: ItemList<Itm>, items: [Itm], at index: Int, in section: Int, with animation: ListViewItemAnimation) {
// Check if list is to short
if itemList.sections.count <= section {
return
}
if itemList[section].items.count <= index - 1 {
return
}
var indexPaths = [IndexPath]()
itemList[section].items.insert(contentsOf: items, at: index)
for i in index..<index + items.count {
indexPaths.append(IndexPath(row: i, section: section))
}
listView.insertItems(at: indexPaths, with: animation)
}
open func delete(_ listView: ListView, _ itemList: ItemList<Itm>, count: Int, at index: Int, in section: Int) {
_delete(listView, itemList, count: count, at: index, in: section, with: .automatic)
}
private func _delete(_ listView: ListView, _ itemList: ItemList<Itm>, count: Int, at index: Int, in section: Int, with animation: ListViewItemAnimation) {
if itemList.sections.count <= section {
return
}
if itemList[section].items.count <= index + count - 1 {
return
}
var indexPaths = [IndexPath]()
for i in index..<index + count {
indexPaths.append(IndexPath(row: i, section: section))
}
itemList[section].items.removeSubrange(index..<index + count)
listView.deleteItems(at: indexPaths, with: animation)
}
open func update(_ listView: ListView, _ itemList: ItemList<Itm>, at index: Int, in section: Int) {
if itemList.sections.count <= section {
return
}
if itemList[section].items.count <= index {
return
}
listView.reloadItems(at: [IndexPath(row: index, section: section)], with: .automatic)
}
open func updateAll(_ listView: ListView, _ itemList: ItemList<Itm>, in section: Int) {
var indexPaths = [IndexPath]()
for i in 0..<itemList[section].items.count {
indexPaths.append(IndexPath(row: i, section: section))
}
listView.reloadItems(at: indexPaths, with: .automatic)
}
open func clear(_ listView: ListView, _ itemList: ItemList<Itm>, section: Int) {
if itemList.sections.count <= section {
return
}
let itemListSection = itemList[section]
itemListSection.header = nil
let count = itemListSection.items.count
itemListSection.items.removeAll()
itemListSection.footer = nil
var indexPaths = [IndexPath]()
for i in 0..<count {
indexPaths.append(IndexPath(row: i, section: section))
}
listView.deleteItems(at: indexPaths, with: .automatic)
// For header and footer clear we need to reload the complete section
listView.reloadSections(IndexSet(integer: section), with: .automatic)
}
open func set(_ listView: ListView?, _ itemList: ItemList<Itm>, items: [Itm], in section: Int) {
if itemList.sections.count <= section {
return
}
itemList[section].items = items
listView?.reloadSections(IndexSet(integer: section), with: .automatic)
}
open func set(_ listView: ListView, _ itemList: ItemList<Itm>, item: Itm, at index: Int, in section: Int) {
if itemList.sections.count <= section {
return
}
if itemList[section].items.count <= index {
itemList[section].items.append(item)
listView.reloadItems(at: [IndexPath(row: itemList[section].items.count - 1, section: section)], with: .automatic)
} else {
itemList[section].items[index] = item
listView.reloadItems(at: [IndexPath(row: index, section: section)], with: .automatic)
}
}
open func expand(_ listView: ListView, _ itemList: ItemList<Itm>, items: [Itm], at index: Int, in section: Int) {
if itemList.sections.count <= section {
return
}
let itemsInSection = itemList[section].items
if itemsInSection.count <= index {
return
}
let item = itemsInSection[index]
guard var expandable = item as? Expandable, !expandable.expanded else {
return
}
expandable.expanded = true
var measuredSubItemsCount = 0
if let subItems = expandable.subItems {
for subItem in subItems {
guard subItem is Itm else {
continue
}
measuredSubItemsCount += 1
}
}
if measuredSubItemsCount == 0 {
return
}
// When list got unexpected shorter then the item index to expand stop expanding
if itemList.sections[section].items.count <= index {
return
}
if itemList.subItemCount[section] == nil {
itemList.subItemCount[section] = [Int : Int]()
} else {
// Count all keys up that higher then the current index
if let keys = itemList.subItemCount[section]?.keys {
for key in keys {
if key > index {
if let entry = itemList.subItemCount[section]?.removeValue(forKey: key) {
itemList.subItemCount[section]?[key + measuredSubItemsCount] = entry
}
}
}
}
}
itemList.subItemCount[section]?[index] = measuredSubItemsCount
_insert(listView, itemList, items: items, at: index + 1, in: section, with: .top)
}
open func collapse(_ listView: ListView, _ itemList: ItemList<Itm>, at index: Int, in section: Int) {
if itemList.sections.count <= section {
return
}
if itemList.subItemCount[section]?.count ?? 0 <= section {
return
}
guard var count = itemList.subItemCount[section]?[index], count > 0 else {
return
}
let item = itemList[section].items[index]
guard var expandable = item as? Expandable, expandable.expanded else {
return
}
expandable.expanded = false
// Checks if an sub item is also expanded
for i in index + 1...index + count {
if let subItemCount = itemList.subItemCount[section]?[i] {
count += subItemCount
}
}
// Prevent the _delete to return without correcting the subItemCount
if itemList[section].items.count <= index + count {
return
}
// Count all keys down that are higher then the current index
if let keys = itemList.subItemCount[section]?.keys {
for key in keys {
if key > index {
if let entry = itemList.subItemCount[section]?.removeValue(forKey: key) {
itemList.subItemCount[section]?[key - count] = entry
}
}
}
}
itemList.subItemCount[section]?.removeValue(forKey: index)
_delete(listView, itemList, count: count, at: index + 1, in: section, with: .top)
}
}