You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
255 lines
9.0 KiB
255 lines
9.0 KiB
//
|
|
|
|
import UIKit
|
|
|
|
class ExchangeRateCustomView: UIView {
|
|
|
|
var passTextFieldText: ((String) -> Void)?
|
|
var didTappedDoneButton: (() -> Void)?
|
|
var didSelectCountry: (() -> ())?
|
|
var isReciver: Bool = false
|
|
var text: String?
|
|
var countryListTapGuesture: UITapGestureRecognizer?
|
|
var updatedText: String? {
|
|
didSet {
|
|
updateText()
|
|
}
|
|
}
|
|
|
|
var currency: String? {
|
|
didSet {
|
|
updateCurrency()
|
|
}
|
|
}
|
|
|
|
var flag: UIImage? {
|
|
didSet {
|
|
updateFlag()
|
|
}
|
|
}
|
|
|
|
var title: String? {
|
|
didSet {
|
|
titleLabel.text = title
|
|
}
|
|
}
|
|
|
|
var eightPixelSpace: UIView = {
|
|
let pixelView = UIView()
|
|
pixelView.translatesAutoresizingMaskIntoConstraints = false
|
|
pixelView.widthAnchor.constraint(equalToConstant: 1).isActive = true
|
|
pixelView.heightAnchor.constraint(equalToConstant: 1).isActive = true
|
|
|
|
return pixelView
|
|
}()
|
|
|
|
var fourPixelSpace: UIView = {
|
|
let pixelView = UIView()
|
|
pixelView.translatesAutoresizingMaskIntoConstraints = false
|
|
pixelView.widthAnchor.constraint(equalToConstant: 1).isActive = true
|
|
pixelView.heightAnchor.constraint(equalToConstant: 1).isActive = true
|
|
|
|
return pixelView
|
|
}()
|
|
|
|
var dropdownIcon: UIImageView = {
|
|
let dropdownIconImageView = UIImageView()
|
|
dropdownIconImageView.image = UIImage.init(named: "dropdown_white")
|
|
dropdownIconImageView.translatesAutoresizingMaskIntoConstraints = false
|
|
dropdownIconImageView.clipsToBounds = true
|
|
dropdownIconImageView.contentMode = .center
|
|
|
|
|
|
// dropdownIconImageView.ima
|
|
// dropdownIconImageView.leadingAnchor.constraint(lessThanOrEqualTo: dropdownIconImageView.trailingAnchor, constant: 4).isActive = true
|
|
// dropdownIconImageView.leadingAnchor.constraint(lessThanOrEqualTo: dropdownIconImageView.trailingAnchor, constant: 4).isActive = true
|
|
// dropdownIconImageView.anchor(paddingTop: 4, paddingBottom: 4, paddingLeft: 4, paddingRight: 4, width: 16, height: 16)
|
|
// dropdownIconImageView.topAnchor.constraint(equalToConstant: 4).isActive = true
|
|
// dropdownIconImageView.bottomAnchor.constraint(equalToConstant: 4).isActive = true
|
|
// dropdownIconImageView.leftAnchor.constraint(equalToConstant: 4).isActive = true
|
|
// dropdownIconImageView.rightAnchor.constraint(equalToConstant: 4).isActive = true
|
|
dropdownIconImageView.heightAnchor.constraint(equalToConstant: 16).isActive = true
|
|
dropdownIconImageView.widthAnchor.constraint(equalToConstant: 16).isActive = true
|
|
|
|
return dropdownIconImageView
|
|
|
|
}()
|
|
|
|
var dropdownIconView: UIView = {
|
|
let imageHolderView = UIView()
|
|
imageHolderView.heightAnchor.constraint(equalToConstant: 24).isActive = true
|
|
imageHolderView.widthAnchor.constraint(equalToConstant: 24).isActive = true
|
|
imageHolderView.backgroundColor = .red
|
|
return imageHolderView
|
|
|
|
}()
|
|
|
|
private var textfield: CurrencyTextField = {
|
|
let textfield = CurrencyTextField()
|
|
textfield.keyboardType = .numberPad
|
|
textfield.tintColor = .theme2E89FF
|
|
textfield.textColor = .init(hex: "#2E89FF").withAlphaComponent(0.87)
|
|
textfield.font = .sanfrancisco(.semibold, size: 16)
|
|
textfield.translatesAutoresizingMaskIntoConstraints = false
|
|
return textfield
|
|
}()
|
|
|
|
private var titleLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = .sanfrancisco(.semibold, size: 12)
|
|
label.textColor = .themeGray1
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
return label
|
|
}()
|
|
|
|
|
|
var currencyText: UILabel = {
|
|
let text = UILabel()
|
|
text.translatesAutoresizingMaskIntoConstraints = false
|
|
return text
|
|
}()
|
|
|
|
var flagImageView: UIImageView = {
|
|
let imageView = UIImageView()
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
imageView.heightAnchor.constraint(equalToConstant: 52).isActive = true
|
|
imageView.widthAnchor.constraint(equalToConstant: 36).isActive = true
|
|
imageView.clipsToBounds = true
|
|
imageView.contentMode = .scaleAspectFit
|
|
return imageView
|
|
}()
|
|
|
|
private var leftStackView: UIStackView!
|
|
private var rightStackView: UIStackView!
|
|
|
|
init(isReciver: Bool) {
|
|
super.init(frame: .zero)
|
|
self.isReciver = isReciver
|
|
setup()
|
|
}
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
private func setup() {
|
|
uiSetup()
|
|
propertiesSetup()
|
|
}
|
|
|
|
private func uiSetup() {
|
|
|
|
leftStackView = UIStackView(arrangedSubviews: [titleLabel, textfield])
|
|
leftStackView.translatesAutoresizingMaskIntoConstraints = false
|
|
leftStackView.axis = .vertical
|
|
leftStackView.distribution = .fill
|
|
leftStackView.spacing = 0
|
|
leftStackView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.size.width - 120).isActive = true
|
|
|
|
|
|
if (isReciver) {
|
|
|
|
let nameIconHolder = UIStackView(arrangedSubviews: [currencyText, dropdownIcon])
|
|
nameIconHolder.translatesAutoresizingMaskIntoConstraints = false
|
|
nameIconHolder.axis = .horizontal
|
|
// nameIconHolder.distribution = .fill
|
|
nameIconHolder.spacing = 4
|
|
nameIconHolder.alignment = .center
|
|
rightStackView = UIStackView(arrangedSubviews: [fourPixelSpace, flagImageView, currencyText, dropdownIcon, eightPixelSpace])
|
|
rightStackView.backgroundColor = .theme2E89FF
|
|
|
|
}
|
|
else {
|
|
rightStackView = UIStackView(arrangedSubviews: [eightPixelSpace, flagImageView, currencyText])
|
|
}
|
|
// var _imageHolderView = dropdownIconView
|
|
// dropdownIcon.center = _imageHolderView.center
|
|
// _imageHolderView.addSubview(dropdownIcon)
|
|
|
|
rightStackView.translatesAutoresizingMaskIntoConstraints = false
|
|
rightStackView.axis = .horizontal
|
|
rightStackView.distribution = .fill
|
|
rightStackView.spacing = 8
|
|
|
|
rightStackView.widthAnchor.constraint(equalToConstant: 116).isActive = true
|
|
|
|
self.addSubviews(leftStackView,
|
|
rightStackView
|
|
)
|
|
|
|
leftStackView.anchor(top: self.topAnchor,
|
|
paddingTop: isReciver ? 16 : 10,
|
|
bottom: self.bottomAnchor,
|
|
paddingBottom: -13,
|
|
left: self.leadingAnchor,
|
|
paddingLeft: 16
|
|
)
|
|
|
|
textfield.passTextFieldText = { [weak self] text in
|
|
self?.text = text
|
|
self?.passTextFieldText?(text)
|
|
}
|
|
|
|
textfield.didTextFieldEndEditing = { [weak self] _ in
|
|
self?.didTappedDoneButton?()
|
|
}
|
|
|
|
rightStackView.anchor(right: self.trailingAnchor,
|
|
paddingRight: 0
|
|
)
|
|
|
|
rightStackView.center(centerX: nil,
|
|
paddingX: 0,
|
|
centerY: self.centerYAnchor,
|
|
paddingY: 0)
|
|
|
|
rightStackView.heightAnchor.constraint(equalToConstant: 64).isActive = true
|
|
rightStackView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
|
|
// rightStackView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
|
|
rightStackView.leadingAnchor.constraint(greaterThanOrEqualTo: leftStackView.trailingAnchor, constant: 8).isActive = true
|
|
|
|
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
|
|
leftStackView.addGestureRecognizer(tap)
|
|
|
|
}
|
|
|
|
private func propertiesSetup() {
|
|
flagImageView.image = UIImage(named: "flag_japan")
|
|
currencyText.text = "JPY"
|
|
currencyText.textColor = isReciver ? .white : .themeBlack
|
|
currencyText.font = .sanfrancisco(.semibold, size: 16)
|
|
|
|
titleLabel.textColor = .themeBlack.withAlphaComponent(0.6)
|
|
titleLabel.font = .sanfrancisco(.semibold, size: 14)
|
|
|
|
|
|
if(isReciver) {
|
|
countryListTapGuesture = UITapGestureRecognizer(target: self, action: #selector(showCurrencyCountryPickerview))
|
|
rightStackView.addGestureRecognizer(countryListTapGuesture ?? UITapGestureRecognizer())
|
|
} else {
|
|
countryListTapGuesture = nil
|
|
}
|
|
}
|
|
|
|
@objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {
|
|
textfield.becomeFirstResponder()
|
|
}
|
|
|
|
@objc private func showCurrencyCountryPickerview() {
|
|
didSelectCountry?()
|
|
}
|
|
|
|
private func updateText() {
|
|
textfield.text = updatedText
|
|
}
|
|
|
|
private func updateCurrency() {
|
|
currencyText.text = currency
|
|
}
|
|
|
|
private func updateFlag() {
|
|
flagImageView.image = flag
|
|
}
|
|
|
|
}
|