Skip to content
This repository has been archived by the owner on Jun 17, 2023. It is now read-only.

Commit

Permalink
Compare data using Equatable if it conforms to
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir Khorsandi committed Nov 7, 2021
1 parent 4ff0fd8 commit eddc2f6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
21 changes: 19 additions & 2 deletions Lib/SwiftUI/PagingCollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,16 @@ public class PagingCollectionViewController<ValueType: Identifiable, PageContent
// MARK: Internal functions

func update(list: [ValueType], currentIndex: Int?) {
let oldIds = self.list.map(\.id)
var needsUpdate = false

if let self = self as? PagingCollectionViewControllerEquatableList {
needsUpdate = !self.isListSame(as: list)
} else {
let oldIds = self.list.map(\.id)
needsUpdate = list.map(\.id) != oldIds
}
self.list = list
if list.map(\.id) != oldIds {
if needsUpdate {
collectionView?.reloadData()
layout.invalidateLayoutInBatchUpdate(invalidateOffset: true)
}
Expand Down Expand Up @@ -142,6 +149,16 @@ public class PagingCollectionViewController<ValueType: Identifiable, PageContent

}

private protocol PagingCollectionViewControllerEquatableList {
func isListSame<T>(as list: [T]) -> Bool
}

@available(iOS 13.0, *)
extension PagingCollectionViewController: PagingCollectionViewControllerEquatableList where ValueType: Equatable {
func isListSame<T>(as list: [T]) -> Bool {
self.list == (list as? [ValueType])
}
}

@available(iOS 13.0, *)
private extension UICollectionView {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1220"
LastUpgradeVersion = "1310"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down

0 comments on commit eddc2f6

Please sign in to comment.