diff --git a/assets/changelog.json b/assets/changelog.json index 6607d80..1e4a98c 100644 --- a/assets/changelog.json +++ b/assets/changelog.json @@ -2,7 +2,8 @@ "2.10.2": { "content": [ "• Added tap and hold gesture on empty map area to add a node at that location", - "• Added ability to accept direct links to a specific node for callbacks from e.g. FlockHopper" + "• Added ability to accept direct links to a specific node for callbacks from e.g. FlockHopper", + "• Fixed app hang during startup when suspected locations database needed updating - refresh now happens in background" ] }, "2.10.1": { diff --git a/lib/app_state.dart b/lib/app_state.dart index c59cebb..1cf00a8 100644 --- a/lib/app_state.dart +++ b/lib/app_state.dart @@ -250,6 +250,9 @@ class AppState extends ChangeNotifier { _isInitialized = true; + // Start background refresh of suspected locations if needed (non-blocking) + _suspectedLocationState.initBackgroundRefresh(offlineMode: _settingsState.offlineMode); + // Check for initial deep link after a small delay to let navigation settle Future.delayed(const Duration(milliseconds: 500), () { DeepLinkService().checkInitialLink(); diff --git a/lib/services/suspected_location_service.dart b/lib/services/suspected_location_service.dart index 2d8ee87..6557cdc 100644 --- a/lib/services/suspected_location_service.dart +++ b/lib/services/suspected_location_service.dart @@ -35,16 +35,31 @@ class SuspectedLocationService { // Load cache data await _cache.loadFromStorage(); - // Only auto-fetch if enabled, data is stale or missing, and we are not offline - if (_isEnabled && (await _shouldRefresh()) && !offlineMode) { - debugPrint('[SuspectedLocationService] Auto-refreshing CSV data on startup (older than $_maxAge or missing)'); - await _fetchData(); - } else if (_isEnabled && (await _shouldRefresh()) && offlineMode) { + // Note: Auto-refresh logic moved to initBackgroundRefresh() to avoid blocking app startup + if (_isEnabled && (await _shouldRefresh()) && offlineMode) { final lastFetch = await _cache.lastFetchTime; debugPrint('[SuspectedLocationService] Skipping auto-refresh due to offline mode - data is ${lastFetch != null ? 'outdated' : 'missing'}'); } } + /// Check if background refresh should happen and start it (non-blocking) + Future initBackgroundRefresh({bool offlineMode = false}) async { + // Only auto-fetch if enabled, data is stale or missing, and we are not offline + if (_isEnabled && (await _shouldRefresh()) && !offlineMode) { + debugPrint('[SuspectedLocationService] Starting background auto-refresh of CSV data (older than $_maxAge or missing)'); + // Don't await - let it run in background + _fetchData().then((success) { + if (success) { + debugPrint('[SuspectedLocationService] Background auto-refresh completed successfully'); + } else { + debugPrint('[SuspectedLocationService] Background auto-refresh failed'); + } + }).catchError((error) { + debugPrint('[SuspectedLocationService] Background auto-refresh error: $error'); + }); + } + } + /// Enable or disable suspected locations Future setEnabled(bool enabled) async { _isEnabled = enabled; diff --git a/lib/state/suspected_location_state.dart b/lib/state/suspected_location_state.dart index 5bdaa7b..f302051 100644 --- a/lib/state/suspected_location_state.dart +++ b/lib/state/suspected_location_state.dart @@ -61,6 +61,11 @@ class SuspectedLocationState extends ChangeNotifier { notifyListeners(); } + /// Start background refresh if needed (non-blocking) + Future initBackgroundRefresh({bool offlineMode = false}) async { + await _service.initBackgroundRefresh(offlineMode: offlineMode); + } + /// Enable or disable suspected locations Future setEnabled(bool enabled) async { await _service.setEnabled(enabled);