keitin / RxSwiftPractice

0 stars 0 forks source link

RxSampleのサンプルを読んで見るwiki編 #1

Open keitin opened 8 years ago

keitin commented 8 years ago
import UIKit
#if !RX_NO_MODULE
import RxSwift
import RxCocoa
#endif

class WikipediaSearchViewController: ViewController {
    @IBOutlet var searchBar: UISearchBar!
    @IBOutlet var resultsTableView: UITableView!
    @IBOutlet var emptyView: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    // lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        self.edgesForExtendedLayout = .all

        configureTableDataSource()
        configureKeyboardDismissesOnScroll()
        configureNavigateOnRowClick()
        configureActivityIndicatorsShow()
    }

    func configureTableDataSource() {
        resultsTableView.register(UINib(nibName: "WikipediaSearchCell", bundle: nil), forCellReuseIdentifier: "WikipediaSearchCell")

        resultsTableView.rowHeight = 194
        resultsTableView.hideEmptyCells()

        // This is for clarity only, don't use static dependencies
        let API = DefaultWikipediaAPI.sharedAPI

        let results = searchBar.rx.text #サーチバーにテキストが入力されたら...
            .asDriver() //Driver Unitとして扱う
            .throttle(0.3)
            .distinctUntilChanged()
            .flatMapLatest { query in
                API.getSearchResults(query)
                    .retry(3)
                    .retryOnBecomesReachable([], reachabilityService: Dependencies.sharedDependencies.reachabilityService)
                    .startWith([]) // clears results on new search term
                    .asDriver(onErrorJustReturn: [])
            }
            .map { results in
                results.map(SearchResultViewModel.init)
            }

        results
            .drive(resultsTableView.rx.items(cellIdentifier: "WikipediaSearchCell", cellType: WikipediaSearchCell.self)) { (_, viewModel, cell) in
                cell.viewModel = viewModel
            }
            .addDisposableTo(disposeBag)

        results
            .map { $0.count != 0 }
            .drive(self.emptyView.rx.hidden)
            .addDisposableTo(disposeBag)
    }

    func configureKeyboardDismissesOnScroll() {
        let searchBar = self.searchBar

        resultsTableView.rx.contentOffset
            .asDriver()
            .drive(onNext: { _ in
                if searchBar?.isFirstResponder ?? false {
                    _ = searchBar?.resignFirstResponder()
                }
            })
            .addDisposableTo(disposeBag)
    }

    func configureNavigateOnRowClick() {
        let wireframe = DefaultWireframe.sharedInstance

        resultsTableView.rx.modelSelected(SearchResultViewModel.self)
            .asDriver()
            .drive(onNext: { searchResult in
                wireframe.open(url:searchResult.searchResult.URL)
            })
            .addDisposableTo(disposeBag)
    }

    func configureActivityIndicatorsShow() {
        Driver.combineLatest(
            DefaultWikipediaAPI.sharedAPI.loadingWikipediaData,
            DefaultImageService.sharedImageService.loadingImage
        ) { $0 || $1 }
            .distinctUntilChanged()
            .drive(UIApplication.shared.rx.networkActivityIndicatorVisible)
            .addDisposableTo(disposeBag)
    }
}
keitin commented 8 years ago
.asDriver()

について

reference https://github.com/ReactiveX/RxSwift/blob/master/Documentation/Units.md#driver-unit

特徴

keitin commented 8 years ago
throttle(3.0)

について

reference http://cocoadocs.org/docsets/RxSwift/0.9/

300ms throttle interval
>- throttle(300, $.mainScheduler) 

0.3秒間のインターバル → 0.3秒ごとに監視。 おそらくタイピングが止まったタイミングで発火させたい感じ

keitin commented 8 years ago
distinctUntilChanged()

reference http://cocoadocs.org/docsets/RxSwift/0.9/

// forward only distinct values
  >- distinctUntilChanged 

別々の値のみ追う → 重複したvalueは弾いてくれる