Last active
February 2, 2017 07:00
-
-
Save TheAdamBorek/e861284b4d87633e241bc62dc4dca758 to your computer and use it in GitHub Desktop.
This file contains 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
private var selectedOption: Observable<ImageSource> { | |
return Observable.create { [weak self] observer in | |
guard let `self` = self else { | |
observer.onCompleted() | |
return Disposables.create() | |
} | |
let actionSheet = self.prepareActionSheet(with: observer) | |
self.presenter?.present(actionSheet) | |
return Disposables.create { | |
actionSheet.dismiss(animated: true, completion: nil) | |
} | |
} | |
} | |
private func prepareActionSheet(with actionTapObserver: AnyObserver<ImageSource>) -> UIAlertController { | |
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) | |
prepareActionSheetActions(with: actionTapObserver) | |
.forEach { actionSheet.addAction($0) } | |
return actionSheet | |
} | |
private func prepareActionSheetActions(with tapObserver: AnyObserver<ImageSource>) -> [UIAlertAction] { | |
var actions = createSourcesActions(with: tapObserver) | |
let cancel = createCancelAction(with: tapObserver) | |
actions.append(cancel) | |
return actions | |
} | |
private func createSourcesActions(with tapObserver: AnyObserver<ImageSource>) -> [UIAlertAction] { | |
return sources.map { source in | |
return UIAlertAction(title: source.description, style: .default) { _ in | |
tapObserver.onNext(source) | |
tapObserver.onCompleted() | |
} | |
} | |
} | |
private func createCancelAction(with tapObserver: AnyObserver<ImageSource>) -> UIAlertAction { | |
return UIAlertAction(title: Strings.cancel, style: .cancel) { _ in | |
tapObserver.onCompleted() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment