Last active
January 4, 2019 04:35
-
-
Save FranDepascuali/c8c12610a18905e2149dbf76fda44d65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public protocol PaginationServiceType { | |
associatedtype Element | |
init(elementProvider: (page: Int, numberOfElements: Int) -> SignalProducer<[Element], NoError>) | |
func fetchNextPage(numberOfElements: Int) -> SignalProducer<[Element], NoError> | |
} | |
public final class PaginationService<ElementType>: PaginationServiceType { | |
public typealias ElementProvider = (page: Int, numberOfElements: Int) -> SignalProducer<[ElementType], NoError> | |
private let _elementProvider: ElementProvider | |
private let _currentPage = MutableProperty(0) | |
public init(elementProvider: ElementProvider) { | |
_elementProvider = elementProvider | |
} | |
public func fetchNextPage(numberOfElements: Int) -> SignalProducer<[ElementType], NoError> { | |
let updateCurrentPage: [ElementType] -> () = { [unowned self] _ in | |
self._currentPage.value = self._currentPage.value + 1 | |
} | |
return _elementProvider(page: _currentPage.value, numberOfElements: numberOfElements) | |
.on(next: updateCurrentPage) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
extension ViewController: UITableViewDelegate { | |
public func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { | |
let isLastElement: Bool = { indexPath.row == tableView.numberOfRowsInSection(0) - 1 }() | |
if isLastElement { | |
_viewModel.fetchNewPage().startWithNext { [unowned self] _ in | |
self._view.debtsTableView.reloadData() | |
} | |
} | |
} | |
} | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class DebtsViewModel { | |
private let _cellViewModels: MutableProperty<[CellViewModel]> = MutableProperty([]) | |
private let _paginationService: PaginationService<CellModel> | |
public init(paginationService: PaginationService<CellModel>) { | |
_paginationService = paginationService | |
} | |
public func fetchNewPage() -> SignalProducer<(), NoError> { | |
let appendNewValues: [CellModel] -> () = { [unowned self] in self._cellViewModels.value.appendContentsOf($0.map(CellViewModel.init)) } | |
return _paginationService | |
.fetchNextPage(10) // Replace with number of elements | |
.on(next: appendNewValues) | |
.map { _ in () } | |
} | |
public subscript(position: Int) -> DebtViewModel { | |
return _cellViewModels.value[position] | |
} | |
public var numberOfItems: Int { | |
return _cellViewModels.value.count | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment