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, height: data['height'] as int,
amount: data['amount'] as int, amount: data['amount'] as int,
fee: data['fee'] 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), date: DateTime.fromMillisecondsSinceEpoch(data['date'] as int),
isPending: data['isPending'] as bool, isPending: data['isPending'] as bool,
confirmations: data['confirmations'] as int); confirmations: data['confirmations'] as int);
@ -228,7 +228,7 @@ class ElectrumTransactionInfo extends TransactionInfo {
m['id'] = id; m['id'] = id;
m['height'] = height; m['height'] = height;
m['amount'] = amount; m['amount'] = amount;
m['direction'] = direction.index; m['direction'] = direction.raw;
m['date'] = date.millisecondsSinceEpoch; m['date'] = date.millisecondsSinceEpoch;
m['isPending'] = isPending; m['isPending'] = isPending;
m['confirmations'] = confirmations; 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) { final int raw;
switch (raw) { final String title;
case 0: final String? iconPath;
return TransactionDirection.incoming;
case 1: static const incoming =
return TransactionDirection.outgoing; TransactionDirection(raw: 0, title: 'incoming', iconPath: 'assets/images/down_arrow.png');
default: static const outgoing =
throw Exception('Unexpected token: raw for TransactionDirection parseTransactionDirectionFromInt'); 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, HavenTransactionInfo(this.id, this.height, this.direction, this.date,
this.isPending, this.amount, this.accountIndex, this.addressIndex, this.fee); 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) HavenTransactionInfo.fromRow(TransactionInfoRow row)
: id = row.getHash(), : id = row.getHash(),
height = row.blockHeight, height = row.blockHeight,
direction = parseTransactionDirectionFromInt(row.direction) ?? direction = TransactionDirection.parseFromInt(row.direction),
TransactionDirection.incoming,
date = DateTime.fromMillisecondsSinceEpoch(row.getDatetime() * 1000), date = DateTime.fromMillisecondsSinceEpoch(row.getDatetime() * 1000),
isPending = row.isPending != 0, isPending = row.isPending != 0,
amount = row.getAmount(), amount = row.getAmount(),

View file

@ -10,33 +10,10 @@ class MoneroTransactionInfo extends TransactionInfo {
MoneroTransactionInfo(this.id, this.height, this.direction, this.date, MoneroTransactionInfo(this.id, this.height, this.direction, this.date,
this.isPending, this.amount, this.accountIndex, this.addressIndex, this.fee, this.unlockTime); 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) MoneroTransactionInfo.fromRow(TransactionInfoRow row)
: id = row.getHash(), : id = row.getHash(),
height = row.blockHeight, height = row.blockHeight,
direction = parseTransactionDirectionFromInt(row.direction) ?? direction = TransactionDirection.parseFromInt(row.direction),
TransactionDirection.incoming,
date = DateTime.fromMillisecondsSinceEpoch(row.getDatetime() * 1000), date = DateTime.fromMillisecondsSinceEpoch(row.getDatetime() * 1000),
isPending = row.isPending != 0, isPending = row.isPending != 0,
amount = row.getAmount(), amount = row.getAmount(),

View file

@ -37,9 +37,7 @@ class TransactionRow extends StatelessWidget {
color: Theme.of(context).textTheme!.overline!.decorationColor! color: Theme.of(context).textTheme!.overline!.decorationColor!
), ),
child: Image.asset( child: Image.asset(
direction == TransactionDirection.incoming direction.iconPath ?? ''),
? 'assets/images/down_arrow.png'
: 'assets/images/up_arrow.png'),
), ),
SizedBox(width: 12), SizedBox(width: 12),
Expanded( Expanded(