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

157 lines
6.3 KiB
Swift

//
// CCCPControllerPriority.swift
// PayCash
//
// Created by Saveliy Stavitsky on 9/19/20.
// Copyright © 2020 List. All rights reserved.
//
import UIKit
class CCCPControllerPriority: UIViewController {
var service: CCCP.Service.Priority!
@IBOutlet private weak var tableView: UITableView!
var cashTokens: [Network.Model.Token] { Wallet.Service.Tokens.shared.cashTokens }
var balances: [Network.Model.Token] { Wallet.Service.Tokens.shared.balances.collection.filter(\.isCertificate) }
override func viewDidLoad() {
super.viewDidLoad()
title = L10n.Cccp.Priority.title
navigationItem.leftBarButtonItem = .pop(self)
navigationItem.rightBarButtonItem = .info {
Alert.hint(text: L10n.Cccp.Priority.popupDescription)
}
tableView.tableFooterView = UIView(width: tableView.frame.size.width, height: 0.5, color: UIColor(hex: "C5C5C7")!)
observers.append(contentsOf: [
Notification.subscribe(name: .didChangeAccount) { _ in Loader.show() },
Notification.subscribe(name: .didUpdateBalances) { [weak self] _ in self?.setupView() }
])
setupView()
}
var observers: [AnyObject] = []
deinit { observers.forEach({ Notification.unsubscribe(observer: $0) }) }
func setupView() {
tableView.reloadData()
Loader.hide()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
Accounts().isLocked = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Accounts().isLocked = false
}
}
extension CCCPControllerPriority: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
cashTokens.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(WalletCellToken.self, indexPath: indexPath, identifier: "cell")
let username = Accounts().current?.username ?? ""
let token = cashTokens[indexPath.row]
if service.allPriority[token.symbol] ?? false {
cell.adjustPriority(to: token, present: true)
cell.didTapAction = { [weak self] in
guard let self = self else { return }
AccountViewAuthorize.showGetPrivateKey(in: self) {
Loader.show(in: self.view)
Network.Service.Blockchain.execute(contract: .cash, action: .cashUnSetprtgrnt, data: ["user": username, "token": "\(token.symbol)"], privateKeys: [$0]) { result in
Loader.hide(in: self.view)
switch result {
case .success:
self.service.allPriority[token.symbol] = false
self.tableView.reloadData()
Alert.success()
case let .failure(error):
Alert.error(text: "\(error)")
}
}
}
}
} else {
if balances.contains(where: { String($0.symbol[$0.symbol.index($0.symbol.startIndex, offsetBy: 1)]) == token.certName }) {
cell.adjustPriority(to: token, present: false)
cell.didTapAction = { [weak self] in
guard let self = self else { return }
AccountViewAuthorize.showGetPrivateKey(in: self) {
Loader.show(in: self.view)
Network.Service.Blockchain.execute(contract: .cash, action: .cashSetprtgrnt, data: ["user": username, "token": "\(token.symbol)"], privateKeys: [$0]) { result in
Loader.hide(in: self.view)
switch result {
case .success:
self.service.allPriority[token.symbol] = true
self.tableView.reloadData()
Alert.success()
case let .failure(error):
Alert.error(text: "\(error)")
}
}
}
}
} else {
cell.adjustPriority(to: token, present: nil)
}
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let username = Accounts().current?.username ?? ""
let token = cashTokens[indexPath.row]
if service.allPriority[token.symbol] ?? false {
AccountViewAuthorize.showGetPrivateKey(in: self) {
Loader.show(in: self.view)
Network.Service.Blockchain.execute(contract: .cash, action: .cashUnSetprtgrnt, data: ["user": username, "token": "\(token.symbol)"], privateKeys: [$0]) { result in
Loader.hide(in: self.view)
switch result {
case .success:
self.service.allPriority[token.symbol] = false
self.tableView.reloadData()
Alert.success()
case let .failure(error):
Alert.error(text: "\(error)")
}
}
}
} else {
if balances.contains(where: { String($0.symbol[$0.symbol.index($0.symbol.startIndex, offsetBy: 1)]) == token.certName && $0.amount > 0 }) {
AccountViewAuthorize.showGetPrivateKey(in: self) {
Loader.show(in: self.view)
Network.Service.Blockchain.execute(contract: .cash, action: .cashSetprtgrnt, data: ["user": username, "token": "\(token.symbol)"], privateKeys: [$0]) { result in
Loader.hide(in: self.view)
switch result {
case .success:
self.service.allPriority[token.symbol] = true
self.tableView.reloadData()
Alert.success()
case let .failure(error):
Alert.error(text: "\(error)")
}
}
}
}
}
}
}