From 647933c272f4d60a0ec6088e3bb0450d4066cfae Mon Sep 17 00:00:00 2001
From: vjrj <vjrj@comunes.org>
Date: Sat, 30 Sep 2023 22:01:59 +0200
Subject: [PATCH] Notifications with comments

---
 .../multi_wallet_transaction_cubit.dart       | 12 ++++---
 lib/ui/notification_controller_mobile.dart    | 34 ++++++-------------
 lib/ui/notification_controller_web.dart       | 34 ++++++-------------
 lib/ui/ui_helpers.dart                        | 34 +++++++++++++++++++
 4 files changed, 63 insertions(+), 51 deletions(-)

diff --git a/lib/data/models/multi_wallet_transaction_cubit.dart b/lib/data/models/multi_wallet_transaction_cubit.dart
index 289498b..0348f9a 100644
--- a/lib/data/models/multi_wallet_transaction_cubit.dart
+++ b/lib/data/models/multi_wallet_transaction_cubit.dart
@@ -158,11 +158,6 @@ class MultiWalletTransactionCubit
         appCubit.setUd(newParsedState.currentUd!);
       }
 
-      logger(
-          'Last received notification: ${newParsedState.latestReceivedNotification.toIso8601String()})}');
-      logger(
-          'Last sent notification: ${newParsedState.latestSentNotification.toIso8601String()})}');
-
       // Check pending transactions
       final TransactionState newState =
           _checkPendingTx(cursor, newParsedState, pubKey, node);
@@ -170,6 +165,11 @@ class MultiWalletTransactionCubit
 
       TransactionState currentModifiedState = newState;
 
+      logger(
+          'Last received notification: ${currentModifiedState.latestReceivedNotification.toIso8601String()})}');
+      logger(
+          'Last sent notification: ${currentModifiedState.latestSentNotification.toIso8601String()})}');
+
       for (final Transaction tx in currentModifiedState.transactions.reversed) {
         bool stateModified = false;
 
@@ -181,6 +181,7 @@ class MultiWalletTransactionCubit
               tx.time.millisecondsSinceEpoch.toString(),
               amount: tx.amount,
               currentUd: appCubit.currentUd,
+              comment: tx.comment,
               from: from.title,
               isG1: isG1);
           currentModifiedState = currentModifiedState.copyWith(
@@ -196,6 +197,7 @@ class MultiWalletTransactionCubit
               tx.time.millisecondsSinceEpoch.toString(),
               amount: -tx.amount,
               currentUd: appCubit.currentUd,
+              comment: tx.comment,
               to: to.title,
               isG1: isG1);
           currentModifiedState =
diff --git a/lib/ui/notification_controller_mobile.dart b/lib/ui/notification_controller_mobile.dart
index ce95b60..e4a661f 100644
--- a/lib/ui/notification_controller_mobile.dart
+++ b/lib/ui/notification_controller_mobile.dart
@@ -144,31 +144,19 @@ class NotificationController {
       {required double amount,
       String? to,
       String? from,
-      String? description = '',
+      String? comment = '',
       required bool isG1,
       required double currentUd}) async {
-    final String title = from != null
-        ? tr('notification_new_payment_title')
-        : tr('notification_new_sent_title');
-    final String desc = from != null
-        ? '${tr('notification_new_payment_desc', namedArgs: <String, String>{
-                'amount': formatKAmountInViewWithLocale(
-                    locale: locale.languageCode,
-                    amount: amount,
-                    isG1: isG1,
-                    currentUd: currentUd,
-                    useSymbol: true),
-                'from': from,
-              })} ($description)'
-        : '${tr('notification_new_sent_desc', namedArgs: <String, String>{
-                'amount': formatKAmountInViewWithLocale(
-                    locale: locale.languageCode,
-                    amount: amount,
-                    isG1: isG1,
-                    currentUd: currentUd,
-                    useSymbol: true),
-                'to': to!,
-              })} ($description)';
+    final String title = buildTxNotifTitle(from);
+    final String desc = buildTxNotifDescription(
+      from: from,
+      to: to,
+      comment: comment,
+      localeLanguageCode: locale.languageCode,
+      amount: amount,
+      isG1: isG1,
+      currentUd: currentUd,
+    );
     await notify(title: title, desc: desc, id: id);
   }
 
diff --git a/lib/ui/notification_controller_web.dart b/lib/ui/notification_controller_web.dart
index 411f03f..59ab257 100644
--- a/lib/ui/notification_controller_web.dart
+++ b/lib/ui/notification_controller_web.dart
@@ -97,29 +97,17 @@ class NotificationController {
       String? description = '',
       required double currentUd,
       required bool isG1}) async {
-    // FIXME: DUP CODE!!
-    final String title = from != null
-        ? tr('notification_new_payment_title')
-        : tr('notification_new_sent_title');
-    final String desc = from != null
-        ? '${tr('notification_new_payment_desc', namedArgs: <String, String>{
-                'amount': formatKAmountInViewWithLocale(
-                    locale: locale.languageCode,
-                    amount: amount,
-                    isG1: isG1,
-                    currentUd: currentUd,
-                    useSymbol: true),
-                'from': from,
-              })} ($description)'
-        : '${tr('notification_new_sent_desc', namedArgs: <String, String>{
-                'amount': formatKAmountInViewWithLocale(
-                    locale: locale.languageCode,
-                    amount: amount,
-                    isG1: isG1,
-                    currentUd: currentUd,
-                    useSymbol: true),
-                'to': to!
-              })} ($description)';
+    final String title = buildTxNotifTitle(from);
+    final String desc = buildTxNotifDescription(
+      from: from,
+      to: to,
+      comment: description,
+      localeLanguageCode: locale.languageCode,
+      amount: amount,
+      isG1: isG1,
+      currentUd: currentUd,
+    );
+
     await notify(title: title, desc: desc, id: '');
   }
 
diff --git a/lib/ui/ui_helpers.dart b/lib/ui/ui_helpers.dart
index 8b437fe..a6cd24a 100644
--- a/lib/ui/ui_helpers.dart
+++ b/lib/ui/ui_helpers.dart
@@ -655,3 +655,37 @@ Future<void> hydratedInit() async {
         await HydratedStorage.build(storageDirectory: tmpDir);
   }
 }
+
+String buildTxNotifTitle(String? from) {
+  final String title = from != null
+      ? tr('notification_new_payment_title')
+      : tr('notification_new_sent_title');
+  return title;
+}
+
+String buildTxNotifDescription({
+  required String? from,
+  required String? to,
+  required String? comment,
+  required String localeLanguageCode,
+  required double amount,
+  required bool isG1,
+  required double currentUd,
+}) {
+  final String formattedAmount = formatKAmountInViewWithLocale(
+    locale: localeLanguageCode,
+    amount: amount,
+    isG1: isG1,
+    currentUd: currentUd,
+    useSymbol: true,
+  );
+  final String desc = from != null
+      ? tr('notification_new_payment_desc', namedArgs: <String, String>{
+          'amount': formattedAmount,
+          'from': from,
+        })
+      : tr('notification_new_sent_desc',
+          namedArgs: <String, String>{'amount': formattedAmount, 'to': to!});
+
+  return comment != null && comment.isNotEmpty ? '$desc ($comment)' : desc;
+}
-- 
GitLab