Skip to content

Commit 5093418

Browse files
committed
Added logic for adding items.
1 parent 92457de commit 5093418

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

SPDiffable.podspec

Lines changed: 1 addition & 1 deletion
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.3.8'
4+
s.version = '1.4.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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,26 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
8282
public func apply(_ sections: [SPDiffableSection], animated: Bool) {
8383
if #available(iOS 14, *) {
8484

85-
// Remove section if it deleted from content.
86-
8785
var snapshot = self.snapshot()
8886

87+
// Delete
88+
8989
let deletedSections = snapshot.sectionIdentifiers.filter({ (checkSection) -> Bool in
9090
return !sections.contains(where: { $0.identifier == checkSection.identifier })
9191
})
9292
if !deletedSections.isEmpty {
9393
snapshot.deleteSections(deletedSections)
94-
apply(snapshot, animated: true)
94+
}
95+
96+
// Add
97+
98+
let addedSections = sections.filter { checkSection in
99+
return !snapshot.sectionIdentifiers.contains(where: {
100+
$0.identifier == checkSection.identifier
101+
})
102+
}
103+
if !addedSections.isEmpty {
104+
snapshot.appendSections(addedSections)
95105
}
96106

97107
// Reoder
@@ -105,11 +115,11 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
105115
snapshot.moveSection(section, afterSection: previousSection)
106116
}
107117

108-
// Apply Sections Changes
118+
// Apply Changes
109119

110120
apply(snapshot, animated: true)
111121

112-
// Update current sections.
122+
// Update items in Sections
113123

114124
for section in sections {
115125
var sectionSnapshot = SPDiffableSectionSnapshot()

0 commit comments

Comments
 (0)