|
| 1 | +# AlertReactor |
| 2 | + |
| 3 | + |
| 4 | +[](https://cocoapods.org/pods/AlertReactor) |
| 5 | +[](https://travis-ci.org/devxoul/AlertReactor) |
| 6 | +[](https://codecov.io/gh/devxoul/AlertReactor) |
| 7 | + |
| 8 | +ReactorKit extension for UIAlertController. It provides an elegant way to deal with an UIAlertController. Best fits for lazy-loaded alert actions. |
| 9 | + |
| 10 | +## Features |
| 11 | + |
| 12 | +* Statically typed alert actions |
| 13 | +* Reactive and dynamic action bindings |
| 14 | + |
| 15 | +## At a Glance |
| 16 | + |
| 17 | +With AlertReactor, you can write a reactive code for alert controller. The code below displays an action sheet when `menuButton` is tapped. When an user selects an item in the action sheet, the selected menu item is converted into an action which is binded to a reactor. |
| 18 | + |
| 19 | +```swift |
| 20 | +// Menu Button -> Action Sheet -> Reactor Action |
| 21 | +menuButton.rx.tap |
| 22 | + .flatMap { [weak self] _ -> Observable<UserAlertAction> in |
| 23 | + let reactor = UserAlertReactor() |
| 24 | + let controller = AlertController<UserAlertAction>(reactor: reactor, preferredStyle: .actionSheet) |
| 25 | + self?.present(controller, animated: true, completion: nil) |
| 26 | + return controller.rx.actionSelected.asObservable() |
| 27 | + } |
| 28 | + .map { alertAction -> Reactor.Action? in |
| 29 | + switch action { |
| 30 | + case .follow: return .followUser |
| 31 | + case .unfollow: return .unfollowUser |
| 32 | + case .block: return .blockUser |
| 33 | + case .cancel: return nil |
| 34 | + } |
| 35 | + } |
| 36 | + .filterNil() |
| 37 | + .bind(to: reactor.action) |
| 38 | +``` |
| 39 | + |
| 40 | +## Getting Started |
| 41 | + |
| 42 | +### 1. Defining an Alert Action |
| 43 | + |
| 44 | +AlertReactor provides a `AlertActionType ` protocol. This is an abstraction model of `UIAlertAction`. Create a new type conforming this protocol. This protocol requires a `title` and `style` property. |
| 45 | + |
| 46 | +```swift |
| 47 | +enum UserAlertAction: AlertActionType { |
| 48 | + case follow |
| 49 | + case unfollow |
| 50 | + case block |
| 51 | + case cancel |
| 52 | + |
| 53 | + // required |
| 54 | + var title: String { |
| 55 | + case follow: return "Follow" |
| 56 | + case unfollow: return "Unfollow" |
| 57 | + case block: return "Block" |
| 58 | + case cancel: return "Cancel" |
| 59 | + } |
| 60 | + |
| 61 | + // optional |
| 62 | + var style: UIAlertActionStyle { |
| 63 | + case follow: return .default |
| 64 | + case unfollow: return .default |
| 65 | + case block: return .destructive |
| 66 | + case cancel: return .cancel |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | + |
| 72 | +### 2. Creating an Alert Reactor |
| 73 | + |
| 74 | +`AlertReactor` is a reactor class. It has an action, mutation and state: |
| 75 | + |
| 76 | +```swift |
| 77 | +enum Action { |
| 78 | + case prepare |
| 79 | +} |
| 80 | + |
| 81 | +enum Mutation { |
| 82 | + case setTitle(String?) |
| 83 | + case setMessage(String?) |
| 84 | + case setActions([AlertAction]) |
| 85 | +} |
| 86 | + |
| 87 | +struct State { |
| 88 | + public var title: String? |
| 89 | + public var message: String? |
| 90 | + public var actions: [AlertAction] |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +Override this class to implement `mutate(action:)` method so that the reactor can emit mutations to change the state. Here is an example of lazy-loaded actions. |
| 95 | + |
| 96 | +```swift |
| 97 | +final class UserAlertReactor: AlertReactor<UserAlertAction> { |
| 98 | + let userID: Int |
| 99 | + |
| 100 | + init(userID: Int) { |
| 101 | + self.userID = userID |
| 102 | + } |
| 103 | + |
| 104 | + override func mutate(action: Action) -> Observable<Mutation> { |
| 105 | + return Observable.concat([ |
| 106 | + // Initial actions |
| 107 | + Observable.just(Mutation.setTitle("Loading...")), |
| 108 | + Observable.just(Mutation.setActions([.block, .cancel])), |
| 109 | + |
| 110 | + // Call API to choose follow or unfollow |
| 111 | + api.isFollowing(userID: userID) |
| 112 | + .map { isFollowing -> Mutation in |
| 113 | + if isFollowing { |
| 114 | + return Mutation.setActions([.unfollow, .block, .cancel]) |
| 115 | + } else { |
| 116 | + return Mutation.setActions([.follow, .block, .cancel]) |
| 117 | + } |
| 118 | + } |
| 119 | + Observable.just(Mutation.setTitle(nil)), |
| 120 | + ]) |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +### 3. Using with Alert Controller |
| 126 | + |
| 127 | +A generic class `AlertController` is provided. You can create it with some parameters: `reactor` and `preferredStyle`. There's also a `actionSelected` control event property in a reactive extension. |
| 128 | + |
| 129 | +```swift |
| 130 | +let reactor = UserAlertReactor(userID: 12345) |
| 131 | +let controller = AlertController<UserAlertAction>(reactor: reactor, preferredStyle: .actionSheet) |
| 132 | +controller.rx.actionSelected |
| 133 | + .subscribe(onNext: { action in |
| 134 | + switch action { |
| 135 | + case .follow: print("Follow user") |
| 136 | + case .unfollow: print("Unfollow user") |
| 137 | + case .block: print("Block user") |
| 138 | + case .cancel: print("Cancel") |
| 139 | + } |
| 140 | + }) |
| 141 | +``` |
| 142 | + |
| 143 | +## Installation |
| 144 | + |
| 145 | +```ruby |
| 146 | +pod 'AlertReactor' |
| 147 | +``` |
| 148 | + |
| 149 | +## License |
| 150 | + |
| 151 | +AlertReactor is under MIT license. See the [LICENSE](LICENSE) for more info. |
0 commit comments