From 67bfc8bbce9fe0ed2e54744ae1e6fcdd244b856c Mon Sep 17 00:00:00 2001 From: julian Date: Thu, 11 Jul 2024 12:50:06 -0600 Subject: [PATCH] WIP hook up to ui --- .../sub_widgets/wallet_options_button.dart | 94 ++++++++++++++++++- lib/services/csv_exporter.dart | 28 ++++-- 2 files changed, 111 insertions(+), 11 deletions(-) diff --git a/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/wallet_options_button.dart b/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/wallet_options_button.dart index 4f955c00f..de2c2b865 100644 --- a/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/wallet_options_button.dart +++ b/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/wallet_options_button.dart @@ -9,17 +9,24 @@ */ import 'dart:async'; +import 'dart:developer'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/svg.dart'; +import 'package:isar/isar.dart'; +import '../../../../models/isar/models/blockchain_data/v2/transaction_v2.dart'; +import '../../../../models/isar/models/isar_models.dart'; import '../../../../pages/settings_views/wallet_settings_view/frost_ms/frost_ms_options_view.dart'; import '../../../../pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/change_representative_view.dart'; import '../../../../pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/xpub_view.dart'; +import '../../../../providers/db/main_db_provider.dart'; +import '../../../../providers/global/locale_provider.dart'; import '../../../../providers/global/wallets_provider.dart'; import '../../../../route_generator.dart'; +import '../../../../services/csv_exporter.dart'; import '../../../../themes/stack_colors.dart'; import '../../../../utilities/assets.dart'; import '../../../../utilities/constants.dart'; @@ -31,6 +38,7 @@ import '../../../../wallets/crypto_currency/intermediate/frost_currency.dart'; import '../../../../wallets/crypto_currency/intermediate/nano_currency.dart'; import '../../../../wallets/isar/providers/wallet_info_provider.dart'; import '../../../../wallets/wallet/wallet_mixin_interfaces/extended_keys_interface.dart'; +import '../../../../wallets/wallet/wallet_mixin_interfaces/spark_interface.dart'; import '../../../addresses/desktop_wallet_addresses_view.dart'; import '../../../lelantus_coins/lelantus_coins_view.dart'; import '../../../spark_coins/spark_coins_view.dart'; @@ -43,7 +51,8 @@ enum _WalletOptions { showXpub, lelantusCoins, sparkCoins, - frostOptions; + frostOptions, + exportTxsCsv; String get prettyName { switch (this) { @@ -61,10 +70,49 @@ enum _WalletOptions { return "Spark Coins"; case _WalletOptions.frostOptions: return "FROST settings"; + case _WalletOptions.exportTxsCsv: + return "Export to CSV"; } } } +void _export(String walletId, WidgetRef ref) async { + final wallet = ref.read(pWallets).getWallet(walletId); + final mainDB = ref.read(mainDBProvider); + + final transactions = await mainDB.isar.transactionV2s + .where() + .walletIdEqualTo(walletId) + .findAll(); + final notes = await mainDB.isar.transactionNotes + .where() + .walletIdEqualTo(walletId) + .findAll(); + final locale = ref.read(localeServiceChangeNotifierProvider).locale; + + EthContract? ethContract; + String? sparkChangeAddress; + + if (wallet is SparkInterface) { + sparkChangeAddress = wallet.sparkChangeAddress; + } + + // final ethContract = ref + // .read(mainDBProvider) + // .getEthContractSync(_transaction.contractAddress!); + + final csv = CsvExporter.transactionV2sToCsv( + transactions, + notes, + wallet.info.coin, + locale, + ethContract, + sparkChangeAddress, + ); + + log(csv); +} + class WalletOptionsButton extends ConsumerWidget { const WalletOptionsButton({ super.key, @@ -110,6 +158,9 @@ class WalletOptionsButton extends ConsumerWidget { onFrostMSWalletOptionsPressed: () async { Navigator.of(context).pop(_WalletOptions.frostOptions); }, + onExportTOCsvPressed: () async { + Navigator.of(context).pop(_WalletOptions.exportTxsCsv); + }, walletId: walletId, ); }, @@ -117,6 +168,10 @@ class WalletOptionsButton extends ConsumerWidget { if (context.mounted && func != null) { switch (func) { + case _WalletOptions.exportTxsCsv: + _export(walletId, ref); + break; + case _WalletOptions.addressList: unawaited( Navigator.of(context).pushNamed( @@ -277,6 +332,7 @@ class WalletOptionsPopupMenu extends ConsumerWidget { required this.onFiroShowLelantusCoins, required this.onFiroShowSparkCoins, required this.onFrostMSWalletOptionsPressed, + required this.onExportTOCsvPressed, required this.walletId, }); @@ -287,6 +343,7 @@ class WalletOptionsPopupMenu extends ConsumerWidget { final VoidCallback onFiroShowLelantusCoins; final VoidCallback onFiroShowSparkCoins; final VoidCallback onFrostMSWalletOptionsPressed; + final VoidCallback onExportTOCsvPressed; final String walletId; @override @@ -543,6 +600,41 @@ class WalletOptionsPopupMenu extends ConsumerWidget { const SizedBox( height: 8, ), + TransparentButton( + onPressed: onExportTOCsvPressed, + child: Padding( + padding: const EdgeInsets.all(8), + child: Row( + mainAxisAlignment: MainAxisAlignment.start, + children: [ + SvgPicture.asset( + Assets.svg.arrowUpRight, + width: 20, + height: 20, + color: Theme.of(context) + .extension()! + .textFieldActiveSearchIconLeft, + ), + const SizedBox(width: 14), + Expanded( + child: Text( + _WalletOptions.exportTxsCsv.prettyName, + style: STextStyles.desktopTextExtraExtraSmall( + context, + ).copyWith( + color: Theme.of(context) + .extension()! + .textDark, + ), + ), + ), + ], + ), + ), + ), + const SizedBox( + height: 8, + ), TransparentButton( onPressed: onDeletePressed, child: Padding( diff --git a/lib/services/csv_exporter.dart b/lib/services/csv_exporter.dart index 8c08e5d18..0191b81c7 100644 --- a/lib/services/csv_exporter.dart +++ b/lib/services/csv_exporter.dart @@ -8,7 +8,7 @@ import '../utilities/amount/amount_unit.dart'; import '../wallets/crypto_currency/crypto_currency.dart'; abstract final class CsvExporter { - String transactionV2sToCsv( + static String transactionV2sToCsv( List transactions, List notes, CryptoCurrency coin, @@ -19,14 +19,16 @@ abstract final class CsvExporter { final List?> rows = []; rows.add([ - "timestamp", - "height", - "txid", - "amount", - "fee", - "type", - "sub type", - "note", + "Timestamp", + "Height", + "Txid", + "Amount", + "Fee", + "Type", + "Sub Type", + "Note", + "Input Addresses", + "Output Addresses", ]); for (final _transaction in transactions) { @@ -80,6 +82,12 @@ abstract final class CsvExporter { ); row.add(note.value); + // in addresses + row.add(_transaction.inputs.map((e) => e.addresses.join(",")).join(",")); + + // out addresses + row.add(_transaction.outputs.map((e) => e.addresses.join(",")).join(",")); + // finally add row rows.add(row); } @@ -90,7 +98,7 @@ abstract final class CsvExporter { return csv; } - String _parseAmountFromTxnV2( + static String _parseAmountFromTxnV2( TransactionV2 txn, AmountFormatter amountFormatter, EthContract? ethContract,