View.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // View.swift
  3. // ReactorKit
  4. //
  5. // Created by Suyeol Jeon on 13/04/2017.
  6. // Copyright © 2017 Suyeol Jeon. All rights reserved.
  7. //
  8. #if !os(Linux)
  9. import RxSwift
  10. import WeakMapTable
  11. private typealias AnyView = AnyObject
  12. private enum MapTables {
  13. static let reactor = WeakMapTable<AnyView, Any>()
  14. }
  15. /// A View displays data. A view controller and a cell are treated as a view. The view binds user
  16. /// inputs to the action stream and binds the view states to each UI component. There's no business
  17. /// logic in a view layer. A view just defines how to map the action stream and the state stream.
  18. public protocol View: class {
  19. associatedtype Reactor: ReactorKit.Reactor
  20. /// A dispose bag. It is disposed each time the `reactor` is assigned.
  21. var disposeBag: DisposeBag { get set }
  22. /// A view's reactor. `bind(reactor:)` gets called when the new value is assigned to this property.
  23. var reactor: Reactor? { get set }
  24. /// Creates RxSwift bindings. This method is called each time the `reactor` is assigned.
  25. ///
  26. /// Here is a typical implementation example:
  27. ///
  28. /// ```
  29. /// func bind(reactor: MyReactor) {
  30. /// // Action
  31. /// increaseButton.rx.tap
  32. /// .bind(to: Reactor.Action.increase)
  33. /// .disposed(by: disposeBag)
  34. ///
  35. /// // State
  36. /// reactor.state.map { $0.count }
  37. /// .bind(to: countLabel.rx.text)
  38. /// .disposed(by: disposeBag)
  39. /// }
  40. /// ```
  41. ///
  42. /// - warning: It's not recommended to call this method directly.
  43. func bind(reactor: Reactor)
  44. }
  45. // MARK: - Default Implementations
  46. extension View {
  47. public var reactor: Reactor? {
  48. get { return MapTables.reactor.value(forKey: self) as? Reactor }
  49. set {
  50. MapTables.reactor.setValue(newValue, forKey: self)
  51. self.disposeBag = DisposeBag()
  52. if let reactor = newValue {
  53. self.bind(reactor: reactor)
  54. }
  55. }
  56. }
  57. }
  58. #endif