From 6aecb654a2f535b9f57a446248d6c9e6063d4c1c Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Thu, 27 Apr 2023 22:05:10 -0400 Subject: [PATCH] Add automatic updateAllContacts on app startup and every 8 hours after if not in low bandwidth mode. --- lib/di_initialization.dart | 26 ++++++++++++++++++++++---- lib/screens/contacts_screen.dart | 1 + lib/services/connections_manager.dart | 9 +++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/di_initialization.dart b/lib/di_initialization.dart index eff785f..cf67132 100644 --- a/lib/di_initialization.dart +++ b/lib/di_initialization.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:logging/logging.dart'; import 'data/interfaces/connections_repo_intf.dart'; @@ -71,10 +73,26 @@ Future dependencyInjectionInitialization() async { getIt.registerSingleton>( ActiveProfileSelector( - (p) => ConnectionsManager( - getIt>().getForProfile(p).value, - getIt>().getForProfile(p).value, - ), + (p) { + final manager = ConnectionsManager( + getIt>().getForProfile(p).value, + getIt>().getForProfile(p).value, + ); + + Future.delayed(Duration.zero, () async { + if (!settingsService.lowBandwidthMode) { + await manager.updateAllContacts(); + } + }); + + Timer.periodic(const Duration(hours: 8), (_) async { + if (!settingsService.lowBandwidthMode) { + await manager.updateAllContacts(); + } + }); + + return manager; + }, )); getIt.registerSingleton>( diff --git a/lib/screens/contacts_screen.dart b/lib/screens/contacts_screen.dart index 17d9163..a768b9c 100644 --- a/lib/screens/contacts_screen.dart +++ b/lib/screens/contacts_screen.dart @@ -128,6 +128,7 @@ class _ContactsScreenState extends State { ) ], ), + Text(manager.lastUpdateStatus), StandardLinearProgressIndicator(nss.connectionUpdateStatus), Expanded(child: body), ], diff --git a/lib/services/connections_manager.dart b/lib/services/connections_manager.dart index 7bdd254..d1a9944 100644 --- a/lib/services/connections_manager.dart +++ b/lib/services/connections_manager.dart @@ -15,11 +15,16 @@ import '../models/exec_error.dart'; import '../models/group_data.dart'; import 'auth_service.dart'; +const _notUpdatedYetMesasge = 'Not updated since app started'; + class ConnectionsManager extends ChangeNotifier { static final _logger = Logger('$ConnectionsManager'); late final IConnectionsRepo conRepo; late final IGroupsRepo groupsRepo; var groupsNotInitialized = true; + var _lastUpdateStatus = _notUpdatedYetMesasge; + + String get lastUpdateStatus => _lastUpdateStatus; ConnectionsManager(this.conRepo, this.groupsRepo); @@ -27,6 +32,7 @@ class ConnectionsManager extends ChangeNotifier { conRepo.clear(); groupsRepo.clear(); groupsNotInitialized = true; + _lastUpdateStatus = _notUpdatedYetMesasge; notifyListeners(); } @@ -148,6 +154,8 @@ class ConnectionsManager extends ChangeNotifier { Future updateAllContacts() async { _logger.fine('Updating all contacts'); + _lastUpdateStatus = 'Updating'; + notifyListeners(); final client = RelationshipsClient(getIt().currentProfile); final results = {}; var moreResults = true; @@ -203,6 +211,7 @@ class ConnectionsManager extends ChangeNotifier { final myContacts = conRepo.getMyContacts().toList(); myContacts.sort((c1, c2) => c1.name.compareTo(c2.name)); _logger.fine('# Contacts:${myContacts.length}'); + _lastUpdateStatus = 'Last updated at: ${DateTime.now()}'; notifyListeners(); }