2024-01-05 06:20:55 +01:00
|
|
|
import 'package:auto_route/auto_route.dart';
|
2022-06-21 01:10:23 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-05-02 22:59:14 +02:00
|
|
|
import 'package:immich_mobile/providers/backup/backup.provider.dart';
|
2024-11-26 19:43:44 +01:00
|
|
|
import 'package:immich_mobile/providers/auth.provider.dart';
|
2024-05-02 22:59:14 +02:00
|
|
|
import 'package:immich_mobile/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';
|
2023-07-28 05:05:27 +02:00
|
|
|
import 'package:logging/logging.dart';
|
2022-06-21 01:10:23 +02:00
|
|
|
|
2024-01-15 17:50:33 +01:00
|
|
|
@RoutePage()
|
2024-12-05 16:11:48 +01:00
|
|
|
class SplashScreenPage extends StatefulHookConsumerWidget {
|
2024-01-27 17:14:32 +01:00
|
|
|
const SplashScreenPage({super.key});
|
2022-06-21 01:10:23 +02:00
|
|
|
|
|
|
|
@override
|
2024-12-05 16:11:48 +01:00
|
|
|
SplashScreenPageState createState() => SplashScreenPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class SplashScreenPageState extends ConsumerState<SplashScreenPage> {
|
|
|
|
final log = Logger("SplashScreenPage");
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
ref
|
|
|
|
.read(authProvider.notifier)
|
|
|
|
.setOpenApiServiceEndpoint()
|
|
|
|
.then(logConnectionInfo)
|
|
|
|
.whenComplete(() => resumeSession());
|
|
|
|
}
|
|
|
|
|
|
|
|
void logConnectionInfo(String? endpoint) {
|
|
|
|
if (endpoint == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info("Resuming session at $endpoint");
|
|
|
|
}
|
|
|
|
|
|
|
|
void resumeSession() async {
|
2023-03-23 02:36:44 +01:00
|
|
|
final serverUrl = Store.tryGet(StoreKey.serverUrl);
|
2024-07-30 20:15:48 +02:00
|
|
|
final endpoint = Store.tryGet(StoreKey.serverEndpoint);
|
2023-03-23 02:36:44 +01:00
|
|
|
final accessToken = Store.tryGet(StoreKey.accessToken);
|
2022-06-21 01:10:23 +02:00
|
|
|
|
2024-12-05 16:11:48 +01:00
|
|
|
bool isAuthSuccess = false;
|
2024-04-23 23:09:10 +02:00
|
|
|
|
2024-12-05 16:11:48 +01:00
|
|
|
if (accessToken != null && serverUrl != null && endpoint != null) {
|
|
|
|
try {
|
|
|
|
isAuthSuccess = await ref.read(authProvider.notifier).saveAuthInfo(
|
|
|
|
accessToken: accessToken,
|
|
|
|
);
|
|
|
|
} catch (error, stackTrace) {
|
2024-04-23 23:09:10 +02:00
|
|
|
log.severe(
|
2024-12-05 16:11:48 +01:00
|
|
|
'Cannot set success login info',
|
|
|
|
error,
|
|
|
|
stackTrace,
|
2024-04-23 23:09:10 +02:00
|
|
|
);
|
2024-07-30 20:15:48 +02:00
|
|
|
}
|
2024-12-05 16:11:48 +01:00
|
|
|
} else {
|
|
|
|
isAuthSuccess = false;
|
|
|
|
log.severe(
|
|
|
|
'Missing authentication, server, or endpoint info from the local store',
|
|
|
|
);
|
|
|
|
}
|
2024-04-23 23:09:10 +02:00
|
|
|
|
2024-12-05 16:11:48 +01:00
|
|
|
if (!isAuthSuccess) {
|
|
|
|
log.severe(
|
|
|
|
'Unable to login using offline or online methods - Logging out completely',
|
|
|
|
);
|
|
|
|
ref.read(authProvider.notifier).logout();
|
|
|
|
context.replaceRoute(const LoginRoute());
|
|
|
|
return;
|
|
|
|
}
|
2024-07-30 20:15:48 +02:00
|
|
|
|
2024-12-05 16:11:48 +01:00
|
|
|
context.replaceRoute(const TabControllerRoute());
|
2024-07-30 20:15:48 +02:00
|
|
|
|
2024-12-05 16:11:48 +01:00
|
|
|
final hasPermission =
|
|
|
|
await ref.read(galleryPermissionNotifier.notifier).hasPermission;
|
|
|
|
if (hasPermission) {
|
|
|
|
// Resume backup (if enable) then navigate
|
|
|
|
ref.watch(backupProvider.notifier).resumeBackup();
|
2022-06-21 01:10:23 +02:00
|
|
|
}
|
2024-12-05 16:11:48 +01:00
|
|
|
}
|
2022-06-21 01:10:23 +02:00
|
|
|
|
2024-12-05 16:11:48 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-02-06 07:41:07 +01:00
|
|
|
return const Scaffold(
|
2022-06-21 01:10:23 +02:00
|
|
|
body: Center(
|
2023-02-06 07:41:07 +01:00
|
|
|
child: Image(
|
2024-03-13 18:14:59 +01:00
|
|
|
image: AssetImage('assets/immich-logo.png'),
|
2023-02-06 07:41:07 +01:00
|
|
|
width: 80,
|
|
|
|
filterQuality: FilterQuality.high,
|
2022-06-21 01:10:23 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|