@@ -16,6 +16,7 @@ import 'package:flutter/foundation.dart';
1616import 'package:tuple/tuple.dart' ;
1717
1818import '../pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart' ;
19+ import '../utilities/enums/backup_frequency_type.dart' ;
1920import '../utilities/flutter_secure_storage_interface.dart' ;
2021import '../utilities/fs.dart' ;
2122import '../utilities/logger.dart' ;
@@ -24,7 +25,12 @@ import '../utilities/prefs.dart';
2425enum AutoSWBStatus { idle, backingUp, error }
2526
2627class AutoSWBService extends ChangeNotifier {
28+ /// Static instance reference set by the provider layer so that non-widget
29+ /// code (e.g. `Wallet` ) can request a backup without Riverpod access.
30+ static AutoSWBService ? instance;
31+
2732 Timer ? _timer;
33+ Timer ? _debounceTimer;
2834
2935 AutoSWBStatus _status = AutoSWBStatus .idle;
3036 AutoSWBStatus get status => _status;
@@ -36,6 +42,36 @@ class AutoSWBService extends ChangeNotifier {
3642
3743 AutoSWBService ({required this .secureStorageInterface});
3844
45+ /// Convenience method to request a backup after an editable change such as
46+ /// a transaction note or address book contact edit. Only triggers if
47+ /// auto-backup is enabled and the frequency is set to
48+ /// [BackupFrequencyType.afterChanges] .
49+ static void requestBackupAfterChange (Prefs prefs) {
50+ if (instance == null ) return ;
51+ if (! prefs.isAutoBackupEnabled) return ;
52+ if (prefs.backupFrequencyType != BackupFrequencyType .afterChanges) return ;
53+
54+ Logging .instance.d (
55+ "AutoSWBService.requestBackupAfterChange() triggered" ,
56+ );
57+ instance! .requestBackup ();
58+ }
59+
60+ /// Request a debounced backup. Multiple calls within [debounceDuration]
61+ /// will be collapsed into a single backup. This prevents backup storms
62+ /// during wallet sync or rapid changes.
63+ void requestBackup ({
64+ Duration debounceDuration = const Duration (seconds: 5 ),
65+ }) {
66+ _debounceTimer? .cancel ();
67+ _debounceTimer = Timer (debounceDuration, () {
68+ Logging .instance.d (
69+ "AutoSWBService.requestBackup() debounce fired, running doBackup()" ,
70+ );
71+ doBackup ();
72+ });
73+ }
74+
3975 /// Attempt a backup.
4076 Future <void > doBackup () async {
4177 if (_status == AutoSWBStatus .backingUp) {
@@ -199,6 +235,8 @@ class AutoSWBService extends ChangeNotifier {
199235
200236 @override
201237 void dispose () {
238+ _debounceTimer? .cancel ();
239+ _debounceTimer = null ;
202240 stopPeriodicBackupTimer (shouldNotifyListeners: false );
203241 super .dispose ();
204242 }
0 commit comments