-
-
Save arturdent/96f520894c1ce8928db7d8d21308cc61 to your computer and use it in GitHub Desktop.
View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PhoneNumberKit | |
import Resources | |
import SkyFloatingLabelTextField | |
import TKFoundation | |
import UIComponents | |
public final class GetMoneyView: UIView, HUDView { | |
private struct Appereance: Grid { | |
let textFieldHeight = 54 | |
let buttonHeight = 50 | |
let keyboardWillHide = 240 | |
let keyboardWillShow = 0 | |
let textFieldLeading = 28 | |
let textFieldBottomLineLeading = 12 | |
let textFieldLineHeight = 2 | |
let buttonCornerRadius: CGFloat = 8 | |
let stackViewSpaces: CGFloat = 8 | |
} | |
private let appereance: Appereance | |
// MARK: - View privat | |
var moneyButtons = [UIButton]() | |
private func moneyCardButton() -> UIButton { | |
let button = UIButton(type: .system) | |
button.titleLabel?.font = .systemFont(ofSize: 16, weight: .bold) | |
button.layer.cornerRadius = appereance.buttonCornerRadius | |
button.setTitleColor(Colors.text.color, for: .normal) | |
button.titleLabel?.numberOfLines = 2 | |
button.snp.makeConstraints { make in | |
make.height.equalTo(appereance.xxxlSpace) | |
} | |
return button | |
} | |
func createViewBorderLayer() -> CAShapeLayer { | |
let shapeLayer = CAShapeLayer() | |
shapeLayer.strokeColor = UIColor.black.cgColor | |
shapeLayer.lineDashPattern = [2, 2] | |
shapeLayer.fillColor = nil | |
return shapeLayer | |
} | |
private lazy var comissionLabel: UILabel = { | |
let label = UILabel(frame: .zero) | |
label.textColor = Colors.text60.color | |
label.textAlignment = .center | |
label.font = UIFont.systemFont(ofSize: 12, weight: .regular) | |
return label | |
}() | |
private lazy var verticalStackView: UIStackView = { | |
let stackView = UIStackView(frame: .zero) | |
stackView.axis = .vertical | |
stackView.alignment = .fill | |
stackView.distribution = .equalSpacing | |
stackView.spacing = 8 | |
return stackView | |
}() | |
private lazy var textFieldBottomLineView: UIView = { | |
let view = UIView(frame: .zero) | |
view.backgroundColor = Colors.text.color | |
return view | |
}() | |
private lazy var iconLabel: UILabel = { | |
let iconLabel = UILabel() | |
iconLabel.text = "₽" | |
iconLabel.textAlignment = .center | |
iconLabel.font = .systemFont(ofSize: 16, weight: .regular) | |
iconLabel.textColor = Colors.text32.color | |
return iconLabel | |
}() | |
// MARK: - Views public | |
public lazy var moneyTextField: SkyFloatingLabelTextField = { | |
let textField = SkyFloatingLabelTextField(frame: .zero) | |
textField.placeholder = Text.GetMoney.textFieldPlaceholder | |
textField.lineColor = .clear | |
textField.lineErrorColor = .clear | |
textField.selectedLineColor = .clear | |
textField.titleColor = Colors.text32.color | |
textField.selectedTitleColor = Colors.text32.color | |
textField.keyboardType = .decimalPad | |
return textField | |
}() | |
public lazy var loadButton: PrimaryButton = { | |
let button = PrimaryButton.mainInit(title: Text.OnBoarding.Button.load) | |
button.isHighlighted = true | |
return button | |
}() | |
private func initHorizontalStackView() -> UIStackView { | |
let stackView = UIStackView(frame: .zero) | |
stackView.axis = .horizontal | |
stackView.alignment = .fill | |
stackView.distribution = .fillEqually | |
stackView.spacing = appereance.stackViewSpaces | |
return stackView | |
} | |
// MARK: - Overrided | |
override init(frame: CGRect) { | |
self.appereance = Appereance() | |
super.init(frame: frame) | |
commonInit() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
self.appereance = Appereance() | |
super.init(coder: aDecoder) | |
commonInit() | |
} | |
// MARK: - Init | |
private func setupStyle() { | |
backgroundColor = .white | |
} | |
private func commonInit() { | |
for _ in 0 ... 5 { | |
moneyButtons.append(moneyCardButton()) | |
} | |
setupStyle() | |
createStackView() | |
addSubviews() | |
makeConstraints() | |
} | |
private func transformButtonWithBorder() { | |
for button in moneyButtons { | |
let viewBorderLayer = createViewBorderLayer() | |
viewBorderLayer.path = UIBezierPath(roundedRect: button.bounds, cornerRadius: appereance.xxsSpace).cgPath | |
viewBorderLayer.frame = button.bounds | |
button.layer.addSublayer(viewBorderLayer) | |
button.clipsToBounds = true | |
} | |
} | |
private func createStackView() { | |
let horizontStackViewFirst = initHorizontalStackView() | |
let horizontStackViewSecond = initHorizontalStackView() | |
makeConstraints(horizontStackViewFirst, horizontStackViewSecond) | |
horizontStackViewFirst.addArrangedSubviews([moneyButtons[0], moneyButtons[1]]) | |
horizontStackViewSecond.addArrangedSubviews([moneyButtons[2], moneyButtons[3]]) | |
verticalStackView.addArrangedSubviews([horizontStackViewFirst, horizontStackViewSecond, moneyButtons.last!]) | |
} | |
private func addSubviews() { | |
addSubview(verticalStackView) | |
addSubview(moneyTextField) | |
addSubview(comissionLabel) | |
addSubview(loadButton) | |
addSubview(textFieldBottomLineView) | |
addSubview(iconLabel) | |
} | |
private func makeConstraints() { | |
moneyTextField.snp.makeConstraints { make in | |
make.trailing.equalTo(iconLabel.snp.leading).inset(appereance.xxsSpace) | |
make.leading.equalToSuperview().inset(appereance.textFieldLeading) | |
if #available(iOS 11.0, *) { | |
make.top.equalTo(safeAreaLayoutGuide.snp.top).inset(appereance.mSpace) | |
} else { | |
make.top.equalToSuperview().inset(appereance.mSpace) | |
} | |
make.height.equalTo(appereance.textFieldHeight) | |
} | |
textFieldBottomLineView.snp.makeConstraints { make in | |
make.trailing.equalTo(iconLabel) | |
make.top.equalTo(moneyTextField.snp.bottom) | |
make.leading.equalTo(moneyTextField.snp.leading).offset(-appereance.textFieldBottomLineLeading) | |
make.height.equalTo(appereance.textFieldLineHeight) | |
} | |
iconLabel.snp.makeConstraints { make in | |
make.size.equalTo(appereance.mSize) | |
make.leading.equalTo(moneyTextField.snp.trailing).inset(appereance.xxsSpace) | |
make.trailing.equalToSuperview().inset(appereance.sSpace) | |
make.centerY.equalTo(moneyTextField).inset(appereance.xxsSpace) | |
} | |
verticalStackView.snp.makeConstraints { make in | |
make.height.equalTo(appereance.keyboardWillHide) | |
make.bottom.equalTo(loadButton.snp.top).offset(-appereance.lSpace) | |
make.leading.trailing.equalTo(loadButton) | |
} | |
comissionLabel.snp.makeConstraints { make in | |
make.leading.trailing.equalToSuperview().inset(appereance.sSpace) | |
if #available(iOS 11.0, *) { | |
make.bottom.equalTo(safeAreaLayoutGuide.snp.bottom).inset(appereance.sSpace) | |
} else { | |
make.bottom.equalToSuperview().inset(appereance.sSpace) | |
} | |
make.height.equalTo(appereance.sSpace) | |
} | |
loadButton.snp.makeConstraints { make in | |
make.bottom.equalTo(comissionLabel.snp.top).offset(-appereance.sSpace) | |
make.leading.trailing.equalTo(comissionLabel) | |
make.height.equalTo(appereance.buttonHeight) | |
} | |
} | |
private func makeConstraints(_ horizontStackViewFirst: UIStackView, _ horizontStackViewSecond: UIStackView) { | |
horizontStackViewFirst.snp.makeConstraints { make in | |
make.height.equalTo(appereance.xxxlSpace) | |
} | |
horizontStackViewSecond.snp.makeConstraints { make in | |
make.height.equalTo(appereance.xxxlSpace) | |
} | |
} | |
public func configure(model: PropertiesModel) { | |
for (key, sums) in model.sums.enumerated() { | |
moneyButtons[key].setTitle(sums.formattedWithCurrencyWIthoutDecimal, for: .normal) | |
moneyButtons[key].tag = sums | |
} | |
let allAmountString = NSMutableAttributedString( | |
string: "\(Text.GetMoney.allSumm)\n", | |
attributes: [ | |
.font: UIFont.systemFont(ofSize: 12, weight: .regular), | |
.foregroundColor: Colors.text32.color, | |
] | |
) | |
let ammountString = NSAttributedString( | |
string: model.maxAmount.formattedWithCurrency, | |
attributes: [ | |
.font: UIFont.systemFont(ofSize: 16, weight: .bold), | |
.foregroundColor: Colors.text.color, | |
] | |
) | |
allAmountString.append(ammountString) | |
moneyButtons.last?.setAttributedTitle(allAmountString, for: .normal) | |
moneyButtons.last?.tag = model.maxAmount.int | |
moneyButtons.last?.titleLabel?.textAlignment = .center | |
comissionLabel.text = "\(Text.GetMoney.comission) – \(model.commission.int) ₽" | |
transformButtonWithBorder() | |
} | |
// MARK: - Constratints animation with keyboard | |
public func keyboardShowAnimate(keyboadHeight: CGFloat) { | |
verticalStackView.isHidden = true | |
frame = CGRect(origin: frame.origin, size: CGSize(width: frame.width, height: frame.height - keyboadHeight)) | |
} | |
public func keyboardHideAnimation() { | |
frame = UIScreen.main.bounds | |
verticalStackView.isHidden = false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment