Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with reloading data #157

Merged
merged 3 commits into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Parchment/Classes/EMPageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ open class EMPageViewController: UIViewController, UIScrollViewDelegate {
self.removeChildIfNeeded(beforeViewController)
self.removeChildIfNeeded(selectedViewController)
self.removeChildIfNeeded(afterViewController)

beforeViewController = nil
selectedViewController = nil
afterViewController = nil
}

/**
Expand Down
26 changes: 23 additions & 3 deletions Parchment/Classes/PagingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ open class PagingViewController<T: PagingItem>:
indexedDataSource?.items = items

if let pagingItem = items.first(where: { $0 == previouslySelected }) {
select(pagingItem: pagingItem, animated: false)
resetItems(around: pagingItem)
} else if let firstItem = items.first {
select(pagingItem: firstItem, animated: false)
resetItems(around: firstItem)
} else {
stateMachine.fire(.removeAll)
}
Expand All @@ -344,7 +344,7 @@ open class PagingViewController<T: PagingItem>:
/// after the data reloads.
open func reloadData(around pagingItem: T) {
indexedDataSource?.items = generateItemsForIndexedDataSource()
select(pagingItem: pagingItem, animated: false)
resetItems(around: pagingItem)
}

/// Selects a given paging item. This need to be called after you
Expand Down Expand Up @@ -717,6 +717,26 @@ open class PagingViewController<T: PagingItem>:
return items
}

private func resetItems(around pagingItem: T) {
let toItems = generateItems(around: pagingItem)
let sortedItems = Array(toItems).sorted()

visibleItems = PagingItems(
items: sortedItems,
hasItemsBefore: hasItemBefore(pagingItem: sortedItems.first),
hasItemsAfter: hasItemAfter(pagingItem: sortedItems.last))
collectionViewLayout.visibleItems = visibleItems

stateMachine.fire(.select(pagingItem: pagingItem, direction: .none, animated: false))
collectionView.reloadData()
selectViewController(pagingItem, direction: .none, animated: false)

// Reloading the data triggers the didFinishScrollingFrom delegate
// to be called which in turn means the wrong item will be selected.
// For now, we just fix this by selecting the correct item manually.
stateMachine.fire(.select(pagingItem: pagingItem, direction: .none, animated: false))
}

private func removeAll() {
visibleItems = PagingItems(items: [])
collectionViewLayout.visibleItems = visibleItems
Expand Down
2 changes: 1 addition & 1 deletion ParchmentTests/PagingViewControllerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class PagingViewControllerSpec: QuickSpec {
expect(viewController.state).to(equal(PagingState.selected(pagingItem: third)))
}

fit("display an empty view after reloading data with no items") {
it("display an empty view after reloading data with no items") {
dataSource.items = []
viewController.reloadData()

Expand Down