From 1fa7bc40a87649ace1daead9274900faeccaaa8c Mon Sep 17 00:00:00 2001
From: vjrj <vjrj@comunes.org>
Date: Sat, 30 Sep 2023 12:51:18 +0200
Subject: [PATCH] Notifications refactor

---
 lib/data/models/transaction_cubit.dart     |  4 +-
 lib/ui/notification_controller_mobile.dart | 37 +++++++++---------
 lib/ui/notification_controller_web.dart    | 44 ++++++++++++----------
 3 files changed, 46 insertions(+), 39 deletions(-)

diff --git a/lib/data/models/transaction_cubit.dart b/lib/data/models/transaction_cubit.dart
index 0c01395e..c8b142fe 100644
--- a/lib/data/models/transaction_cubit.dart
+++ b/lib/data/models/transaction_cubit.dart
@@ -197,7 +197,7 @@ class TransactionCubitRemove extends HydratedCubit<TransactionState> {
             newState.latestReceivedNotification.isBefore(tx.time)) {
           // Future
           final Contact from = tx.from;
-          NotificationController.createNewNotification(
+          NotificationController.notifyTransaction(
               tx.time.millisecondsSinceEpoch.toString(),
               amount: tx.amount,
               currentUd: appCubit.currentUd,
@@ -209,7 +209,7 @@ class TransactionCubitRemove extends HydratedCubit<TransactionState> {
             newState.latestSentNotification.isBefore(tx.time)) {
           // Future
           final Contact to = tx.to;
-          NotificationController.createNewNotification(
+          NotificationController.notifyTransaction(
               tx.time.millisecondsSinceEpoch.toString(),
               amount: -tx.amount,
               currentUd: appCubit.currentUd,
diff --git a/lib/ui/notification_controller_mobile.dart b/lib/ui/notification_controller_mobile.dart
index f895e9df..ce95b605 100644
--- a/lib/ui/notification_controller_mobile.dart
+++ b/lib/ui/notification_controller_mobile.dart
@@ -144,30 +144,31 @@ class NotificationController {
       {required double amount,
       String? to,
       String? from,
+      String? description = '',
       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,
-          })
-        : tr('notification_new_sent_desc', namedArgs: <String, String>{
-            'amount': formatKAmountInViewWithLocale(
-                locale: locale.languageCode,
-                amount: amount,
-                isG1: isG1,
-                currentUd: currentUd,
-                useSymbol: true),
-            'to': to!,
-          });
+        ? '${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)';
     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 c38d4f91..411f03fb 100644
--- a/lib/ui/notification_controller_web.dart
+++ b/lib/ui/notification_controller_web.dart
@@ -90,10 +90,11 @@ class NotificationController {
   ///     NOTIFICATION CREATION METHODS
   ///  *********************************************
   ///
-  static Future<void> createNewNotification(String id,
+  static Future<void> notifyTransaction(String id,
       {required double amount,
       String? to,
       String? from,
+      String? description = '',
       required double currentUd,
       required bool isG1}) async {
     // FIXME: DUP CODE!!
@@ -101,24 +102,29 @@ class NotificationController {
         ? 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,
-          })
-        : tr('notification_new_sent_desc', namedArgs: <String, String>{
-            'amount': formatKAmountInViewWithLocale(
-                locale: locale.languageCode,
-                amount: amount,
-                isG1: isG1,
-                currentUd: currentUd,
-                useSymbol: true),
-            'to': to!,
-          });
+        ? '${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)';
+    await notify(title: title, desc: desc, id: '');
+  }
+
+  static Future<void> notify(
+      {required String title, required String desc, required String id}) async {
     try {
       if (html.Notification.permission != 'granted') {
         await html.Notification.requestPermission();
-- 
GitLab