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

130 lines
4.9 KiB
Swift

//
// CCCPControllerExchangeRequests.swift
// PayCash
//
// Created by Saveliy Stavitsky on 7/30/20.
// Copyright © 2020 List. All rights reserved.
//
import UIKit
import class Resolver.Resolver
class CCCPControllerExchangeRequests: UITableViewController {
var requestsService: CCCP.Service.ExchangeRequests { Resolver.resolve() }
private lazy var emptyView: CommonViewEmpty = {
let text = type == .outgoingRequests ? L10n.Cccp.OutgoingExchangeRequests.Empty.text :
(type == .incomingRequests ? L10n.Cccp.IncomingExchangeRequests.Empty.text : "")
let info = type == .outgoingRequests ? L10n.Cccp.OutgoingExchangeRequests.Empty.info :
(type == .incomingRequests ? L10n.Cccp.IncomingExchangeRequests.Empty.info : "")
return CommonViewEmpty(
title: text,
text: info,
image: Asset.cccpRequestsEmpty.image,
submit: type == .outgoingRequests ? L10n.Cccp.OutgoingExchangeRequests.Empty.submit : nil
) { [weak self] in
self?.navigationController?.pushViewController(
StoryboardScene.Cccp.cccpControllerCreateExchangeRequest.instantiate(),
animated: true
)
}
}()
var type = CCCP.Service.ExchangeRequests.RequestsType.outgoingRequests
override func viewDidLoad() {
super.viewDidLoad()
requestsService.type = type
navigationItem.title = type == .outgoingRequests ? L10n.Cccp.OutgoingExchangeRequests.headerTitle
: (type == .incomingRequests ? L10n.Cccp.IncomingExchangeRequests.headerTitle
: L10n.Cccp.Main.soldExchangeRequestsTitle)
navigationItem.leftBarButtonItem = .pop(self)
switch type {
case .incomingRequests:
navigationItem.rightBarButtonItem = .info {
Alert.hint(text: L10n.Cccp.IncomingExchangeRequests.popupDescription)
}
case .outgoingRequests:
navigationItem.rightBarButtonItem = .info {
Alert.hint(text: L10n.Cccp.OutgoingExchangeRequests.popupDescription)
}
case .soldRequests:
navigationItem.rightBarButtonItem = .info {
Alert.hint(text: L10n.Cccp.SoldExchangeRequests.popupDescription)
}
}
tableView.showsVerticalScrollIndicator = false
tableView.separatorStyle = .none
tableView.register(cell: CCCPCellExchangeRequest.self)
refreshControl = UIRefreshControl()
refreshControl?.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged)
Notification.subscribe(name: .didUpdateCryptocashExchangeRequests) {[weak self] _ in
guard let self = self else { return }
DispatchQueue.main.async {
Loader.hide()
self.refreshControl?.endRefreshing()
self.tableView.reloadData()
}
}
observers.append(contentsOf: [
Notification.subscribe(name: .didChangeAccount) { _ in Loader.show() },
// Notification.subscribe(name: .didUpdateBalances) { [weak self] _ in self?.requestsService.fetch() }
])
// requestsService.fetch()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
Wallet.Service.Tokens.shared.balances.fetch()
}
var observers: [AnyObject] = []
deinit { observers.forEach({ Notification.unsubscribe(observer: $0) }) }
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
emptyView.frame = .init(
x: 0,
y: 0,
width: view.frameWidth,
height: view.frameHeight - (navigationController?.tabBarController?.tabBar.frameHeight ?? 0)
)
}
@objc func refresh(_ sender: AnyObject) { Wallet.Service.Tokens.shared.balances.fetch() }
@IBAction func createPressed(_ sender: Any) {
let createRequestViewController = StoryboardScene.Cccp.cccpControllerCreateExchangeRequest.instantiate()
navigationController?.pushViewController(createRequestViewController, animated: true)
}
}
extension CCCPControllerExchangeRequests {
override func numberOfSections(in tableView: UITableView) -> Int { 1 }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
type != .soldRequests && requestsService.requests.count == 0
? view.addSubview(emptyView) : emptyView.removeFromSuperview()
return requestsService.requests.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(CCCPCellExchangeRequest.self, indexPath: indexPath)
cell.tokens = Wallet.Service.Tokens.shared.balances.collection
cell.setup(request: requestsService.requests[indexPath.row])
cell.parent = self
return cell
}
}