179 lines
7.1 KiB
Swift
179 lines
7.1 KiB
Swift
//
|
|
// ResourcesController.swift
|
|
// Wallet
|
|
//
|
|
// Created by Igor Danich on 13.08.2020.
|
|
// Copyright © 2020 List. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Combine
|
|
import WalletFoundation
|
|
|
|
final class ResourcesControllerSetup: UIViewController {
|
|
|
|
@IBOutlet weak var avaliableLabel: UILabel!
|
|
@IBOutlet weak var buyBtn: UIButton!
|
|
@IBOutlet weak var switchControl: UISwitch!
|
|
@IBOutlet weak var ramResourcesView: ResourcesItemContainerView!
|
|
@IBOutlet weak var cpuNetResourcesView: ResourcesItemContainerView!
|
|
|
|
private var service: Resources.Service.Popup.Statistics?
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
private var account: String? { Accounts().current?.name }
|
|
|
|
var shouldHideBackButton = false
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
self.title = L10n.Resources.Setup.title
|
|
self.navigationItem.leftBarButtonItem = .pop(self)
|
|
|
|
let string = NSMutableAttributedString(string: L10n.Resources.Setup.buyTransactions, attributes: [
|
|
.font: UIFont.font(style: .medium, size: 14),
|
|
.foregroundColor: Asset.textDeepWater.color
|
|
])
|
|
self.buyBtn.setAttributedTitle(string, for: .normal)
|
|
|
|
Accounts().activePublisher
|
|
.sink { [weak self] _ in
|
|
self?.service?.fetch { _ in
|
|
self?.ramResourcesView.clear()
|
|
self?.cpuNetResourcesView.clear()
|
|
self?.loadResourcesViews()
|
|
}
|
|
}
|
|
.store(in: &self.cancellables)
|
|
}
|
|
|
|
private func updateAvailableQuota() {
|
|
let availableString = NSMutableAttributedString()
|
|
let avalText = L10n.Resources.Performance.Available.kb( Accounts().quota.count)
|
|
availableString.append(.init(string: avalText, attributes: [
|
|
.font: UIFont.font(style: .regular, size: 14),
|
|
.foregroundColor: Asset.textPebble.color
|
|
]))
|
|
|
|
self.avaliableLabel.attributedText = availableString
|
|
}
|
|
|
|
private func loadResourcesViews() {
|
|
|
|
guard let username = self.account else { return }
|
|
|
|
self.service = .init(account: username)
|
|
|
|
guard let service = service else { return }
|
|
|
|
service.didUpdate = { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.ramResourcesView.clear()
|
|
self.cpuNetResourcesView.clear()
|
|
|
|
self.ramResourcesView.update(models: [service.performance[.ram]],
|
|
leftButtonTitle: L10n.P2p.Deal.Buy.submit,
|
|
leftButtonAction: { [weak self] in
|
|
guard let self else { return }
|
|
let vc = StoryboardScene.Resources.ram.instantiate()
|
|
vc.action = .buy
|
|
vc.performance = service
|
|
self.navigationController?.pushViewController(self.hideBackButtonIfNeeded(vc: vc), animated: true)
|
|
},
|
|
rightButtonTitle: L10n.P2p.Deal.Sell.submit,
|
|
rightButtonAction: { [weak self] in
|
|
guard let self else { return }
|
|
let vc = StoryboardScene.Resources.ram.instantiate()
|
|
vc.action = .sell
|
|
vc.performance = service
|
|
self.navigationController?.pushViewController(self.hideBackButtonIfNeeded(vc: vc), animated: true)
|
|
})
|
|
|
|
self.cpuNetResourcesView.update(models: [service.performance[.cpu],
|
|
service.performance[.net]],
|
|
leftButtonTitle: L10n.Resources.Setup.Action.stake,
|
|
leftButtonAction: { [weak self] in
|
|
guard let self else { return }
|
|
let vc = StoryboardScene.Resources.resources.instantiate()
|
|
vc.action = .stake
|
|
self.navigationController?.pushViewController(self.hideBackButtonIfNeeded(vc: vc), animated: true)
|
|
},
|
|
rightButtonTitle: L10n.Resources.Setup.Action.unstake,
|
|
rightButtonAction: { [weak self] in
|
|
guard let self else { return }
|
|
let vc = StoryboardScene.Resources.resources.instantiate()
|
|
vc.action = .unstake
|
|
self.navigationController?.pushViewController(self.hideBackButtonIfNeeded(vc: vc), animated: true)
|
|
})
|
|
}
|
|
service.fetch()
|
|
}
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
self.navigationController >>- {
|
|
$0.setNavigationBarHidden(false, animated: true)
|
|
$0.navigationBar.isTranslucent = true
|
|
$0.navigationBar.backgroundColor = Asset.snow.color
|
|
}
|
|
|
|
self.hideBackButtonIfNeeded()
|
|
self.switchControl.isOn = Accounts().quota.isEnabled
|
|
self.updateAvailableQuota()
|
|
|
|
Accounts().isLocked = true
|
|
self.loadResourcesViews()
|
|
}
|
|
|
|
override func viewWillDisappear(_ animated: Bool) {
|
|
|
|
self.cancellables.removeAll()
|
|
Accounts().isLocked = false
|
|
|
|
super.viewWillDisappear(animated)
|
|
}
|
|
|
|
private func hideBackButtonIfNeeded() {
|
|
if self.shouldHideBackButton {
|
|
self.navigationItem.leftBarButtonItem = nil
|
|
self.navigationItem.setHidesBackButton(true, animated: false)
|
|
}
|
|
}
|
|
|
|
private func hideBackButtonIfNeeded(vc: UIViewController) -> UIViewController {
|
|
if !self.shouldHideBackButton {
|
|
vc.navigationItem
|
|
.leftBarButtonItem = .pop(self)
|
|
} else {
|
|
vc.navigationItem
|
|
.hidesBackButton = true
|
|
}
|
|
return vc
|
|
}
|
|
|
|
@IBAction private func onBuy(_ : AnyObject?) {
|
|
let vc = StoryboardScene.Resources.quotaController.instantiate()
|
|
self.navigationController?.pushViewController(self.hideBackButtonIfNeeded(vc: vc), animated: true)
|
|
}
|
|
|
|
@IBAction func switchControlDidChanged(_ sender: Any) {
|
|
Accounts().quota.isEnabled = switchControl.isOn
|
|
}
|
|
|
|
}
|
|
|
|
extension UILabel {
|
|
@IBInspectable public var uppercased: Bool {
|
|
get { return false }
|
|
set(key) {
|
|
if key {
|
|
text = text?.uppercased()
|
|
}
|
|
}
|
|
}
|
|
}
|