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

Commit eddc2f6

Browse files
author
Amir Khorsandi
committed
Compare data using Equatable if it conforms to
1 parent 4ff0fd8 commit eddc2f6

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Lib/SwiftUI/PagingCollectionViewController.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,16 @@ public class PagingCollectionViewController<ValueType: Identifiable, PageContent
5757
// MARK: Internal functions
5858

5959
func update(list: [ValueType], currentIndex: Int?) {
60-
let oldIds = self.list.map(\.id)
60+
var needsUpdate = false
61+
62+
if let self = self as? PagingCollectionViewControllerEquatableList {
63+
needsUpdate = !self.isListSame(as: list)
64+
} else {
65+
let oldIds = self.list.map(\.id)
66+
needsUpdate = list.map(\.id) != oldIds
67+
}
6168
self.list = list
62-
if list.map(\.id) != oldIds {
69+
if needsUpdate {
6370
collectionView?.reloadData()
6471
layout.invalidateLayoutInBatchUpdate(invalidateOffset: true)
6572
}
@@ -142,6 +149,16 @@ public class PagingCollectionViewController<ValueType: Identifiable, PageContent
142149

143150
}
144151

152+
private protocol PagingCollectionViewControllerEquatableList {
153+
func isListSame<T>(as list: [T]) -> Bool
154+
}
155+
156+
@available(iOS 13.0, *)
157+
extension PagingCollectionViewController: PagingCollectionViewControllerEquatableList where ValueType: Equatable {
158+
func isListSame<T>(as list: [T]) -> Bool {
159+
self.list == (list as? [ValueType])
160+
}
161+
}
145162

146163
@available(iOS 13.0, *)
147164
private extension UICollectionView {

Samples/PagingLayoutSamples.xcodeproj/xcshareddata/xcschemes/PagingLayoutSamples.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1220"
3+
LastUpgradeVersion = "1310"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

0 commit comments

Comments
 (0)