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

108 lines
3.8 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()
self.title = L10n.Resources.Setup.Buy.transactions
tokenView.action = .cards(tokens: [])
service = .init(account: Accounts().current?.name ?? "")
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 = { [weak self] in self?.update() }
countStackView.arrangedSubviews.forEach({ $0.removeFromSuperview() })
service.collection.enumerated().forEach({ (idx,element) in
countStackView.addArrangedSubview(.button(title: "\(element)", size: .small) { [weak self] in
guard let self else { return }
self.service.count = self.service.collection[idx]
self.update()
})
})
update()
}
private func update() {
if tokenView.action?.selected == nil {
tokenView.action = .cards(tokens: service.tokens, selected: service.token) { [weak self] menu in
guard let self else { return }
self.service.token = self.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.priceStr)
.attributed(style: .regular, size: 14, color: Asset.textCoal.color)
submitBtn.isEnabled = service.token?.amount.doubleValue ?? 0.0 >= service.price?.doubleValue ?? 0.0
}
@IBAction func onSubmit(_: AnyObject?) {
submitPopup(price: service.priceStr, 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)
}
}
}
}
}