Skip to content

Commit 8d8fd79

Browse files
committed
Fixed updating snapshot logic.
1 parent 1175864 commit 8d8fd79

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
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.5'
4+
s.version = '1.3.6'
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: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ 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+
// Remove section if it deleted from content
88+
8989
let deletedSections = snapshot.sectionIdentifiers.filter({ (checkSection) -> Bool in
9090
return !sections.contains(where: { $0.identifier == checkSection.identifier })
9191
})
@@ -94,22 +94,7 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
9494
apply(snapshot, animated: true)
9595
}
9696

97-
// Reoder
98-
99-
for (sectionIndex, section) in sections.enumerated() {
100-
let previousSectionIndex = sectionIndex - 1
101-
guard (sections.count > previousSectionIndex) && (previousSectionIndex >= 0) else { continue }
102-
let previousSection = sections[previousSectionIndex]
103-
guard let _ = snapshot.sectionIdentifiers.first(where: { $0.identifier == section.identifier }) else { continue }
104-
guard let _ = snapshot.sectionIdentifiers.first(where: { $0.identifier == previousSection.identifier }) else { continue }
105-
snapshot.moveSection(section, afterSection: previousSection)
106-
}
107-
108-
// Apply Sections Changes
109-
110-
apply(snapshot, animated: true)
111-
112-
// Update current sections.
97+
// Add new sections and update current sections
11398

11499
for section in sections {
115100
var sectionSnapshot = SPDiffableSectionSnapshot()
@@ -127,6 +112,22 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
127112
sectionSnapshot.expand(sectionSnapshot.items)
128113
apply(sectionSnapshot, to: section, animatingDifferences: animated)
129114
}
115+
116+
// Reoder sections
117+
118+
for (sectionIndex, section) in sections.enumerated() {
119+
let previousSectionIndex = sectionIndex - 1
120+
guard (sections.count > previousSectionIndex) && (previousSectionIndex >= 0) else { continue }
121+
let previousSection = sections[previousSectionIndex]
122+
guard let _ = snapshot.sectionIdentifiers.first(where: { $0.identifier == section.identifier }) else { continue }
123+
guard let _ = snapshot.sectionIdentifiers.first(where: { $0.identifier == previousSection.identifier }) else { continue }
124+
snapshot.moveSection(section, afterSection: previousSection)
125+
}
126+
127+
// Apply changes
128+
129+
apply(snapshot, animated: true)
130+
130131
} else {
131132
var snapshot = SPDiffableSnapshot()
132133
snapshot.appendSections(sections)

0 commit comments

Comments
 (0)