Guest User

Untitled

a guest
Feb 15th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. import UIKit
  2. import AsyncDisplayKit
  3. import RxSwift
  4. import RxCocoa_Texture
  5.  
  6. extension Reactive where Base: TestNode {
  7.  
  8. var didTapPreviewImage: Observable<Void> {
  9. return base.previewImageNode.rx.tap.asObservable()
  10. }
  11. }
  12.  
  13. class TestNode: ASDisplayNode {
  14.  
  15. struct Const {
  16. static let imagePlaceholderColor: UIColor = .lightGray
  17. static let titleAttr: [VStyleAttribute] = [.font(.systemFont(ofSize: 13.0))]
  18. }
  19.  
  20. var titleNode: ASTextNode = {
  21. let node = ASTextNode()
  22. // TODO: set node attributes
  23. return node
  24. }()
  25.  
  26. var previewImageNode: ASNetworkImageNode = {
  27. let node = ASNetworkImageNode()
  28. // NOTE: use `Const` property
  29. node.backgroundColor = Const.imagePlaceholderColor
  30. return node
  31. }()
  32.  
  33. private let longpressGesture = UILongPressGestureRecognizer()
  34. public let disposeBag = DisposeBag()
  35.  
  36. init(viewModel: CardShowViewModel) {
  37. super.init()
  38. self.automaticallyManagesSubnodes = true // ⚠️ important! ⚠️
  39. self.rxBind(viewModel)
  40. }
  41.  
  42. override func didLoad() {
  43. super.didLoad()
  44. self.view.addGestureRecognizer(longpressGesture)
  45. }
  46. }
  47.  
  48. // MARK: - RxBinding
  49. extension TestNode {
  50.  
  51. private func rxBind(_ viewModel: CardShowViewModel) {
  52. viewModel.title
  53. .bind(to: titleNode.rx.text(Const.titleAttr),
  54. setNeedsLayout: self)
  55. .disposed(by: disposeBag)
  56.  
  57. viewModel.profileImageURL
  58. .bind(to: previewImageNode.rx.url)
  59. .disposed(by: disposeBag)
  60. }
  61. }
  62.  
  63. // MARK: - LayoutSpec
  64. extension TestNode {
  65.  
  66. override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
  67. let previewImageLayout = self.previewImageLayoutSpec(constrainedSize)
  68.  
  69. // NOTE: define flexBox attribute
  70. titleNode.style.shrink(1.0).nonGrow()
  71. previewImageLayout.style.shrink(1.0).grow(1.0)
  72.  
  73. return ASStackLayoutSpec(direction: .vertical,
  74. spacing: 0.0,
  75. justifyContent: .start,
  76. alignItems: .stretch,
  77. children: [titleNode, previewImageLayout])
  78. }
  79.  
  80. private func previewImageLayoutSpec(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
  81. let ratioLayout = ASRatioLayoutSpec(ratio: 1.0, child: previewImageNode)
  82. return ASInsetLayoutSpec(insets: .zero, child: ratioLayout)
  83. }
  84. }
Add Comment
Please, Sign In to add comment