SHARE
TWEET

Untitled

a guest Oct 21st, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import PhoneNumberKit
  2. import Resources
  3. import SkyFloatingLabelTextField
  4. import TKFoundation
  5. import UIComponents
  6.  
  7. public final class GetMoneyView: UIView, HUDView {
  8.     private struct Appereance: Grid {
  9.         let textFieldHeight = 54
  10.         let buttonHeight = 50
  11.         let keyboardWillHide = 240
  12.         let keyboardWillShow = 0
  13.         let textFieldLeading = 28
  14.         let textFieldBottomLineLeading = 12
  15.         let textFieldLineHeight = 2
  16.         let buttonCornerRadius: CGFloat = 8
  17.         let stackViewSpaces: CGFloat = 8
  18.     }
  19.  
  20.     private let appereance: Appereance
  21.  
  22.     // MARK: - View privat
  23.  
  24.     var moneyButtons = [UIButton]()
  25.  
  26.     private func moneyCardButton() -> UIButton {
  27.         let button = UIButton(type: .system)
  28.         button.titleLabel?.font = .systemFont(ofSize: 16, weight: .bold)
  29.         button.layer.cornerRadius = appereance.buttonCornerRadius
  30.         button.setTitleColor(Colors.text.color, for: .normal)
  31.         button.titleLabel?.numberOfLines = 2
  32.         button.snp.makeConstraints { make in
  33.             make.height.equalTo(appereance.xxxlSpace)
  34.         }
  35.         return button
  36.     }
  37.  
  38.     func createViewBorderLayer() -> CAShapeLayer {
  39.         let shapeLayer = CAShapeLayer()
  40.         shapeLayer.strokeColor = UIColor.black.cgColor
  41.         shapeLayer.lineDashPattern = [2, 2]
  42.         shapeLayer.fillColor = nil
  43.         return shapeLayer
  44.     }
  45.  
  46.     private lazy var comissionLabel: UILabel = {
  47.         let label = UILabel(frame: .zero)
  48.         label.textColor = Colors.text60.color
  49.         label.textAlignment = .center
  50.         label.font = UIFont.systemFont(ofSize: 12, weight: .regular)
  51.         return label
  52.     }()
  53.  
  54.     private lazy var verticalStackView: UIStackView = {
  55.         let stackView = UIStackView(frame: .zero)
  56.         stackView.axis = .vertical
  57.         stackView.alignment = .fill
  58.         stackView.distribution = .equalSpacing
  59.         stackView.spacing = 8
  60.         return stackView
  61.     }()
  62.  
  63.     private lazy var textFieldBottomLineView: UIView = {
  64.         let view = UIView(frame: .zero)
  65.         view.backgroundColor = Colors.text.color
  66.         return view
  67.     }()
  68.  
  69.     private lazy var iconLabel: UILabel = {
  70.         let iconLabel = UILabel()
  71.         iconLabel.text = "₽"
  72.         iconLabel.textAlignment = .center
  73.         iconLabel.font = .systemFont(ofSize: 16, weight: .regular)
  74.         iconLabel.textColor = Colors.text32.color
  75.         return iconLabel
  76.     }()
  77.  
  78.     // MARK: - Views public
  79.  
  80.     public lazy var moneyTextField: SkyFloatingLabelTextField = {
  81.         let textField = SkyFloatingLabelTextField(frame: .zero)
  82.         textField.placeholder = Text.GetMoney.textFieldPlaceholder
  83.         textField.lineColor = .clear
  84.         textField.lineErrorColor = .clear
  85.         textField.selectedLineColor = .clear
  86.         textField.titleColor = Colors.text32.color
  87.         textField.selectedTitleColor = Colors.text32.color
  88.         textField.keyboardType = .decimalPad
  89.         return textField
  90.     }()
  91.  
  92.     public lazy var loadButton: PrimaryButton = {
  93.         let button = PrimaryButton.mainInit(title: Text.OnBoarding.Button.load)
  94.         button.isHighlighted = true
  95.         return button
  96.     }()
  97.  
  98.     private func initHorizontalStackView() -> UIStackView {
  99.         let stackView = UIStackView(frame: .zero)
  100.         stackView.axis = .horizontal
  101.         stackView.alignment = .fill
  102.         stackView.distribution = .fillEqually
  103.         stackView.spacing = appereance.stackViewSpaces
  104.         return stackView
  105.     }
  106.  
  107.     // MARK: - Overrided
  108.  
  109.     override init(frame: CGRect) {
  110.         self.appereance = Appereance()
  111.         super.init(frame: frame)
  112.         commonInit()
  113.     }
  114.  
  115.     required init?(coder aDecoder: NSCoder) {
  116.         self.appereance = Appereance()
  117.         super.init(coder: aDecoder)
  118.         commonInit()
  119.     }
  120.  
  121.     // MARK: - Init
  122.  
  123.     private func setupStyle() {
  124.         backgroundColor = .white
  125.     }
  126.  
  127.     private func commonInit() {
  128.         for _ in 0 ... 5 {
  129.             moneyButtons.append(moneyCardButton())
  130.         }
  131.         setupStyle()
  132.         createStackView()
  133.         addSubviews()
  134.         makeConstraints()
  135.     }
  136.  
  137.     private func transformButtonWithBorder() {
  138.         for button in moneyButtons {
  139.             let viewBorderLayer = createViewBorderLayer()
  140.             viewBorderLayer.path = UIBezierPath(roundedRect: button.bounds, cornerRadius: appereance.xxsSpace).cgPath
  141.             viewBorderLayer.frame = button.bounds
  142.             button.layer.addSublayer(viewBorderLayer)
  143.             button.clipsToBounds = true
  144.         }
  145.     }
  146.  
  147.     private func createStackView() {
  148.         let horizontStackViewFirst = initHorizontalStackView()
  149.         let horizontStackViewSecond = initHorizontalStackView()
  150.         makeConstraints(horizontStackViewFirst, horizontStackViewSecond)
  151.         horizontStackViewFirst.addArrangedSubviews([moneyButtons[0], moneyButtons[1]])
  152.         horizontStackViewSecond.addArrangedSubviews([moneyButtons[2], moneyButtons[3]])
  153.         verticalStackView.addArrangedSubviews([horizontStackViewFirst, horizontStackViewSecond, moneyButtons.last!])
  154.     }
  155.  
  156.     private func addSubviews() {
  157.         addSubview(verticalStackView)
  158.         addSubview(moneyTextField)
  159.         addSubview(comissionLabel)
  160.         addSubview(loadButton)
  161.         addSubview(textFieldBottomLineView)
  162.         addSubview(iconLabel)
  163.     }
  164.  
  165.     private func makeConstraints() {
  166.         moneyTextField.snp.makeConstraints { make in
  167.             make.trailing.equalTo(iconLabel.snp.leading).inset(appereance.xxsSpace)
  168.             make.leading.equalToSuperview().inset(appereance.textFieldLeading)
  169.             if #available(iOS 11.0, *) {
  170.                 make.top.equalTo(safeAreaLayoutGuide.snp.top).inset(appereance.mSpace)
  171.             } else {
  172.                 make.top.equalToSuperview().inset(appereance.mSpace)
  173.             }
  174.             make.height.equalTo(appereance.textFieldHeight)
  175.         }
  176.         textFieldBottomLineView.snp.makeConstraints { make in
  177.             make.trailing.equalTo(iconLabel)
  178.             make.top.equalTo(moneyTextField.snp.bottom)
  179.             make.leading.equalTo(moneyTextField.snp.leading).offset(-appereance.textFieldBottomLineLeading)
  180.             make.height.equalTo(appereance.textFieldLineHeight)
  181.         }
  182.         iconLabel.snp.makeConstraints { make in
  183.             make.size.equalTo(appereance.mSize)
  184.             make.leading.equalTo(moneyTextField.snp.trailing).inset(appereance.xxsSpace)
  185.             make.trailing.equalToSuperview().inset(appereance.sSpace)
  186.             make.centerY.equalTo(moneyTextField).inset(appereance.xxsSpace)
  187.         }
  188.         verticalStackView.snp.makeConstraints { make in
  189.             make.height.equalTo(appereance.keyboardWillHide)
  190.             make.bottom.equalTo(loadButton.snp.top).offset(-appereance.lSpace)
  191.             make.leading.trailing.equalTo(loadButton)
  192.         }
  193.         comissionLabel.snp.makeConstraints { make in
  194.             make.leading.trailing.equalToSuperview().inset(appereance.sSpace)
  195.             if #available(iOS 11.0, *) {
  196.                 make.bottom.equalTo(safeAreaLayoutGuide.snp.bottom).inset(appereance.sSpace)
  197.             } else {
  198.                 make.bottom.equalToSuperview().inset(appereance.sSpace)
  199.             }
  200.             make.height.equalTo(appereance.sSpace)
  201.         }
  202.         loadButton.snp.makeConstraints { make in
  203.             make.bottom.equalTo(comissionLabel.snp.top).offset(-appereance.sSpace)
  204.             make.leading.trailing.equalTo(comissionLabel)
  205.             make.height.equalTo(appereance.buttonHeight)
  206.         }
  207.     }
  208.  
  209.     private func makeConstraints(_ horizontStackViewFirst: UIStackView, _ horizontStackViewSecond: UIStackView) {
  210.         horizontStackViewFirst.snp.makeConstraints { make in
  211.             make.height.equalTo(appereance.xxxlSpace)
  212.         }
  213.         horizontStackViewSecond.snp.makeConstraints { make in
  214.             make.height.equalTo(appereance.xxxlSpace)
  215.         }
  216.     }
  217.  
  218.     public func configure(model: PropertiesModel) {
  219.         for (key, sums) in model.sums.enumerated() {
  220.             moneyButtons[key].setTitle(sums.formattedWithCurrencyWIthoutDecimal, for: .normal)
  221.             moneyButtons[key].tag = sums
  222.         }
  223.         let allAmountString = NSMutableAttributedString(
  224.             string: "\(Text.GetMoney.allSumm)\n",
  225.             attributes: [
  226.                 .font: UIFont.systemFont(ofSize: 12, weight: .regular),
  227.                 .foregroundColor: Colors.text32.color,
  228.             ]
  229.         )
  230.         let ammountString = NSAttributedString(
  231.             string: model.maxAmount.formattedWithCurrency,
  232.             attributes: [
  233.                 .font: UIFont.systemFont(ofSize: 16, weight: .bold),
  234.                 .foregroundColor: Colors.text.color,
  235.             ]
  236.         )
  237.         allAmountString.append(ammountString)
  238.         moneyButtons.last?.setAttributedTitle(allAmountString, for: .normal)
  239.         moneyButtons.last?.tag = model.maxAmount.int
  240.         moneyButtons.last?.titleLabel?.textAlignment = .center
  241.         comissionLabel.text = "\(Text.GetMoney.comission) – \(model.commission.int) ₽"
  242.         transformButtonWithBorder()
  243.     }
  244.  
  245.     // MARK: - Constratints animation with keyboard
  246.  
  247.     public func keyboardShowAnimate(keyboadHeight: CGFloat) {
  248.         verticalStackView.isHidden = true
  249.         frame = CGRect(origin: frame.origin, size: CGSize(width: frame.width, height: frame.height - keyboadHeight))
  250.     }
  251.  
  252.     public func keyboardHideAnimation() {
  253.         frame = UIScreen.main.bounds
  254.         verticalStackView.isHidden = false
  255.     }
  256. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top