From 2103aa06bfb3f0c9c59eb1e1f37ee91e9072f085 Mon Sep 17 00:00:00 2001 From: Serhii Date: Mon, 6 Feb 2023 21:42:24 +0200 Subject: [PATCH] transaction direction refactoring --- cw_bitcoin/lib/electrum_transaction_info.dart | 4 +- cw_core/lib/transaction_direction.dart | 53 ++++++++++++------- cw_haven/lib/haven_transaction_info.dart | 18 +------ cw_monero/lib/monero_transaction_info.dart | 25 +-------- .../dashboard/widgets/transaction_raw.dart | 4 +- 5 files changed, 38 insertions(+), 66 deletions(-) diff --git a/cw_bitcoin/lib/electrum_transaction_info.dart b/cw_bitcoin/lib/electrum_transaction_info.dart index 4f7f6b62d..e8ed955fb 100644 --- a/cw_bitcoin/lib/electrum_transaction_info.dart +++ b/cw_bitcoin/lib/electrum_transaction_info.dart @@ -186,7 +186,7 @@ class ElectrumTransactionInfo extends TransactionInfo { height: data['height'] as int, amount: data['amount'] as int, fee: data['fee'] as int, - direction: parseTransactionDirectionFromInt(data['direction'] as int), + direction: TransactionDirection.parseFromInt(data['direction'] as int), date: DateTime.fromMillisecondsSinceEpoch(data['date'] as int), isPending: data['isPending'] as bool, confirmations: data['confirmations'] as int); @@ -228,7 +228,7 @@ class ElectrumTransactionInfo extends TransactionInfo { m['id'] = id; m['height'] = height; m['amount'] = amount; - m['direction'] = direction.index; + m['direction'] = direction.raw; m['date'] = date.millisecondsSinceEpoch; m['isPending'] = isPending; m['confirmations'] = confirmations; diff --git a/cw_core/lib/transaction_direction.dart b/cw_core/lib/transaction_direction.dart index 8d1ddfe02..8f0117e4f 100644 --- a/cw_core/lib/transaction_direction.dart +++ b/cw_core/lib/transaction_direction.dart @@ -1,23 +1,36 @@ -enum TransactionDirection { incoming, outgoing } +class TransactionDirection { + const TransactionDirection({required this.raw, required this.title, this.iconPath}); -TransactionDirection parseTransactionDirectionFromInt(int raw) { - switch (raw) { - case 0: - return TransactionDirection.incoming; - case 1: - return TransactionDirection.outgoing; - default: - throw Exception('Unexpected token: raw for TransactionDirection parseTransactionDirectionFromInt'); + final int raw; + final String title; + final String? iconPath; + + static const incoming = + TransactionDirection(raw: 0, title: 'incoming', iconPath: 'assets/images/down_arrow.png'); + static const outgoing = + TransactionDirection(raw: 1, title: 'outgoing', iconPath: 'assets/images/up_arrow.png'); + + static TransactionDirection parseFromInt(int raw) { + switch (raw) { + case 0: + return TransactionDirection.incoming; + case 1: + return TransactionDirection.outgoing; + default: + throw Exception( + 'Unexpected token: raw for TransactionDirection parseTransactionDirectionFromInt'); + } + } + + static TransactionDirection parseFromString(String raw) { + switch (raw) { + case "0": + return TransactionDirection.incoming; + case "1": + return TransactionDirection.outgoing; + default: + throw Exception( + 'Unexpected token: raw for TransactionDirection parseTransactionDirectionFromNumber'); + } } } - -TransactionDirection parseTransactionDirectionFromNumber(String raw) { - switch (raw) { - case "0": - return TransactionDirection.incoming; - case "1": - return TransactionDirection.outgoing; - default: - throw Exception('Unexpected token: raw for TransactionDirection parseTransactionDirectionFromNumber'); - } -} \ No newline at end of file diff --git a/cw_haven/lib/haven_transaction_info.dart b/cw_haven/lib/haven_transaction_info.dart index 62519487b..870319b37 100644 --- a/cw_haven/lib/haven_transaction_info.dart +++ b/cw_haven/lib/haven_transaction_info.dart @@ -10,26 +10,10 @@ class HavenTransactionInfo extends TransactionInfo { HavenTransactionInfo(this.id, this.height, this.direction, this.date, this.isPending, this.amount, this.accountIndex, this.addressIndex, this.fee); - HavenTransactionInfo.fromMap(Map map) - : id = (map['hash'] ?? '') as String, - height = (map['height'] ?? 0) as int, - direction = - parseTransactionDirectionFromNumber(map['direction'] as String) ?? - TransactionDirection.incoming, - date = DateTime.fromMillisecondsSinceEpoch( - int.parse(map['timestamp'] as String? ?? '0') * 1000), - isPending = parseBoolFromString(map['isPending'] as String), - amount = map['amount'] as int, - accountIndex = int.parse(map['accountIndex'] as String), - addressIndex = map['addressIndex'] as int, - key = getTxKey((map['hash'] ?? '') as String), - fee = map['fee'] as int? ?? 0; - HavenTransactionInfo.fromRow(TransactionInfoRow row) : id = row.getHash(), height = row.blockHeight, - direction = parseTransactionDirectionFromInt(row.direction) ?? - TransactionDirection.incoming, + direction = TransactionDirection.parseFromInt(row.direction), date = DateTime.fromMillisecondsSinceEpoch(row.getDatetime() * 1000), isPending = row.isPending != 0, amount = row.getAmount(), diff --git a/cw_monero/lib/monero_transaction_info.dart b/cw_monero/lib/monero_transaction_info.dart index 4e78bca05..05f332c1d 100644 --- a/cw_monero/lib/monero_transaction_info.dart +++ b/cw_monero/lib/monero_transaction_info.dart @@ -10,33 +10,10 @@ class MoneroTransactionInfo extends TransactionInfo { MoneroTransactionInfo(this.id, this.height, this.direction, this.date, this.isPending, this.amount, this.accountIndex, this.addressIndex, this.fee, this.unlockTime); - MoneroTransactionInfo.fromMap(Map map) - : id = (map['hash'] ?? '') as String, - height = (map['height'] ?? 0) as int, - direction = - parseTransactionDirectionFromNumber(map['direction'] as String) ?? - TransactionDirection.incoming, - date = DateTime.fromMillisecondsSinceEpoch( - (int.parse(map['timestamp'] as String) ?? 0) * 1000), - isPending = parseBoolFromString(map['isPending'] as String), - amount = map['amount'] as int, - accountIndex = int.parse(map['accountIndex'] as String), - addressIndex = map['addressIndex'] as int, - unlockTime = map['unlockTime'] as int, - key = getTxKey((map['hash'] ?? '') as String), - fee = map['fee'] as int ?? 0 { - additionalInfo = { - 'key': key, - 'accountIndex': accountIndex, - 'addressIndex': addressIndex - }; - } - MoneroTransactionInfo.fromRow(TransactionInfoRow row) : id = row.getHash(), height = row.blockHeight, - direction = parseTransactionDirectionFromInt(row.direction) ?? - TransactionDirection.incoming, + direction = TransactionDirection.parseFromInt(row.direction), date = DateTime.fromMillisecondsSinceEpoch(row.getDatetime() * 1000), isPending = row.isPending != 0, amount = row.getAmount(), diff --git a/lib/src/screens/dashboard/widgets/transaction_raw.dart b/lib/src/screens/dashboard/widgets/transaction_raw.dart index 7ba6cbf0e..9e6e89c61 100644 --- a/lib/src/screens/dashboard/widgets/transaction_raw.dart +++ b/lib/src/screens/dashboard/widgets/transaction_raw.dart @@ -37,9 +37,7 @@ class TransactionRow extends StatelessWidget { color: Theme.of(context).textTheme!.overline!.decorationColor! ), child: Image.asset( - direction == TransactionDirection.incoming - ? 'assets/images/down_arrow.png' - : 'assets/images/up_arrow.png'), + direction.iconPath ?? ''), ), SizedBox(width: 12), Expanded(