Skip to content

clementleroy/RxAlertController

 
 

Repository files navigation

RxAlertController

CI Status Version License Platform

I'm tired of copying same file into the project every time I need to display a message in the application, so I decided to create a pod.

Changelog

  • 1.1 Add methods to display already instantiated alert controller

Introduction

RxAlertController allows you to display messages on the screen, using the sequence of RxSwift observable streams instead of traditional closures. Thus, the dialog box can be chained with other observables, for example, as follows:

api.someNetworkFunctionThatMayFail()
.retryWhen({ (error) -> Observable<Int> in
    return error.flatMap({ error -> Observable<Int> in
        return UIAlertController.rx.show(in: self, title: "Error", message: error.localizedDescription, buttonTitles: ["Retry", "Abort"])
            .filter({value in value == 0})
    })
})
.subscribe(onNext, onError, etc)

And using UIImagePickerController+RxCreate from RxSwift examples, you can choose pictures like this:

UIAlertController.rx.show(in: self,
                       title: "Change avatar", 
                     message: "Select source", 
                     buttons: [.default("Take a picture"), .default("Select from gallery"), .cancel("Cancel")],
              preferredStyle: .actionSheet)
    .flatMap({ choice in
        if choice == 0 {
            // Create and return UIImagePickerController with source type camera
            return UIImagePickerController.rx.createWithParent(self) { picker in
                picker.sourceType = .camera
                picker.allowsEditing = false
            }
        } else {
            // Create and return UIImagePickerController with source type photo library
            return UIImagePickerController.rx.createWithParent(self) { picker in
                picker.sourceType = .photoLibrary
                picker.allowsEditing = false
            }
        }
    })
    .flatMap { $0.rx.didFinishPickingMediaWithInfo }
    .map { info in
        return info[UIImagePickerControllerOriginalImage] as? UIImage
    }
    .bind(to: imageView.rx.image)
    .disposed(by: disposeBag)

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

RxSwift is required, obviously.

Installation

RxAlertController is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "RxAlertController"

Author

Evgeny Sureev, u@litka.ru

License

RxAlertController is available under the Apache License 2.0. See the LICENSE file for more info.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 87.3%
  • Objective-C 7.6%
  • C 2.9%
  • Shell 2.0%
  • Ruby 0.2%