transaction direction refactoring

This commit is contained in:
Serhii 2023-02-06 21:42:24 +02:00
parent b180445a7b
commit 2103aa06bf
5 changed files with 38 additions and 66 deletions

View file

@ -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;

View file

@ -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');
}
}

View file

@ -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<String, Object> 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(),

View file

@ -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<String, Object?> 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 = <String, dynamic>{
'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(),

View file

@ -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(