Ditch TransactionHeaderBaseViewModel

pull/2/head
Michael Scoff 7 years ago
parent 891a4f58dc
commit a3442af24e
  1. 12
      Trust.xcodeproj/project.pbxproj
  2. 8
      Trust/Exchange/Coordinators/ExchangeCoordinator.swift
  3. 11
      Trust/Transactions/ViewControllers/TransactionViewController.swift
  4. 1
      Trust/Transactions/ViewControllers/TransactionsViewController.swift
  5. 11
      Trust/Transactions/ViewModels/TransactionCellViewModel.swift
  6. 58
      Trust/Transactions/ViewModels/TransactionViewModel.swift
  7. 8
      Trust/Transfer/Coordinators/SendCoordinator.swift
  8. 14
      Trust/Transfer/ViewControllers/ConfirmPaymentViewController.swift
  9. 79
      Trust/Transfer/ViewModels/ConfirmPaymentViewModel.swift
  10. 98
      Trust/Transfer/ViewModels/ConfirmTransactionHeaderViewModel.swift
  11. 7
      Trust/Transfer/ViewModels/TransactionHeaderBaseViewModel.swift
  12. 52
      Trust/Transfer/ViewModels/TransactionHeaderViewModel.swift
  13. 7
      Trust/UI/Form/TransactionAppearance.swift

