Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kyobashi.swift x AKIBA.swift: RxSwiftへの依存を考えてみる

Takeshi Ihara
February 08, 2017

Kyobashi.swift x AKIBA.swift: RxSwiftへの依存を考えてみる

Takeshi Ihara

February 08, 2017
Tweet

More Decks by Takeshi Ihara

Other Decks in Programming

Transcript

  1. RxSwift Rx is a generic abstraction of computation expressed through

    Observable<Element> interface. This is a Swift version of Rx.
  2. Array<String> @IBOutlet private weak var label: UILabel! override func viewDidLoad()

    { super.viewDidLoad() var array: [String] = ["hello"] label.text = array.reduce("") { $0 + $1 } array.append(",") array.append("world") array.append(".") }
  3. @IBOutlet private weak var label: UILabel! override func viewDidLoad() {

    super.viewDidLoad() var array: [String] = ["hello"] label.text = array.reduce("") { $0 + $1 } array.append(",") array.append("world") array.append(".") label.text = array.reduce("") { $0 + $1 } // ߋ৽ } ߋ৽๨Εͯͨʂ Array<String>
  4. Observable<Array<String>> @IBOutlet private weak var label: UILabel! override func viewDidLoad()

    { super.viewDidLoad() let array = Variable<Array<String>>(["hello"]) let disposeBag = DisposeBag() array .asObservable() .map { $0.reduce("") { $0 + $1 } } .bindTo(label.rx.text) .addDisposableTo(disposeBag) array.value.append(",") array.value.append("world") array.value.append(".") }
  5. Observable<Array<String>> @IBOutlet private weak var label: UILabel! override func viewDidLoad()

    { super.viewDidLoad() let array = Variable<Array<String>>(["hello"]) let disposeBag = DisposeBag() array .asObservable() .map { $0.reduce("") { $0 + $1 } } .bindTo(label.rx.text) .addDisposableTo(disposeBag) array.value.append(",") array.value.append("world") array.value.append(".") } ఆ͓͚ٛͯͩ͘͠ʂ
  6. class ProgressView: UIView { enum State { case start, finished

    } let animationSubject = PublishSubject<State>() func start() { animationSubject.onNext(.start) UIView.animate( withDuration: 1.0, animations: { ... } ) { _ in animationSubject.onNext(.finished) } } } Custom View with Animation
  7. protocol ProgressViewDelegate: class { func animationDidStart() func animationDidFinish() } class

    ProgressView: UIView { weak var delegate: ProgressViewDelegate! func start() { delegate?.animationDidStart() UIView.animate( withDuration: 1.0, animations: { ... } ) { _ in delegate?.animationDidFinish() } } } Custom View w/o RxSwift
  8. UITabBar w/ Rx and w/o Rx let tabBar = UITabBar()

    tabBar.delegate = self tabBar.rx.didSelectItem .subscribe { _ in ... } .addDisposableTo(disposeBag) func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { ... } Item͕બ୒͞Εͨ͜ͱΛ ͲͪΒͰ΋ݕ஌Ͱ͖Δʂ
  9. UITabBar+Rx (Ұ෦ൈਮ) extension Reactive where Base: UITabBar { /// Reactive

    wrapper for `delegate`. /// /// For more information take a look at `DelegateProxyType` protocol documentation. public var delegate: DelegateProxy { return RxTabBarDelegateProxy.proxyForObject(base) } /// Reactive wrapper for `delegate` message `tabBar:didSelectItem:`. public var didSelectItem: ControlEvent<UITabBarItem> { let source = delegate.methodInvoked(#selector(UITabBarDelegate.tabBar(_:didSelect:))) .map { a in return try castOrThrow(UITabBarItem.self, a[1]) } return ControlEvent(events: source) } }
  10. ProgressView+Rx (Ұ෦ൈਮ) extension Reactive where Base: ProgressView { var delegate:

    DelegateProxy { return RxTabBarDelegateProxy.proxyForObject(base) } var animationDidStart: ControlEvent<ProgressView> { let source = delegate.methodInvoked(#selector(ProgressViewDelegate.animationDidStart)) .map { a in return try castOrThrow(UITabBarItem.self, a[1]) } return ControlEvent(events: source) } var animationDidFinish: ControlEvent<ProgressView> { let source = delegate.methodInvoked(#selector(ProgressViewDelegate.animationDidFinish)) .map { a in return try castOrThrow(UITabBarItem.self, a[1]) } return ControlEvent(events: source) } }
  11. ProgressView w/ Rx let progressView = ProgressView() progressView.rx.didStartAnimation .subscribe {

    _ in ... } .addDisposableTo(disposeBag) progressView.rx.didFinishAnimation .subscribe { _ in ... } .addDisposableTo(disposeBag)
  12. ProgressView w/o Rx let progressView = ProgressView() progressView.delegate = self

    func animationDidStart() { ... } func animationDidFinish() { ... }