McFamous

Untitled

Dec 24th, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.61 KB | None | 0 0
  1. import ThunderUIKit
  2. import UIKit
  3. import Utils
  4. import Combine
  5.  
  6. class LoginWithPhoneViewController: UIViewController, BindableType, MovableContentByKeyboard {
  7. func bindViewModel() {
  8. continueButton.publisher(for: .touchUpInside)
  9. .sink { [weak self] in
  10. self?.viewModel.initiateAuthSession(with: "7" + (self?.phoneNumber.textField.text ?? ""))
  11. }
  12. .store(in: &cancellables)
  13. }
  14.  
  15. // private let caretDownImage = UIImage(named: "common.caretDown")!.withRenderingMode(.alwaysTemplate)
  16. private let russiaCountryImage = UIImage(named: "country.russia")!.withRenderingMode(.alwaysOriginal)
  17. private let tappableTextAttributes: [NSAttributedString.Key: Any] = [.font: UIFont(name: Fonts.medium, size: 12)!, .foregroundColor: ColorScheme.blue300]
  18. private let defaultTextAttributes: [NSAttributedString.Key: Any] = [.font: UIFont(name: Fonts.regular, size: 12)!, .foregroundColor: ColorScheme.gray400]
  19.  
  20. var bottomConstraint: NSLayoutConstraint?
  21. var viewModel: LoginViewModel!
  22. private var cancellables = Set<AnyCancellable>()
  23.  
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. view.backgroundColor = ColorScheme.white
  27.  
  28. view.addSubview(navigationBar)
  29. view.addSubview(messagesStack)
  30. view.addSubview(phoneNumber)
  31. view.addSubview(bottomStack)
  32.  
  33. view.subviews.forEach({ $0.translatesAutoresizingMaskIntoConstraints = false })
  34.  
  35. bottomConstraint = bottomStack.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor)
  36. bottomConstraint?.isActive = true
  37.  
  38. NSLayoutConstraint.activate([
  39. navigationBar.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
  40. navigationBar.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
  41. navigationBar.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
  42.  
  43. messagesStack.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 17.5),
  44. messagesStack.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -17.5),
  45. messagesStack.topAnchor.constraint(equalTo: navigationBar.bottomAnchor, constant: 40),
  46.  
  47. phoneNumber.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 12),
  48. phoneNumber.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -12),
  49. phoneNumber.topAnchor.constraint(equalTo: messagesStack.bottomAnchor, constant: 32),
  50.  
  51. bottomStack.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 12),
  52. bottomStack.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -12),
  53. bottomStack.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
  54. ])
  55.  
  56. self.addKeyboardObserver()
  57. }
  58.  
  59. @objc
  60. override func keyboardWillShow(notification: NSNotification) {
  61. moveContentContainerWithKeyboard(notification: notification, keyboardWillShow: true)
  62. }
  63.  
  64. @objc
  65. override func keyboardWillHide(notification: NSNotification) {
  66. moveContentContainerWithKeyboard(notification: notification, keyboardWillShow: false)
  67. }
  68.  
  69. private lazy var navigationBar: CustomNavigationBar = {
  70. $0.backButton.publisher(for: .touchUpInside)
  71. .sink { [weak self] in self?.dismiss(animated: true) }
  72. .store(in: &cancellables)
  73. return $0
  74. }(CustomNavigationBar())
  75.  
  76. private lazy var messagesStack: UIStackView = {
  77. $0.axis = .vertical
  78. $0.alignment = .center
  79. $0.distribution = .fill
  80. $0.spacing = 12
  81. return $0
  82. }(UIStackView(arrangedSubviews: [message, subMessage]))
  83.  
  84. private lazy var message: UILabel = {
  85. $0.text = "Введите номер телефона\nдля входа или регистрации"
  86. $0.numberOfLines = 0
  87. $0.textColor = ColorScheme.black
  88. $0.font = UIFont(name: Fonts.semiBold, size: 24)
  89. return $0
  90. }(UILabel())
  91.  
  92. private lazy var subMessage: UILabel = {
  93. $0.text = "Мы отправим код подтверждения на ваш номер телефона"
  94. $0.numberOfLines = 0
  95. $0.textColor = ColorScheme.black
  96. $0.font = UIFont(name: Fonts.regular, size: 12)
  97. return $0
  98. }(UILabel())
  99.  
  100. private lazy var phoneNumber: TitledTextField = {
  101. $0.textField.placeholder = "900 000 00 00"
  102. $0.textField.leftTextPadding = 0
  103. $0.textField.keyboardType = .numberPad
  104. $0.textField.returnKeyType = .done
  105. $0.errorDescription = "Некорректный номер телефона"
  106. $0.setLeftView(on: countryCode)
  107. $0.setRightView(on: countryButton, withSeparator: true, withLoadingIndicator: true)
  108.  
  109. $0.textField.publisher(for: .editingChanged)
  110. .sink { [weak self] in
  111. let text = self?.phoneNumber.textField.text ?? ""
  112. self?.phoneNumber.textField.text = TextFormatter.formatEnterText(text: text, fieldMask: .phoneNumberWithoutRegion)
  113. }
  114. .store(in: &cancellables)
  115. return $0
  116. }(TitledTextField(title: "Номер телефона", rightViewPadding: 4))
  117.  
  118. private lazy var countryCode: UILabel = {
  119. $0.text = "+7 "
  120. $0.font = UIFont(name: Fonts.regular, size: 16)
  121. return $0
  122. }(UILabel())
  123.  
  124. private lazy var countryButton: CustomButton = {
  125. // $0.setImage(caretDownImage)
  126. // $0.imageView?.contentMode = .scaleAspectFill
  127. // $0.layer.cornerRadius = 6
  128. // $0.imageEdgeInsets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 0)
  129. $0.isEnabled = false
  130.  
  131. let countryImageView = UIImageView(image: russiaCountryImage)
  132. $0.addSubview(countryImageView)
  133. $0.subviews.forEach { $0.translatesAutoresizingMaskIntoConstraints = false }
  134.  
  135. NSLayoutConstraint.activate([
  136. $0.widthAnchor.constraint(equalToConstant: 50),
  137. countryImageView.leadingAnchor.constraint(equalTo: $0.leadingAnchor, constant: 4),
  138. countryImageView.centerYAnchor.constraint(equalTo: $0.centerYAnchor)
  139. ])
  140. return $0
  141. }(CustomButton(style: .ghost(defaultTintColor: ColorScheme.gray500, highlightTintColor: ColorScheme.gray600)))
  142.  
  143. private lazy var bottomStack: FixedButtonsView = {
  144. $0.addButtons(buttons: [continueButton, disclaimer], axis: .vertical)
  145. return $0
  146. }(FixedButtonsView())
  147.  
  148. private lazy var continueButton: CustomButton = {
  149. $0.heightAnchor.constraint(equalToConstant: 52).isActive = true
  150. $0.setTitle("Продолжить", for: .normal)
  151. $0.titleLabel?.font = UIFont(name: Fonts.medium, size: 16)
  152. $0.titleLabel?.textAlignment = .center
  153. $0.layer.cornerRadius = 14
  154. $0.publisher(for: .touchUpInside)
  155. .sink { [weak self] in
  156. self?.continueButton.buttonState = .loading
  157. self?.countryButton.buttonState = .loading
  158. }
  159. .store(in: &cancellables)
  160. return $0
  161. }(CustomButton(style: .primary(defaultColor: ColorScheme.blue300, defaultTintColor: ColorScheme.white, highlightColor: ColorScheme.blue400)))
  162.  
  163. private lazy var disclaimer: UITextView = {
  164. $0.attributedText = disclaimerText
  165. $0.textAlignment = .center
  166. $0.isEditable = false
  167. $0.isScrollEnabled = false
  168. // $0.delegate = self
  169. return $0
  170. }(UITextView())
  171.  
  172. private lazy var disclaimerText: NSMutableAttributedString = {
  173. $0.append(NSAttributedString(string: "Продолжая авторизацию, я прочитал и согласен", attributes: defaultTextAttributes))
  174. $0.append(NSAttributedString(string: "\n" + "с условиями" + " ", attributes: defaultTextAttributes))
  175. $0.append(NSAttributedString(string: "политики конфинденциальности", attributes: tappableTextAttributes))
  176. $0.append(NSAttributedString(string: "\n" + "и" + " ", attributes: defaultTextAttributes))
  177. $0.append(NSAttributedString(string: "пользовательского соглашения", attributes: tappableTextAttributes))
  178. return $0
  179. }(NSMutableAttributedString())
  180. }
  181.  
  182. extension LoginWithPhoneViewController: UITextViewDelegate {
  183. func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
  184. return false
  185. }
  186. }
  187.  
Advertisement
Add Comment
Please, Sign In to add comment