Created
July 6, 2020 13:42
-
-
Save iamchiwon/9b7cd274d09daa9d50588cd05f4d9f02 to your computer and use it in GitHub Desktop.
[코드리뷰쇼] #1. Relay 5개라굽쇼?
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 RxCocoa | |
| import RxSwift | |
| import UIKit | |
| class ViewController: UIViewController { | |
| enum SortType { | |
| case none, newest, oldest | |
| } | |
| enum MonthType { | |
| case none, one, three, six | |
| } | |
| let sortRelay = BehaviorRelay(value: SortType.none) // 정렬기준 | |
| let monthRelay = BehaviorRelay(value: MonthType.none) // 기간설정 | |
| var disposeBag = DisposeBag() | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| binding() | |
| } | |
| private func binding() { | |
| // sort | |
| Observable.merge( | |
| optionSortNewest.rx.tap.map { SortType.newest }, | |
| optionSortOldest.rx.tap.map { SortType.oldest } | |
| ) | |
| .bind(to: sortRelay) | |
| .disposed(by: disposeBag) | |
| sortRelay.flatMap { [weak self] in | |
| Observable.from([ | |
| ($0 == .newest, self?.optionSortNewest), | |
| ($0 == .oldest, self?.optionSortOldest), | |
| ]) | |
| } | |
| .asDriver(onErrorJustReturn: (false, nil)) | |
| .drive(onNext: { selected, button in | |
| button?.isSelected = selected | |
| }) | |
| .disposed(by: disposeBag) | |
| // month | |
| Observable.merge( | |
| optionTermOneMonth.rx.tap.map { MonthType.one }, | |
| optionTermThreeMonth.rx.tap.map { MonthType.three }, | |
| optionTermSixMonth.rx.tap.map { MonthType.six } | |
| ) | |
| .bind(to: monthRelay) | |
| .disposed(by: disposeBag) | |
| monthRelay.flatMap { [weak self] in | |
| Observable.from([ | |
| ($0 == .one, self?.optionTermOneMonth), | |
| ($0 == .three, self?.optionTermThreeMonth), | |
| ($0 == .six, self?.optionTermSixMonth), | |
| ]) | |
| } | |
| .asDriver(onErrorJustReturn: (false, nil)) | |
| .drive(onNext: { selected, button in | |
| button?.isSelected = selected | |
| }) | |
| .disposed(by: disposeBag) | |
| // confirm button | |
| Observable.combineLatest( | |
| sortRelay.map { $0 != .none }, | |
| monthRelay.map { $0 != .none } | |
| ) { $0 && $1 } | |
| .bind(to: confirmButton.rx.isEnabled) | |
| .disposed(by: disposeBag) | |
| } | |
| // MARK: - UI | |
| @IBOutlet var optionSortNewest: UIButton! | |
| @IBOutlet var optionSortOldest: UIButton! | |
| @IBOutlet var optionTermOneMonth: UIButton! | |
| @IBOutlet var optionTermThreeMonth: UIButton! | |
| @IBOutlet var optionTermSixMonth: UIButton! | |
| @IBOutlet var confirmButton: UIButton! | |
| @IBAction func onCancel(sender: Any) { | |
| dismiss(animated: true, completion: nil) | |
| } | |
| @IBAction func onOK(sender: Any) { | |
| dismiss(animated: true, completion: nil) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment