diff --git a/mobile/lib/pages/common/splash_screen.page.dart b/mobile/lib/pages/common/splash_screen.page.dart index 7ed601734b..2de8bb4364 100644 --- a/mobile/lib/pages/common/splash_screen.page.dart +++ b/mobile/lib/pages/common/splash_screen.page.dart @@ -19,6 +19,7 @@ class SplashScreenPage extends HookConsumerWidget { Widget build(BuildContext context, WidgetRef ref) { final apiService = ref.watch(apiServiceProvider); final serverUrl = Store.tryGet(StoreKey.serverUrl); + final endpoint = Store.tryGet(StoreKey.serverEndpoint); final accessToken = Store.tryGet(StoreKey.accessToken); final log = Logger("SplashScreenPage"); @@ -26,30 +27,8 @@ class SplashScreenPage extends HookConsumerWidget { bool isSuccess = false; bool deviceIsOffline = false; - if (accessToken != null && serverUrl != null) { - try { - // Resolve API server endpoint from user provided serverUrl - await apiService.resolveAndSetEndpoint(serverUrl); - } on ApiException catch (error, stackTrace) { - log.severe( - "Failed to resolve endpoint [ApiException]", - error, - stackTrace, - ); - // okay, try to continue anyway if offline - if (error.code == 503) { - deviceIsOffline = true; - log.warning("Device seems to be offline upon launch"); - } else { - log.severe("Failed to resolve endpoint", error); - } - } catch (error, stackTrace) { - log.severe( - "Failed to resolve endpoint [Catch All]", - error, - stackTrace, - ); - } + if (accessToken != null && serverUrl != null && endpoint != null) { + apiService.setEndpoint(endpoint); try { isSuccess = await ref @@ -66,29 +45,30 @@ class SplashScreenPage extends HookConsumerWidget { stackTrace, ); } - } - - // If the device is offline and there is a currentUser stored locallly - // Proceed into the app - if (deviceIsOffline && Store.tryGet(StoreKey.currentUser) != null) { - context.replaceRoute(const TabControllerRoute()); - } else if (isSuccess) { - // If device was able to login through the internet successfully - final hasPermission = - await ref.read(galleryPermissionNotifier.notifier).hasPermission; - if (hasPermission) { - // Resume backup (if enable) then navigate - ref.watch(backupProvider.notifier).resumeBackup(); - } - context.replaceRoute(const TabControllerRoute()); } else { log.severe( - 'Unable to login through offline or online methods - logging out completely', + 'Missing authentication and server information from the local storage', ); + isSuccess = false; + } + + if (!isSuccess) { + log.severe( + 'Unable to login using offline or online methods - Logging out completely', + ); ref.read(authenticationProvider.notifier).logout(); - // User was unable to login through either offline or online methods context.replaceRoute(const LoginRoute()); + return; + } + + context.replaceRoute(const TabControllerRoute()); + + final hasPermission = + await ref.read(galleryPermissionNotifier.notifier).hasPermission; + if (hasPermission) { + // Resume backup (if enable) then navigate + ref.watch(backupProvider.notifier).resumeBackup(); } } diff --git a/mobile/lib/services/api.service.dart b/mobile/lib/services/api.service.dart index c128a2c2fc..8a9c03765e 100644 --- a/mobile/lib/services/api.service.dart +++ b/mobile/lib/services/api.service.dart @@ -64,7 +64,9 @@ class ApiService implements Authentication { } Future resolveAndSetEndpoint(String serverUrl) async { - final endpoint = await _resolveEndpoint(serverUrl); + var endpoint = Store.tryGet(StoreKey.serverEndpoint); + + endpoint ??= await _resolveEndpoint(serverUrl); setEndpoint(endpoint); // Save in hivebox for next startup