Guest User

Untitled

a guest
Mar 22nd, 2018
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2.  
  3. import UIKit
  4. import PlaygroundSupport
  5.  
  6. protocol TextChangedEvent {
  7. func onTextChanged(_ closure: @escaping (String?) -> ()) -> Self
  8. }
  9.  
  10. @objc class TextFieldChangeHandler: NSObject {
  11. private var closure: (String?) -> ()
  12.  
  13. init(closure: @escaping (String?) -> ()) {
  14. self.closure = closure
  15. }
  16.  
  17. @objc func change(_ textField: UITextField) {
  18. closure(textField.text)
  19. }
  20. }
  21.  
  22. private var TextFieldChangeHandlerAssociatedObjectHandle: UInt8 = 0
  23.  
  24. extension UITextField {
  25. func onTextChanged(_ closure: @escaping (String?) -> ()) -> Self {
  26. let context = TextFieldChangeHandler(closure: closure)
  27. objc_setAssociatedObject(self, &TextFieldChangeHandlerAssociatedObjectHandle, context, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  28. addTarget(context, action: #selector(TextFieldChangeHandler.change), for: .editingChanged)
  29. return self
  30. }
  31. }
  32.  
  33. protocol TapEvent {
  34. func onTap(_ closure: @escaping () -> ()) -> Self
  35. }
  36.  
  37. @objc class TapContext: NSObject {
  38. private var closure: () -> ()
  39.  
  40. init(closure: @escaping () -> ()) {
  41. self.closure = closure
  42. }
  43.  
  44. @objc func press() {
  45. closure()
  46. }
  47. }
  48.  
  49. private var TapContextAssociatedObjectHandle: UInt8 = 0
  50.  
  51. extension UIButton: TapEvent {
  52. func onTap(_ closure: @escaping () -> ()) -> Self {
  53. let context = TapContext(closure: closure)
  54. objc_setAssociatedObject(self, &TapContextAssociatedObjectHandle, context, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  55. addTarget(context, action: #selector(TapContext.press), for: .touchUpInside)
  56. return self
  57. }
  58. }
  59.  
  60. protocol Initializable {
  61. init()
  62. }
  63.  
  64. func createInstance<T>(_ type: T.Type) -> T where T: Initializable {
  65. return type.init()
  66. }
  67.  
  68. extension UIView: Initializable { }
  69.  
  70. extension UIControl: Initializable { }
  71.  
  72. func component<T: Initializable>(_ type: T.Type, configuration: (T) -> ()) -> T {
  73. let instance: T = createInstance(type)
  74. configuration(instance)
  75. return instance
  76. }
  77.  
  78. protocol Handler {
  79. func login()
  80. func changePassword(_ text: String?)
  81. func changeEmail(_ text: String?)
  82. }
  83.  
  84. class MockHandler: Handler {
  85. func login() {
  86. print("Login pressed")
  87. }
  88.  
  89. func changeEmail(_ text: String?) {
  90. print("Changed email to \(text)")
  91. }
  92.  
  93. func changePassword(_ text: String?) {
  94. print("Changed password to \(text)")
  95. }
  96. }
  97.  
  98. let handler: Handler = MockHandler()
  99.  
  100. let email = component(UITextField.self) {
  101. $0.translatesAutoresizingMaskIntoConstraints = false
  102. $0.placeholder = "Email"
  103. $0.keyboardType = .emailAddress
  104. $0.returnKeyType = .next
  105. $0.autocapitalizationType = .none
  106. $0.autocorrectionType = .no
  107. $0.onTextChanged(handler.changeEmail(_:))
  108. }
  109.  
  110. let password = component(UITextField.self) {
  111. $0.translatesAutoresizingMaskIntoConstraints = false
  112. $0.placeholder = "Password"
  113. $0.keyboardType = .default
  114. $0.returnKeyType = .send
  115. $0.isSecureTextEntry = true
  116. $0.autocapitalizationType = .none
  117. $0.autocorrectionType = .no
  118. $0.onTextChanged(handler.changePassword(_:))
  119. }
  120.  
  121. let button = component(UIButton.self) {
  122. $0.translatesAutoresizingMaskIntoConstraints = false
  123. $0.setTitle("Login", for: .normal)
  124. $0.backgroundColor = .gray
  125. $0.layer.borderWidth = 1 / UIScreen.main.scale
  126. $0.layer.borderColor = UIColor.black.cgColor
  127. $0.onTap(handler.login)
  128. }
  129.  
  130. let view = component(UIView.self) {
  131. $0.frame = CGRect(x: 0, y: 0, width: 300, height: 230)
  132. $0.backgroundColor = .white
  133. $0.addSubview(email)
  134. $0.addSubview(password)
  135. $0.addSubview(button)
  136.  
  137. NSLayoutConstraint.activate([
  138. email.leadingAnchor.constraint(equalTo: $0.leadingAnchor, constant: 20), email.trailingAnchor.constraint(equalTo: $0.trailingAnchor, constant: -20), email.topAnchor.constraint(equalTo: $0.topAnchor, constant: 20), email.heightAnchor.constraint(equalToConstant: 44),
  139. password.leadingAnchor.constraint(equalTo: $0.leadingAnchor, constant: 20), password.trailingAnchor.constraint(equalTo: $0.trailingAnchor, constant: -20), password.topAnchor.constraint(equalTo: email.bottomAnchor, constant: 20), password.heightAnchor.constraint(equalToConstant: 44),
  140. button.leadingAnchor.constraint(equalTo: $0.leadingAnchor, constant: 20), button.trailingAnchor.constraint(equalTo: $0.trailingAnchor, constant: -20), button.topAnchor.constraint(equalTo: password.bottomAnchor, constant: 20), button.heightAnchor.constraint(equalToConstant: 60)
  141. ])
  142. }
  143.  
  144. PlaygroundPage.current.liveView = view
  145.  
  146. email.text = "test@email.com"
  147. password.text = "password123"
  148. button.sendActions(for: .touchUpInside)
Add Comment
Please, Sign In to add comment