From a8025ffbec13f05009c5f688464544a72223fdb5 Mon Sep 17 00:00:00 2001 From: OmarHatem Date: Tue, 7 Feb 2023 16:53:57 +0200 Subject: [PATCH] Add cool-down of 7 days between reports --- lib/entities/preferences_key.dart | 1 + lib/utils/exception_handler.dart | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/entities/preferences_key.dart b/lib/entities/preferences_key.dart index 90d4dcad7..a44dcb3d9 100644 --- a/lib/entities/preferences_key.dart +++ b/lib/entities/preferences_key.dart @@ -29,6 +29,7 @@ class PreferencesKey { static const moneroWalletPasswordUpdateV1Base = 'monero_wallet_update_v1'; static const pinTimeOutDuration = 'pin_timeout_duration'; static const lastAuthTimeMilliseconds = 'last_auth_time_milliseconds'; + static const lastPopupDate = 'last_popup_date'; static String moneroWalletUpdateV1Key(String name) diff --git a/lib/utils/exception_handler.dart b/lib/utils/exception_handler.dart index b8d0089ec..0886cd8de 100644 --- a/lib/utils/exception_handler.dart +++ b/lib/utils/exception_handler.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'dart:io'; +import 'package:cake_wallet/entities/preferences_key.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/main.dart'; import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; @@ -9,6 +10,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_mailer/flutter_mailer.dart'; import 'package:path_provider/path_provider.dart'; +import 'package:shared_preferences/shared_preferences.dart'; class ExceptionHandler { static bool _hasError = false; @@ -59,7 +61,7 @@ class ExceptionHandler { } } - static void onError(FlutterErrorDetails errorDetails) { + static void onError(FlutterErrorDetails errorDetails) async { if (kDebugMode) { FlutterError.presentError(errorDetails); return; @@ -71,11 +73,22 @@ class ExceptionHandler { _saveException(errorDetails.exception.toString(), errorDetails.stack); - if (_hasError) { + final sharedPrefs = await SharedPreferences.getInstance(); + + final lastPopupDate = + DateTime.tryParse(sharedPrefs.getString(PreferencesKey.lastPopupDate) ?? '') ?? + DateTime.parse("2001-01-01"); + + final durationSinceLastReport = DateTime.now().difference(lastPopupDate).inDays; + + // cool-down duration to be 7 days between reports + if (_hasError || durationSinceLastReport < 7) { return; } _hasError = true; + sharedPrefs.setString(PreferencesKey.lastPopupDate, DateTime.now().toString()); + WidgetsBinding.instance.addPostFrameCallback( (timeStamp) async { await showPopUp(