An advanced Ethereum/EVM mobile wallet
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.
alpha-wallet-ios/AlphaWallet/UI/BalanceTitleView.swift

115 lines
3.4 KiB

// Copyright SIX DAY LLC. All rights reserved.
7 years ago
import UIKit
enum BalanceMode {
case short
case full
}
7 years ago
class BalanceTitleView: UIView {
@objc dynamic var titleTextColor: UIColor? {
get { return self.titleLabel.textColor }
set { self.titleLabel.textColor = newValue }
}
@objc dynamic var subTitleTextColor: UIColor? {
get { return self.subTitleLabel.textColor }
set { self.subTitleLabel.textColor = newValue }
}
let titleLabel = UILabel()
let subTitleLabel = UILabel()
var viewModel: BalanceBaseViewModel? {
didSet {
guard let viewModel = viewModel else { return }
configure(viewModel: viewModel)
}
}
private var mode = BalanceMode.short
7 years ago
override init(frame: CGRect) {
super.init(frame: .zero)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.textAlignment = .center
titleLabel.adjustsFontSizeToFitWidth = true
subTitleLabel.translatesAutoresizingMaskIntoConstraints = false
subTitleLabel.textAlignment = .center
subTitleLabel.adjustsFontSizeToFitWidth = true
let stackView = [
titleLabel,
subTitleLabel,
].asStackView(axis: .vertical, spacing: 2)
stackView.translatesAutoresizingMaskIntoConstraints = false
addSubview(stackView)
7 years ago
NSLayoutConstraint.activate([
stackView.trailingAnchor.constraint(equalTo: trailingAnchor),
stackView.leadingAnchor.constraint(equalTo: leadingAnchor),
stackView.topAnchor.constraint(equalTo: topAnchor),
stackView.bottomAnchor.constraint(equalTo: bottomAnchor),
7 years ago
])
stackView.addGestureRecognizer(
UITapGestureRecognizer(target: self, action: #selector(self.switchModel))
)
7 years ago
}
private func configure(viewModel: BalanceBaseViewModel) {
titleLabel.font = Fonts.semibold(size: 18)
subTitleLabel.font = Fonts.regular(size: 14)
let amount: String
switch mode {
case .full:
amount = viewModel.amountFull
case .short:
amount = viewModel.amountShort
}
titleLabel.text = "\(amount) \(viewModel.symbol)"
subTitleLabel.text = viewModel.currencyAmount
}
@objc func switchModel() {
switch mode {
case .full:
mode = .short
case .short:
mode = .full
}
guard let viewModel = viewModel else { return }
configure(viewModel: viewModel)
}
7 years ago
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension BalanceTitleView {
static func make(from session: WalletSession, _ transferType: TransferType) -> BalanceTitleView {
let view = BalanceTitleView(frame: .zero)
view.translatesAutoresizingMaskIntoConstraints = false
switch transferType {
case .ether:
session.balanceViewModel.subscribe { [weak view] viewModel in
guard let viewModel = viewModel else { return }
view?.viewModel = viewModel
}
case .token(let token):
view.viewModel = BalanceTokenViewModel(token: token)
case .stormBird(let token):
view.viewModel = BalanceTokenViewModel(token: token)
case .stormBirdOrder: break
}
session.refresh(.ethBalance)
return view
}
}