Files
raspberry/iOS/Wallet/Sources/P2P/View/DealOrderDetailView.swift

182 lines
7.0 KiB
Swift

//
// OrderDetailView.swift
// Wallet
//
// Created by grigori on 04.11.2021.
// Copyright © 2021 AM. All rights reserved.
//
import Foundation
import UIKit
class OrderDetailView: UIView {
let typeImageView = UIImageView()
let typeLbl = UILabel()
let stackView = UIStackView()
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupViews()
}
private func setupViews() {
layer.masksToBounds = false
layer.cornerRadius = 16
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.10
layer.shadowRadius = 4.0
layer.shadowOffset = CGSize(width: 0, height: 0)
addSubview(typeImageView)
addSubview(typeLbl)
addSubview(stackView)
let line = UIView()
line.backgroundColor = Asset.fog.color
addSubview(line)
UIView.activate(constraints: [
typeImageView.topAnchor.constraint(equalTo: topAnchor, constant: 14),
typeImageView.leftAnchor.constraint(equalTo: leftAnchor, constant: 16),
typeLbl.topAnchor.constraint(equalTo: topAnchor, constant: 18),
typeLbl.leftAnchor.constraint(equalTo: typeImageView.rightAnchor, constant: 6),
line.heightAnchor.constraint(equalToConstant: 1),
line.topAnchor.constraint(equalTo: topAnchor, constant: 52),
line.leftAnchor.constraint(equalTo: leftAnchor, constant: 14),
line.rightAnchor.constraint(equalTo: rightAnchor, constant: -14),
stackView.topAnchor.constraint(equalTo: line.bottomAnchor, constant: 14),
stackView.leftAnchor.constraint(equalTo: leftAnchor, constant: 14),
stackView.rightAnchor.constraint(equalTo: rightAnchor, constant: -14),
stackView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -14)
])
}
func update(order: P2POrder) {
typeImageView.image = UIImage(named: "p2p-order-card-type-\(order.contentType.rawValue.lowercased())")
typeLbl.attributedText = "p2p.order.type.\(order.contentType.rawValue.lowercased()).title".localized.attributed(named: "bold_12", color: Asset.deepWater.color)
stackView.axis = .vertical
stackView.distribution = .fill
stackView.spacing = 16
let avaliable = OrderDetailStackItem()
avaliable.update(title: L10n.P2p.Dashboard.Order.volume(""), value: order.volume.decimalValue.toReadebleString(precisionMax: order.precision, symbol: order.symbol))
let rate = OrderDetailStackItem()
let rateText = L10n.P2p.Order.Rate.text(
order.rateIsReversed ? order.currency?.name ?? "" : order.symbol,
order.rate.decimalValue.toReadebleString(precisionMax: 10),
order.rateIsReversed ? order.symbol : order.currency?.name ?? ""
)
rate.update(title: L10n.P2p.Dashboard.Order.rate, value: rateText)
let limit = OrderDetailStackItem()
limit.update(title: L10n.P2p.Dashboard.Order.limit, value: order.min.decimalValue.toReadebleString(precisionMax: 2) + " - " + order.max.decimalValue.toReadebleString(precisionMax: 2) + " \(order.symbol)")
stackView.addArrangedSubview(UIStackView(
subviews: [avaliable, rate] + (!order.hasEqualAmount ? [limit] : []),
axis: .vertical, distribution: .fill, alignment: .fill, spacing: 4
))
let country = OrderDetailStackItem()
country.update(title: L10n.P2p.Dashboard.Order.country, value: order.country?.name ?? "")
let city = OrderDetailStackItem()
city.update(title: L10n.P2p.Dashboard.Order.city, value: order.city?.name ?? "")
let banks = OrderDetailStackItem()
banks.update(title: L10n.P2p.Dashboard.Order.bank, value: order.banks.map(\.name).joined(separator: ", "))
let paymentSystem = OrderDetailStackItem()
paymentSystem.update(title: L10n.P2p.Dashboard.Order.paymentSystem, value: order.paymentSystem?.name ?? "")
let token = OrderDetailStackItem()
token.update(title: L10n.P2p.Dashboard.Order.token, value: order.currency?.name ?? "")
let network = OrderDetailStackItem()
network.update(title: L10n.P2p.Dashboard.Order.netwrok, value: order.blockchain?.name ?? "")
let memo = OrderDetailStackItem()
memo.update(title: L10n.P2p.Dashboard.Order.memo, value: order.memo ?? "")
let tempStackView = UIStackView(subviews: [], axis: .vertical, distribution: .fill, alignment: .fill, spacing: 4)
switch order.contentType {
case .card:
if order.country != nil {
tempStackView.addArrangedSubview(country)
}
if !order.banks.isEmpty {
tempStackView.addArrangedSubview(banks)
}
case .url:
if order.country != nil {
tempStackView.addArrangedSubview(country)
}
case .cash:
if order.country != nil {
tempStackView.addArrangedSubview(country)
}
if order.city != nil {
tempStackView.addArrangedSubview(city)
}
case .vcard:
if order.country != nil {
tempStackView.addArrangedSubview(country)
}
case .emoney:
if order.paymentSystem != nil {
tempStackView.addArrangedSubview(paymentSystem)
}
case .crypto:
if order.currency != nil {
tempStackView.addArrangedSubview(token)
}
if order.blockchain != nil {
tempStackView.addArrangedSubview(network)
}
}
if !tempStackView.arrangedSubviews.isEmpty {
stackView.addArrangedSubview(tempStackView)
}
if !(order.memo ?? "").isEmpty {
stackView.addArrangedSubview(memo)
}
}
}
class OrderDetailStackItem: UIView {
let titleView = UILabel()
let valueView = UILabel()
func update(title: String, value: String) {
titleView.numberOfLines = 0
titleView.textAlignment = .left
valueView.numberOfLines = 0
valueView.textAlignment = .left
titleView.attributedText = title.attributed(named: "regular_14", color: Asset.textGranite.color)
valueView.attributedText = value.attributed(named: "bold_14", color: Asset.textCoal.color)
titleView.addWidthConstraint(width: 72)
let stackView = UIStackView(subviews: [titleView, valueView], axis: .horizontal, distribution: .fill, alignment: .firstBaseline, spacing: 12)
self.addSubview(stackView)
UIView.activate(constraints: [
stackView.topAnchor.constraint(equalTo: topAnchor),
stackView.leftAnchor.constraint(equalTo: leftAnchor),
stackView.rightAnchor.constraint(equalTo: rightAnchor),
stackView.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor)
])
}
}