282 lines
12 KiB
Swift
282 lines
12 KiB
Swift
//
|
|
// P2PCellOrder.swift
|
|
// PayCash
|
|
//
|
|
// Created by Igor on 20.12.2020.
|
|
// Copyright © 2020 AM. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class P2PCellOrder: UITableViewCell {
|
|
|
|
@IBOutlet private weak var topImageView: UIImageView!
|
|
@IBOutlet private weak var typeMineLbl: UILabel!
|
|
@IBOutlet private weak var ownerTextLbl: UILabel!
|
|
@IBOutlet private weak var ratingLbl: UILabel!
|
|
|
|
@IBOutlet private weak var dateLbl: UILabel!
|
|
@IBOutlet private weak var idLbl: UILabel!
|
|
|
|
@IBOutlet private weak var pausedLbl: UILabel!
|
|
|
|
@IBOutlet private weak var typeImageView: UIImageView!
|
|
@IBOutlet private weak var typeLbl: UILabel!
|
|
|
|
@IBOutlet private weak var volumeTitleLbl: UILabel!
|
|
@IBOutlet private weak var volumeLbl: UILabel!
|
|
|
|
@IBOutlet private weak var limitLbl: UILabel!
|
|
@IBOutlet private weak var rateLbl: UILabel!
|
|
|
|
@IBOutlet var dataStackView: UIStackView!
|
|
|
|
@IBOutlet private weak var buttonsStackView: UIStackView!
|
|
|
|
weak var tableView: UITableView?
|
|
|
|
|
|
|
|
@IBOutlet private weak var ownerSectionStackView: UIStackView!
|
|
|
|
@IBOutlet private weak var countryLbl: UILabel!
|
|
@IBOutlet private weak var bankLbl: UILabel!
|
|
|
|
|
|
@IBOutlet private var topSectionButton: UIButton!
|
|
|
|
enum Action: String {
|
|
case buy
|
|
case sell
|
|
case cancel
|
|
case pause
|
|
case resume
|
|
case chat
|
|
case more
|
|
var title: String { "p2p.dashboard.order.action.\(rawValue)".localized }
|
|
}
|
|
|
|
override func awakeFromNib() {
|
|
super.awakeFromNib()
|
|
}
|
|
|
|
var completionTopSection: (() -> Void)?
|
|
|
|
override func prepareForReuse() {
|
|
super.prepareForReuse()
|
|
|
|
pausedLbl.superview?.superview?.isHidden = true
|
|
|
|
countryLbl.superview?.isHidden = false
|
|
bankLbl.superview?.isHidden = false
|
|
}
|
|
|
|
func setup(model: P2POrder, lockedFunds: Bool = false, completion: @escaping (Action) -> Void) {
|
|
let typeOrderCardImage = UIImage(named: "p2p-order-card-type-\(model.contentType.rawValue.lowercased())")
|
|
|
|
topImageView.image = model.owner == Accounts().current?.username
|
|
? typeOrderCardImage : Asset.p2pUser.image
|
|
typeMineLbl.lzText = "p2p.order.type.\(model.contentType.rawValue.lowercased()).title".localized
|
|
ownerTextLbl.lzText = model.owner
|
|
if let requests = model.rating?.requests {
|
|
let rating = ((model.rating?.rating ?? 0) * 100.0).toNormalizedString(precisionMax: 0)
|
|
ratingLbl.lzText = L10n.P2p.Dashboard.Order.rating("\(requests)", "\(rating)")
|
|
} else {
|
|
ratingLbl.lzText = " "
|
|
}
|
|
|
|
dateLbl.lzText = model.createdAt.string(date: .medium, time: .short)
|
|
idLbl.lzText = "# \(model.id)"
|
|
|
|
pausedLbl.superview?.superview?.isHidden = model.status == .active
|
|
|
|
typeImageView.image = typeOrderCardImage
|
|
typeLbl.lzText = "p2p.order.type.\(model.contentType.rawValue.lowercased()).title".localized //
|
|
|
|
typeMineLbl.isHidden = model.owner != Accounts().current?.username
|
|
ownerTextLbl.isHidden = model.owner == Accounts().current?.username
|
|
ratingLbl.isHidden = model.owner == Accounts().current?.username
|
|
typeLbl.superview?.superview?.isHidden = model.owner == Accounts().current?.username
|
|
|
|
volumeTitleLbl.lzText = L10n.P2p.Dashboard.Order.volume(model.symbol)
|
|
volumeLbl.lzText = model.volume.decimalValue.toNormalizedString(precisionMax: 5, symbol: model.symbol)
|
|
limitLbl.lzText = "\(model.min.decimalValue.toNormalizedString(precisionMax: 5)) - \(model.max.decimalValue.toNormalizedString(precisionMax: 5)) \(model.symbol)"
|
|
rateLbl.lzText = L10n.P2p.Order.Rate.text(
|
|
model.rateIsReversed ? model.currency?.name ?? "" : model.symbol,
|
|
model.rate.decimalValue.toNormalizedString(precisionMax: 10),
|
|
model.rateIsReversed ? model.symbol : model.currency?.name ?? ""
|
|
)
|
|
|
|
limitLbl.superview?.isHidden = model.hasEqualAmount
|
|
|
|
setupData(model: model, lockedFunds: lockedFunds)
|
|
|
|
// typeLbl.attributedText = ((
|
|
// model.kind == .sell
|
|
// ? (model.contentType == .card ? L10n.P2p.Dashboard.Order.State.card : L10n.P2p.Dashboard.Order.State.link)
|
|
// : (model.owner == Accounts().current?.username ? L10n.P2p.Dashboard.Order.State.buy : L10n.P2p.Dashboard.Order.State.card)
|
|
// ) + (model.isActive ? "" : (" " + L10n.P2p.Dashboard.Order.paused)))
|
|
// .attributed(style: .bold, size: 14, color: model.isActive ? Asset.textDeepWater.color : Asset.textBrick.color)
|
|
|
|
countryLbl.attributedText = "\(model.countryId)".attributed(style: .bold, size: 14, color: Asset.textCoal.color)
|
|
countryLbl.superview?.isHidden = model.countryId == nil
|
|
bankLbl.attributedText = "\(model.bankIds.first)".attributed(style: .bold, size: 14, color: Asset.textCoal.color)
|
|
bankLbl.superview?.isHidden = model.bankIds.first == nil
|
|
if //let data = model.backendData, !data.isEmpty &&
|
|
model.owner == Accounts().current?.username && model.kind == .sell {
|
|
// dataStackView.isHidden = false
|
|
if model.contentType == .card {
|
|
countryLbl.superview?.isHidden = true
|
|
// dataTypeTitleLabel.text = L10n.P2p.Dashboard.Deal.card
|
|
} else {
|
|
bankLbl.superview?.isHidden = true
|
|
// dataTypeTitleLabel.text = L10n.P2p.Dashboard.Deal.link
|
|
}
|
|
// dataValue.attributedText = data.attributed(style: .bold, size: 14, color: Asset.textCoal.color)
|
|
}
|
|
|
|
topSectionButton.isHidden = !lockedFunds
|
|
// minStackView.isHidden = lockedFunds || model.hasEqualAmount
|
|
// maxStackView.isHidden = lockedFunds || model.hasEqualAmount
|
|
// rateStackView.isHidden = lockedFunds
|
|
// ownerSectionStackView.isHidden = lockedFunds
|
|
|
|
setupButtons(model: model, lockedFunds: lockedFunds, completion: completion)
|
|
}
|
|
|
|
private func createDataView(title: String, data: String) -> UIView {
|
|
let copyButton = UIButton(width: 18, height: 18)
|
|
copyButton.setImage(Asset.commonCopy.image, for: .normal)
|
|
copyButton.addAction(for: .touchUpInside) {
|
|
Alert.copy(data)
|
|
}
|
|
|
|
return UIStackView(subviews: [
|
|
.label(text: title, style: .regular, size: 14, color: Asset.textGranite.color, horizontalHuggingPriority: .required),
|
|
.label(text: data, style: .bold, size: 14, color: Asset.textCoal.color, numberOfLines: 0),
|
|
copyButton
|
|
], axis: .horizontal, distribution: .fill, alignment: .top, spacing: 16)
|
|
}
|
|
|
|
private func createInfoView(info: String) -> UIView {
|
|
let point = UIButton(width: 2, height: 12)
|
|
point.setImage(Asset.p2pOrderCardInfoPoint.image, for: .normal)
|
|
|
|
return UIStackView(subviews: [
|
|
point,
|
|
.label(text: info, style: .regular, size: 12, color: Asset.textGranite.color, numberOfLines: 0),
|
|
], axis: .horizontal, distribution: .fill, alignment: .top, spacing: 6)
|
|
}
|
|
|
|
private func setupData(model: P2POrder, lockedFunds: Bool = false) {
|
|
dataStackView.arrangedSubviews.forEach({ $0.removeFromSuperview() })
|
|
|
|
if let cardNumber = model.cardNumber, !cardNumber.isEmpty {
|
|
dataStackView.addArrangedSubview(createDataView(title: L10n.P2p.Dashboard.Order.cardNumber, data: Cards.Model.formmated(number: cardNumber)))
|
|
}
|
|
if let walletNumber = model.walletNumber, !walletNumber.isEmpty {
|
|
dataStackView.addArrangedSubview(createDataView(title: L10n.P2p.Dashboard.Order.accunt,
|
|
data: walletNumber))
|
|
}
|
|
if let walletAddress = model.walletAddress, !walletAddress.isEmpty {
|
|
dataStackView.addArrangedSubview(createDataView(title: L10n.P2p.Dashboard.Order.wallet,
|
|
data: walletAddress))
|
|
}
|
|
if let paymentUrl = model.paymentUrl, !paymentUrl.isEmpty {
|
|
let copyButton = UIButton(width: 18, height: 18)
|
|
copyButton.setImage(Asset.commonCopy.image, for: .normal)
|
|
copyButton.addAction(for: .touchUpInside) {
|
|
Alert.copy(paymentUrl)
|
|
}
|
|
|
|
let dataLabel = UIView.label(text: paymentUrl, style: .bold, size: 14, color: Asset.textCoal.color, numberOfLines: 2)
|
|
|
|
let urlView = UIStackView(subviews: [
|
|
.label(text: L10n.P2p.Dashboard.Order.bill, style: .regular, size: 14, color: Asset.textGranite.color, horizontalHuggingPriority: .required),
|
|
dataLabel,
|
|
copyButton
|
|
], axis: .horizontal, distribution: .fill, alignment: .top, spacing: 16)
|
|
|
|
if let url = URL(string: paymentUrl), UIApplication.shared.canOpenURL(url) {
|
|
dataLabel.underlineBold()
|
|
urlView.addTapGesture { [weak self] in
|
|
UIApplication.shared.open(url)
|
|
}
|
|
}
|
|
dataStackView.addArrangedSubview(urlView)
|
|
}
|
|
|
|
switch model.contentType {
|
|
case .card:
|
|
dataStackView.addArrangedSubview(createInfoView(info: "\(model.country?.name ?? "") · \(model.banks.map(\.name).joined(separator: " · "))"))
|
|
case .url:
|
|
dataStackView.addArrangedSubview(createInfoView(info: "\(model.country?.name ?? "")"))
|
|
case .cash:
|
|
dataStackView.addArrangedSubview(createInfoView(info: "\(model.country?.name ?? "") · \(model.city?.name ?? "")"))
|
|
case .vcard:
|
|
dataStackView.addArrangedSubview(createInfoView(info: "\(model.country?.name ?? "")\(model.kind == .sell ? "" : " · ")\(model.banks.map(\.name).joined(separator: " · "))"))
|
|
case .emoney:
|
|
dataStackView.addArrangedSubview(createInfoView(info: "\(model.paymentSystem?.name ?? "")"))
|
|
case .crypto:
|
|
dataStackView.addArrangedSubview(createInfoView(info: "\(model.currency?.name ?? "") · \(model.blockchain?.name ?? "")"))
|
|
}
|
|
|
|
if let memo = model.memo, !memo.isEmpty {
|
|
let point = UIButton(width: 2, height: 12)
|
|
point.setImage(Asset.p2pOrderCardInfoPoint.image, for: .normal)
|
|
let memoLabel = UIView.label(text: memo, style: .regular, size: 12, color: Asset.textDryGranite.color, numberOfLines: 2)
|
|
|
|
let stack = UIStackView(subviews: [
|
|
point,
|
|
memoLabel
|
|
], axis: .horizontal, distribution: .fill, alignment: .top, spacing: 6)
|
|
|
|
dataStackView.addArrangedSubview(stack)
|
|
|
|
if memoLabel.calculateMaxLines(width: dataStackView.frame.width) > 2 {
|
|
let arrow = UIImageView(width: 28, height: 28)
|
|
arrow.image = Asset.commonArrowDownP2pMemo.image
|
|
stack.addArrangedSubview(arrow)
|
|
stack.addTapGesture({ [weak tableView] in
|
|
arrow.image = memoLabel.numberOfLines == 2 ? Asset.commonArrowUpP2pMemo.image : Asset.commonArrowDownP2pMemo.image
|
|
memoLabel.numberOfLines = memoLabel.numberOfLines == 2 ? 0 : 2
|
|
tableView?.performBatchUpdates(nil)
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
private func setupButtons(model: P2POrder, lockedFunds: Bool = false, completion: @escaping (Action) -> Void) {
|
|
buttonsStackView.arrangedSubviews.forEach({ $0.removeFromSuperview() })
|
|
|
|
if model.owner == Accounts().current?.username {
|
|
buttonsStackView.distribution = .fill
|
|
buttonsStackView.addArrangedSubview(.button(
|
|
title: (model.status == .active ? Action.pause : Action.resume).title,
|
|
size: .small,
|
|
isEnabled: true
|
|
) { completion(model.status == .active ? .pause : .resume) })
|
|
let moreButton: UIView = .button(style: .secondary, image: Asset.p2pOrderCardMore.image, size: .small) {
|
|
completion(.more)
|
|
}
|
|
moreButton.addWidthConstraint(width: 44)
|
|
buttonsStackView.addArrangedSubview(moreButton)
|
|
} else {
|
|
buttonsStackView.distribution = .fillEqually
|
|
buttonsStackView.addArrangedSubview(
|
|
.button(title: (model.kind == .buy ? Action.sell : Action.buy).title, size: .small) { completion(model.kind == .buy ? .sell : .buy)
|
|
}
|
|
)
|
|
buttonsStackView.addArrangedSubview(
|
|
.button(style: .secondary, title: Action.chat.title, size: .small) {
|
|
completion(.chat)
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
@IBAction func onTopSection(_: AnyObject?) {
|
|
completionTopSection?()
|
|
}
|
|
}
|