2022-08-08 21:43:48 -07:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-07-07 11:40:54 -07:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2022-08-07 17:43:09 -07:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-02-03 09:06:44 -07:00
|
|
|
import 'package:flutter/material.dart';
|
2023-02-03 09:26:05 -07:00
|
|
|
import 'package:flutter/services.dart';
|
2022-08-07 17:43:09 -07:00
|
|
|
import 'package:flutter_displaymode/flutter_displaymode.dart';
|
2022-02-03 09:06:44 -07:00
|
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-08-18 07:41:59 -07:00
|
|
|
import 'package:immich_mobile/constants/locales.dart';
|
|
|
|
import 'package:immich_mobile/modules/backup/background_service/background.service.dart';
|
2022-05-06 05:22:23 -07:00
|
|
|
import 'package:immich_mobile/modules/backup/models/hive_backup_albums.model.dart';
|
2022-10-25 07:51:03 -07:00
|
|
|
import 'package:immich_mobile/modules/backup/models/hive_duplicated_assets.model.dart';
|
2022-06-22 21:14:14 -07:00
|
|
|
import 'package:immich_mobile/modules/backup/providers/backup.provider.dart';
|
2022-04-24 19:33:10 -07:00
|
|
|
import 'package:immich_mobile/modules/login/models/hive_saved_login_info.model.dart';
|
2022-06-11 14:12:06 -07:00
|
|
|
import 'package:immich_mobile/modules/login/providers/authentication.provider.dart';
|
2023-02-21 05:28:52 -07:00
|
|
|
import 'package:immich_mobile/modules/settings/providers/permission.provider.dart';
|
2022-02-03 09:06:44 -07:00
|
|
|
import 'package:immich_mobile/routing/router.dart';
|
2022-03-16 08:19:31 -07:00
|
|
|
import 'package:immich_mobile/routing/tab_navigation_observer.dart';
|
2022-11-27 13:34:19 -07:00
|
|
|
import 'package:immich_mobile/shared/models/immich_logger_message.model.dart';
|
2023-02-09 10:32:08 -07:00
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
2022-02-03 09:06:44 -07:00
|
|
|
import 'package:immich_mobile/shared/providers/app_state.provider.dart';
|
2022-06-22 21:14:14 -07:00
|
|
|
import 'package:immich_mobile/shared/providers/asset.provider.dart';
|
2023-02-09 10:32:08 -07:00
|
|
|
import 'package:immich_mobile/shared/providers/db.provider.dart';
|
2022-06-11 14:12:06 -07:00
|
|
|
import 'package:immich_mobile/shared/providers/release_info.provider.dart';
|
2022-03-21 23:22:04 -07:00
|
|
|
import 'package:immich_mobile/shared/providers/server_info.provider.dart';
|
2022-02-14 09:40:41 -07:00
|
|
|
import 'package:immich_mobile/shared/providers/websocket.provider.dart';
|
2022-11-27 13:34:19 -07:00
|
|
|
import 'package:immich_mobile/shared/services/immich_logger.service.dart';
|
2022-04-23 19:08:45 -07:00
|
|
|
import 'package:immich_mobile/shared/views/immich_loading_overlay.dart';
|
2022-06-11 14:12:06 -07:00
|
|
|
import 'package:immich_mobile/shared/views/version_announcement_overlay.dart';
|
2022-08-15 16:53:30 -07:00
|
|
|
import 'package:immich_mobile/utils/immich_app_theme.dart';
|
2023-02-09 10:32:08 -07:00
|
|
|
import 'package:immich_mobile/utils/migration.dart';
|
|
|
|
import 'package:isar/isar.dart';
|
2023-02-22 23:36:17 -07:00
|
|
|
import 'package:logging/logging.dart';
|
2023-02-09 10:32:08 -07:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2022-02-03 09:06:44 -07:00
|
|
|
import 'constants/hive_box.dart';
|
|
|
|
|
|
|
|
void main() async {
|
2023-01-21 20:43:28 -07:00
|
|
|
await initApp();
|
2023-02-09 10:32:08 -07:00
|
|
|
final db = await loadDb();
|
|
|
|
await migrateHiveToStoreIfNecessary();
|
|
|
|
runApp(getMainWidget(db));
|
2023-01-21 20:43:28 -07:00
|
|
|
}
|
2022-05-06 05:22:23 -07:00
|
|
|
|
2023-01-21 20:43:28 -07:00
|
|
|
Future<void> openBoxes() async {
|
2022-10-31 07:38:24 -07:00
|
|
|
await Future.wait([
|
2022-11-27 13:34:19 -07:00
|
|
|
Hive.openBox<ImmichLoggerMessage>(immichLoggerBox),
|
2022-10-31 07:38:24 -07:00
|
|
|
Hive.openBox(userInfoBox),
|
|
|
|
Hive.openBox<HiveSavedLoginInfo>(hiveLoginInfoBox),
|
|
|
|
Hive.openBox(hiveGithubReleaseInfoBox),
|
|
|
|
Hive.openBox(userSettingInfoBox),
|
|
|
|
EasyLocalization.ensureInitialized(),
|
|
|
|
]);
|
2023-01-21 20:43:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> initApp() async {
|
|
|
|
await Hive.initFlutter();
|
|
|
|
Hive.registerAdapter(HiveSavedLoginInfoAdapter());
|
|
|
|
Hive.registerAdapter(HiveBackupAlbumsAdapter());
|
|
|
|
Hive.registerAdapter(HiveDuplicatedAssetsAdapter());
|
|
|
|
Hive.registerAdapter(ImmichLoggerMessageAdapter());
|
|
|
|
|
|
|
|
await openBoxes();
|
2022-02-03 09:06:44 -07:00
|
|
|
|
2022-08-08 21:43:48 -07:00
|
|
|
if (kReleaseMode && Platform.isAndroid) {
|
|
|
|
try {
|
|
|
|
await FlutterDisplayMode.setHighRefreshRate();
|
2023-01-18 08:59:23 -07:00
|
|
|
debugPrint("Enabled high refresh mode");
|
2022-08-08 21:43:48 -07:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error setting high refresh rate: $e");
|
|
|
|
}
|
2022-08-07 17:43:09 -07:00
|
|
|
}
|
|
|
|
|
2022-11-27 13:34:19 -07:00
|
|
|
// Initialize Immich Logger Service
|
|
|
|
ImmichLogger().init();
|
2023-02-22 23:36:17 -07: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-21 20:43:28 -07:00
|
|
|
}
|
2022-11-27 13:34:19 -07:00
|
|
|
|
2023-02-09 10:32:08 -07:00
|
|
|
Future<Isar> loadDb() async {
|
|
|
|
final dir = await getApplicationDocumentsDirectory();
|
|
|
|
Isar db = await Isar.open(
|
|
|
|
[StoreValueSchema],
|
|
|
|
directory: dir.path,
|
|
|
|
maxSizeMiB: 256,
|
|
|
|
);
|
|
|
|
Store.init(db);
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget getMainWidget(Isar db) {
|
2023-01-21 20:43:28 -07:00
|
|
|
return EasyLocalization(
|
|
|
|
supportedLocales: locales,
|
|
|
|
path: translationsPath,
|
|
|
|
useFallbackTranslations: true,
|
|
|
|
fallbackLocale: locales.first,
|
2023-02-09 10:32:08 -07:00
|
|
|
child: ProviderScope(
|
|
|
|
overrides: [dbProvider.overrideWithValue(db)],
|
|
|
|
child: const ImmichApp(),
|
|
|
|
),
|
2022-07-13 05:23:48 -07:00
|
|
|
);
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class ImmichApp extends ConsumerStatefulWidget {
|
2022-06-22 21:14:14 -07:00
|
|
|
const ImmichApp({super.key});
|
2022-02-03 09:06:44 -07:00
|
|
|
|
|
|
|
@override
|
2022-06-21 22:23:35 -07:00
|
|
|
ImmichAppState createState() => ImmichAppState();
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
|
2022-06-21 22:23:35 -07:00
|
|
|
class ImmichAppState extends ConsumerState<ImmichApp>
|
|
|
|
with WidgetsBindingObserver {
|
2022-02-03 09:06:44 -07:00
|
|
|
@override
|
|
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
|
|
switch (state) {
|
|
|
|
case AppLifecycleState.resumed:
|
|
|
|
debugPrint("[APP STATE] resumed");
|
2022-02-14 09:40:41 -07:00
|
|
|
ref.watch(appStateProvider.notifier).state = AppStateEnum.resumed;
|
2022-06-11 14:12:06 -07:00
|
|
|
|
|
|
|
var isAuthenticated = ref.watch(authenticationProvider).isAuthenticated;
|
|
|
|
|
|
|
|
if (isAuthenticated) {
|
2022-11-08 10:00:24 -07:00
|
|
|
ref.read(backupProvider.notifier).resumeBackup();
|
2022-08-18 07:41:59 -07:00
|
|
|
ref.read(backgroundServiceProvider).resumeServiceIfEnabled();
|
2022-06-11 14:12:06 -07:00
|
|
|
ref.watch(assetProvider.notifier).getAllAsset();
|
|
|
|
ref.watch(serverInfoProvider.notifier).getServerVersion();
|
|
|
|
}
|
|
|
|
|
2022-02-14 09:40:41 -07:00
|
|
|
ref.watch(websocketProvider.notifier).connect();
|
2022-06-11 14:12:06 -07:00
|
|
|
|
|
|
|
ref.watch(releaseInfoProvider.notifier).checkGithubReleaseInfo();
|
2022-03-21 23:22:04 -07:00
|
|
|
|
2023-02-22 23:36:17 -07:00
|
|
|
ref
|
|
|
|
.watch(notificationPermissionProvider.notifier)
|
|
|
|
.getNotificationPermission();
|
2023-02-21 05:28:52 -07:00
|
|
|
|
2022-02-03 09:06:44 -07:00
|
|
|
break;
|
2022-03-21 23:22:04 -07:00
|
|
|
|
2022-02-03 09:06:44 -07:00
|
|
|
case AppLifecycleState.inactive:
|
|
|
|
debugPrint("[APP STATE] inactive");
|
2022-02-14 09:40:41 -07:00
|
|
|
ref.watch(appStateProvider.notifier).state = AppStateEnum.inactive;
|
|
|
|
ref.watch(websocketProvider.notifier).disconnect();
|
2022-03-11 10:02:33 -07:00
|
|
|
ref.watch(backupProvider.notifier).cancelBackup();
|
|
|
|
|
2022-02-03 09:06:44 -07:00
|
|
|
break;
|
2022-03-21 23:22:04 -07:00
|
|
|
|
2022-02-03 09:06:44 -07:00
|
|
|
case AppLifecycleState.paused:
|
|
|
|
debugPrint("[APP STATE] paused");
|
2022-02-14 09:40:41 -07:00
|
|
|
ref.watch(appStateProvider.notifier).state = AppStateEnum.paused;
|
2022-02-03 09:06:44 -07:00
|
|
|
break;
|
2022-03-21 23:22:04 -07:00
|
|
|
|
2022-02-03 09:06:44 -07:00
|
|
|
case AppLifecycleState.detached:
|
|
|
|
debugPrint("[APP STATE] detached");
|
2022-02-14 09:40:41 -07:00
|
|
|
ref.watch(appStateProvider.notifier).state = AppStateEnum.detached;
|
2022-02-03 09:06:44 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> initApp() async {
|
2022-05-28 20:35:45 -07:00
|
|
|
WidgetsBinding.instance.addObserver(this);
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
initState() {
|
|
|
|
super.initState();
|
|
|
|
initApp().then((_) => debugPrint("App Init Completed"));
|
2022-08-18 07:41:59 -07:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
// needs to be delayed so that EasyLocalization is working
|
|
|
|
ref.read(backgroundServiceProvider).resumeServiceIfEnabled();
|
|
|
|
});
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2022-05-28 20:35:45 -07:00
|
|
|
WidgetsBinding.instance.removeObserver(this);
|
2022-02-03 09:06:44 -07:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-07-13 05:23:48 -07:00
|
|
|
var router = ref.watch(appRouterProvider);
|
2022-06-11 14:12:06 -07:00
|
|
|
ref.watch(releaseInfoProvider.notifier).checkGithubReleaseInfo();
|
|
|
|
|
2023-02-03 09:26:05 -07:00
|
|
|
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
|
|
|
|
|
2022-04-23 19:08:45 -07:00
|
|
|
return MaterialApp(
|
2022-07-07 11:40:54 -07:00
|
|
|
localizationsDelegates: context.localizationDelegates,
|
|
|
|
supportedLocales: context.supportedLocales,
|
|
|
|
locale: context.locale,
|
2022-02-03 09:06:44 -07:00
|
|
|
debugShowCheckedModeBanner: false,
|
2022-04-23 19:08:45 -07:00
|
|
|
home: Stack(
|
|
|
|
children: [
|
|
|
|
MaterialApp.router(
|
|
|
|
title: 'Immich',
|
|
|
|
debugShowCheckedModeBanner: false,
|
2022-08-15 16:53:30 -07:00
|
|
|
themeMode: ref.watch(immichThemeProvider),
|
|
|
|
darkTheme: immichDarkTheme,
|
|
|
|
theme: immichLightTheme,
|
2022-07-13 05:23:48 -07:00
|
|
|
routeInformationParser: router.defaultRouteParser(),
|
|
|
|
routerDelegate: router.delegate(
|
|
|
|
navigatorObservers: () => [TabNavigationObserver(ref: ref)],
|
|
|
|
),
|
2022-04-23 19:08:45 -07:00
|
|
|
),
|
|
|
|
const ImmichLoadingOverlay(),
|
2022-06-11 14:12:06 -07:00
|
|
|
const VersionAnnouncementOverlay(),
|
2022-04-23 19:08:45 -07:00
|
|
|
],
|
2022-02-03 09:06:44 -07:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|