Files
raspberry/iOS/Wallet/Sources/Resources/Controller/Setup/ResourcesQuotaViewController.swift
T

108 lines
3.7 KiB
Swift

//
// ResourcesQuotaViewController.swift
// Wallet
//
// Created by grigori on 28.09.2021.
// Copyright © 2021 AM. All rights reserved.
//
import Foundation
import UIKit
class ResourcesQuotaViewController: UIViewController {
@IBOutlet private weak var tokenView: CommonViewCard!
@IBOutlet private weak var countStackView: UIStackView!
@IBOutlet private weak var priceLbl: UILabel!
@IBOutlet private weak var submitBtn: UIButton!
var service: Resources.Service.Popup.Quota!
override func viewDidLoad() {
super.viewDidLoad()
title = L10n.Resources.Setup.Buy.transactions
navigationItem.leftBarButtonItem = .pop(self)
tokenView.action = .cards(tokens: [])
service = .init(account: Accounts().current?.username ?? "")
service.fetch { [weak self] _ in
self?.preload()
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
Accounts().isLocked = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Accounts().isLocked = false
}
private func preload() {
service.didUpdate = { [unowned self] in update() }
countStackView.arrangedSubviews.forEach({ $0.removeFromSuperview() })
service.collection.enumerated().forEach({ (idx,element) in
countStackView.addArrangedSubview(.button(title: "\(element)", size: .small) { [unowned self] in
service.count = service.collection[idx]
update()
})
})
update()
}
private func update() {
if tokenView.action?.selected == nil {
tokenView.action = .cards(tokens: service.tokens, selected: service.token) { [unowned self] menu in
service.token = service.tokens.object(menu: menu)
self.view.layoutIfNeeded()
}
}
countStackView.arrangedSubviews
.compactMap({ $0 as? CommonButtonAction })
.enumerated()
.forEach({ $0.element.style = service.collection[$0.offset] == service.count ? .primary : .secondary })
priceLbl.attributedText = L10n.Resources.Popup.Transactions.rate("\(service.count)", service.price)
.attributed(style: .regular, size: 14, color: Asset.textCoal.color)
submitBtn.isEnabled = service.price.count > 0
}
@IBAction func onSubmit(_: AnyObject?) {
submitPopup(price: service.price, trancastions: "\(service.count)")
}
private func submitPopup(price: String, trancastions: String) {
let popupTitle = L10n.Resources.Popup.Transactions.subtitle
let field11 = L10n.Wallet.Send.Amount.send
let field12 = price
let field21 = L10n.Wallet.Send.Amount.get
let field22 = L10n.Resources.Popup.Transactions.amount("\(service.count)")
var views: [UIView] = [
.label(
text: popupTitle.attributed(style: .regular, size: 14, color: Asset.textCoal.color),
numberOfLines: 0
)
]
views.append(contentsOf:
[.field(title: field11, text: field12), .field(title: field21, text: field22)]
)
Popup.show(title: L10n.P2p.Order.Confirm.title, views: views, submit: L10n.P2p.Order.Confirm.submit, in: self) { [weak self] ctrl in
guard let self = self else { return }
AccountViewAuthorize.showGetPrivateKey(in: self) {
Loader.show()
self.service.submit(privateKey: $0) { (error) in
Loader.hide()
Popup.hide()
Alert.notify(error)
}
}
}
}
}