forked from LinkedInAttic/LayoutKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReloadableView.swift
More file actions
153 lines (126 loc) · 5.3 KB
/
ReloadableView.swift
File metadata and controls
153 lines (126 loc) · 5.3 KB
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
// Copyright 2016 LinkedIn Corp.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import UIKit
// MARK: - ReloadableView
/**
A view that can be reloaded with data.
UITableView and UICollectionView conform to this protocol.
*/
public protocol ReloadableView: class {
/// The bounds rectangle, which describes the view’s location and size in its own coordinate system.
var bounds: CGRect { get }
/// Returns whether the user has touched the content to initiate scrolling.
var isTracking: Bool { get }
/// Returns whether the content is moving in the scroll view after the user lifted their finger.
var isDecelerating: Bool { get }
/**
Reloads the data synchronously.
This means that it must be safe to immediately call other operations such as `insert`.
*/
func reloadDataSynchronously()
/// Registers views for the reuse identifier.
func registerViews(withReuseIdentifier reuseIdentifier: String)
/**
Performs a set of updates in a batch.
The reloadable view must follow the same semantics for handling the index paths
of concurrent inserts/updates/deletes as UICollectionView documents in `performBatchUpdates`.
*/
func perform(batchUpdates: BatchUpdates, completion: (() -> Void)?)
}
// MARK: - UICollectionView
/// Make UICollectionView conform to ReloadableView protocol.
extension UICollectionView: ReloadableView {
@objc
open func reloadDataSynchronously() {
reloadData()
// Force a layout so that it is safe to call insert after this.
layoutIfNeeded()
}
@objc
open func registerViews(withReuseIdentifier reuseIdentifier: String) {
register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: reuseIdentifier)
register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: reuseIdentifier)
}
@objc
open func perform(batchUpdates: BatchUpdates, completion: (() -> Void)?) {
performBatchUpdates({
if batchUpdates.insertItems.count > 0 {
self.insertItems(at: batchUpdates.insertItems)
}
if batchUpdates.deleteItems.count > 0 {
self.deleteItems(at: batchUpdates.deleteItems)
}
if batchUpdates.reloadItems.count > 0 {
self.reloadItems(at: batchUpdates.reloadItems)
}
for move in batchUpdates.moveItems {
self.moveItem(at: move.from, to: move.to)
}
if batchUpdates.insertSections.count > 0 {
self.insertSections(batchUpdates.insertSections)
}
if batchUpdates.deleteSections.count > 0 {
self.deleteSections(batchUpdates.deleteSections)
}
if batchUpdates.reloadSections.count > 0 {
self.reloadSections(batchUpdates.reloadSections)
}
for move in batchUpdates.moveSections {
self.moveSection(move.from, toSection: move.to)
}
}, completion: { _ in
completion?()
})
}
}
// MARK: - UITableView
/// Make UITableView conform to ReloadableView protocol.
extension UITableView: ReloadableView {
@objc
open func reloadDataSynchronously() {
reloadData()
}
@objc
open func registerViews(withReuseIdentifier reuseIdentifier: String) {
register(UITableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: reuseIdentifier)
}
@objc
open func perform(batchUpdates: BatchUpdates, completion: (() -> Void)?) {
beginUpdates()
// Update items.
if batchUpdates.insertItems.count > 0 {
insertRows(at: batchUpdates.insertItems, with: .automatic)
}
if batchUpdates.deleteItems.count > 0 {
deleteRows(at: batchUpdates.deleteItems, with: .automatic)
}
if batchUpdates.reloadItems.count > 0 {
reloadRows(at: batchUpdates.reloadItems, with: .automatic)
}
for move in batchUpdates.moveItems {
moveRow(at: move.from, to: move.to)
}
// Update sections.
if batchUpdates.insertSections.count > 0 {
insertSections(batchUpdates.insertSections, with: .automatic)
}
if batchUpdates.deleteSections.count > 0 {
deleteSections(batchUpdates.deleteSections, with: .automatic)
}
if batchUpdates.reloadSections.count > 0 {
reloadSections(batchUpdates.reloadSections, with: .automatic)
}
for move in batchUpdates.moveSections {
moveSection(move.from, toSection: move.to)
}
endUpdates()
completion?()
}
}