From 262efcea70268a8630388fb2bc4ca0d83f45e7e5 Mon Sep 17 00:00:00 2001
From: vjrj <vjrj@comunes.org>
Date: Sat, 8 Apr 2023 01:53:15 +0200
Subject: [PATCH] Improve notification. Use cron instead of once

---
 lib/data/models/transaction_cubit.dart |  2 +-
 lib/main.dart                          | 32 ++++++++++++++------------
 pubspec.lock                           |  8 +++++++
 pubspec.yaml                           |  1 +
 4 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/lib/data/models/transaction_cubit.dart b/lib/data/models/transaction_cubit.dart
index fbb73950..6ec5eeca 100644
--- a/lib/data/models/transaction_cubit.dart
+++ b/lib/data/models/transaction_cubit.dart
@@ -68,7 +68,7 @@ class TransactionsCubit extends HydratedCubit<TransactionsAndBalanceState> {
         final Contact to = await ContactsCache().getContact(tx.from);
         NotificationController.createNewNotification(
             tx.time.millisecondsSinceEpoch.toString(),
-            amount: tx.amount / 100,
+            amount: -tx.amount / 100,
             to: to.title);
         emit(newState.copyWith(lastSentNotification: tx.time));
       }
diff --git a/lib/main.dart b/lib/main.dart
index a22034f9..93224a74 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -1,6 +1,7 @@
 import 'dart:io';
 
 import 'package:connectivity_wrapper/connectivity_wrapper.dart';
+import 'package:cron/cron.dart';
 import 'package:easy_localization/easy_localization.dart';
 import 'package:filesystem_picker/filesystem_picker.dart';
 import 'package:flutter/foundation.dart';
@@ -276,6 +277,22 @@ class _GinkgoAppState extends State<GinkgoApp> {
     NodeManager().loadFromCubit(context.read<NodeListCubit>());
     // Only after at least the action method is set, the notification events are delivered
     NotificationController.startListeningNotificationEvents();
+    final Cron cron = Cron();
+    cron.schedule(Schedule.parse(kReleaseMode ? '*/10 * * * *' : '*/2 * * * *'),
+        () async {
+      logger('---------- fetchTransactions via cron');
+      fetchTransactions(context);
+    });
+    Once.runHourly('load_nodes', callback: () {
+      logger('load nodes via once');
+      _loadNodes();
+    }, fallback: () {
+      _printNodeStatus(prefix: 'After once hourly having');
+    });
+    Once.runDaily('clear_errors', callback: () {
+      logger('clearErrors via once');
+      NodeManager().cleanErrorStats();
+    });
   }
 
   @override
@@ -288,21 +305,6 @@ class _GinkgoAppState extends State<GinkgoApp> {
   Widget build(BuildContext context) {
     return BlocBuilder<NodeListCubit, NodeListState>(
         builder: (BuildContext nodeContext, NodeListState state) {
-      Once.runHourly('load_nodes',
-          callback: () => _loadNodes(),
-          fallback: () {
-            _printNodeStatus(prefix: 'After once hourly having');
-          });
-      Once.runCustom('clear_errors', callback: () {
-        NodeManager().cleanErrorStats();
-      }, duration: const Duration(minutes: 90));
-      Once.runCustom('fetch_transactions', callback: () {
-        logger('---------- fetchTransactions via once');
-        fetchTransactions(context);
-      }, fallback: () {
-        logger('fetch_transactions fallback, already called');
-      }, duration: const Duration(minutes: kReleaseMode ? 10 : 2));
-      // Not necessary with previous one ?? fetchTransactions(context);
       return ConnectivityAppWrapper(
           app: FilesystemPickerDefaultOptions(
               fileTileSelectMode: FileTileSelectMode.wholeTile,
diff --git a/pubspec.lock b/pubspec.lock
index d2a05f12..5a1edf2b 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -273,6 +273,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "5.0.2"
+  cron:
+    dependency: "direct main"
+    description:
+      name: cron
+      sha256: d98aa8cdad0cccdb6b098e6a1fb89339c180d8a229145fa4cd8c6fc538f0e35f
+      url: "https://pub.dev"
+    source: hosted
+    version: "0.5.1"
   cross_file:
     dependency: transitive
     description:
diff --git a/pubspec.yaml b/pubspec.yaml
index 0ecf59bb..c7cc2eb2 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -72,6 +72,7 @@ dependencies:
   path: ^1.8.2
   path_provider: ^2.0.14
   awesome_notifications: ^0.7.4+1
+  cron: ^0.5.1
 
 dev_dependencies:
   flutter_test:
-- 
GitLab