@ -127,7 +127,6 @@
2961BD091FB14B6D00C4B840 /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2961BD081FB14B6D00C4B840 /* Config.swift */; };
2963A2881FC401490095447D /* LocalizedOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2963A2871FC401490095447D /* LocalizedOperation.swift */; };
2963A28A1FC402940095447D /* LocalizedOperationObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2963A2891FC402940095447D /* LocalizedOperationObject.swift */; };
2963B6AB1F9818C7003063C1 /* TransactionHeaderViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2963B6AA1F9818C7003063C1 /* TransactionHeaderViewModel.swift */; };
2963B6AD1F981A96003063C1 /* TransactionAppearance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2963B6AC1F981A96003063C1 /* TransactionAppearance.swift */; };
2963B6AF1F9823E6003063C1 /* UnconfirmedTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2963B6AE1F9823E6003063C1 /* UnconfirmedTransaction.swift */; };
2963B6B11F9891F5003063C1 /* UIButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2963B6B01F9891F5003063C1 /* UIButton.swift */; };
@ -210,8 +209,6 @@
29C80D351FB2CD110037B1E0 /* Block.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C80D341FB2CD110037B1E0 /* Block.swift */; };
29C80D371FB2CD230037B1E0 /* ParsedTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C80D361FB2CD230037B1E0 /* ParsedTransaction.swift */; };
29C80D411FB2E1610037B1E0 /* EthplorerService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C80D401FB2E1610037B1E0 /* EthplorerService.swift */; };
29C80D451FB51B1A0037B1E0 /* TransactionHeaderBaseViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C80D441FB51B1A0037B1E0 /* TransactionHeaderBaseViewModel.swift */; };
29C80D471FB51B680037B1E0 /* ConfirmTransactionHeaderViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C80D461FB51B680037B1E0 /* ConfirmTransactionHeaderViewModel.swift */; };
29C80D491FB51C380037B1E0 /* Dictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C80D481FB51C380037B1E0 /* Dictionary.swift */; };
29C80D4B1FB51C460037B1E0 /* Decimal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C80D4A1FB51C460037B1E0 /* Decimal.swift */; };
29C80D4D1FB5202C0037B1E0 /* BalanceBaseViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C80D4C1FB5202C0037B1E0 /* BalanceBaseViewModel.swift */; };
@ -420,7 +417,6 @@
2961BD081FB14B6D00C4B840 /* Config.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; };
2963A2871FC401490095447D /* LocalizedOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalizedOperation.swift; sourceTree = "<group>"; };
2963A2891FC402940095447D /* LocalizedOperationObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalizedOperationObject.swift; sourceTree = "<group>"; };
2963B6AA1F9818C7003063C1 /* TransactionHeaderViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionHeaderViewModel.swift; sourceTree = "<group>"; };
2963B6AC1F981A96003063C1 /* TransactionAppearance.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionAppearance.swift; sourceTree = "<group>"; };
2963B6AE1F9823E6003063C1 /* UnconfirmedTransaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnconfirmedTransaction.swift; sourceTree = "<group>"; };
2963B6B01F9891F5003063C1 /* UIButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIButton.swift; sourceTree = "<group>"; };
@ -505,8 +501,6 @@
29C80D341FB2CD110037B1E0 /* Block.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Block.swift; sourceTree = "<group>"; };
29C80D361FB2CD230037B1E0 /* ParsedTransaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParsedTransaction.swift; sourceTree = "<group>"; };
29C80D401FB2E1610037B1E0 /* EthplorerService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EthplorerService.swift; sourceTree = "<group>"; };
29C80D441FB51B1A0037B1E0 /* TransactionHeaderBaseViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionHeaderBaseViewModel.swift; sourceTree = "<group>"; };
29C80D461FB51B680037B1E0 /* ConfirmTransactionHeaderViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfirmTransactionHeaderViewModel.swift; sourceTree = "<group>"; };
29C80D481FB51C380037B1E0 /* Dictionary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dictionary.swift; sourceTree = "<group>"; };
29C80D4A1FB51C460037B1E0 /* Decimal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Decimal.swift; sourceTree = "<group>"; };
29C80D4C1FB5202C0037B1E0 /* BalanceBaseViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BalanceBaseViewModel.swift; sourceTree = "<group>"; };
@ -1434,9 +1428,6 @@
children = (
29CA4B761F6FBBFB0032313D /* RequestViewModel.swift */,
29BB94961F6FCD60009B09CC /* SendViewModel.swift */,
29C80D441FB51B1A0037B1E0 /* TransactionHeaderBaseViewModel.swift */,
2963B6AA1F9818C7003063C1 /* TransactionHeaderViewModel.swift */,
29C80D461FB51B680037B1E0 /* ConfirmTransactionHeaderViewModel.swift */,
299B5E331FCBC5180051361C /* ConfirmPaymentViewModel.swift */,
299B5E441FD22FB40051361C /* ConfigureTransactionViewModel.swift */,
);
@ -2133,7 +2124,6 @@
293B8B431F70815900356286 /* BalanceTitleView.swift in Sources */,
29DBF2A11F9DA6EF00327C60 /* BackupViewController.swift in Sources */,
29FC9BC61F830899000209CD /* MirgrationInitializer.swift in Sources */,
2963B6AB1F9818C7003063C1 /* TransactionHeaderViewModel.swift in Sources */,
29AD8A091F93F8B2008E10E7 /* Session.swift in Sources */,
294DFB981FDF8CBB004CEB56 /* CrashReportInitializer.swift in Sources */,
2985430A1FC188A700CB5081 /* ExchangeTokenInputField.swift in Sources */,
@ -2165,7 +2155,6 @@
296106C21F76403A0006164B /* TokenViewCell.swift in Sources */,
29F114F01FA6D53700114A29 /* ImportSelectionType.swift in Sources */,
29850D251F6B27A800791A49 /* R.generated.swift in Sources */,
29C80D451FB51B1A0037B1E0 /* TransactionHeaderBaseViewModel.swift in Sources */,
296105931FA2AA2100292494 /* SignTransaction.swift in Sources */,
296105951FA2DEF000292494 /* TransactionDirection.swift in Sources */,
291ED08B1F6F5D2100E7E93A /* Bundle.swift in Sources */,
@ -2224,7 +2213,6 @@
29CA4B791F6FBFD50032313D /* Balance.swift in Sources */,
291F52A71F6B766100B369AB /* BalanceRequest.swift in Sources */,
2932488A1F88D593008A9818 /* OnePasswordConfig.swift in Sources */,
29C80D471FB51B680037B1E0 /* ConfirmTransactionHeaderViewModel.swift in Sources */,
29D72A2A1F6A8D1500CE9209 /* AppCoordinator.swift in Sources */,
290B2B671F9266630053C83E /* SettingsAction.swift in Sources */,
293E626F1FA2ED1400CB0A66 /* InCoordinator.swift in Sources */,

