Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ThunderUIKit
- import UIKit
- import Utils
- import Combine
- class LoginWithPhoneViewController: UIViewController, BindableType, MovableContentByKeyboard {
- func bindViewModel() {
- continueButton.publisher(for: .touchUpInside)
- .sink { [weak self] in
- self?.viewModel.initiateAuthSession(with: "7" + (self?.phoneNumber.textField.text ?? ""))
- }
- .store(in: &cancellables)
- }
- // private let caretDownImage = UIImage(named: "common.caretDown")!.withRenderingMode(.alwaysTemplate)
- private let russiaCountryImage = UIImage(named: "country.russia")!.withRenderingMode(.alwaysOriginal)
- private let tappableTextAttributes: [NSAttributedString.Key: Any] = [.font: UIFont(name: Fonts.medium, size: 12)!, .foregroundColor: ColorScheme.blue300]
- private let defaultTextAttributes: [NSAttributedString.Key: Any] = [.font: UIFont(name: Fonts.regular, size: 12)!, .foregroundColor: ColorScheme.gray400]
- var bottomConstraint: NSLayoutConstraint?
- var viewModel: LoginViewModel!
- private var cancellables = Set<AnyCancellable>()
- override func viewDidLoad() {
- super.viewDidLoad()
- view.backgroundColor = ColorScheme.white
- view.addSubview(navigationBar)
- view.addSubview(messagesStack)
- view.addSubview(phoneNumber)
- view.addSubview(bottomStack)
- view.subviews.forEach({ $0.translatesAutoresizingMaskIntoConstraints = false })
- bottomConstraint = bottomStack.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor)
- bottomConstraint?.isActive = true
- NSLayoutConstraint.activate([
- navigationBar.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
- navigationBar.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
- navigationBar.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
- messagesStack.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 17.5),
- messagesStack.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -17.5),
- messagesStack.topAnchor.constraint(equalTo: navigationBar.bottomAnchor, constant: 40),
- phoneNumber.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 12),
- phoneNumber.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -12),
- phoneNumber.topAnchor.constraint(equalTo: messagesStack.bottomAnchor, constant: 32),
- bottomStack.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 12),
- bottomStack.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -12),
- bottomStack.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
- ])
- self.addKeyboardObserver()
- }
- @objc
- override func keyboardWillShow(notification: NSNotification) {
- moveContentContainerWithKeyboard(notification: notification, keyboardWillShow: true)
- }
- @objc
- override func keyboardWillHide(notification: NSNotification) {
- moveContentContainerWithKeyboard(notification: notification, keyboardWillShow: false)
- }
- private lazy var navigationBar: CustomNavigationBar = {
- $0.backButton.publisher(for: .touchUpInside)
- .sink { [weak self] in self?.dismiss(animated: true) }
- .store(in: &cancellables)
- return $0
- }(CustomNavigationBar())
- private lazy var messagesStack: UIStackView = {
- $0.axis = .vertical
- $0.alignment = .center
- $0.distribution = .fill
- $0.spacing = 12
- return $0
- }(UIStackView(arrangedSubviews: [message, subMessage]))
- private lazy var message: UILabel = {
- $0.text = "Введите номер телефона\nдля входа или регистрации"
- $0.numberOfLines = 0
- $0.textColor = ColorScheme.black
- $0.font = UIFont(name: Fonts.semiBold, size: 24)
- return $0
- }(UILabel())
- private lazy var subMessage: UILabel = {
- $0.text = "Мы отправим код подтверждения на ваш номер телефона"
- $0.numberOfLines = 0
- $0.textColor = ColorScheme.black
- $0.font = UIFont(name: Fonts.regular, size: 12)
- return $0
- }(UILabel())
- private lazy var phoneNumber: TitledTextField = {
- $0.textField.placeholder = "900 000 00 00"
- $0.textField.leftTextPadding = 0
- $0.textField.keyboardType = .numberPad
- $0.textField.returnKeyType = .done
- $0.errorDescription = "Некорректный номер телефона"
- $0.setLeftView(on: countryCode)
- $0.setRightView(on: countryButton, withSeparator: true, withLoadingIndicator: true)
- $0.textField.publisher(for: .editingChanged)
- .sink { [weak self] in
- let text = self?.phoneNumber.textField.text ?? ""
- self?.phoneNumber.textField.text = TextFormatter.formatEnterText(text: text, fieldMask: .phoneNumberWithoutRegion)
- }
- .store(in: &cancellables)
- return $0
- }(TitledTextField(title: "Номер телефона", rightViewPadding: 4))
- private lazy var countryCode: UILabel = {
- $0.text = "+7 "
- $0.font = UIFont(name: Fonts.regular, size: 16)
- return $0
- }(UILabel())
- private lazy var countryButton: CustomButton = {
- // $0.setImage(caretDownImage)
- // $0.imageView?.contentMode = .scaleAspectFill
- // $0.layer.cornerRadius = 6
- // $0.imageEdgeInsets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 0)
- $0.isEnabled = false
- let countryImageView = UIImageView(image: russiaCountryImage)
- $0.addSubview(countryImageView)
- $0.subviews.forEach { $0.translatesAutoresizingMaskIntoConstraints = false }
- NSLayoutConstraint.activate([
- $0.widthAnchor.constraint(equalToConstant: 50),
- countryImageView.leadingAnchor.constraint(equalTo: $0.leadingAnchor, constant: 4),
- countryImageView.centerYAnchor.constraint(equalTo: $0.centerYAnchor)
- ])
- return $0
- }(CustomButton(style: .ghost(defaultTintColor: ColorScheme.gray500, highlightTintColor: ColorScheme.gray600)))
- private lazy var bottomStack: FixedButtonsView = {
- $0.addButtons(buttons: [continueButton, disclaimer], axis: .vertical)
- return $0
- }(FixedButtonsView())
- private lazy var continueButton: CustomButton = {
- $0.heightAnchor.constraint(equalToConstant: 52).isActive = true
- $0.setTitle("Продолжить", for: .normal)
- $0.titleLabel?.font = UIFont(name: Fonts.medium, size: 16)
- $0.titleLabel?.textAlignment = .center
- $0.layer.cornerRadius = 14
- $0.publisher(for: .touchUpInside)
- .sink { [weak self] in
- self?.continueButton.buttonState = .loading
- self?.countryButton.buttonState = .loading
- }
- .store(in: &cancellables)
- return $0
- }(CustomButton(style: .primary(defaultColor: ColorScheme.blue300, defaultTintColor: ColorScheme.white, highlightColor: ColorScheme.blue400)))
- private lazy var disclaimer: UITextView = {
- $0.attributedText = disclaimerText
- $0.textAlignment = .center
- $0.isEditable = false
- $0.isScrollEnabled = false
- // $0.delegate = self
- return $0
- }(UITextView())
- private lazy var disclaimerText: NSMutableAttributedString = {
- $0.append(NSAttributedString(string: "Продолжая авторизацию, я прочитал и согласен", attributes: defaultTextAttributes))
- $0.append(NSAttributedString(string: "\n" + "с условиями" + " ", attributes: defaultTextAttributes))
- $0.append(NSAttributedString(string: "политики конфинденциальности", attributes: tappableTextAttributes))
- $0.append(NSAttributedString(string: "\n" + "и" + " ", attributes: defaultTextAttributes))
- $0.append(NSAttributedString(string: "пользовательского соглашения", attributes: tappableTextAttributes))
- return $0
- }(NSMutableAttributedString())
- }
- extension LoginWithPhoneViewController: UITextViewDelegate {
- func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
- return false
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment