-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromptAlert.swift
71 lines (56 loc) · 2.7 KB
/
PromptAlert.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// PromptAlert.swift
// app
//
// Created by Remi Robert on 16/04/16.
// Copyright © 2016 Remi Robert. All rights reserved.
//
import UIKit
import RxSwift
class PromptAlert {
class func show(parentController: UIViewController, title: String, message: String? = nil) -> Observable<Double?> {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
return Observable.create({ observer in
let chargeAction = UIAlertAction(title: "Ok", style: .Default) { (_) in
let loginTextField = alertController.textFields![0] as UITextField
if let textAmount = loginTextField.text {
let amount = Double(textAmount)
observer.onNext(amount)
}
else {
observer.onNext(nil)
}
}
chargeAction.enabled = false
let cancelAction = UIAlertAction(title: "Cancel", style: .Destructive) { (_) in
observer.onNext(nil)
}
alertController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Login"
textField.keyboardType = .NumberPad
NSNotificationCenter.defaultCenter().addObserverForName(UITextFieldTextDidChangeNotification, object: textField, queue: NSOperationQueue.mainQueue()) { (notification) in
chargeAction.enabled = textField.text != ""
}
}
alertController.addAction(cancelAction)
alertController.addAction(chargeAction)
parentController.presentViewController(alertController, animated: true, completion: nil)
return NopDisposable.instance
})
}
class func ask(parentController: UIViewController, title: String, message: String? = nil) -> Observable<Bool> {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
return Observable.create({ observer in
let acceptAction = UIAlertAction(title: "Ok", style: .Default) { (_) in
observer.onNext(true)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Destructive) { (_) in
observer.onNext(false)
}
alertController.addAction(cancelAction)
alertController.addAction(acceptAction)
parentController.presentViewController(alertController, animated: true, completion: nil)
return NopDisposable.instance
})
}
}