cake_wallet/lib/view_model/transaction_details_view_model.dart

207 lines
8.1 KiB
Dart
Raw Normal View History

import 'package:cake_wallet/entities/fiat_api_mode.dart';
import 'package:cw_bitcoin/bitcoin_amount_format.dart';
import 'package:cw_core/monero_amount_format.dart';
2021-12-24 12:37:24 +00:00
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/transaction_info.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cake_wallet/src/screens/transaction_details/standart_list_item.dart';
import 'package:cake_wallet/src/screens/transaction_details/textfield_list_item.dart';
import 'package:cake_wallet/src/screens/transaction_details/transaction_details_list_item.dart';
import 'package:cake_wallet/src/screens/transaction_details/blockexplorer_list_item.dart';
2021-12-24 12:37:24 +00:00
import 'package:cw_core/transaction_direction.dart';
import 'package:cake_wallet/utils/date_formatter.dart';
import 'package:cake_wallet/entities/transaction_description.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:url_launcher/url_launcher.dart';
2022-01-31 12:08:38 +00:00
import 'package:cake_wallet/monero/monero.dart';
import 'package:cake_wallet/core/fiat_conversion_service.dart';
import 'dashboard/dashboard_view_model.dart';
part 'transaction_details_view_model.g.dart';
2021-01-05 20:37:25 +00:00
class TransactionDetailsViewModel = TransactionDetailsViewModelBase
with _$TransactionDetailsViewModel;
abstract class TransactionDetailsViewModelBase with Store {
2021-01-05 20:37:25 +00:00
TransactionDetailsViewModelBase(
2022-10-12 17:09:57 +00:00
{required this.transactionInfo,
required this.transactionDescriptionBox,
required this.dashboardViewModel,
2022-10-12 17:09:57 +00:00
required this.wallet,
required this.settingsStore})
: items = ObservableList<TransactionDetailsListItem>(),
2023-05-19 06:30:47 +00:00
isRecipientAddressShown = false,
showRecipientAddress = settingsStore.shouldSaveRecipientAddress {
final dateFormat = DateFormatter.withCurrentLocal();
final tx = transactionInfo;
2021-12-24 12:37:24 +00:00
if (wallet.type == WalletType.monero) {
2022-10-12 17:09:57 +00:00
final key = tx.additionalInfo['key'] as String?;
final accountIndex = tx.additionalInfo['accountIndex'] as int;
final addressIndex = tx.additionalInfo['addressIndex'] as int;
2022-10-12 17:09:57 +00:00
final feeFormatted = tx.feeFormatted();
final _items = [
2023-05-19 06:30:47 +00:00
StandartListItem(title: S.current.transaction_details_transaction_id, value: tx.id),
StandartListItem(
2023-05-19 06:30:47 +00:00
title: S.current.transaction_details_date, value: dateFormat.format(tx.date)),
StandartListItem(title: S.current.transaction_details_height, value: '${tx.height}'),
StandartListItem(title: S.current.transaction_details_amount, value: tx.amountFormatted()),
2022-10-12 17:09:57 +00:00
if (feeFormatted != null)
2023-05-19 06:30:47 +00:00
StandartListItem(title: S.current.transaction_details_fee, value: feeFormatted),
2022-01-31 12:08:38 +00:00
if (key?.isNotEmpty ?? false)
2022-10-12 17:09:57 +00:00
StandartListItem(title: S.current.transaction_key, value: key!)
];
2022-01-31 12:08:38 +00:00
if (tx.direction == TransactionDirection.incoming &&
accountIndex != null &&
addressIndex != null) {
try {
2022-10-12 17:09:57 +00:00
final address = monero!.getTransactionAddress(wallet, accountIndex, addressIndex);
final label = monero!.getSubaddressLabel(wallet, accountIndex, addressIndex);
2021-12-24 12:37:24 +00:00
2022-01-31 12:08:38 +00:00
if (address?.isNotEmpty ?? false) {
isRecipientAddressShown = true;
2023-05-19 06:30:47 +00:00
_items.add(StandartListItem(
title: S.current.transaction_details_recipient_address, value: address));
2022-01-31 12:08:38 +00:00
}
if (label?.isNotEmpty ?? false) {
2023-05-19 06:30:47 +00:00
_items.add(StandartListItem(title: S.current.address_label, value: label));
}
2022-01-31 12:08:38 +00:00
} catch (e) {
print(e.toString());
}
}
items.addAll(_items);
}
2023-05-19 06:30:47 +00:00
if (wallet.type == WalletType.bitcoin || wallet.type == WalletType.litecoin) {
final _items = [
2023-05-19 06:30:47 +00:00
StandartListItem(title: S.current.transaction_details_transaction_id, value: tx.id),
StandartListItem(
2023-05-19 06:30:47 +00:00
title: S.current.transaction_details_date, value: dateFormat.format(tx.date)),
StandartListItem(title: S.current.confirmations, value: tx.confirmations.toString()),
StandartListItem(title: S.current.transaction_details_height, value: '${tx.height}'),
StandartListItem(title: S.current.transaction_details_amount, value: tx.amountFormatted()),
2022-10-12 17:09:57 +00:00
if (tx.feeFormatted()?.isNotEmpty ?? false)
2023-05-19 06:30:47 +00:00
StandartListItem(title: S.current.transaction_details_fee, value: tx.feeFormatted()!)
];
items.addAll(_items);
}
2022-03-30 15:57:04 +00:00
if (wallet.type == WalletType.haven) {
items.addAll([
2023-05-19 06:30:47 +00:00
StandartListItem(title: S.current.transaction_details_transaction_id, value: tx.id),
2022-03-30 15:57:04 +00:00
StandartListItem(
2023-05-19 06:30:47 +00:00
title: S.current.transaction_details_date, value: dateFormat.format(tx.date)),
StandartListItem(title: S.current.transaction_details_height, value: '${tx.height}'),
StandartListItem(title: S.current.transaction_details_amount, value: tx.amountFormatted()),
2022-10-12 17:09:57 +00:00
if (tx.feeFormatted()?.isNotEmpty ?? false)
2023-05-19 06:30:47 +00:00
StandartListItem(title: S.current.transaction_details_fee, value: tx.feeFormatted()!)
2022-03-30 15:57:04 +00:00
]);
}
if (showRecipientAddress && !isRecipientAddressShown) {
2022-10-12 17:09:57 +00:00
try {
final recipientAddress = transactionDescriptionBox.values
.firstWhere((val) => val.id == transactionInfo.id)
.recipientAddress;
2022-10-12 17:09:57 +00:00
if (recipientAddress?.isNotEmpty ?? false) {
items.add(StandartListItem(
2023-05-19 06:30:47 +00:00
title: S.current.transaction_details_recipient_address, value: recipientAddress!));
2022-10-12 17:09:57 +00:00
}
2023-05-19 06:30:47 +00:00
} catch (_) {
2022-10-12 17:09:57 +00:00
// FIX-ME: Unhandled exception
}
}
2021-12-24 12:37:24 +00:00
final type = wallet.type;
items.add(BlockExplorerListItem(
title: S.current.view_in_block_explorer,
value: _explorerDescription(type),
onTap: () {
try {
launch(_explorerUrl(type, tx.id));
} catch (e) {}
}));
final description = dashboardViewModel.getTransactionDescription(transactionInfo);
2021-01-05 20:37:25 +00:00
items.add(TextFieldListItem(
title: S.current.note_tap_to_change,
value: description.note,
onSubmitted: (value) {
description.transactionNote = value;
if (description.isInBox) {
description.save();
2021-01-05 20:37:25 +00:00
} else {
transactionDescriptionBox.add(description);
}
}));
2023-05-19 06:30:47 +00:00
if (settingsStore.showHistoricalFiatAmount &&
2023-05-19 06:30:47 +00:00
description.historicalFiatRate != null &&
description.historicalFiatRate! > 0 && settingsStore.fiatApiMode != FiatApiMode.disabled) {
2023-05-19 06:30:47 +00:00
final index =
items.indexWhere((element) => element.title == S.current.transaction_details_fee);
items.insert(
index + 1,
StandartListItem(
title: S.current.historical_fiat_amount,
2023-05-19 06:30:47 +00:00
value: description.historicalFiatRate!.toStringAsFixed(2) +
' ' +
settingsStore.fiatCurrency.toString()));
}
}
final TransactionInfo transactionInfo;
final Box<TransactionDescription> transactionDescriptionBox;
final DashboardViewModel dashboardViewModel;
final SettingsStore settingsStore;
final WalletBase wallet;
final ObservableList<TransactionDetailsListItem> items;
bool showRecipientAddress;
bool isRecipientAddressShown;
String _explorerUrl(WalletType type, String txId) {
switch (type) {
case WalletType.monero:
return 'https://monero.com/tx/${txId}';
case WalletType.bitcoin:
return 'https://mempool.space/tx/${txId}';
case WalletType.litecoin:
return 'https://blockchair.com/litecoin/transaction/${txId}';
2022-03-30 15:57:04 +00:00
case WalletType.haven:
return 'https://explorer.havenprotocol.org/search?value=${txId}';
default:
return '';
}
}
String _explorerDescription(WalletType type) {
switch (type) {
case WalletType.monero:
return S.current.view_transaction_on + 'Monero.com';
case WalletType.bitcoin:
return S.current.view_transaction_on + 'mempool.space';
case WalletType.litecoin:
return S.current.view_transaction_on + 'Blockchair.com';
2022-03-30 15:57:04 +00:00
case WalletType.haven:
return S.current.view_transaction_on + 'explorer.havenprotocol.org';
default:
return '';
}
}
2021-01-05 20:37:25 +00:00
}