Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Added Regex scanner template #6

Merged
merged 12 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added new transaction detection for adding scanner
  • Loading branch information
Droyder7 committed Apr 4, 2024
commit 5f486b31fe9d948c8f9c4810f27e3fb41b3b1b88
112 changes: 52 additions & 60 deletions budget/lib/pages/addEmailTemplate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ class _AddEmailTemplateState extends State<AddEmailTemplate> {
setState(() {});
},
onSubmit: (selection) {
onSubmit(selection);
determineBottomButton();
setState(() {});
Navigator.pop(context);
onSubmit(selection);
},
),
);
Expand Down Expand Up @@ -425,7 +425,7 @@ class _AddEmailTemplateState extends State<AddEmailTemplate> {
dateTimeModified: null,
amountTransactionAfter: "",
amountTransactionBefore: "",
contains: selectedSubject ?? "",
contains: "",
defaultCategoryFk: selectedCategory!.categoryPk,
templateName: selectedName ?? "",
titleTransactionAfter: "",
Expand Down Expand Up @@ -562,69 +562,61 @@ class _AddEmailTemplateState extends State<AddEmailTemplate> {
onTap: (messageString) {
setMessageString(messageString);
Navigator.pop(context);
},
),
),
);

if (selectedMessageString == null) {
return;
}

final msg = selectedMessageString!;
selectedSubject = msg;
setState(() {});
final msg = selectedMessageString!;

