-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #617 from breez/535-export-to-csv-is-missing
- Loading branch information
Showing
7 changed files
with
263 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:breez_sdk/bridge_generated.dart'; | ||
import 'package:breez_translations/breez_translations_locales.dart'; | ||
import 'package:c_breez/bloc/account/account_state.dart'; | ||
import 'package:c_breez/bloc/account/payment_filters.dart'; | ||
import 'package:c_breez/models/payment_minutiae.dart'; | ||
import 'package:c_breez/utils/date.dart'; | ||
import 'package:csv/csv.dart'; | ||
import 'package:fimber/fimber.dart'; | ||
import 'package:intl/intl.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
|
||
final _log = FimberLog("CsvExporter"); | ||
|
||
class CsvExporter { | ||
final AccountState accountState; | ||
final bool usesUtcTime; | ||
final String fiatCurrency; | ||
final PaymentTypeFilter filter; | ||
final DateTime? startDate; | ||
final DateTime? endDate; | ||
|
||
CsvExporter( | ||
this.filter, | ||
this.fiatCurrency, | ||
this.accountState, { | ||
this.usesUtcTime = false, | ||
this.startDate, | ||
this.endDate, | ||
}); | ||
|
||
Future<String> export() async { | ||
_log.i("export payments started"); | ||
String tmpFilePath = | ||
await _saveCsvFile(const ListToCsvConverter().convert(_generateList() as List<List>)); | ||
_log.i("export payments finished"); | ||
return tmpFilePath; | ||
} | ||
|
||
List _generateList() { | ||
// fetch currencystate map values accordingly | ||
_log.i("generating payment list started"); | ||
|
||
final texts = getSystemAppLocalizations(); | ||
final data = _filterPaymentData(accountState.payments, accountState.paymentFilters); | ||
List<List<String>> paymentList = List.generate(data.length, (index) { | ||
List<String> paymentItem = []; | ||
final data = accountState.payments.elementAt(index); | ||
final paymentInfo = data; | ||
paymentItem.add( | ||
BreezDateUtils.formatYearMonthDayHourMinute( | ||
paymentInfo.paymentTime, | ||
), | ||
); | ||
paymentItem.add(paymentInfo.title); | ||
paymentItem.add(paymentInfo.description); | ||
paymentItem.add(paymentInfo.destinationPubkey); | ||
paymentItem.add(paymentInfo.amountSat.toString()); | ||
paymentItem.add(paymentInfo.paymentPreimage); | ||
paymentItem.add(paymentInfo.id); | ||
paymentItem.add(paymentInfo.feeSat.toString()); | ||
return paymentItem; | ||
}); | ||
paymentList.insert(0, [ | ||
texts.csv_exporter_date_and_time, | ||
texts.csv_exporter_title, | ||
texts.csv_exporter_description, | ||
texts.csv_exporter_node_id, | ||
texts.csv_exporter_amount, | ||
texts.csv_exporter_preimage, | ||
texts.csv_exporter_tx_hash, | ||
texts.csv_exporter_fee, | ||
]); | ||
_log.i("generating payment finished"); | ||
return paymentList; | ||
} | ||
|
||
List<PaymentMinutiae?> _filterPaymentData(List<PaymentMinutiae?> payments, PaymentFilters filter) { | ||
if (payments.isEmpty) { | ||
return payments; | ||
} | ||
|
||
if (startDate != null && endDate != null) { | ||
List<PaymentMinutiae?> results = []; | ||
for (var element in payments) { | ||
if (element != null && BreezDateUtils.isBetween(element.paymentTime, startDate!, endDate!)) { | ||
results.add(element); | ||
} | ||
} | ||
return results; | ||
} | ||
return payments; | ||
} | ||
|
||
Future<String> _saveCsvFile(String csv) async { | ||
_log.i("save breez payments to csv started"); | ||
String filePath = await _createCsvFilePath(); | ||
final file = File(filePath); | ||
await file.writeAsString(csv); | ||
_log.i("save breez payments to csv finished"); | ||
return file.path; | ||
} | ||
|
||
Future<String> _createCsvFilePath() async { | ||
_log.i("create breez payments path started"); | ||
final directory = await getTemporaryDirectory(); | ||
String filePath = '${directory.path}/BreezPayments'; | ||
filePath = _appendFilterInformation(filePath); | ||
filePath += ".csv"; | ||
_log.i("create breez payments path finished"); | ||
return filePath; | ||
} | ||
|
||
String _appendFilterInformation(String filePath) { | ||
_log.i("add filter information to path started"); | ||
if (filter == PaymentTypeFilter.Sent) { | ||
filePath += "_sent"; | ||
} else if (filter == PaymentTypeFilter.Received) { | ||
filePath += "_received"; | ||
} | ||
if (startDate != null && endDate != null) { | ||
DateFormat dateFilterFormat = DateFormat("d.M.yy"); | ||
String dateFilter = '${dateFilterFormat.format(startDate!)}-${dateFilterFormat.format(endDate!)}'; | ||
filePath += "_$dateFilter"; | ||
} | ||
_log.i("add filter information to path finished"); | ||
return filePath; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
lib/routes/home/widgets/payments_filter/payment_filter_exporter.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import 'package:breez_sdk/bridge_generated.dart'; | ||
import 'package:breez_translations/breez_translations_locales.dart'; | ||
import 'package:c_breez/bloc/account/account_bloc.dart'; | ||
import 'package:c_breez/bloc/account/account_state.dart'; | ||
import 'package:c_breez/bloc/csv_exporter.dart'; | ||
import 'package:c_breez/bloc/currency/currency_bloc.dart'; | ||
import 'package:c_breez/theme/theme_provider.dart'; | ||
import 'package:c_breez/widgets/flushbar.dart'; | ||
import 'package:c_breez/widgets/loader.dart'; | ||
import 'package:fimber/fimber.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:share_plus/share_plus.dart'; | ||
|
||
class PaymentmentFilterExporter extends StatelessWidget { | ||
final _log = FimberLog("PaymentmentFilterExporter"); | ||
final PaymentTypeFilter filter; | ||
|
||
PaymentmentFilterExporter( | ||
this.filter, { | ||
Key? key, | ||
}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final texts = context.texts(); | ||
final themeData = Theme.of(context); | ||
|
||
return BlocBuilder<AccountBloc, AccountState>( | ||
builder: (context, account) { | ||
return Padding( | ||
padding: const EdgeInsets.only(right: 0.0), | ||
child: PopupMenuButton( | ||
color: themeData.colorScheme.background, | ||
icon: Icon( | ||
Icons.more_vert, | ||
color: themeData.paymentItemTitleTextStyle.color, | ||
), | ||
padding: EdgeInsets.zero, | ||
offset: const Offset(12, 24), | ||
onSelected: _select, | ||
itemBuilder: (context) => [ | ||
PopupMenuItem( | ||
height: 36, | ||
value: Choice(() => _exportPayments(context)), | ||
child: Text( | ||
texts.payments_filter_action_export, | ||
style: themeData.textTheme.labelLarge, | ||
), | ||
), | ||
], | ||
)); | ||
}, | ||
); | ||
} | ||
|
||
void _select(Choice choice) { | ||
choice.function(); | ||
} | ||
|
||
Future _exportPayments(BuildContext context) async { | ||
final texts = context.texts(); | ||
final navigator = Navigator.of(context); | ||
final currencyState = context.read<CurrencyBloc>().state; | ||
final accountState = context.read<AccountBloc>().state; | ||
var loaderRoute = createLoaderRoute(context); | ||
navigator.push(loaderRoute); | ||
String filePath; | ||
|
||
try { | ||
if (accountState.paymentFilters.fromTimestamp != null || | ||
accountState.paymentFilters.toTimestamp != null) { | ||
final startDate = DateTime.fromMillisecondsSinceEpoch(accountState.paymentFilters.fromTimestamp!); | ||
final endDate = DateTime.fromMillisecondsSinceEpoch(accountState.paymentFilters.toTimestamp!); | ||
filePath = await CsvExporter(filter, currencyState.fiatId, accountState, | ||
startDate: startDate, endDate: endDate) | ||
.export(); | ||
} else { | ||
filePath = await CsvExporter(filter, currencyState.fiatId, accountState).export(); | ||
} | ||
if (loaderRoute.isActive) { | ||
navigator.removeRoute(loaderRoute); | ||
} | ||
Share.shareXFiles([XFile(filePath)]); | ||
} catch (error) { | ||
{ | ||
if (loaderRoute.isActive) { | ||
navigator.removeRoute(loaderRoute); | ||
} | ||
_log.e("Received error: $error"); | ||
showFlushbar( | ||
context, | ||
message: texts.payments_filter_action_export_failed, | ||
); | ||
} | ||
} finally { | ||
if (loaderRoute.isActive) { | ||
navigator.removeRoute(loaderRoute); | ||
} | ||
} | ||
} | ||
} | ||
|
||
class Choice { | ||
const Choice(this.function); | ||
|
||
final Function function; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters