https://github.com/tryswift/RxPagination
The demo project for "Protocol-Oriented Programming in Networking".
https://github.com/tryswift/RxPagination
Last synced: 7 months ago
JSON representation
The demo project for "Protocol-Oriented Programming in Networking".
- Host: GitHub
- URL: https://github.com/tryswift/RxPagination
- Owner: tryswift
- License: mit
- Created: 2016-03-12T06:07:32.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-12T16:39:58.000Z (about 8 years ago)
- Last Synced: 2024-11-14T09:39:00.539Z (about 1 year ago)
- Language: Swift
- Size: 158 KB
- Stars: 363
- Watchers: 17
- Forks: 24
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-rxswift - RxPagination - Oriented Programming in Networking". (Libraries)
README
# RxPagination
This is the demo project for my presentation at try! Swift conference 2016.
- Slides: https://speakerdeck.com/ishkawa/protocol-oriented-programming-in-networking
- Video: https://news.realm.io/news/tryswift-yosuke-ishikawa-protocol-oriented-networking/
## Set Up
- `carthage bootstrap --platform iOS`
## Requirements
- Swift 3.0.1
- Xcode 8.1
## Summary
This demo project illustrates how to use RxSwift, Action and APIKit. The demo app fetches repositories via GitHub search API and displays them using the libraries.

### ViewModel
`PaginationViewModel` is a view model for pagination. It has an initializer with type parameter `Request`, which is constrained to conform to `PaginationRequest` protocol. When `PaginationViewModel` is instantiated via `init(baseRequest:)`, the type of its property that represents pagination elements will be inferred as `Observable<[Request.Response.Element]>`.
```swift
class PaginationViewModel {
let indicatorViewAnimating: Driver
let elements: Driver<[Element]>
let loadError: Driver
init(
baseRequest: Request,
viewWillAppear: Driver,
scrollViewDidReachBottom: Driver) where Request.Response.Element == Element {...}
}
```
### ViewController
Once ViewModel is instantiated with a `Request` type parameter, remained task that ViewController have to do is binding input streams and output streams.
```swift
class SearchRepositoriesViewController: UITableViewController {
@IBOutlet weak var indicatorView: UIActivityIndicatorView!
private let disposeBag = DisposeBag()
private var viewModel: PaginationViewModel!
override func viewDidLoad() {
super.viewDidLoad()
let baseRequest = GitHubAPI.SearchRepositoriesRequest(query: "Swift")
viewModel = PaginationViewModel(
baseRequest: baseRequest,
viewWillAppear: rx.viewWillAppear.asDriver(),
scrollViewDidReachBottom: tableView.rx.reachedBottom.asDriver())
disposeBag.insert([
viewModel.indicatorViewAnimating.drive(indicatorView.rx.isAnimating),
viewModel.elements.drive(tableView.rx.items(cellIdentifier: "Cell", cellType: RepositoryCell.self)),
viewModel.loadError.drive(onNext: { print($0) }),
])
}
}
```
## Contact
Twitter: https://twitter.com/_ishkawa