Skip to content

Commit

Permalink
Merge pull request #157 from rechsteiner/fix-reload-data
Browse files Browse the repository at this point in the history
 Fix issues with reloading data
  • Loading branch information
rechsteiner authored Mar 26, 2018
2 parents 01bf555 + fa46fbb commit cf51304
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
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

0 comments on commit cf51304

Please sign in to comment.