Skip to content

Instantly share code, notes, and snippets.

@TheAdamBorek
Last active February 2, 2017 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheAdamBorek/e861284b4d87633e241bc62dc4dca758 to your computer and use it in GitHub Desktop.
Save TheAdamBorek/e861284b4d87633e241bc62dc4dca758 to your computer and use it in GitHub Desktop.
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