Skip to content

Instantly share code, notes, and snippets.

@GeekTree0101
Last active July 7, 2019 12:33
Show Gist options
  • Save GeekTree0101/1f2af5fa0cfa341231848e43044f60a4 to your computer and use it in GitHub Desktop.
Save GeekTree0101/1f2af5fa0cfa341231848e43044f60a4 to your computer and use it in GitHub Desktop.
RxVIP inspired by Cycle.js Composable
protocol AnimalFeedInputLogics {
// 피드 메커니즘에 대한 비즈니스 로직만 수행할 수 있게 뷰컨으로 부터 이벤트를 받는 로직
var loadAnimalItem: PublishRelay<NextPage> { get } // Load more animation cell items
}
protocol AnimalCatCellInputLogics {
// 오직 고양이 셀로 부터 이벤트를 받는 로직
var didTapFollow: PublishRelay<Int> { get } // Int: Cat identifier
}
protocol AnimalDogCellInputLogics {
// 오직 강아지 셀로 부터 이벤트를 받는 로직
var didTapLike: PublishRelay<String> { get } // String: Dog identifier
}
protocol AnimalInteractor: AnimalFeedInputLogics, AnimalCatCellInputLogic, AnimalDogCellInputLogics {
// TODO:
}
protocol AnimalFeedPresenterLogics {
// 오직 뷰컨을 위한 프리젠터
var sections: Driver<[AnimationSection]> { get }
}
protocol AnimalCatCellPresenterLogics {
// 오직 고양이 셀만을 위한 프리젠터, 단 최초로 뷰컨의 RxDataSource로 랜더링 된 이후 고양이 셀의 일부 변경사항에 대해서만 사용함
var configureCatCell: Driver<CatViewModel> { get }
}
protocol AnimalDogCellPresenterLogics {
// 오직 강아지 셀만을 위한 프리젠터
var configureDogCell: Driver<DogViewModel> { get }
}
protocol AnimalPresenter: AnimalFeedPresenterLogics, AnimalCatCellPresenterLogics, AnimalDogCellPresenterLogics {
// TODO:
}
class ViewController {
private var catInteractor: AnimalCatCellInputLogics!
private var dogInteractor: AnimalDogCellInputLogics!
private var interactor: AnimalFeedInputLogics!
private var presenter: AnimalFeedPresenterLogics!
private var catCellPresenter: AnimalCatCellPresenterLogics!
private var dogCellPResenter: AnimalDogCellPresenterLogics!
lazy var dataSource = DataSource.init(itemHandle: { _, _, indexPath, item -> Cell in
switch item.type {
case .cat:
let catCell = CatCell()
let id: Int = item.id
// configure viewmodel(item)
catCell.configure(item)
// input
catCell.rx.didTapFollow
.withLatestFrom(Observable.just(id))
.bind(to: catInteractor.didTapLike)
.disposed(by: catCell.dispsoeBag)
// output
catCellPresetner.configureCatCell
.filter { $0.id == id }
.bind(to: catCell.rx.configure)
.disposed(by: catCell.dispsoeBag)
return catCell
case .dog:
let dogCell = DogCell()
let id: String = item.id
// configure viewModel(item)
dogCell.configure(item)
// input
dogCell.rx.didTapLike
.withLatestFrom(Observable.just(id))
.bind(to: dogInteractor.didTapFollow)
.disposed(by: dogCell.disposeBag)
dogCellPresenter.configureDogCell
.filter { $0.id == id }
.bind(to: dogCell.rx.configure)
.disposed(by: dogCell.disposeBag)
return dogCell
}
})
func configure() {
presenter?.sections
.bind(to: collectionView.rx.items(dataSource)
.disposed(by: bag)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment