// // CreateWalletViewController.swift // Malinka // // Created by Nut.Tech on 27.07.2022. // import UIKit final class CreateWalletViewController: UIViewController { private enum Geometry { enum Common { static let left: CGFloat = 12 static let cornerRadius: CGFloat = 2 static let standartFontSize: CGFloat = 12 static let mediumFontSize: CGFloat = 14 static let titleFontSize: CGFloat = 24 static let inputLength = 12 } enum Header { static let commonHeight: CGFloat = 4 static let indicatorsInterval: CGFloat = 4 static let indicatorTop: CGFloat = 10 static let stepTextTop: CGFloat = 16 static let stepTextHeight: CGFloat = 14 static let stepTitleTop: CGFloat = 4 static let stepTitleHeight: CGFloat = 29 } enum WarningView { static let top: CGFloat = 25 } enum InputStack { static let labelTop: CGFloat = 30 static let stackTop: CGFloat = 15 static let stackHeight: CGFloat = 20 } enum AttentionView { static let bottom: CGFloat = 16 static let cornerRadius: CGFloat = 8 } enum SubmitButton { static let height: CGFloat = 48 } } var canClose: Bool = false private lazy var attentionView: UIView = { let attentionView = CommonViewAlert() attentionView.translatesAutoresizingMaskIntoConstraints = false attentionView.containerView?.backgroundColor = Asset.marble.color attentionView.textLbl.text = L10n.Account.Onboarding.limitationsWarning attentionView.textLbl.font = FontFamily.GolosUI.regular.font(size: Geometry.Common.standartFontSize) attentionView.textLbl.textColor = Asset.dark.color attentionView.imgView.image = Asset.appartmentsWarning.image attentionView.imgView.tintColor = Asset.dark.color attentionView.clipsToBounds = true attentionView.layer.cornerRadius = Geometry.AttentionView.cornerRadius return attentionView }() private lazy var submitButton: UIButton = { let action = CommonMenuAction { [weak self] _ in self?.onSubmit() } let button = CommonButtonAction(action: action, height: Geometry.SubmitButton.height) button.lzTitle = L10n.Common.Button.continue button.isEnabled = false button.translatesAutoresizingMaskIntoConstraints = false return button }() private lazy var warningView: UIView = { let view = CommonViewWarning() view.translatesAutoresizingMaskIntoConstraints = false view.textLbl.text = L10n.Account.Onboarding.Username.warning3 view.textLbl.font = FontFamily.GolosUI.regular.font(size: Geometry.Common.standartFontSize) view.textLbl.textColor = Asset.liSTRedEnd.color return view }() private lazy var inputStack: WalletTextInputView = { let inputView = WalletTextInputView(length: Geometry.Common.inputLength, delegate: self) inputView.translatesAutoresizingMaskIntoConstraints = false return inputView }() private lazy var stepTitle: UILabel = { let stepTitle = UILabel() stepTitle.translatesAutoresizingMaskIntoConstraints = false stepTitle.font = FontFamily.GolosUI.bold.font(size: Geometry.Common.titleFontSize) stepTitle.textColor = Asset.dark.color stepTitle.text = L10n.Account.Onboarding.Step1.title return stepTitle }() private var walletIdentifier: String? override func viewDidLoad() { super.viewDidLoad() self.title = L10n.Account.Onboarding.freeTitle self.view.backgroundColor = .white self.overrideUserInterfaceStyle = .light self.setupNavigationBar() self.setupHeader() self.setupWarningView() self.setupTextInputStackView() self.setupSubmitButton() self.setupAttetionView() let tap = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard)) tap.cancelsTouchesInView = false view.addGestureRecognizer(tap) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) if Accounts().current != nil, !self.canClose { self.dismiss(animated: false) } } private func setupHeader() { let stepIndicatorFull = UIView() stepIndicatorFull.translatesAutoresizingMaskIntoConstraints = false stepIndicatorFull.backgroundColor = Asset.deepWater.color stepIndicatorFull.clipsToBounds = true stepIndicatorFull.layer.cornerRadius = Geometry.Common.cornerRadius let stepIndicatorEmpty = UIView() stepIndicatorEmpty.translatesAutoresizingMaskIntoConstraints = false stepIndicatorEmpty.backgroundColor = Asset.disabled.color stepIndicatorEmpty.clipsToBounds = true stepIndicatorEmpty.layer.cornerRadius = Geometry.Common.cornerRadius let stepText = UILabel() stepText.translatesAutoresizingMaskIntoConstraints = false stepText.font = FontFamily.GolosUI.bold.font(size: Geometry.Common.standartFontSize) stepText.textColor = Asset.lightGray.color stepText.text = L10n.Account.Onboarding.Step1.step self.view.addSubview(stepIndicatorFull) self.view.addSubview(stepIndicatorEmpty) self.view.addSubview(stepText) self.view.addSubview(self.stepTitle) NSLayoutConstraint.activate([ stepIndicatorFull.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: Geometry.Header.indicatorTop), stepIndicatorFull.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: Geometry.Common.left), stepIndicatorFull.heightAnchor.constraint(equalToConstant: Geometry.Header.commonHeight), stepIndicatorFull.rightAnchor.constraint(equalTo: stepIndicatorEmpty.leftAnchor, constant: -Geometry.Header.indicatorsInterval), stepIndicatorEmpty.lastBaselineAnchor.constraint(equalTo: stepIndicatorFull.lastBaselineAnchor), stepIndicatorEmpty.heightAnchor.constraint(equalToConstant: Geometry.Header.commonHeight), stepIndicatorEmpty.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -Geometry.Common.left), stepIndicatorEmpty.widthAnchor.constraint(equalTo: stepIndicatorFull.widthAnchor), stepText.topAnchor.constraint(equalTo: stepIndicatorFull.bottomAnchor, constant: Geometry.Header.stepTextTop), stepText.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: Geometry.Common.left), stepText.heightAnchor.constraint(equalToConstant: Geometry.Header.stepTextHeight), self.stepTitle.topAnchor.constraint(equalTo: stepText.bottomAnchor, constant: Geometry.Header.stepTitleTop), self.stepTitle.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: Geometry.Common.left), self.stepTitle.heightAnchor.constraint(equalToConstant: Geometry.Header.stepTitleHeight) ]) } private func setupWarningView() { self.view.addSubview(self.warningView) NSLayoutConstraint.activate([ self.warningView.topAnchor.constraint(equalTo: stepTitle.bottomAnchor, constant: Geometry.WarningView.top), self.warningView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: Geometry.Common.left), self.warningView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -Geometry.Common.left) ]) } private func setupTextInputStackView() { let inputInvitationLabel = UILabel() inputInvitationLabel.translatesAutoresizingMaskIntoConstraints = false inputInvitationLabel.textColor = Asset.dark.color inputInvitationLabel.font = FontFamily.GolosUI.medium.font(size: Geometry.Common.mediumFontSize) inputInvitationLabel.text = L10n.Account.Onboarding.Step1.typeAnyInfo inputInvitationLabel.numberOfLines = 0 self.view.addSubview(inputInvitationLabel) self.view.addSubview(self.inputStack) NSLayoutConstraint.activate([ inputInvitationLabel.topAnchor.constraint(equalTo: warningView.bottomAnchor, constant: Geometry.InputStack.labelTop), inputInvitationLabel.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: Geometry.Common.left), inputInvitationLabel.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -Geometry.Common.left), self.inputStack.topAnchor.constraint(equalTo: inputInvitationLabel.bottomAnchor, constant: Geometry.InputStack.stackTop), self.inputStack.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: Geometry.Common.left), self.inputStack.rightAnchor.constraint(lessThanOrEqualTo: self.view.rightAnchor, constant: -Geometry.Common.left), self.inputStack.heightAnchor.constraint(equalToConstant: Geometry.InputStack.stackHeight) ]) } private func setupAttetionView() { self.view.addSubview(self.attentionView) NSLayoutConstraint.activate([ attentionView.bottomAnchor.constraint(equalTo: self.submitButton.topAnchor, constant: -Geometry.AttentionView.bottom), attentionView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: Geometry.Common.left), attentionView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -Geometry.Common.left) ]) self.attentionView = attentionView } private func setupSubmitButton() { self.view.addSubview(self.submitButton) NSLayoutConstraint.activate([ self.submitButton.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor), self.submitButton.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: Geometry.Common.left), self.submitButton.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -Geometry.Common.left) ]) } private func setupNavigationBar() { self.navigationItem.leftBarButtonItem = .back { [weak self] in if self?.canClose ?? false { self?.navigationController?.dismiss(animated: true) } else { self?.navigationController?.popViewController(animated: true) } } if self.canClose { self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) self.navigationController?.navigationBar.shadowImage = UIImage() self.navigationController?.navigationBar.isTranslucent = true } } @objc private func dismissKeyboard() { self.inputStack.hideKeyboard() } private func onSubmit() { let ctrl = StoryboardScene.AccountOnboarding.setPrivateKeyViewController.instantiate() ctrl.selectedUsername = (self.walletIdentifier ?? "").lowercased() ctrl.showCreateManual = !self.canClose self.navigationController?.pushViewController(ctrl, animated: true) } } extension CreateWalletViewController: WalletTextInputViewDelegate { func updateOutput(text: String?) { self.walletIdentifier = text guard text != nil else { self.submitButton.isEnabled = false return } // TODO: Check wallet identifier self.submitButton.isEnabled = true } }