Skip to content

Instantly share code, notes, and snippets.

@arthur-here

arthur-here/ReactiveDelegates.swift Secret

Last active Apr 10, 2019
Embed
What would you like to do?
/// Shows a list of languages.
class LanguageListViewController: UIViewController {
private let _cancel = PublishSubject<Void>()
var didCancel: Observable<Void> { return _cancel.asObservable() }
private let _selectLanguage = PublishSubject<String>()
var didSelectLanguage: Observable<String> { return _selectLanguage.asObservable() }
private func setupBindings() {
cancelButton.rx.tap
.bind(to: _cancel)
.disposed(by: disposeBag)
tableView.rx.itemSelected
.map { [unowned self] in self.languages[$0.row] }
.bind(to: _selectLanguage)
.disposed(by: disposeBag)
}
}
/// Shows a list of the most starred repositories filtered by a language.
class RepositoryListViewController: UIViewController {
/// Subscribes on the `LanguageListViewController` observables before navigation.
///
/// - Parameter viewController: `LanguageListViewController` to prepare.
private func prepareLanguageListViewController(_ viewController: LanguageListViewController) {
// We need to dismiss the LanguageListViewController if a language was selected or if a cancel button was tapped.
let dismiss = Observable.merge([
viewController.didCancel,
viewController.didSelectLanguage.map { _ in }
])
dismiss
.subscribe(onNext: { [weak self] in self?.dismiss(animated: true) })
.disposed(by: viewController.disposeBag)
viewController.didSelectLanguage
.subscribe(onNext: { [weak self] in
self?.currentLanguage = $0
self?.reloadData()
})
.disposed(by: viewController.disposeBag)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment