Skip to content

Commit 5138bac

Browse files
committed
Fix sidebar deleted sections
1 parent 4e426d4 commit 5138bac

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

Sources/SPDiffable/DataSource/SPDiffableCollectionDataSource.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,21 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
5454
- parameter animating: Shoud apply changes with animation or not.
5555
*/
5656
public func apply(_ sections: [SPDiffableSection], animated: Bool) {
57-
var snapshot = SPDiffableSnapshot()
58-
snapshot.appendSections(sections)
5957
if #available(iOS 14, *) {
58+
59+
// Remove section if it deleted from content.
60+
61+
var snapshot = self.snapshot()
62+
let deletedSections = snapshot.sectionIdentifiers.filter({ (checkSection) -> Bool in
63+
return !sections.contains(where: { $0.identifier == checkSection.identifier })
64+
})
65+
if !deletedSections.isEmpty {
66+
snapshot.deleteSections(deletedSections)
67+
apply(snapshot, animated: true)
68+
}
69+
70+
// Update current sections.
71+
6072
for section in sections {
6173
var sectionSnapshot = SPDiffableSectionSnapshot()
6274
let header = section.header
@@ -68,6 +80,8 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
6880
apply(sectionSnapshot, to: section, animatingDifferences: animated)
6981
}
7082
} else {
83+
var snapshot = SPDiffableSnapshot()
84+
snapshot.appendSections(sections)
7185
for section in sections {
7286
snapshot.appendItems(section.items, toSection: section)
7387
}

0 commit comments

Comments
 (0)