Skip to content

Commit

Permalink
Even more awesome code
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalwojcik committed Dec 14, 2016
1 parent 80fac50 commit ad1c717
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
1 change: 1 addition & 0 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ target 'RxSwift-Playground' do
pod 'Alamofire', '~> 4.0'
pod 'RxSwift', '~> 3.0'
pod 'RxCocoa', '~> 3.0'
pod 'RxDataSources', '~> 1.0'
end
7 changes: 6 additions & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ PODS:
- Alamofire (4.0.1)
- RxCocoa (3.0.1):
- RxSwift (~> 3.0)
- RxDataSources (1.0.0):
- RxCocoa (~> 3.0)
- RxSwift (~> 3.0)
- RxSwift (3.0.1)

DEPENDENCIES:
- Alamofire (~> 4.0)
- RxCocoa (~> 3.0)
- RxDataSources (~> 1.0)
- RxSwift (~> 3.0)

SPEC CHECKSUMS:
Alamofire: 7682d43245de14874acd142ec137b144aa1dd335
RxCocoa: 15a52fc590dcc700cb4a690a633b5c5184ce3a78
RxDataSources: 824693e84d00781d0a5e5cedc1ff95ce9250aacd
RxSwift: af5680055c4ad04480189c52d28385b1029493a6

PODFILE CHECKSUM: f06c889e745705bcc97811ad85aaff7efb787a5b
PODFILE CHECKSUM: 7b20eb76b8d1bdf01eb2fca194b5a1073b8edadf

COCOAPODS: 1.1.1
3 changes: 0 additions & 3 deletions RxSwift-Playground/ViewControllers/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,6 @@
</tableViewCell>
</prototypes>
<sections/>
<connections>
<outlet property="dataSource" destination="dyf-eA-Ftg" id="9UP-kR-yFQ"/>
</connections>
</tableView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,19 @@ class RxSearchViewController: UIViewController {
@IBOutlet weak var searchField: UISearchBar!
@IBOutlet weak var tableView: UITableView!

var users: Variable<[GithubUser]> = Variable([])
let disposeBag = DisposeBag()

override func viewDidLoad() {
super.viewDidLoad()
searchField.becomeFirstResponder()

self.searchField.rx.text.unwrap().throttle(1.0, scheduler: MainScheduler.instance).distinctUntilChanged().flatMapLatest({
let searchUserSignal = self.searchField.rx.text.unwrap().throttle(0.3, scheduler: MainScheduler.instance).distinctUntilChanged().flatMapLatest({
GithubAPI.shared.rx.searchUser(username: $0).retry(3).catchErrorJustReturn([])
}).shareReplay(1).bindTo(users) >>> disposeBag
}).shareReplay(1)

users.asObservable().subscribe(onNext: { _ in
self.tableView.reloadData()
}) >>> disposeBag
}
}

extension RxSearchViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.value.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SimpleCell", for: indexPath)
if let user = users.value[safe: indexPath.row] {
cell.textLabel?.text = user.username
}
return cell
searchUserSignal.bindTo(tableView.rx.items(cellIdentifier: "SimpleCell")) { _, model, cell in
cell.textLabel?.text = model.username
} >>> disposeBag
}
}

Expand All @@ -48,4 +33,8 @@ You can in 3 lines:
[] and return something on error
[] also share results in how many places you want

Can we do more?

And one more thing

*/

0 comments on commit ad1c717

Please sign in to comment.