Skip to content

Commit 3d62377

Browse files
committed
Added reload methods to diffable data source.
1 parent f27e22b commit 3d62377

File tree

5 files changed

+88
-9
lines changed

5 files changed

+88
-9
lines changed

Readme.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,22 @@ var content: [SPDiffableSection] {
137137
You can add more items or sections. Last step - apply:
138138

139139
```swift
140-
diffableDataSource?.apply(sections: content, animating: true)
140+
diffableDataSource?.apply(content, animating: true)
141141
```
142142

143143
Call this when you need update content. When you call `setCellProviders`, it set content by default without animation.
144144
That all. You can each time create new order or count cells and it automatically show with diffable animation.
145145

146+
#### Reload Content
147+
148+
If you need something like old function `.reloadData()` in collection and table, look at this method:
149+
150+
```swift
151+
diffableDataSource?.reload(content)
152+
```
153+
154+
Changes apply without animation and like deep reload.
155+
146156
### Mediator
147157

148158
Some methods in diffable data source can't ovveride without custom data source. It solved with mediator delegate. It simple. Next example for table. Set delegate `SPTableDiffableMediator`, all method optional:

SPDiffable.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = 'SPDiffable'
4-
s.version = '1.5.1'
4+
s.version = '1.6.0'
55
s.summary = 'Extenshion of Diffable API which allow not duplicate code and use less models.'
66
s.homepage = 'https://github.com/ivanvorobei/SPDiffable'
77
s.source = { :git => 'https://github.com/ivanvorobei/SPDiffable.git', :tag => s.version }

Sources/SPDiffable/DataSource/SPDiffableCollectionDataSource.swift

+35-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
105105
}
106106

107107
// Reoder
108-
108+
109109
for (sectionIndex, section) in sections.enumerated() {
110110
let previousSectionIndex = sectionIndex - 1
111111
guard (sections.count > previousSectionIndex) && (previousSectionIndex >= 0) else { continue }
@@ -129,7 +129,7 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
129129
apply(snapshot, animated: true)
130130

131131
// Update items in Sections
132-
132+
133133
for section in sections {
134134
var sectionSnapshot = SPDiffableSectionSnapshot()
135135

@@ -166,6 +166,39 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
166166
apply(snapshot, animatingDifferences: animated, completion: nil)
167167
}
168168

169+
// MARK: - Reload Content
170+
171+
/**
172+
SPDiffable: Reload current snapshot with new snapshot.
173+
174+
Deep reload like reload data in old way collection view.
175+
176+
- parameter sections: Array of `SPDiffableSection`, it content of table.
177+
*/
178+
public func reload(_ sections: [SPDiffableSection]) {
179+
var snapshot = SPDiffableSnapshot()
180+
snapshot.appendSections(sections)
181+
for section in sections {
182+
snapshot.appendItems(section.items, toSection: section)
183+
}
184+
reload(snapshot)
185+
}
186+
187+
/**
188+
SPDiffable: Reload current snapshot with new snapshot.
189+
190+
Deep reload like reload data in old way collection view.
191+
192+
- parameter snapshot: New snapshot.
193+
*/
194+
public func reload(_ snapshot: SPDiffableSnapshot) {
195+
if #available(iOS 15.0, *) {
196+
applySnapshotUsingReloadData(snapshot, completion: nil)
197+
} else {
198+
apply(snapshot, animatingDifferences: false, completion: nil)
199+
}
200+
}
201+
169202
// MARK: - Get Content
170203

171204
/**

Sources/SPDiffable/DataSource/SPDiffableTableDataSource.swift

+41-5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ open class SPDiffableTableDataSource: UITableViewDiffableDataSource<SPDiffableSe
6666
- parameter animating: Shoud apply changes with animation or not.
6767
*/
6868
public func apply(_ sections: [SPDiffableSection], animated: Bool) {
69-
var snapshot = SPDiffableSnapshot()
70-
snapshot.appendSections(sections)
71-
for section in sections {
72-
snapshot.appendItems(section.items, toSection: section)
73-
}
69+
let snapshot = convertToSnapshot(sections)
7470
apply(snapshot, animatingDifferences: animated)
7571
}
7672

@@ -84,6 +80,35 @@ open class SPDiffableTableDataSource: UITableViewDiffableDataSource<SPDiffableSe
8480
apply(snapshot, animatingDifferences: animated, completion: nil)
8581
}
8682

83+
// MARK: - Reload Content
84+
85+
/**
86+
SPDiffable: Reload current snapshot with new snapshot.
87+
88+
Deep reload like reload data in old way table view.
89+
90+
- parameter sections: Array of `SPDiffableSection`, it content of table.
91+
*/
92+
public func reload(_ sections: [SPDiffableSection]) {
93+
let snapshot = convertToSnapshot(sections)
94+
reload(snapshot)
95+
}
96+
97+
/**
98+
SPDiffable: Reload current snapshot with new snapshot.
99+
100+
Deep reload like reload data in old way table view.
101+
102+
- parameter snapshot: New snapshot.
103+
*/
104+
public func reload(_ snapshot: SPDiffableSnapshot) {
105+
if #available(iOS 15.0, *) {
106+
applySnapshotUsingReloadData(snapshot, completion: nil)
107+
} else {
108+
apply(snapshot, animatingDifferences: false, completion: nil)
109+
}
110+
}
111+
87112
// MARK: - Get Content
88113

89114
/**
@@ -125,6 +150,17 @@ open class SPDiffableTableDataSource: UITableViewDiffableDataSource<SPDiffableSe
125150
return cell
126151
}
127152

153+
// MARK: - Helpers
154+
155+
public func convertToSnapshot(_ sections: [SPDiffableSection]) -> SPDiffableSnapshot {
156+
var snapshot = SPDiffableSnapshot()
157+
snapshot.appendSections(sections)
158+
for section in sections {
159+
snapshot.appendItems(section.items, toSection: section)
160+
}
161+
return snapshot
162+
}
163+
128164
// MARK: - Mediator
129165

130166
public override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

0 commit comments

Comments
 (0)