Last active
February 14, 2019 05:54
-
-
Save GeekTree0101/2482ce0b1bf896302dd9fb1544b79672 to your computer and use it in GitHub Desktop.
Texture clean code style guide (Vingle.inc)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import AsyncDisplayKit | |
import RxSwift | |
import RxCocoa_Texture | |
extension Reactive where Base: TestNode { | |
var didTapPreviewImage: Observable<Void> { | |
return base.previewImageNode.rx.tap.asObservable() | |
} | |
} | |
class TestNode: ASDisplayNode { | |
struct Const { | |
static let imagePlaceholderColor: UIColor = .lightGray | |
static let titleAttr: [VStyleAttribute] = [.font(.systemFont(ofSize: 13.0))] | |
} | |
var titleNode: ASTextNode = { | |
let node = ASTextNode() | |
// TODO: set node attributes | |
return node | |
}() | |
var previewImageNode: ASNetworkImageNode = { | |
let node = ASNetworkImageNode() | |
// NOTE: use `Const` property | |
node.backgroundColor = Const.imagePlaceholderColor | |
return node | |
}() | |
private let longpressGesture = UILongPressGestureRecognizer() | |
public let disposeBag = DisposeBag() | |
init(viewModel: CardShowViewModel) { | |
super.init() | |
self.automaticallyManagesSubnodes = true // ⚠️ important! ⚠️ | |
self.rxBind(viewModel) | |
} | |
override func didLoad() { | |
super.didLoad() | |
self.view.addGestureRecognizer(longpressGesture) | |
} | |
} | |
// MARK: - RxBinding | |
extension TestNode { | |
private func rxBind(_ viewModel: CardShowViewModel) { | |
viewModel.title | |
.bind(to: titleNode.rx.text(Const.titleAttr), | |
setNeedsLayout: self) | |
.disposed(by: disposeBag) | |
viewModel.profileImageURL | |
.bind(to: previewImageNode.rx.url) | |
.disposed(by: disposeBag) | |
} | |
} | |
// MARK: - LayoutSpec | |
extension TestNode { | |
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { | |
let previewImageLayout = self.previewImageLayoutSpec(constrainedSize) | |
// NOTE: define flexBox attribute | |
titleNode.style.shrink(1.0).nonGrow() | |
previewImageLayout.style.shrink(1.0).grow(1.0) | |
return ASStackLayoutSpec(direction: .vertical, | |
spacing: 0.0, | |
justifyContent: .start, | |
alignItems: .stretch, | |
children: [titleNode, previewImageLayout]) | |
} | |
private func previewImageLayoutSpec(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { | |
let ratioLayout = ASRatioLayoutSpec(ratio: 1.0, child: previewImageNode) | |
return ASInsetLayoutSpec(insets: .zero, child: ratioLayout) | |
} | |
} |
rxinit 에서 node별로 묶는 것보다
- view -> viewmodel (bindAction)(bindView)
- viewmodel -> view (bindState)(bindData)
를 구분해서 한 번에 처리하는게 좋을 것 같아요 (이름은 자유롭게)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
backgroundColor 넣기? (applyBlender)
여기서도 필요할지는 잘 모르겠어요