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';
|
2023-03-23 03:36:44 +02:00
|
|
|
import 'package:immich_mobile/shared/models/store.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-12 07:33:26 +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);
|
2023-07-28 05:05:27 +02:00
|
|
|
} on ApiException catch (e) {
|
2023-03-15 23:29:07 +02:00
|
|
|
// okay, try to continue anyway if offline
|
2023-07-28 05:05:27 +02:00
|
|
|
if (e.code == 503) {
|
|
|
|
deviceIsOffline = true;
|
|
|
|
log.fine("Device seems to be offline upon launch");
|
|
|
|
} else {
|
2024-02-24 05:38:57 +02:00
|
|
|
log.severe("Failed to resolve endpoint", e);
|
2023-07-28 05:05:27 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-02-24 05:38:57 +02:00
|
|
|
log.severe("Failed to resolve endpoint", e);
|
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-12 07:33:26 +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
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|