@ -44,17 +44,11 @@ extension ExchangeCoordinator: ExchangeViewControllerDelegate {
address: to.token.address // TODO FIX IT
)
let viewModel = ConfirmTransactionHeaderViewModel(
transaction: transaction,
config: Config()
)
let controller = ConfirmPaymentViewController(
session: session,
keystore: keystore,
transaction: transaction,
gasPrice: .none,
headerViewModel: viewModel
gasPrice: .none
)
controller.delegate = self
navigationController.pushViewController(controller, animated: true)

@ -35,14 +35,13 @@ class TransactionViewController: UIViewController {
title = viewModel.title
view.backgroundColor = viewModel.backgroundColor
let header = TransactionHeaderView()
header.translatesAutoresizingMaskIntoConstraints = false
header.amountLabel.attributedText = viewModel.amountAttributedString
let items: [UIView] = [
.spacer(),
TransactionAppearance.header(
viewModel: TransactionHeaderViewModel(
value: viewModel.value,
direction: transaction.direction
)
),
header,
TransactionAppearance.divider(color: Colors.lightGray, alpha: 0.3),
item(title: "From", value: viewModel.from),
item(title: "To", value: viewModel.to),

@ -177,6 +177,7 @@ extension TransactionsViewController: UITableViewDataSource {
let cell = TransactionViewCell(style: .default, reuseIdentifier: TransactionViewCell.identifier)
cell.configure(viewModel: .init(
transaction: transaction,
config: session.config,
chainState: session.chainState
)
)

@ -7,14 +7,25 @@ import UIKit
struct TransactionCellViewModel {
let transaction: Transaction
let config: Config
let chainState: ChainState
let shortFormatter = EtherNumberFormatter.short
lazy var transactionViewModel: TransactionViewModel = {
return TransactionViewModel(
transaction: transaction,
config: config,
chainState: chainState
)
}()
init(
transaction: Transaction,
config: Config,
chainState: ChainState
) {
self.transaction = transaction
self.config = config
self.chainState = chainState
}

@ -44,16 +44,7 @@ struct TransactionViewModel {
}
var value: TransactionValue {
if let operation = transaction.operation, let symbol = operation.symbol {
return TransactionValue(
amount: shortFormatter.string(from: BigInt(operation.value) ?? BigInt(), decimals: operation.decimals),
symbol: symbol
)
}
return TransactionValue(
amount: fullFormatter.string(from: BigInt(transaction.value) ?? BigInt()),
symbol: config.server.symbol
)
return transactionValue(for: shortFormatter)
}
var createdAt: String {
@ -95,4 +86,51 @@ struct TransactionViewModel {
var blockNumber: String {
return String(transaction.blockNumber)
}
var amountTextColor: UIColor {
switch transaction.direction {
case .incoming: return Colors.green
case .outgoing: return Colors.red
}
}
var valueString: String {
guard value.amount != "0" else { return value.amount }
switch transaction.direction {
case .incoming: return "+\(value.amount)"
case .outgoing: return "-\(value.amount)"
}
}
var amountAttributedString: NSAttributedString {
let amount = NSAttributedString(
string: valueString,
attributes: [
.font: UIFont.systemFont(ofSize: 24),
.foregroundColor: amountTextColor,
]
)
let currency = NSAttributedString(
string: " " + value.symbol,
attributes: [
.font: UIFont.systemFont(ofSize: 14),
]
)
return amount + currency
}
private func transactionValue(for formatter: EtherNumberFormatter) -> TransactionValue {
if let operation = transaction.operation, let symbol = operation.symbol {
return TransactionValue(
amount: formatter.string(from: BigInt(operation.value) ?? BigInt(), decimals: operation.decimals),
symbol: symbol
)
}
return TransactionValue(
amount: fullFormatter.string(from: BigInt(transaction.value) ?? BigInt()),
symbol: config.server.symbol
)
}
}

@ -68,17 +68,11 @@ class SendCoordinator: Coordinator {
extension SendCoordinator: SendViewControllerDelegate {
func didPressConfirm(transaction: UnconfirmedTransaction, transferType: TransferType, gasPrice: BigInt?, in viewController: SendViewController) {
let viewModel = ConfirmTransactionHeaderViewModel(
transaction: transaction,
config: session.config
)
let controller = ConfirmPaymentViewController(
session: session,
keystore: keystore,
transaction: transaction,
gasPrice: gasPrice,
headerViewModel: viewModel
gasPrice: gasPrice
)
controller.delegate = self
navigationController.pushViewController(controller, animated: true)

@ -42,8 +42,6 @@ class ConfirmPaymentViewController: UIViewController {
)
}
var headerViewModel: TransactionHeaderBaseViewModel
private lazy var transactionSpeed: TransactionSpeed = {
return .regular
}()
@ -80,15 +78,13 @@ class ConfirmPaymentViewController: UIViewController {
session: WalletSession,
keystore: Keystore,
transaction: UnconfirmedTransaction,
gasPrice: BigInt?,
headerViewModel: TransactionHeaderBaseViewModel
gasPrice: BigInt?
) {
self.session = session
self.keystore = keystore
self.transaction = transaction
self.configuration = TransactionConfiguration()
self.gasPrice = gasPrice
self.headerViewModel = headerViewModel
super.init(nibName: nil, bundle: nil)
@ -108,11 +104,13 @@ class ConfirmPaymentViewController: UIViewController {
private func reloadView() {
stackViewController.items.forEach { stackViewController.removeItem($0) }
let header = TransactionHeaderView()
header.translatesAutoresizingMaskIntoConstraints = false
header.amountLabel.attributedText = viewModel.amountAttributedString
let items: [UIView] = [
.spacer(),
TransactionAppearance.header(
viewModel: headerViewModel
),
header,
TransactionAppearance.divider(color: Colors.lightGray, alpha: 0.3),
TransactionAppearance.item(
title: viewModel.paymentFromTitle,

@ -98,4 +98,83 @@ struct ConfirmPaymentViewModel {
var gasLimitText: String {
return gasLimit.description
}
var amountTextColor: UIColor {
return Colors.red
}
var amountAttributedString: NSAttributedString {
switch transaction.transferType {
case .token(let token):
return amountAttributedText(
string: fullFormatter.string(from: transaction.value, decimals: token.decimals)
)
case .ether:
return amountAttributedText(
string: fullFormatter.string(from: transaction.value)
)
case .exchange(let from, let to):
let fromAttributedString: NSAttributedString = {
let amount = NSAttributedString(
string: fullFormatter.string(from: from.amount),
attributes: [
.font: UIFont.systemFont(ofSize: 24),
.foregroundColor: Colors.red,
]
)
let currency = NSAttributedString(
string: " " + from.token.symbol,
attributes: [
.font: UIFont.systemFont(ofSize: 16),
]
)
return amount + currency
}()
let toAttributedString: NSAttributedString = {
let amount = NSAttributedString(
string: fullFormatter.string(from: to.amount),
attributes: [
.font: UIFont.systemFont(ofSize: 24),
.foregroundColor: Colors.green,
]
)
let currency = NSAttributedString(
string: " " + to.token.symbol,
attributes: [
.font: UIFont.systemFont(ofSize: 16),
]
)
return amount + currency
}()
let amount = NSAttributedString(
string: String(" for "),
attributes: [
.font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.light),
]
)
return fromAttributedString + amount + toAttributedString
}
}
private func amountAttributedText(string: String) -> NSAttributedString {
let amount = NSAttributedString(
string: string,
attributes: [
.font: UIFont.systemFont(ofSize: 28),
.foregroundColor: amountTextColor,
]
)
let currency = NSAttributedString(
string: " \(transaction.transferType.symbol(server: config.server))",
attributes: [
.font: UIFont.systemFont(ofSize: 20),
]
)
return amount + currency
}
}

@ -1,98 +0,0 @@
// Copyright SIX DAY LLC. All rights reserved.
import Foundation
import UIKit
struct ConfirmTransactionHeaderViewModel: TransactionHeaderBaseViewModel {
private let transaction: UnconfirmedTransaction
let config: Config
private let fullFormatter = EtherNumberFormatter.full
init(
transaction: UnconfirmedTransaction,
config: Config
) {
self.transaction = transaction
self.config = config
}
var amountTextColor: UIColor {
return Colors.red
}
var amountAttributedString: NSAttributedString {
switch transaction.transferType {
case .token(let token):
return amountAttributedText(
string: fullFormatter.string(from: transaction.value, decimals: token.decimals)
)
case .ether:
return amountAttributedText(
string: fullFormatter.string(from: transaction.value)
)
case .exchange(let from, let to):
let fromAttributedString: NSAttributedString = {
let amount = NSAttributedString(
string: fullFormatter.string(from: from.amount),
attributes: [
.font: UIFont.systemFont(ofSize: 24),
.foregroundColor: Colors.red,
]
)
let currency = NSAttributedString(
string: " " + from.token.symbol,
attributes: [
.font: UIFont.systemFont(ofSize: 16),
]
)
return amount + currency
}()
let toAttributedString: NSAttributedString = {
let amount = NSAttributedString(
string: fullFormatter.string(from: to.amount),
attributes: [
.font: UIFont.systemFont(ofSize: 24),
.foregroundColor: Colors.green,
]
)
let currency = NSAttributedString(
string: " " + to.token.symbol,
attributes: [
.font: UIFont.systemFont(ofSize: 16),
]
)
return amount + currency
}()
let amount = NSAttributedString(
string: String(" for "),
attributes: [
.font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.light),
]
)
return fromAttributedString + amount + toAttributedString
}
}
private func amountAttributedText(string: String) -> NSAttributedString {
let amount = NSAttributedString(
string: string,
attributes: [
.font: UIFont.systemFont(ofSize: 28),
.foregroundColor: amountTextColor,
]
)
let currency = NSAttributedString(
string: " \(transaction.transferType.symbol(server: config.server))",
attributes: [
.font: UIFont.systemFont(ofSize: 20),
]
)
return amount + currency
}
}

@ -1,7 +0,0 @@
// Copyright SIX DAY LLC. All rights reserved.
import Foundation
protocol TransactionHeaderBaseViewModel {
var amountAttributedString: NSAttributedString { get }
}

@ -1,52 +0,0 @@
// Copyright SIX DAY LLC. All rights reserved.
import Foundation
import UIKit
struct TransactionHeaderViewModel: TransactionHeaderBaseViewModel {
private let value: TransactionValue
private let direction: TransactionDirection
init(
value: TransactionValue,
direction: TransactionDirection
) {
self.value = value
self.direction = direction
}
var amountTextColor: UIColor {
switch direction {
case .incoming: return Colors.green
case .outgoing: return Colors.red
}
}
var valueString: String {
guard value.amount != "0" else { return value.amount }
switch direction {
case .incoming: return "+\(value.amount)"
case .outgoing: return "-\(value.amount)"
}
}
var amountAttributedString: NSAttributedString {
let amount = NSAttributedString(
string: valueString,
attributes: [
.font: UIFont.systemFont(ofSize: 24),
.foregroundColor: amountTextColor,
]
)
let currency = NSAttributedString(
string: " " + value.symbol,
attributes: [
.font: UIFont.systemFont(ofSize: 14),
]
)
return amount + currency
}
}

@ -12,13 +12,6 @@ struct TransactionAppearance {
return view
}
static func header(viewModel: TransactionHeaderBaseViewModel) -> UIView {
let view = TransactionHeaderView(frame: .zero)
view.translatesAutoresizingMaskIntoConstraints = false
view.amountLabel.attributedText = viewModel.amountAttributedString
return view
}
static func item(title: String, subTitle: String, completion:((_ title: String, _ value: String, _ sender: UIView) -> Void)? = .none) -> UIView {
let titleLabel = UILabel(frame: .zero)
titleLabel.translatesAutoresizingMaskIntoConstraints = false

Loading…
Cancel
Save