await selectMessagePart(
context,
title: 'Select Amount',
onSubmit: (selection) {
selectedAmount = msg.substring(
selection.start,
selection.end,
);
selectedSubject = MessagePart.amount
.replaceWithRegex(selectedSubject ?? '',
selectedAmount ?? '');
},
);
selectMessagePart(
context,
title: 'Select Amount',
onSubmit: (selection) {
selectedAmount = msg.substring(
selection.start,
selection.end,
);
selectedSubject = msg;
selectedSubject = MessagePart.amount
.replaceWithRegex(selectedSubject ?? '',
selectedAmount ?? '');

if (selectedAmount == null) {
return;
}
setState(() {});

await selectMessagePart(
context,
title: 'Select Title',
onSubmit: (selection) {
selectedTitle = msg.substring(
selection.start,
selection.end,
);
selectedSubject = MessagePart.title
.replaceWithRegex(
selectedSubject ?? '', selectedTitle ?? '');
},
);
if (selectedTitle == null) {
return;
}
selectMessagePart(
context,
title: 'Select Title',
onSubmit: (selection) {
selectedTitle = msg.substring(
selection.start,
selection.end,
);
selectedSubject = MessagePart.title
.replaceWithRegex(
selectedSubject ?? '',
selectedTitle ?? '');

await selectMessagePart(
context,
title: 'Select Ignored Part',
onSubmit: (selection) {
final substring = msg.substring(
selection.start,
selection.end,
);
ignoredParts.add(substring);
selectedSubject =
MessagePart.ignored.replaceWithRegex(
selectedSubject ?? '',
substring,
);
},
selectMessagePart(
context,
title: 'Select Ignored Part',
onSubmit: (selection) {
final substring = msg.substring(
selection.start,
selection.end,
);
ignoredParts.add(substring);
selectedSubject = MessagePart
.ignored
.replaceWithRegex(
selectedSubject ?? '',
substring,
);
},
);
},
);
},
);
},
),
),
);
},
),
Expand Down
25 changes: 16 additions & 9 deletions budget/lib/pages/autoTransactionsPageEmail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,23 @@ Future queueTransactionFromMessage(String messageString) async {
ScannerTemplate? templateFound;

for (ScannerTemplate scannerTemplate in scannerTemplates) {
if (messageString.contains(scannerTemplate.contains)) {
final regExp = RegExp(scannerTemplate.regex);
if (regExp.hasMatch(messageString)) {
templateFound = scannerTemplate;
title = getTransactionTitleFromEmail(
messageString,
scannerTemplate.titleTransactionBefore,
scannerTemplate.titleTransactionAfter);
amountDouble = getTransactionAmountFromEmail(
messageString,
scannerTemplate.amountTransactionBefore,
scannerTemplate.amountTransactionAfter);
final match = regExp.firstMatch(messageString)!;

for (final group in match.groupNames) {
final val = match.namedGroup(group);
switch (group) {
case 'title':
title = val ?? '';
break;
case 'amount':
amountDouble = double.tryParse((val ?? '').replaceAll(',', ''));
break;
default:
}
}
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:budget/database/tables.dart';
import 'package:budget/functions.dart';
import 'package:budget/main.dart';
import 'package:budget/pages/addEmailTemplate.dart';
import 'package:budget/pages/addTransactionPage.dart';
import 'package:budget/pages/autoTransactionsPageEmail.dart';
import 'package:budget/struct/databaseGlobal.dart';
import 'package:budget/struct/notification_controller/awesome_notifications/extensions.dart';
import 'package:budget/struct/notification_controller/models.dart';
import 'package:budget/struct/notification_controller/notification_controller.dart';
import 'package:budget/struct/notification_listener.dart';
import 'package:budget/struct/settings.dart';
import 'package:budget/widgets/notificationsSettings.dart';
import 'package:budget/widgets/openPopup.dart';
Expand Down Expand Up @@ -151,6 +154,19 @@ class AwesomeNotificationController extends NotificationController<
switch (payload['type']) {
case 'newTransaction':
return await handleNewTransactionAction(action);
case 'addScannerTemplate':
pushPageWithoutContext(
AutoTransactionsPageNotifications(),
);

return Future.delayed(
Duration.zero,
() => pushPageWithoutContext(
AddEmailTemplate(
messagesList: recentCapturedNotifications,
),
),
);
default:
return;
}
Expand Down Expand Up @@ -431,25 +447,30 @@ class AwesomeNotificationController extends NotificationController<
}
}

Future<void> handleNewTransactionAction(ReceivedAction action) async {
Future<void> pushPageWithoutContext(Widget page) async {
final navigatorState = navigatorKey.currentState;
if (navigatorState == null) {
return;
}

await navigatorState.push(MaterialPageRoute(builder: (context) => page));
}

Future<void> handleNewTransactionAction(ReceivedAction action) async {
final trxPk = action.payload?['value'];

if (navigatorState == null || trxPk == null) {
if (trxPk == null) {
return;
}

final trx = await database.getTransactionFromPk(trxPk);
final edit = action.buttonKeyPressed == 'EDIT';

navigatorState.push(
MaterialPageRoute(
builder: (context) => AddTransactionPage(
transaction: trx,
quickEdit: edit,
routesToPopAfterDelete: RoutesToPopAfterDelete.None,
),
pushPageWithoutContext(
AddTransactionPage(
transaction: trx,
quickEdit: edit,
routesToPopAfterDelete: RoutesToPopAfterDelete.None,
),
);
}
19 changes: 18 additions & 1 deletion budget/lib/struct/notification_listener.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'dart:isolate';
import 'dart:ui';

import 'package:budget/pages/autoTransactionsPageEmail.dart';
import 'package:budget/struct/notification_controller/models.dart';
import 'package:budget/struct/notificationsGlobal.dart';
import 'package:budget/widgets/util/deepLinks.dart';
import 'package:flutter_notification_listener/flutter_notification_listener.dart';

Expand Down Expand Up @@ -46,13 +48,28 @@ Future<void> initNotificationListener() async {

onNotification(NotificationEvent event) async {
final trxParams = await parseTransactionFromNotification(event);
if (trxParams.isEmpty) return;
if (trxParams.isEmpty) return handleNonTransactionNotification(event);

trxParams['notes'] = '[${event.title}] ${trxParams['notes']}';

await addTransactionFromParams(trxParams);
}

const possibleTransactionKeywords = ['debit', 'credit', '5890', '8389'];

handleNonTransactionNotification(NotificationEvent event) async {
final message = getSanitizedMessage(event.text);
if (possibleTransactionKeywords.any((keyword) => message.contains(keyword))) {
await notificationController.createNotification(
content: NotificationData(
title: 'New Transaction Detected, Add Scanner?', body: message),
payload: {
'type': 'addScannerTemplate',
},
);
}
}

Future<Map<String, String>> parseTransactionFromNotification(
NotificationEvent event) async {
final notificationMessage = getSanitizedMessage(event.text);
Expand Down