Files

234 lines
8.4 KiB
Swift

//
// MainIssueUsdcashViewController.swift
// PayCash
//
// Created by Saveliy Stavitsky on 9/2/22.
// Copyright © 2022 AM. All rights reserved.
//
import UIKit
import Resolver
import SwiftUI
struct Deposit: Codable {
let id: Int
let owner: String
let mlnk_in: String // swiftlint:disable:this identifier_name
let usdt_in: String // swiftlint:disable:this identifier_name
let token_out: String // swiftlint:disable:this identifier_name
let creation_date: String // swiftlint:disable:this identifier_name
var creationDate: Date {
creation_date.serverDate() ?? Date()
}
var creationDateString: String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .medium
return dateFormatter.string(from: creationDate)
}
}
struct HeaderView: View {
var body: some View {
HStack {
Text("MainCryptocashView.header.title.issueByMe")
.font(.custom(FontFamily.GolosUI.bold.name, size: 20))
.foregroundColor(Color(Asset.textCoal.name))
.padding(.horizontal, 12)
.padding(.bottom, 16)
.padding(.top, 8)
.background(Color(Asset.snow.name))
Spacer()
}
}
}
struct DepositView: View {
var deposit: Deposit
var action: (Deposit) -> Void
var body: some View {
VStack(alignment: .leading, spacing: 12) {
HStack(alignment: .top, spacing: 12) {
Image(Asset.tokenUsdcash.name).resizable().frame(width: 40, height: 40)
VStack(alignment: .leading, spacing: 4) {
VStack(alignment: .leading, spacing: 2) {
Text("USDCASH")
.font(.custom(FontFamily.GolosUI.medium.name, size: 14))
.foregroundColor(Color(Asset.textCoal.name))
Text(deposit.creationDateString)
.font(.custom(FontFamily.GolosUI.regular.name, size: 12))
.foregroundColor(Color(Asset.textGranite.name))
}
Text(deposit.usdt_in.amount.toDecimal().toReadebleString(precisionMax: 5, group: true))
.font(.custom(FontFamily.GolosUI.bold.name, size: 20))
.foregroundColor(Color(Asset.textCoal.name))
}
}
Divider()
HStack(alignment: .top, spacing: 6) {
Text("MainCryptocashView.header.pledge")
.font(.custom(FontFamily.GolosUI.regular.name, size: 10))
.foregroundColor(Color(Asset.textDryGranite.name))
VStack(alignment: .leading, spacing: 0) {
Text(deposit.usdt_in.amount.toDecimal().toReadebleString(precisionMax: 4, symbol: "USDT", group: true))
Text(deposit.mlnk_in.amount.toDecimal().toReadebleString(precisionMax: 8, symbol: "MLNK", group: true))
}
.font(.custom(FontFamily.GolosUI.medium.name, size: 12))
.foregroundColor(Color(Asset.textDryGranite.name))
Spacer()
Button {
action(deposit)
} label: {
Text("MainCryptocashView.card.button.return")
.font(.custom(FontFamily.GolosUI.medium.name, size: 14))
.foregroundColor(Color(Asset.textSnow.name))
.padding(.horizontal, 12)
.padding(.vertical, 5.5)
.background(Color(Asset.deepWater.name).cornerRadius(10))
}
}
}
.padding(.horizontal, 12)
.padding(.vertical, 8)
.background(
Color(Asset.snow.name)
.cornerRadius(16)
.shadow(color: Color(.sRGB, red: 0.09, green: 0.27, blue: 0.47, opacity: 0.08), radius: 2, x: 0, y: 0)
.shadow(color: Color(.sRGB, red: 0.09, green: 0.27, blue: 0.47, opacity: 0.08), radius: 12, x: 0, y: 2)
)
.padding(.horizontal, 12)
.padding(.vertical, 6)
.background(Color(Asset.snow.name))
}
}
final class MainIssueUsdcashViewController: UIViewController {
@IBOutlet private weak var mainStackView: UIStackView!
@IBOutlet private weak var requestsScrollView: UIScrollView!
@IBOutlet private weak var requestsStackView: UIStackView!
private var deposits: [Deposit] = [] {
didSet {
updateView()
}
}
private lazy var requestsEmptyView: CommonViewEmpty = {
CommonViewEmpty(
title: L10n.MainCryptocashView.Empty.title,
text: L10n.MainCryptocashView.Empty.description,
image: Asset.finance.image)
}()
private let refresh = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = L10n.MainCryptocashView.title
navigationItem.leftBarButtonItem = .pop(self)
refresh.addTarget(self, action: #selector(refreshViewApearance), for: .valueChanged)
requestsScrollView.addSubview(refresh)
requestsScrollView.showsVerticalScrollIndicator = false
setupRequestsEmptyView()
updateView()
refreshViewApearance()
}
private func setupRequestsEmptyView() {
requestsEmptyView.frame = CGRect(x: 0, y: 50, width: view.frame.width,
height: view.frame.height - 50)
requestsEmptyView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
requestsEmptyView.translatesAutoresizingMaskIntoConstraints = true
requestsEmptyView.isUserInteractionEnabled = false
view.addSubview(requestsEmptyView)
}
@objc func refreshViewApearance() {
guard let username = Accounts().current?.name else { return }
Network.table.fetch(
code: ApplicationEnvironment.shared().current.contract(.cash),
table: "deposits",
scope: ApplicationEnvironment.shared().current.contract(.cash),
keyType: "i64",
indexPosition: "3",
lowerBound: username,
upperBound: username,
type: Deposit.self
) { [weak self] in
self?.deposits = $0
}
}
@IBAction func onCreate(_: AnyObject?) {
navigationController?.pushViewController(IssueCryptocashViewController.loadFromNib(), animated: true)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
Accounts().isLocked = true
if let window = UIApplication.shared.windows.first,
let controller = window.rootViewController as? MainController,
(controller.barNav.items?.count ?? 0) > 1 {
controller.barNav.popItem(animated: true)
}
self.refreshViewApearance()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Accounts().isLocked = false
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
private func updateView() {
refresh.endRefreshing()
requestsEmptyView.isHidden = deposits.count > 0
mainStackView.isHidden = deposits.count == 0
if deposits.count > 0 {
mainStackView.arrangedSubviews.forEach({ $0.isHidden = false })
mainStackView.arrangedSubviews.filter({ $0 is UIHostingView<HeaderView> }).forEach({ $0.removeFromSuperview() })
requestsStackView.arrangedSubviews.forEach({ $0.removeFromSuperview() })
mainStackView.insertArrangedSubview(UIHostingView(rootView: HeaderView()), at: 0)
for deposit in deposits {
let depositView = UIHostingView(rootView: DepositView(deposit: deposit, action: { [weak self] in
let returnCryptocashViewController = ReturnCryptocashViewController.loadFromNib()
returnCryptocashViewController.deposit = $0
self?.navigationController?.pushViewController(returnCryptocashViewController, animated: true)
}))
requestsStackView.addArrangedSubview(depositView)
}
}
}
}