2022-08-09 06:43:48 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-04-06 19:51:32 +02:00
|
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
2022-07-07 20:40:54 +02:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2022-08-08 02:43:09 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2023-02-03 18:26:05 +02:00
|
|
|
import 'package:flutter/services.dart';
|
2022-08-08 02:43:09 +02:00
|
|
|
import 'package:flutter_displaymode/flutter_displaymode.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-08-18 16:41:59 +02:00
|
|
|
import 'package:immich_mobile/constants/locales.dart';
|
|
|
|
import 'package:immich_mobile/modules/backup/background_service/background.service.dart';
|
2023-03-18 16:55:11 +02:00
|
|
|
import 'package:immich_mobile/modules/backup/models/backup_album.model.dart';
|
|
|
|
import 'package:immich_mobile/modules/backup/models/duplicated_asset.model.dart';
|
2022-06-23 06:14:14 +02:00
|
|
|
import 'package:immich_mobile/modules/backup/providers/backup.provider.dart';
|
2023-02-23 20:33:53 +02:00
|
|
|
import 'package:immich_mobile/modules/backup/providers/ios_background_settings.provider.dart';
|
2022-06-11 23:12:06 +02:00
|
|
|
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';
|
|
|
|
import 'package:immich_mobile/modules/settings/providers/notification_permission.provider.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
import 'package:immich_mobile/routing/router.dart';
|
2022-03-16 17:19:31 +02:00
|
|
|
import 'package:immich_mobile/routing/tab_navigation_observer.dart';
|
2023-03-04 00:38:30 +02:00
|
|
|
import 'package:immich_mobile/shared/models/album.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2023-05-25 05:52:43 +02:00
|
|
|
import 'package:immich_mobile/shared/models/etag.dart';
|
2023-03-04 00:38:30 +02:00
|
|
|
import 'package:immich_mobile/shared/models/exif_info.dart';
|
2023-03-23 03:36:44 +02:00
|
|
|
import 'package:immich_mobile/shared/models/logger_message.model.dart';
|
2023-02-09 19:32:08 +02:00
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
2023-03-04 00:38:30 +02:00
|
|
|
import 'package:immich_mobile/shared/models/user.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/app_state.provider.dart';
|
2022-06-23 06:14:14 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/asset.provider.dart';
|
2023-02-09 19:32:08 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/db.provider.dart';
|
2022-06-11 23:12:06 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/release_info.provider.dart';
|
2022-03-22 08:22:04 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/server_info.provider.dart';
|
2022-02-14 18:40:41 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/websocket.provider.dart';
|
2022-11-27 22:34:19 +02:00
|
|
|
import 'package:immich_mobile/shared/services/immich_logger.service.dart';
|
2022-04-24 04:08:45 +02:00
|
|
|
import 'package:immich_mobile/shared/views/immich_loading_overlay.dart';
|
2022-06-11 23:12:06 +02:00
|
|
|
import 'package:immich_mobile/shared/views/version_announcement_overlay.dart';
|
2022-08-16 01:53:30 +02:00
|
|
|
import 'package:immich_mobile/utils/immich_app_theme.dart';
|
2023-02-09 19:32:08 +02:00
|
|
|
import 'package:immich_mobile/utils/migration.dart';
|
|
|
|
import 'package:isar/isar.dart';
|
2023-02-23 08:36:17 +02:00
|
|
|
import 'package:logging/logging.dart';
|
2023-02-09 19:32:08 +02:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2023-02-28 18:22:18 +02:00
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
|
|
|
|
void main() async {
|
2023-03-23 03:36:44 +02:00
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
2023-04-06 19:51:32 +02:00
|
|
|
|
2023-02-09 19:32:08 +02:00
|
|
|
final db = await loadDb();
|
2023-03-23 03:36:44 +02:00
|
|
|
await initApp();
|
2023-03-27 04:35:52 +02:00
|
|
|
await migrateDatabaseIfNeeded(db);
|
2023-02-09 19:32:08 +02:00
|
|
|
runApp(getMainWidget(db));
|
2023-01-22 05:43:28 +02:00
|
|
|
}
|
2022-05-06 14:22:23 +02:00
|
|
|
|
2023-01-22 05:43:28 +02:00
|
|
|
Future<void> initApp() async {
|
2023-03-23 03:36:44 +02:00
|
|
|
await EasyLocalization.ensureInitialized();
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-08-09 06:43:48 +02:00
|
|
|
if (kReleaseMode && Platform.isAndroid) {
|
|
|
|
try {
|
|
|
|
await FlutterDisplayMode.setHighRefreshRate();
|
2023-01-18 17:59:23 +02:00
|
|
|
debugPrint("Enabled high refresh mode");
|
2022-08-09 06:43:48 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error setting high refresh rate: $e");
|
|
|
|
}
|
2022-08-08 02:43:09 +02:00
|
|
|
}
|
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
// Initialize Immich Logger Service
|
2023-03-23 03:36:44 +02:00
|
|
|
ImmichLogger();
|
2023-02-23 08:36:17 +02:00
|
|
|
|
|
|
|
var log = Logger("ImmichErrorLogger");
|
|
|
|
|
|
|
|
FlutterError.onError = (details) {
|
|
|
|
FlutterError.presentError(details);
|
|
|
|
log.severe(details.toString(), details, details.stack);
|
|
|
|
};
|
|
|
|
|
|
|
|
PlatformDispatcher.instance.onError = (error, stack) {
|
|
|
|
log.severe(error.toString(), error, stack);
|
|
|
|
return true;
|
|
|
|
};
|
2023-01-22 05:43:28 +02:00
|
|
|
}
|
2022-11-27 22:34:19 +02:00
|
|
|
|
2023-02-09 19:32:08 +02:00
|
|
|
Future<Isar> loadDb() async {
|
|
|
|
final dir = await getApplicationDocumentsDirectory();
|
|
|
|
Isar db = await Isar.open(
|
2023-03-04 00:38:30 +02:00
|
|
|
[
|
|
|
|
StoreValueSchema,
|
|
|
|
ExifInfoSchema,
|
|
|
|
AssetSchema,
|
|
|
|
AlbumSchema,
|
|
|
|
UserSchema,
|
2023-03-18 16:55:11 +02:00
|
|
|
BackupAlbumSchema,
|
|
|
|
DuplicatedAssetSchema,
|
2023-03-23 03:36:44 +02:00
|
|
|
LoggerMessageSchema,
|
2023-05-25 05:52:43 +02:00
|
|
|
ETagSchema,
|
2023-03-04 00:38:30 +02:00
|
|
|
],
|
2023-02-09 19:32:08 +02:00
|
|
|
directory: dir.path,
|
|
|
|
maxSizeMiB: 256,
|
|
|
|
);
|
|
|
|
Store.init(db);
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget getMainWidget(Isar db) {
|
2023-01-22 05:43:28 +02:00
|
|
|
return EasyLocalization(
|
|
|
|
supportedLocales: locales,
|
|
|
|
path: translationsPath,
|
|
|
|
useFallbackTranslations: true,
|
|
|
|
fallbackLocale: locales.first,
|
2023-02-09 19:32:08 +02:00
|
|
|
child: ProviderScope(
|
|
|
|
overrides: [dbProvider.overrideWithValue(db)],
|
|
|
|
child: const ImmichApp(),
|
|
|
|
),
|
2022-07-13 14:23:48 +02:00
|
|
|
);
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ImmichApp extends ConsumerStatefulWidget {
|
2022-06-23 06:14:14 +02:00
|
|
|
const ImmichApp({super.key});
|
2022-02-03 18:06:44 +02:00
|
|
|
|
|
|
|
@override
|
2022-06-22 07:23:35 +02:00
|
|
|
ImmichAppState createState() => ImmichAppState();
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
class ImmichAppState extends ConsumerState<ImmichApp>
|
|
|
|
with WidgetsBindingObserver {
|
2022-02-03 18:06:44 +02:00
|
|
|
@override
|
|
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
|
|
switch (state) {
|
|
|
|
case AppLifecycleState.resumed:
|
|
|
|
debugPrint("[APP STATE] resumed");
|
2022-02-14 18:40:41 +02:00
|
|
|
ref.watch(appStateProvider.notifier).state = AppStateEnum.resumed;
|
2022-06-11 23:12:06 +02:00
|
|
|
|
|
|
|
var isAuthenticated = ref.watch(authenticationProvider).isAuthenticated;
|
2023-02-28 18:22:18 +02:00
|
|
|
final permission = ref.watch(galleryPermissionNotifier);
|
2022-06-11 23:12:06 +02:00
|
|
|
|
2023-02-28 18:22:18 +02:00
|
|
|
// Needs to be logged in and have gallery permissions
|
|
|
|
if (isAuthenticated && (permission.isGranted || permission.isLimited)) {
|
2022-11-08 19:00:24 +02:00
|
|
|
ref.read(backupProvider.notifier).resumeBackup();
|
2022-08-18 16:41:59 +02:00
|
|
|
ref.read(backgroundServiceProvider).resumeServiceIfEnabled();
|
2022-06-11 23:12:06 +02:00
|
|
|
ref.watch(assetProvider.notifier).getAllAsset();
|
|
|
|
ref.watch(serverInfoProvider.notifier).getServerVersion();
|
|
|
|
}
|
|
|
|
|
2022-02-14 18:40:41 +02:00
|
|
|
ref.watch(websocketProvider.notifier).connect();
|
2022-06-11 23:12:06 +02:00
|
|
|
|
|
|
|
ref.watch(releaseInfoProvider.notifier).checkGithubReleaseInfo();
|
2022-03-22 08:22:04 +02:00
|
|
|
|
2023-03-18 16:55:11 +02:00
|
|
|
ref
|
|
|
|
.watch(notificationPermissionProvider.notifier)
|
|
|
|
.getNotificationPermission();
|
|
|
|
ref
|
|
|
|
.watch(galleryPermissionNotifier.notifier)
|
|
|
|
.getGalleryPermissionStatus();
|
2023-02-21 14:28:52 +02:00
|
|
|
|
2023-02-23 20:33:53 +02:00
|
|
|
ref.read(iOSBackgroundSettingsProvider.notifier).refresh();
|
|
|
|
|
2022-02-03 18:06:44 +02:00
|
|
|
break;
|
2022-03-22 08:22:04 +02:00
|
|
|
|
2022-02-03 18:06:44 +02:00
|
|
|
case AppLifecycleState.inactive:
|
|
|
|
debugPrint("[APP STATE] inactive");
|
2022-02-14 18:40:41 +02:00
|
|
|
ref.watch(appStateProvider.notifier).state = AppStateEnum.inactive;
|
2023-03-23 03:36:44 +02:00
|
|
|
ImmichLogger().flush();
|
2022-02-14 18:40:41 +02:00
|
|
|
ref.watch(websocketProvider.notifier).disconnect();
|
2022-03-11 19:02:33 +02:00
|
|
|
ref.watch(backupProvider.notifier).cancelBackup();
|
|
|
|
|
2022-02-03 18:06:44 +02:00
|
|
|
break;
|
2022-03-22 08:22:04 +02:00
|
|
|
|
2022-02-03 18:06:44 +02:00
|
|
|
case AppLifecycleState.paused:
|
|
|
|
debugPrint("[APP STATE] paused");
|
2022-02-14 18:40:41 +02:00
|
|
|
ref.watch(appStateProvider.notifier).state = AppStateEnum.paused;
|
2022-02-03 18:06:44 +02:00
|
|
|
break;
|
2022-03-22 08:22:04 +02:00
|
|
|
|
2022-02-03 18:06:44 +02:00
|
|
|
case AppLifecycleState.detached:
|
|
|
|
debugPrint("[APP STATE] detached");
|
2022-02-14 18:40:41 +02:00
|
|
|
ref.watch(appStateProvider.notifier).state = AppStateEnum.detached;
|
2022-02-03 18:06:44 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> initApp() async {
|
2022-05-29 05:35:45 +02:00
|
|
|
WidgetsBinding.instance.addObserver(this);
|
2023-04-06 19:51:32 +02:00
|
|
|
|
|
|
|
// Draw the app from edge to edge
|
|
|
|
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
|
|
|
|
|
|
|
|
// Sets the navigation bar color
|
2023-04-17 07:02:07 +02:00
|
|
|
SystemUiOverlayStyle overlayStyle = const SystemUiOverlayStyle(
|
|
|
|
systemNavigationBarColor: Colors.transparent,
|
|
|
|
);
|
2023-04-06 19:51:32 +02:00
|
|
|
if (Platform.isAndroid) {
|
|
|
|
// Android 8 does not support transparent app bars
|
|
|
|
final info = await DeviceInfoPlugin().androidInfo;
|
|
|
|
if (info.version.sdkInt <= 26) {
|
2023-04-17 07:02:07 +02:00
|
|
|
overlayStyle =
|
|
|
|
MediaQuery.of(context).platformBrightness == Brightness.light
|
|
|
|
? SystemUiOverlayStyle.light
|
|
|
|
: SystemUiOverlayStyle.dark;
|
|
|
|
}
|
2023-04-06 19:51:32 +02:00
|
|
|
}
|
|
|
|
SystemChrome.setSystemUIOverlayStyle(overlayStyle);
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
initState() {
|
|
|
|
super.initState();
|
|
|
|
initApp().then((_) => debugPrint("App Init Completed"));
|
2022-08-18 16:41:59 +02:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
// needs to be delayed so that EasyLocalization is working
|
|
|
|
ref.read(backgroundServiceProvider).resumeServiceIfEnabled();
|
|
|
|
});
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2022-05-29 05:35:45 +02:00
|
|
|
WidgetsBinding.instance.removeObserver(this);
|
2022-02-03 18:06:44 +02:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-07-13 14:23:48 +02:00
|
|
|
var router = ref.watch(appRouterProvider);
|
2022-06-11 23:12:06 +02:00
|
|
|
ref.watch(releaseInfoProvider.notifier).checkGithubReleaseInfo();
|
|
|
|
|
2022-04-24 04:08:45 +02:00
|
|
|
return MaterialApp(
|
2022-07-07 20:40:54 +02:00
|
|
|
localizationsDelegates: context.localizationDelegates,
|
|
|
|
supportedLocales: context.supportedLocales,
|
|
|
|
locale: context.locale,
|
2022-02-03 18:06:44 +02:00
|
|
|
debugShowCheckedModeBanner: false,
|
2022-04-24 04:08:45 +02:00
|
|
|
home: Stack(
|
|
|
|
children: [
|
|
|
|
MaterialApp.router(
|
|
|
|
title: 'Immich',
|
|
|
|
debugShowCheckedModeBanner: false,
|
2022-08-16 01:53:30 +02:00
|
|
|
themeMode: ref.watch(immichThemeProvider),
|
|
|
|
darkTheme: immichDarkTheme,
|
|
|
|
theme: immichLightTheme,
|
2022-07-13 14:23:48 +02:00
|
|
|
routeInformationParser: router.defaultRouteParser(),
|
|
|
|
routerDelegate: router.delegate(
|
|
|
|
navigatorObservers: () => [TabNavigationObserver(ref: ref)],
|
|
|
|
),
|
2022-04-24 04:08:45 +02:00
|
|
|
),
|
|
|
|
const ImmichLoadingOverlay(),
|
2022-06-11 23:12:06 +02:00
|
|
|
const VersionAnnouncementOverlay(),
|
2022-04-24 04:08:45 +02:00
|
|
|
],
|
2022-02-03 18:06:44 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|