66// Copyright © 2019 XFLAG. All rights reserved.
77//
88
9+ import UIKit
10+
911// Permission is hereby granted, free of charge, to any person obtaining a copy
1012// of this software and associated documentation files (the "Software"), to deal
1113// in the Software without restriction, including without limitation the rights
2426// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2527// THE SOFTWARE.
2628
29+ import RxCocoa
30+ import RxSwift
31+
2732public protocol RxAlertItem {
2833 static var controllerType : RxAlertController . Type { get }
2934}
@@ -35,14 +40,49 @@ public protocol RxAlertController: UIViewController {
3540 func setAction( for category: RxAlertCategory , item: RxAlertItem ? )
3641}
3742
43+ public struct RxAlertInput {
44+ let placeholder : String ?
45+ let text : String ?
46+ let textAlignment : NSTextAlignment
47+ let onTextChanged : OnTextChanged ?
48+
49+ public init (
50+ placeholder: String ? = nil ,
51+ text: String ? = nil ,
52+ textAlignment: NSTextAlignment = . left,
53+ onTextChanged: OnTextChanged ? = nil
54+ ) {
55+ self . placeholder = placeholder
56+ self . text = text
57+ self . textAlignment = textAlignment
58+ self . onTextChanged = onTextChanged
59+ }
60+
61+ public struct OnTextChanged {
62+ let text : BehaviorRelay < String ? >
63+ let disposeBag : DisposeBag
64+
65+ public init ( text: BehaviorRelay < String ? > , disposeBag: DisposeBag ) {
66+ self . text = text
67+ self . disposeBag = disposeBag
68+ }
69+ }
70+ }
71+
3872public struct UIAlertItem : RxAlertItem {
3973
4074 public static let controllerType : RxAlertController . Type = UIAlertController . self
4175
76+ var inputs : [ RxAlertInput ]
4277 var confirmTitle : String
4378 var denyTitle : String ?
4479
45- public init ( confirmTitle: String , denyTitle: String ? = nil ) {
80+ public init (
81+ inputs: [ RxAlertInput ] = [ ] ,
82+ confirmTitle: String ,
83+ denyTitle: String ? = nil
84+ ) {
85+ self . inputs = inputs
4686 self . confirmTitle = confirmTitle
4787 self . denyTitle = denyTitle
4888 }
@@ -63,7 +103,18 @@ extension UIAlertController: RxAlertController {
63103 if let deny = alertItem. denyTitle {
64104 denyTitle = deny
65105 }
106+ alertItem. inputs. forEach { input in
107+ addTextField {
108+ $0. placeholder = input. placeholder
109+ $0. text = input. text
110+ $0. textAlignment = input. textAlignment
111+ if let onChanged = input. onTextChanged {
112+ $0. rx. text. bind ( to: onChanged. text) . disposed ( by: onChanged. disposeBag)
113+ }
114+ }
115+ }
66116 }
117+
67118 switch category {
68119 case . single( let onConfirm) :
69120 addAction ( UIAlertAction ( title: confirmTitle, style: . cancel) { _ in
0 commit comments