UICollectionViewDiffableDataSource.swift 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //
  2. // UICollectionViewDiffableDataSource.swift
  3. // Mastodon
  4. //
  5. // Created by Cirno MainasuK on 2021-10-11.
  6. //
  7. import UIKit
  8. // ref: https://www.jessesquires.com/blog/2021/07/08/diffable-data-source-behavior-changes-and-reconfiguring-cells-in-ios-15/
  9. extension UICollectionViewDiffableDataSource {
  10. func reloadData(
  11. snapshot: NSDiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType>,
  12. completion: (() -> Void)? = nil
  13. ) {
  14. if #available(iOS 15.0, *) {
  15. self.applySnapshotUsingReloadData(snapshot, completion: completion)
  16. } else {
  17. self.apply(snapshot, animatingDifferences: false, completion: completion)
  18. }
  19. }
  20. func applySnapshot(
  21. _ snapshot: NSDiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType>,
  22. animated: Bool,
  23. completion: (() -> Void)? = nil) {
  24. if #available(iOS 15.0, *) {
  25. self.apply(snapshot, animatingDifferences: animated, completion: completion)
  26. } else {
  27. if animated {
  28. self.apply(snapshot, animatingDifferences: true, completion: completion)
  29. } else {
  30. UIView.performWithoutAnimation {
  31. self.apply(snapshot, animatingDifferences: true, completion: completion)
  32. }
  33. }
  34. }
  35. }
  36. }