2024-01-05 07:20:55 +02:00
|
|
|
import 'package:auto_route/auto_route.dart';
|
2022-06-21 01:10:23 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2023-03-23 03:36:44 +02:00
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart' hide Store;
|
2022-06-21 01:10:23 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:immich_mobile/modules/backup/providers/backup.provider.dart';
|
|
|
|
import 'package:immich_mobile/modules/login/providers/authentication.provider.dart';
|
2023-02-28 18:22:18 +02:00
|
|
|
import 'package:immich_mobile/modules/onboarding/providers/gallery_permission.provider.dart';
|
2022-06-21 01:10:23 +02:00
|
|
|
import 'package:immich_mobile/routing/router.dart';
|
2024-05-01 04:36:40 +02:00
|
|
|
import 'package:immich_mobile/entities/store.entity.dart';
|
2022-11-20 19:43:10 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
2023-07-28 05:05:27 +02:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:openapi/api.dart';
|
2022-06-21 01:10:23 +02:00
|
|
|
|
2024-01-15 18:50:33 +02:00
|
|
|
@RoutePage()
|
2022-06-21 01:10:23 +02:00
|
|
|
class SplashScreenPage extends HookConsumerWidget {
|
2024-01-27 18:14:32 +02:00
|
|
|
const SplashScreenPage({super.key});
|
2022-06-21 01:10:23 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2022-11-20 19:43:10 +02:00
|
|
|
final apiService = ref.watch(apiServiceProvider);
|
2023-03-23 03:36:44 +02:00
|
|
|
final serverUrl = Store.tryGet(StoreKey.serverUrl);
|
|
|
|
final accessToken = Store.tryGet(StoreKey.accessToken);
|
2023-07-28 05:05:27 +02:00
|
|
|
final log = Logger("SplashScreenPage");
|
2022-06-21 01:10:23 +02:00
|
|
|
|
|
|
|
void performLoggingIn() async {
|
2023-03-15 23:29:07 +02:00
|
|
|
bool isSuccess = false;
|
2023-07-28 05:05:27 +02:00
|
|
|
bool deviceIsOffline = false;
|
2024-04-23 23:09:10 +02:00
|
|
|
|
2023-03-23 03:36:44 +02:00
|
|
|
if (accessToken != null && serverUrl != null) {
|
2023-03-15 23:29:07 +02:00
|
|
|
try {
|
2023-01-19 17:45:37 +02:00
|
|
|
// Resolve API server endpoint from user provided serverUrl
|
2023-03-23 03:36:44 +02:00
|
|
|
await apiService.resolveAndSetEndpoint(serverUrl);
|
2024-04-23 23:09:10 +02:00
|
|
|
} on ApiException catch (error, stackTrace) {
|
|
|
|
log.severe(
|
|
|
|
"Failed to resolve endpoint [ApiException]",
|
|
|
|
error,
|
|
|
|
stackTrace,
|
|
|
|
);
|
2023-03-15 23:29:07 +02:00
|
|
|
// okay, try to continue anyway if offline
|
2024-04-23 23:09:10 +02:00
|
|
|
if (error.code == 503) {
|
2023-07-28 05:05:27 +02:00
|
|
|
deviceIsOffline = true;
|
2024-04-23 23:09:10 +02:00
|
|
|
log.warning("Device seems to be offline upon launch");
|
2023-07-28 05:05:27 +02:00
|
|
|
} else {
|
2024-04-23 23:09:10 +02:00
|
|
|
log.severe("Failed to resolve endpoint", error);
|
2023-07-28 05:05:27 +02:00
|
|
|
}
|
2024-04-23 23:09:10 +02:00
|
|
|
} catch (error, stackTrace) {
|
|
|
|
log.severe(
|
|
|
|
"Failed to resolve endpoint [Catch All]",
|
|
|
|
error,
|
|
|
|
stackTrace,
|
|
|
|
);
|
2023-03-15 23:29:07 +02:00
|
|
|
}
|
2022-06-21 01:10:23 +02:00
|
|
|
|
2023-11-13 22:15:36 +02:00
|
|
|
try {
|
|
|
|
isSuccess = await ref
|
|
|
|
.read(authenticationProvider.notifier)
|
|
|
|
.setSuccessLoginInfo(
|
|
|
|
accessToken: accessToken,
|
|
|
|
serverUrl: serverUrl,
|
|
|
|
offlineLogin: deviceIsOffline,
|
|
|
|
);
|
|
|
|
} catch (error, stackTrace) {
|
|
|
|
log.severe(
|
2024-02-24 05:38:57 +02:00
|
|
|
'Cannot set success login info',
|
2023-11-13 22:15:36 +02:00
|
|
|
error,
|
|
|
|
stackTrace,
|
|
|
|
);
|
|
|
|
}
|
2023-03-15 23:29:07 +02:00
|
|
|
}
|
2023-07-28 05:05:27 +02:00
|
|
|
|
|
|
|
// If the device is offline and there is a currentUser stored locallly
|
|
|
|
// Proceed into the app
|
|
|
|
if (deviceIsOffline && Store.tryGet(StoreKey.currentUser) != null) {
|
2024-01-05 07:20:55 +02:00
|
|
|
context.replaceRoute(const TabControllerRoute());
|
2023-07-28 05:05:27 +02:00
|
|
|
} else if (isSuccess) {
|
|
|
|
// If device was able to login through the internet successfully
|
2023-03-15 23:29:07 +02:00
|
|
|
final hasPermission =
|
|
|
|
await ref.read(galleryPermissionNotifier.notifier).hasPermission;
|
|
|
|
if (hasPermission) {
|
|
|
|
// Resume backup (if enable) then navigate
|
|
|
|
ref.watch(backupProvider.notifier).resumeBackup();
|
2022-11-20 19:43:10 +02:00
|
|
|
}
|
2024-01-05 07:20:55 +02:00
|
|
|
context.replaceRoute(const TabControllerRoute());
|
2023-03-15 23:29:07 +02:00
|
|
|
} else {
|
2024-04-23 23:09:10 +02:00
|
|
|
log.severe(
|
|
|
|
'Unable to login through offline or online methods - logging out completely',
|
|
|
|
);
|
|
|
|
|
|
|
|
ref.read(authenticationProvider.notifier).logout();
|
2023-07-28 05:05:27 +02:00
|
|
|
// User was unable to login through either offline or online methods
|
2024-01-05 07:20:55 +02:00
|
|
|
context.replaceRoute(const LoginRoute());
|
2022-06-21 01:10:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
useEffect(
|
|
|
|
() {
|
2023-03-23 03:36:44 +02:00
|
|
|
if (serverUrl != null && accessToken != null) {
|
2022-07-13 14:23:48 +02:00
|
|
|
performLoggingIn();
|
|
|
|
} else {
|
2024-01-05 07:20:55 +02:00
|
|
|
context.replaceRoute(const LoginRoute());
|
2022-07-13 14:23:48 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
2022-06-21 01:10:23 +02:00
|
|
|
|
2023-02-06 08:41:07 +02:00
|
|
|
return const Scaffold(
|
2022-06-21 01:10:23 +02:00
|
|
|
body: Center(
|
2023-02-06 08:41:07 +02:00
|
|
|
child: Image(
|
2024-03-13 19:14:59 +02:00
|
|
|
image: AssetImage('assets/immich-logo.png'),
|
2023-02-06 08:41:07 +02:00
|
|
|
width: 80,
|
|
|
|
filterQuality: FilterQuality.high,
|
2022-06-21 01:10:23 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|