Skip to content

Commit

Permalink
add filtering of payments
Browse files Browse the repository at this point in the history
  • Loading branch information
ubbabeck committed Aug 11, 2023
1 parent fa2325d commit 1298779
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
50 changes: 40 additions & 10 deletions lib/bloc/csv_exporter.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
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 List<PaymentMinutiae> data;
final AccountState accountState;
final bool usesUtcTime;
final String fiatCurrency;
final PaymentFilters filter;
final PaymentTypeFilter filter;
final DateTime? startDate;
final DateTime? endDate;

CsvExporter(
this.filter,
this.fiatCurrency,
this.data, {
this.accountState, {
this.usesUtcTime = false,
this.startDate,
this.endDate,
});

Future<String> export() async {
Expand All @@ -36,10 +43,10 @@ class CsvExporter {
_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 = this.data.elementAt(index);
final data = accountState.payments.elementAt(index);
final paymentInfo = data;
paymentItem.add(
BreezDateUtils.formatYearMonthDayHourMinute(
Expand All @@ -49,12 +56,10 @@ class CsvExporter {
paymentItem.add(paymentInfo.title);
paymentItem.add(paymentInfo.description);
paymentItem.add(paymentInfo.destinationPubkey);
paymentItem.add(paymentInfo.id);
paymentItem.add(paymentInfo.amountSat.toString());
paymentItem.add(paymentInfo.paymentPreimage);
paymentItem.add(paymentInfo.lnAddress);
paymentItem.add(paymentInfo.id);
paymentItem.add(paymentInfo.feeSat.toString());

return paymentItem;
});
paymentList.insert(0, [
Expand All @@ -66,12 +71,28 @@ class CsvExporter {
texts.csv_exporter_preimage,
texts.csv_exporter_tx_hash,
texts.csv_exporter_fee,
fiatCurrency,
]);
_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();
Expand All @@ -93,7 +114,16 @@ class CsvExporter {

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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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';
Expand All @@ -13,7 +14,12 @@ import 'package:share_plus/share_plus.dart';

class PaymentmentFilterExporter extends StatelessWidget {
final _log = FimberLog("PaymentmentFilterExporter");
PaymentmentFilterExporter();
final PaymentTypeFilter filter;

PaymentmentFilterExporter(
this.filter, {
Key? key,
}) : super(key: key);

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -52,30 +58,46 @@ class PaymentmentFilterExporter extends StatelessWidget {
choice.function();
}

_exportPayments(BuildContext context) {
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);
CsvExporter(accountState.paymentFilters, currencyState.fiatId, accountState.payments)
.export()
.then((filePath) {
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)]);
}).catchError((err) {
} 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);
}
_log.e("Received error: $err");
showFlushbar(
context,
message: texts.payments_filter_action_export_failed,
);
});
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class PaymentsFilterState extends State<PaymentsFilter> {
return Row(
children: [
// Insert export paymentfilet here.
PaymentmentFilterExporter(),
PaymentmentFilterExporter(_getFilterType()),
PaymentsFilterCalendar(_getFilterType()),
PaymentsFilterDropdown(
_filter!,
Expand Down

0 comments on commit 1298779

Please sign in to comment.