Skip to content

Commit c52f546

Browse files
authored
Merge pull request #13 from RxSwiftCommunity/develop/input
Support text inputs
2 parents eef8410 + 04aa5fe commit c52f546

File tree

7 files changed

+146
-25
lines changed

7 files changed

+146
-25
lines changed

Example/Podfile.lock

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
PODS:
2-
- Kingfisher (6.0.1)
3-
- RxAlertViewable (1.1):
2+
- Kingfisher (6.3.1)
3+
- RxAlertViewable (1.2):
44
- RxCocoa (~> 6)
55
- RxSwift (~> 6)
6-
- RxCocoa (6.0.0):
7-
- RxRelay (= 6.0.0)
8-
- RxSwift (= 6.0.0)
9-
- RxRelay (6.0.0):
10-
- RxSwift (= 6.0.0)
11-
- RxSwift (6.0.0)
6+
- RxCocoa (6.2.0):
7+
- RxRelay (= 6.2.0)
8+
- RxSwift (= 6.2.0)
9+
- RxRelay (6.2.0):
10+
- RxSwift (= 6.2.0)
11+
- RxSwift (6.2.0)
1212
- SnapKit (5.0.1)
1313

1414
DEPENDENCIES:
@@ -31,13 +31,13 @@ EXTERNAL SOURCES:
3131
:path: "../"
3232

3333
SPEC CHECKSUMS:
34-
Kingfisher: adde87a4f74f6a3845395769354efff593581740
35-
RxAlertViewable: 4657f4a51b8a6fa5dd8c7a7ed5e08ac63398b8b4
36-
RxCocoa: 3f79328fafa3645b34600f37c31e64c73ae3a80e
37-
RxRelay: 8d593be109c06ea850df027351beba614b012ffb
38-
RxSwift: c14e798c59b9f6e9a2df8fd235602e85cc044295
34+
Kingfisher: 016c8b653a35add51dd34a3aba36b580041acc74
35+
RxAlertViewable: 8b8b82e6c22b974813e67a79b0c3d500bb08b676
36+
RxCocoa: 4baf94bb35f2c0ab31bc0cb9f1900155f646ba42
37+
RxRelay: e72dbfd157807478401ef1982e1c61c945c94b2f
38+
RxSwift: d356ab7bee873611322f134c5f9ef379fa183d8f
3939
SnapKit: 97b92857e3df3a0c71833cce143274bf6ef8e5eb
4040

4141
PODFILE CHECKSUM: b4605b4abcc2e96e810853dd0c68eb98e91ba151
4242

43-
COCOAPODS: 1.10.1
43+
COCOAPODS: 1.11.2

Example/RxAlertViewable/ViewController.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ class ViewController: UIViewController, RxAlertViewable {
6363
return button
6464
}()
6565

66+
private lazy var inputButton: UIButton = {
67+
let button = UIButton()
68+
button.setTitle("Open Input AlertView", for: .normal)
69+
button.setTitleColor(.red, for: .normal)
70+
button.rx.tap.bind { [unowned self] in
71+
self.viewModel.showInputAlertView()
72+
}.disposed(by: disposeBag)
73+
return button
74+
}()
75+
6676
private let viewModel: ViewModel
6777
private let disposeBag = DisposeBag()
6878

@@ -84,6 +94,7 @@ class ViewController: UIViewController, RxAlertViewable {
8494
view.addSubview(customizedAlertButton)
8595
view.addSubview(globalAlertButton)
8696
view.addSubview(actionSheetButton)
97+
view.addSubview(inputButton)
8798
createConstraints()
8899

89100
viewModel.alert.bind(to: rx.alert).disposed(by: disposeBag)
@@ -117,6 +128,10 @@ class ViewController: UIViewController, RxAlertViewable {
117128
$0.top.equalTo(globalAlertButton.snp.bottom).offset(20)
118129
}
119130

131+
inputButton.snp.makeConstraints {
132+
$0.centerX.equalToSuperview()
133+
$0.top.equalTo(actionSheetButton.snp.bottom).offset(20)
134+
}
120135
}
121136

122137
}

Example/RxAlertViewable/ViewModel.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ViewModel {
1515
let alert = PublishSubject<RxAlert>()
1616
let globalTip = PublishSubject<RxAlert>()
1717
let actionSheet = PublishSubject<RxActionSheet>()
18+
let disposeBag = DisposeBag()
1819

1920
private var clickTimes = 0
2021

@@ -85,4 +86,27 @@ class ViewModel {
8586
.cancel
8687
)
8788
}
89+
90+
private let textRealy = BehaviorRelay<String?>(value: nil)
91+
92+
func showInputAlertView() {
93+
94+
alert.onNextTip(
95+
"My Input AlertView",
96+
inputs: [
97+
RxAlertInput(
98+
placeholder: "My placeholder",
99+
text: "My text",
100+
textAlignment: .center,
101+
onTextChanged: RxAlertInput.OnTextChanged(
102+
text: textRealy,
103+
disposeBag: disposeBag
104+
)
105+
)
106+
],
107+
onConfirm: { [weak self] in
108+
self?.alert.onNextTip(self?.textRealy.value ?? "")
109+
}
110+
)
111+
}
88112
}

RxAlertViewable.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
Pod::Spec.new do |s|
99
s.name = 'RxAlertViewable'
10-
s.version = '1.1'
10+
s.version = '1.2'
1111
s.summary = 'A simple alert library with RxSwift supported.'
1212

1313
s.description = <<-DESC

RxAlertViewable/Classes/ObserverType+RxAlert.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ import RxSwift
2828

2929
extension ObserverType where Element == RxAlert {
3030

31-
public func onNextTip(_ message: String, onConfirm: RxAlertCompletion = nil) {
32-
onNext(.tip(message, onConfirm: onConfirm))
31+
public func onNextTip(
32+
_ message: String,
33+
inputs: [RxAlertInput] = [],
34+
onConfirm: RxAlertCompletion = nil
35+
) {
36+
onNext(.tip(message, inputs: inputs, onConfirm: onConfirm))
3337
}
3438

3539
public func onNextWarning(_ message: String, onConfirm: RxAlertCompletion = nil) {

RxAlertViewable/Classes/RxAlert.swift

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,16 @@ public struct RxAlert {
3636

3737
private var title: String
3838
private var message: String
39+
3940
private var item: RxAlertItem?
4041
private var category: RxAlertCategory
4142

42-
init(title: String, message: String, item: RxAlertItem? = nil, category: RxAlertCategory) {
43+
init(
44+
title: String,
45+
message: String,
46+
item: RxAlertItem? = nil,
47+
category: RxAlertCategory
48+
) {
4349
self.title = title
4450
self.message = message
4551
self.item = item
@@ -66,29 +72,50 @@ public struct RxAlert {
6672

6773
extension RxAlert {
6874

69-
public static func tip(_ message: String, onConfirm: RxAlertCompletion = nil) -> RxAlert {
75+
public static func tip(
76+
_ message: String,
77+
inputs: [RxAlertInput] = [],
78+
onConfirm: RxAlertCompletion = nil
79+
) -> RxAlert {
7080
return self.init(
7181
title: RxAlertConfig.current.tip,
7282
message: message,
73-
item: UIAlertItem(confirmTitle: RxAlertConfig.current.ok),
83+
item: UIAlertItem(
84+
inputs: inputs,
85+
confirmTitle: RxAlertConfig.current.ok
86+
),
7487
category: .single(onConfirm: onConfirm)
7588
)
7689
}
7790

78-
public static func warning(_ message: String, onConfirm: RxAlertCompletion = nil) -> RxAlert {
91+
public static func warning(
92+
_ message: String,
93+
inputs: [RxAlertInput] = [],
94+
onConfirm: RxAlertCompletion = nil
95+
) -> RxAlert {
7996
return self.init(
8097
title: RxAlertConfig.current.warning,
8198
message: message,
82-
item: UIAlertItem(confirmTitle: RxAlertConfig.current.ok),
99+
item: UIAlertItem(
100+
inputs: inputs,
101+
confirmTitle: RxAlertConfig.current.ok
102+
),
83103
category: .single(onConfirm: onConfirm)
84104
)
85105
}
86106

87-
public static func error(_ message: String, onConfirm: RxAlertCompletion = nil) -> RxAlert {
107+
public static func error(
108+
_ message: String,
109+
inputs: [RxAlertInput] = [],
110+
onConfirm: RxAlertCompletion = nil
111+
) -> RxAlert {
88112
return self.init(
89113
title: RxAlertConfig.current.error,
90114
message: message,
91-
item: UIAlertItem(confirmTitle: RxAlertConfig.current.ok),
115+
item: UIAlertItem(
116+
inputs: inputs,
117+
confirmTitle: RxAlertConfig.current.ok
118+
),
92119
category: .single(onConfirm: onConfirm)
93120
)
94121
}

RxAlertViewable/Classes/RxAlertController.swift

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
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
@@ -24,6 +26,9 @@
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+
2732
public 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+
3872
public 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

Comments
 (0)