Skip to content

Commit

Permalink
Fix issue with reloading data
Browse files Browse the repository at this point in the history
When using PagingViewControllerDataSource, the reloadData(around:)
method would not update the menu items, as it was caching the old
items in the interal IndexedDataSource instance.
  • Loading branch information
rechsteiner committed Mar 25, 2018
1 parent 593ec18 commit 07641e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
6 changes: 1 addition & 5 deletions Parchment/Classes/IndexedPagingDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ import Foundation
class IndexedPagingDataSource<T: PagingItem>:
PagingViewControllerInfiniteDataSource where T: Hashable & Comparable {

let items: [T]
var items: [T] = []
var viewControllerForIndex: ((Int) -> UIViewController?)?

init(items: [T]) {
self.items = items
}

func pagingViewController<U>(
_ pagingViewController: PagingViewController<U>,
viewControllerForPagingItem item: U) -> UIViewController {
Expand Down
26 changes: 13 additions & 13 deletions Parchment/Classes/PagingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,8 @@ open class PagingViewController<T: PagingItem>:
/// - Parameter pagingItem: The `PagingItem` that will be selected
/// after the data reloads.
open func reloadData(around pagingItem: T) {
stateMachine.fire(.select(
pagingItem: pagingItem,
direction: .none,
animated: false))
reloadItems(around: pagingItem)
indexedDataSource?.items = generateItemsForIndexedDataSource()
select(pagingItem: pagingItem, animated: false)
}

/// Selects a given paging item. This need to be called after you
Expand Down Expand Up @@ -521,21 +518,24 @@ open class PagingViewController<T: PagingItem>:
}
}
}
private func configureDataSource() {

private func generateItemsForIndexedDataSource() -> [T] {
let numberOfItems = dataSource?.numberOfViewControllers(in: self) ?? 0
let items = (0..<numberOfItems).enumerated().flatMap {
return (0..<numberOfItems).enumerated().flatMap {
dataSource?.pagingViewController(self, pagingItemForIndex: $0.offset)
}

indexedDataSource = IndexedPagingDataSource(items: items)
}

private func configureDataSource() {
indexedDataSource = IndexedPagingDataSource()
indexedDataSource?.items = generateItemsForIndexedDataSource()
indexedDataSource?.viewControllerForIndex = { [unowned self] in
return self.dataSource?.pagingViewController(self, viewControllerForIndex: $0)
}

infiniteDataSource = indexedDataSource

if let firstItem = items.first {
if let firstItem = indexedDataSource?.items.first {
select(pagingItem: firstItem)
}
}
Expand Down

0 comments on commit 07641e2

Please sign in to comment.