Skip to content

Commit 9f37a0d

Browse files
authored
Merge pull request #210 from surfstudio/feature/paginatable_collection_with_error
Feature/paginatable collection with error
2 parents f6fd2ff + c77fa90 commit 9f37a0d

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

Example/ReactiveDataDisplayManager/Collection/PaginatableCollectionViewController/PaginatableCollectionViewController.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ final class PaginatableCollectionViewController: UIViewController {
3333

3434
private weak var paginatableInput: PaginatableInput?
3535

36+
private var isFirstPageLoading = true
3637
private var currentPage = 0
3738

3839
// MARK: - UIViewController
@@ -102,6 +103,15 @@ private extension PaginatableCollectionViewController {
102103
return TitleCollectionViewCell.rddm.baseGenerator(with: title)
103104
}
104105

106+
func canFillNext() -> Bool {
107+
if isFirstPageLoading {
108+
isFirstPageLoading.toggle()
109+
return false
110+
} else {
111+
return true
112+
}
113+
}
114+
105115
func fillNext() -> Bool {
106116
currentPage += 1
107117

@@ -136,10 +146,16 @@ extension PaginatableCollectionViewController: PaginatableOutput {
136146
input.updateProgress(isLoading: true)
137147

138148
delay(.now() + .seconds(3)) { [weak self, weak input] in
139-
let canIterate = self?.fillNext() ?? false
140-
141-
input?.updateProgress(isLoading: false)
142-
input?.updatePagination(canIterate: canIterate)
149+
let canFillNext = self?.canFillNext() ?? false
150+
if canFillNext {
151+
let canIterate = self?.fillNext() ?? false
152+
153+
input?.updateProgress(isLoading: false)
154+
input?.updatePagination(canIterate: canIterate)
155+
} else {
156+
input?.updateProgress(isLoading: false)
157+
input?.updateError(SampleError.sample)
158+
}
143159
}
144160
}
145161

Example/ReactiveDataDisplayManagerExampleUITests/Plugins/PaginatablePluginExampleUITest.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class PaginatablePluginExampleUITest: BaseUITestCase {
1515

1616
// Description: The first cell of the table is always the same
1717
func testTable_whenSwipeUp_thenCellsCountChanged() throws {
18-
let pageSize = 17
18+
let pageSize = 16
1919

2020
setTab("Table")
2121
tapTableElement("Table with pagination")
@@ -27,13 +27,13 @@ final class PaginatablePluginExampleUITest: BaseUITestCase {
2727
XCTAssertTrue(retryButton.waitForExistence(timeout: Constants.timeout * 2))
2828

2929
while !retryButton.isHittable {
30-
table.swipeUp()
30+
table.swipeUp(velocity: .fast)
3131
}
3232

3333
retryButton.tap()
3434

3535
while table.cells.count <= pageSize {
36-
table.swipeUp()
36+
table.swipeUp(velocity: .fast)
3737
}
3838

3939
XCTAssertGreaterThan(table.cells.count, pageSize)
@@ -45,14 +45,21 @@ final class PaginatablePluginExampleUITest: BaseUITestCase {
4545
tapTableElement("Collection with pagination")
4646

4747
let collection = app.collectionViews.firstMatch
48+
let firstCell = app.collectionViews.cells.firstMatch
49+
let retryButton = app.buttons["Retry"]
4850
XCTAssertTrue(collection.waitForExistence(timeout: Constants.timeout))
4951

50-
while collection.cells.firstMatch.label.contains("page 0") {
51-
collection.swipeUp()
52+
while !retryButton.isHittable {
53+
collection.swipeUp(velocity: .fast)
54+
}
55+
56+
retryButton.tap()
57+
58+
while firstCell.label.hasSuffix("page 0") {
59+
collection.swipeUp(velocity: .fast)
5260
}
5361

54-
let cell = collection.cells.firstMatch
55-
XCTAssertTrue(cell.label.contains("page 1"))
62+
XCTAssertTrue(firstCell.label.hasSuffix("page 1"))
5663
}
5764

5865
func testTable_whenSwipeUp_thenPaginatorErrorAppear_thenHittableActivityIndicator() {
@@ -68,7 +75,7 @@ final class PaginatablePluginExampleUITest: BaseUITestCase {
6875
XCTAssertTrue(retryButton.waitForExistence(timeout: Constants.timeout * 2))
6976

7077
while !retryButton.isHittable {
71-
table.swipeUp()
78+
table.swipeUp(velocity: .fast)
7279
}
7380

7481
XCTAssertFalse(activityIndicator.isHittable)
@@ -80,17 +87,22 @@ final class PaginatablePluginExampleUITest: BaseUITestCase {
8087

8188
func testCollection_whenSwipeUp_thenHittableActivityIndicator() {
8289
let activityIndicator = app.activityIndicators["PaginatorView"]
90+
let retryButton = app.buttons["Retry"]
8391

8492
setTab("Collection")
8593
tapTableElement("Collection with pagination")
8694

8795
let collection = app.collectionViews.firstMatch
8896
XCTAssertTrue(collection.waitForExistence(timeout: Constants.timeout))
8997

90-
while !activityIndicator.isHittable {
91-
collection.swipeUp()
98+
while !retryButton.isHittable {
99+
collection.swipeUp(velocity: .fast)
92100
}
93101

102+
XCTAssertFalse(activityIndicator.isHittable)
103+
104+
retryButton.tap()
105+
94106
XCTAssertTrue(activityIndicator.isHittable)
95107
}
96108

Source/Collection/Plugins/PluginAction/CollectionPaginatablePlugin.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class CollectionPaginatablePlugin: BaseCollectionPlugin<CollectionEvent>
2525
private let progressView: ProgressView
2626
private weak var output: PaginatableOutput?
2727

28+
private var isLoading: Bool = false
29+
2830
private weak var collectionView: UICollectionView?
2931

3032
/// Property which indicating availability of pages
@@ -63,6 +65,12 @@ public class CollectionPaginatablePlugin: BaseCollectionPlugin<CollectionEvent>
6365
collectionView = manager?.view
6466
canIterate = false
6567
output?.onPaginationInitialized(with: self)
68+
self.progressView.setOnRetry { [weak self] in
69+
guard let input = self, let output = self?.output else {
70+
return
71+
}
72+
output.loadNextPage(with: input)
73+
}
6674
}
6775

6876
public override func process(event: CollectionEvent, with manager: BaseCollectionManager?) {
@@ -76,7 +84,7 @@ public class CollectionPaginatablePlugin: BaseCollectionPlugin<CollectionEvent>
7684
let lastCellInLastSectionIndex = generators[lastSectionIndex].count - 1
7785

7886
let lastCellIndexPath = IndexPath(row: lastCellInLastSectionIndex, section: lastSectionIndex)
79-
guard indexPath == lastCellIndexPath, canIterate else {
87+
guard indexPath == lastCellIndexPath, canIterate, !isLoading else {
8088
return
8189
}
8290

@@ -98,6 +106,7 @@ public class CollectionPaginatablePlugin: BaseCollectionPlugin<CollectionEvent>
98106
extension CollectionPaginatablePlugin: PaginatableInput {
99107

100108
public func updateProgress(isLoading: Bool) {
109+
self.isLoading = isLoading
101110
progressView.showProgress(isLoading)
102111
}
103112

0 commit comments

Comments
 (0)