Files
raspberry/iOS/Wallet/Sources/Resources/Controller/Setup/ResourcesControllerSetup.swift
2022-05-27 20:21:01 +03:00

136 lines
5.7 KiB
Swift

//
// ResourcesController.swift
// PayCash
//
// Created by Igor Danich on 13.08.2020.
// Copyright © 2020 List. All rights reserved.
//
import UIKit
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!
var service: Resources.Service.Popup.Statistics?
private var account: String? { Accounts().current?.username }
override func viewDidLoad() {
super.viewDidLoad()
service = .init(account: self.account ?? "")
title = L10n.Resources.Setup.title
navigationItem.leftBarButtonItem = .pop(self)
switchControl.isOn = Accounts().quota.isEnabled
let string = NSMutableAttributedString(string: L10n.Resources.Setup.buyTransactions, attributes: [
.font : UIFont.font(style: .medium, size: 14),
.foregroundColor : Asset.textDeepWater.color
])
buyBtn.setAttributedTitle(string, for: .normal)
let avaliableString = NSMutableAttributedString()
let avalText = L10n.Resources.Performance.Available.kb( Accounts().quota.count)
avaliableString.append(.init(string: avalText, attributes: [
.font : UIFont.font(style: .regular, size: 14),
.foregroundColor : Asset.textPebble.color
]))
avaliableLabel.attributedText = avaliableString
loadResourcesViews()
Notification.subscribe(bag: bag, name: .didChangeAccount) { [weak self] _ in
self?.service?.fetch({ _ in
self?.ramResourcesView.clear()
self?.cpuNetResourcesView.clear()
self?.loadResourcesViews()
})
}
}
private func loadResourcesViews() {
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 = self else { return }
let vc = StoryboardScene.Resources.ram.instantiate()
vc.action = .buy
vc.performance = service
vc.navigationItem.leftBarButtonItem = .pop(self)
self.navigationController?.pushViewController(vc, animated: true)
}, rightButtonTitle: L10n.P2p.Deal.Sell.submit,
rightButtonAction: { [ weak self] in
guard let self = self else { return }
let vc = StoryboardScene.Resources.ram.instantiate()
vc.action = .sell
vc.performance = service
vc.navigationItem.leftBarButtonItem = .pop(self)
self.navigationController?.pushViewController(vc, animated: true)
})
self.cpuNetResourcesView.update(models: [service.performance[.cpu],
service.performance[.net]],
leftButtonTitle: L10n.Resources.Setup.Action.stake,
leftButtonAction: { [ weak self] in
let vc = StoryboardScene.Resources.resources.instantiate()
vc.action = .stake
self?.navigationController?.pushViewController(vc, animated: true)
}, rightButtonTitle: L10n.Resources.Setup.Action.unstake,
rightButtonAction: { [ weak self] in
let vc = StoryboardScene.Resources.resources.instantiate()
vc.action = .unstake
self?.navigationController?.pushViewController(vc, animated: true)
})
}
service.fetch()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(false, animated: true)
Accounts().isLocked = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Accounts().isLocked = false
}
@IBAction private func onBuy(_ : AnyObject?) {
let vc = StoryboardScene.Resources.quotaController.instantiate()
self.navigationController?.pushViewController(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()
}
}
}
}