2024-12-05 15:11:48 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-02-03 16:06:44 +00:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-05-02 20:59:14 +00:00
|
|
|
import 'package:immich_mobile/providers/album/album.provider.dart';
|
|
|
|
import 'package:immich_mobile/services/background.service.dart';
|
2024-05-01 02:36:40 +00:00
|
|
|
import 'package:immich_mobile/models/backup/backup_state.model.dart';
|
2024-05-02 20:59:14 +00:00
|
|
|
import 'package:immich_mobile/providers/backup/backup.provider.dart';
|
|
|
|
import 'package:immich_mobile/providers/backup/ios_background_settings.provider.dart';
|
|
|
|
import 'package:immich_mobile/providers/backup/manual_upload.provider.dart';
|
2024-11-26 18:43:44 +00:00
|
|
|
import 'package:immich_mobile/providers/auth.provider.dart';
|
2024-05-02 20:59:14 +00:00
|
|
|
import 'package:immich_mobile/providers/memory.provider.dart';
|
|
|
|
import 'package:immich_mobile/providers/gallery_permission.provider.dart';
|
|
|
|
import 'package:immich_mobile/providers/notification_permission.provider.dart';
|
|
|
|
import 'package:immich_mobile/providers/asset.provider.dart';
|
|
|
|
import 'package:immich_mobile/providers/server_info.provider.dart';
|
|
|
|
import 'package:immich_mobile/providers/tab.provider.dart';
|
|
|
|
import 'package:immich_mobile/providers/websocket.provider.dart';
|
|
|
|
import 'package:immich_mobile/services/immich_logger.service.dart';
|
2023-08-12 21:02:58 +00:00
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
2022-02-03 16:06:44 +00:00
|
|
|
|
2024-05-02 20:59:14 +00:00
|
|
|
enum AppLifeCycleEnum {
|
2022-02-03 16:06:44 +00:00
|
|
|
active,
|
|
|
|
inactive,
|
|
|
|
paused,
|
|
|
|
resumed,
|
|
|
|
detached,
|
2023-08-18 22:52:40 +00:00
|
|
|
hidden,
|
2022-02-03 16:06:44 +00:00
|
|
|
}
|
|
|
|
|
2024-05-02 20:59:14 +00:00
|
|
|
class AppLifeCycleNotifier extends StateNotifier<AppLifeCycleEnum> {
|
2023-11-13 19:51:16 +00:00
|
|
|
final Ref _ref;
|
|
|
|
bool _wasPaused = false;
|
2023-08-12 21:02:58 +00:00
|
|
|
|
2024-05-02 20:59:14 +00:00
|
|
|
AppLifeCycleNotifier(this._ref) : super(AppLifeCycleEnum.active);
|
2023-08-12 21:02:58 +00:00
|
|
|
|
2024-05-02 20:59:14 +00:00
|
|
|
AppLifeCycleEnum getAppState() {
|
2023-08-12 21:02:58 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2024-12-05 15:11:48 +00:00
|
|
|
void handleAppResume() async {
|
2024-05-02 20:59:14 +00:00
|
|
|
state = AppLifeCycleEnum.resumed;
|
2023-08-12 21:02:58 +00:00
|
|
|
|
2023-11-13 19:51:16 +00:00
|
|
|
// no need to resume because app was never really paused
|
|
|
|
if (!_wasPaused) return;
|
|
|
|
_wasPaused = false;
|
|
|
|
|
2024-11-26 18:43:44 +00:00
|
|
|
final isAuthenticated = _ref.read(authProvider).isAuthenticated;
|
2023-08-12 21:02:58 +00:00
|
|
|
|
2023-11-14 20:30:27 +00:00
|
|
|
// Needs to be logged in
|
|
|
|
if (isAuthenticated) {
|
2024-12-05 15:11:48 +00:00
|
|
|
// switch endpoint if needed
|
|
|
|
final endpoint =
|
|
|
|
await _ref.read(authProvider.notifier).setOpenApiServiceEndpoint();
|
|
|
|
if (kDebugMode) {
|
|
|
|
debugPrint("Using server URL: $endpoint");
|
|
|
|
}
|
|
|
|
|
2023-11-14 20:30:27 +00:00
|
|
|
final permission = _ref.watch(galleryPermissionNotifier);
|
|
|
|
if (permission.isGranted || permission.isLimited) {
|
2024-12-05 15:11:48 +00:00
|
|
|
await _ref.read(backupProvider.notifier).resumeBackup();
|
|
|
|
await _ref.read(backgroundServiceProvider).resumeServiceIfEnabled();
|
2023-11-14 20:30:27 +00:00
|
|
|
}
|
2024-12-05 15:11:48 +00:00
|
|
|
|
|
|
|
await _ref.read(serverInfoProvider.notifier).getServerVersion();
|
|
|
|
|
2023-11-13 19:51:16 +00:00
|
|
|
switch (_ref.read(tabProvider)) {
|
2023-09-10 12:51:18 +00:00
|
|
|
case TabEnum.home:
|
2024-12-05 15:11:48 +00:00
|
|
|
await _ref.read(assetProvider.notifier).getAllAsset();
|
|
|
|
break;
|
2023-09-10 12:51:18 +00:00
|
|
|
case TabEnum.search:
|
2024-12-05 15:11:48 +00:00
|
|
|
// nothing to do
|
|
|
|
break;
|
|
|
|
|
2024-10-10 08:44:14 +00:00
|
|
|
case TabEnum.albums:
|
2024-12-05 15:11:48 +00:00
|
|
|
await _ref.read(albumProvider.notifier).refreshRemoteAlbums();
|
|
|
|
break;
|
2023-09-10 12:51:18 +00:00
|
|
|
case TabEnum.library:
|
2024-12-05 15:11:48 +00:00
|
|
|
// nothing to do
|
|
|
|
break;
|
2023-09-10 12:51:18 +00:00
|
|
|
}
|
2023-08-12 21:02:58 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 19:51:16 +00:00
|
|
|
_ref.read(websocketProvider.notifier).connect();
|
2023-08-12 21:02:58 +00:00
|
|
|
|
2024-12-05 15:11:48 +00:00
|
|
|
await _ref
|
2023-11-13 19:51:16 +00:00
|
|
|
.read(notificationPermissionProvider.notifier)
|
2023-08-12 21:02:58 +00:00
|
|
|
.getNotificationPermission();
|
|
|
|
|
2024-12-05 15:11:48 +00:00
|
|
|
await _ref
|
|
|
|
.read(galleryPermissionNotifier.notifier)
|
|
|
|
.getGalleryPermissionStatus();
|
|
|
|
|
|
|
|
await _ref.read(iOSBackgroundSettingsProvider.notifier).refresh();
|
2023-08-12 21:02:58 +00:00
|
|
|
|
2023-11-13 19:51:16 +00:00
|
|
|
_ref.invalidate(memoryFutureProvider);
|
2023-08-12 21:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleAppInactivity() {
|
2024-05-02 20:59:14 +00:00
|
|
|
state = AppLifeCycleEnum.inactive;
|
2023-11-13 19:51:16 +00:00
|
|
|
// do not stop/clean up anything on inactivity: issued on every orientation change
|
2023-08-12 21:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleAppPause() {
|
2024-05-02 20:59:14 +00:00
|
|
|
state = AppLifeCycleEnum.paused;
|
2023-11-13 19:51:16 +00:00
|
|
|
_wasPaused = true;
|
2024-09-27 02:40:55 +00:00
|
|
|
|
2024-11-26 18:43:44 +00:00
|
|
|
if (_ref.read(authProvider).isAuthenticated) {
|
2024-09-27 02:40:55 +00:00
|
|
|
// Do not cancel backup if manual upload is in progress
|
|
|
|
if (_ref.read(backupProvider.notifier).backupProgress !=
|
|
|
|
BackUpProgressEnum.manualInProgress) {
|
|
|
|
_ref.read(backupProvider.notifier).cancelBackup();
|
|
|
|
}
|
|
|
|
_ref.read(websocketProvider.notifier).disconnect();
|
2023-11-13 19:51:16 +00:00
|
|
|
}
|
2024-09-27 02:40:55 +00:00
|
|
|
|
2023-11-13 19:51:16 +00:00
|
|
|
ImmichLogger().flush();
|
2023-08-12 21:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleAppDetached() {
|
2024-05-02 20:59:14 +00:00
|
|
|
state = AppLifeCycleEnum.detached;
|
2023-11-13 19:51:16 +00:00
|
|
|
// no guarantee this is called at all
|
|
|
|
_ref.read(manualUploadProvider.notifier).cancelBackup();
|
2023-08-12 21:02:58 +00:00
|
|
|
}
|
2023-08-18 22:52:40 +00:00
|
|
|
|
|
|
|
void handleAppHidden() {
|
2024-05-02 20:59:14 +00:00
|
|
|
state = AppLifeCycleEnum.hidden;
|
2023-11-13 19:51:16 +00:00
|
|
|
// do not stop/clean up anything on inactivity: issued on every orientation change
|
2023-08-18 22:52:40 +00:00
|
|
|
}
|
2023-08-12 21:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final appStateProvider =
|
2024-05-02 20:59:14 +00:00
|
|
|
StateNotifierProvider<AppLifeCycleNotifier, AppLifeCycleEnum>((ref) {
|
|
|
|
return AppLifeCycleNotifier(ref);
|
2022-02-03 16:06:44 +00:00
|
|
|
});
|