Rework background updates to avoid circular dependency with active profile provider

merge-requests/67/merge
Hank Grabowski 2024-12-12 14:29:29 -05:00
rodzic 509ac1e8be
commit b137a4cec9
4 zmienionych plików z 40 dodań i 20 usunięć

Wyświetl plik

@ -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() {

Wyświetl plik

@ -54,7 +54,7 @@ final loggedOutProfilesProvider =
);
typedef _$LoggedOutProfiles = Notifier<List<Profile>>;
String _$activeProfileHash() => r'72c565e53c1a58b6469a638c54fe827a5c014138';
String _$activeProfileHash() => r'b831f81dc3880a7980bc29d78573657373837033';
/// See also [ActiveProfile].
@ProviderFor(ActiveProfile)

Wyświetl plik

@ -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<void> 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<void> _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;
}

Wyświetl plik

@ -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<Timer> {
abstract class _$BackgroundUpdaters extends BuildlessNotifier<Timer> {
late final Profile profile;
Timer build(
@ -83,7 +82,7 @@ class BackgroundUpdatersFamily extends Family<Timer> {
/// See also [BackgroundUpdaters].
class BackgroundUpdatersProvider
extends AutoDisposeNotifierProviderImpl<BackgroundUpdaters, Timer> {
extends NotifierProviderImpl<BackgroundUpdaters, Timer> {
/// See also [BackgroundUpdaters].
BackgroundUpdatersProvider(
Profile profile,
@ -139,8 +138,7 @@ class BackgroundUpdatersProvider
}
@override
AutoDisposeNotifierProviderElement<BackgroundUpdaters, Timer>
createElement() {
NotifierProviderElement<BackgroundUpdaters, Timer> 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<Timer> {
mixin BackgroundUpdatersRef on NotifierProviderRef<Timer> {
/// The parameter `profile` of this provider.
Profile get profile;
}
class _BackgroundUpdatersProviderElement
extends AutoDisposeNotifierProviderElement<BackgroundUpdaters, Timer>
extends NotifierProviderElement<BackgroundUpdaters, Timer>
with BackgroundUpdatersRef {
_BackgroundUpdatersProviderElement(super.provider);