From b137a4cec954c08012a465b867113bc7af1fe517 Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Thu, 12 Dec 2024 14:29:29 -0500 Subject: [PATCH] Rework background updates to avoid circular dependency with active profile provider --- .../account_services.dart | 6 +-- .../account_services.g.dart | 2 +- .../background_updater_services.dart | 38 +++++++++++++++---- .../background_updater_services.g.dart | 14 +++---- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/lib/riverpod_controllers/account_services.dart b/lib/riverpod_controllers/account_services.dart index e5288b9..ad78b34 100644 --- a/lib/riverpod_controllers/account_services.dart +++ b/lib/riverpod_controllers/account_services.dart @@ -66,8 +66,6 @@ const _activeProfileIdKey = 'active_profile_id'; @Riverpod(keepAlive: true) class ActiveProfile extends _$ActiveProfile { - Timer? backgroundUpdater; - bool get hasActiveProfile => state != Profile.empty(); @override @@ -76,10 +74,10 @@ class ActiveProfile extends _$ActiveProfile { } void setActiveProfile(Profile profile) { - backgroundUpdater?.cancel(); + ref.read(backgroundUpdatersProvider(state).notifier).stop(); state = profile; _saveStoredLoginState(); - backgroundUpdater = ref.watch(backgroundUpdatersProvider(profile)); + ref.read(backgroundUpdatersProvider(state).notifier).reset(); } void clear() { diff --git a/lib/riverpod_controllers/account_services.g.dart b/lib/riverpod_controllers/account_services.g.dart index 8d0b2b3..5dd7b04 100644 --- a/lib/riverpod_controllers/account_services.g.dart +++ b/lib/riverpod_controllers/account_services.g.dart @@ -54,7 +54,7 @@ final loggedOutProfilesProvider = ); typedef _$LoggedOutProfiles = Notifier>; -String _$activeProfileHash() => r'72c565e53c1a58b6469a638c54fe827a5c014138'; +String _$activeProfileHash() => r'b831f81dc3880a7980bc29d78573657373837033'; /// See also [ActiveProfile]. @ProviderFor(ActiveProfile) diff --git a/lib/riverpod_controllers/background_updater_services.dart b/lib/riverpod_controllers/background_updater_services.dart index 2b2e5b4..dae97c7 100644 --- a/lib/riverpod_controllers/background_updater_services.dart +++ b/lib/riverpod_controllers/background_updater_services.dart @@ -17,40 +17,64 @@ final _logger = Logger('BackgroundUpdatersProvider'); // TODO should this stop and be deleted if a profile is switched? // TODO Confirm this is being bootstrapped correctly and running consistently -@riverpod +@Riverpod(keepAlive: true) class BackgroundUpdaters extends _$BackgroundUpdaters { bool _executingUpdate = false; @override Timer build(Profile profile) { _logger.info('Building BackgroundUpdater for $profile'); - ref.onDispose(() => state.cancel()); + ref.onDispose(() { + _logger.info('Disposing of Background updater for $profile'); + state.cancel(); + }); return Timer.periodic(_timerRefresh, (_) async { - await executeUpdatesForProfile(); + await _executeUpdatesForProfile(); }); } - Future executeUpdatesForProfile() async { + Timer stop() { + _logger.info('Stopping background updates for $profile'); + state.cancel(); + return state; + } + + Timer reset() { + _logger.info('Resetting background updates for $profile'); + state.cancel(); + state = Timer.periodic(_timerRefresh, (_) async { + await _executeUpdatesForProfile(); + }); + return state; + } + + Future _executeUpdatesForProfile() async { + _logger.fine('_executeUpdatesForProfile for $profile'); if (_executingUpdate) { + _logger.fine('Already executing updating so skipping for $profile'); return; } final lowBandwidthMode = ref.watch(lowBandwidthModeSettingProvider); if (lowBandwidthMode) { + _logger.fine('low bandwidth mode so skipping for $profile'); return; } if (ref.read(activeProfileProvider) != profile) { + _logger.fine('Skipping update since no longer active profile: $profile'); + state.cancel(); return; } - _logger.info('Executing background update for $profile'); + _logger.fine('Executing background update for $profile'); final dt = DateTime.now().difference(ref.watch(persistentInfoProvider(profile))); - _logger.finer('Time since last update for ${profile.id}: $dt'); + _logger.fine('Time since last update for ${profile.id}: $dt'); if (dt >= _connectionsRefreshInterval) { if (_executingUpdate) { + _logger.fine('Already executing updating so skipping for $profile'); return; } _executingUpdate = true; @@ -59,7 +83,7 @@ class BackgroundUpdaters extends _$BackgroundUpdaters { .read(allContactsUpdaterProvider(profile).notifier) .updateAllContacts(true); } catch (e) { - _logger.severe('Exception thrwn while executing update: $e'); + _logger.severe('Exception thrown while executing update: $e'); } _executingUpdate = false; } diff --git a/lib/riverpod_controllers/background_updater_services.g.dart b/lib/riverpod_controllers/background_updater_services.g.dart index b5e6020..78dae91 100644 --- a/lib/riverpod_controllers/background_updater_services.g.dart +++ b/lib/riverpod_controllers/background_updater_services.g.dart @@ -7,7 +7,7 @@ part of 'background_updater_services.dart'; // ************************************************************************** String _$backgroundUpdatersHash() => - r'497dff6c83cb42955cdc5adb34c0b7083033f0f9'; + r'54c2ee385cff7c2460e1ab411a0e9349e865d00b'; /// Copied from Dart SDK class _SystemHash { @@ -30,8 +30,7 @@ class _SystemHash { } } -abstract class _$BackgroundUpdaters - extends BuildlessAutoDisposeNotifier { +abstract class _$BackgroundUpdaters extends BuildlessNotifier { late final Profile profile; Timer build( @@ -83,7 +82,7 @@ class BackgroundUpdatersFamily extends Family { /// See also [BackgroundUpdaters]. class BackgroundUpdatersProvider - extends AutoDisposeNotifierProviderImpl { + extends NotifierProviderImpl { /// See also [BackgroundUpdaters]. BackgroundUpdatersProvider( Profile profile, @@ -139,8 +138,7 @@ class BackgroundUpdatersProvider } @override - AutoDisposeNotifierProviderElement - createElement() { + NotifierProviderElement createElement() { return _BackgroundUpdatersProviderElement(this); } @@ -160,13 +158,13 @@ class BackgroundUpdatersProvider @Deprecated('Will be removed in 3.0. Use Ref instead') // ignore: unused_element -mixin BackgroundUpdatersRef on AutoDisposeNotifierProviderRef { +mixin BackgroundUpdatersRef on NotifierProviderRef { /// The parameter `profile` of this provider. Profile get profile; } class _BackgroundUpdatersProviderElement - extends AutoDisposeNotifierProviderElement + extends NotifierProviderElement with BackgroundUpdatersRef { _BackgroundUpdatersProviderElement(super.provider);