Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented auto backup #11

Merged
merged 2 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mobile/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:hive_flutter/hive_flutter.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/routing/router.dart';
import 'package:immich_mobile/shared/providers/app_state.provider.dart';
import 'package:immich_mobile/shared/providers/backup.provider.dart';
import 'constants/hive_box.dart';
import 'package:google_fonts/google_fonts.dart';

Expand Down Expand Up @@ -36,6 +37,7 @@ class _ImmichAppState extends ConsumerState<ImmichApp> with WidgetsBindingObserv
case AppLifecycleState.resumed:
debugPrint("[APP STATE] resumed");
ref.read(appStateProvider.notifier).state = AppStateEnum.resumed;
ref.read(backupProvider.notifier).resumeBackup();
break;
case AppLifecycleState.inactive:
debugPrint("[APP STATE] inactive");
Expand All @@ -53,7 +55,7 @@ class _ImmichAppState extends ConsumerState<ImmichApp> with WidgetsBindingObserv
}

Future<void> initApp() async {
// WidgetsBinding.instance?.addObserver(this);
WidgetsBinding.instance?.addObserver(this);
}

@override
Expand Down
3 changes: 1 addition & 2 deletions mobile/lib/modules/home/ui/profile_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ class ProfileDrawer extends ConsumerWidget {
onTap: () async {
bool res = await ref.read(authenticationProvider.notifier).logout();

ref.read(assetProvider.notifier).clearAllAsset();

if (res) {
AutoRouter.of(context).popUntilRoot();
ref.read(assetProvider.notifier).clearAllAsset();
}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:immich_mobile/shared/services/network.service.dart';
import 'package:immich_mobile/shared/models/device_info.model.dart';

class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
AuthenticationNotifier()
AuthenticationNotifier(this.ref)
: super(
AuthenticationState(
deviceId: "",
Expand All @@ -31,6 +31,7 @@ class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
),
);

final Ref ref;
final DeviceInfoService _deviceInfoService = DeviceInfoService();
final BackupService _backupService = BackupService();
final NetworkService _networkService = NetworkService();
Expand Down Expand Up @@ -126,5 +127,5 @@ class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
}

final authenticationProvider = StateNotifierProvider<AuthenticationNotifier, AuthenticationState>((ref) {
return AuthenticationNotifier();
return AuthenticationNotifier(ref);
});
7 changes: 7 additions & 0 deletions mobile/lib/modules/login/ui/login_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/modules/home/providers/asset.provider.dart';
import 'package:immich_mobile/modules/login/providers/authentication.provider.dart';
import 'package:immich_mobile/shared/providers/backup.provider.dart';
import 'package:immich_mobile/shared/ui/immich_toast.dart';

class LoginForm extends HookConsumerWidget {
Expand Down Expand Up @@ -110,11 +112,16 @@ class LoginButton extends ConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) {
return ElevatedButton(
onPressed: () async {
// This will remove current cache asset state of previous user login.
ref.watch(assetProvider.notifier).clearAllAsset();

var isAuthenicated = await ref
.read(authenticationProvider.notifier)
.login(emailController.text, passwordController.text, serverEndpointController.text);

if (isAuthenicated) {
// Resume backup (if enable) then navigate
ref.watch(backupProvider.notifier).resumeBackup();
AutoRouter.of(context).pushNamed("/home-page");
} else {
ImmichToast.show(
Expand Down
40 changes: 37 additions & 3 deletions mobile/lib/shared/providers/backup.provider.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/constants/hive_box.dart';
import 'package:immich_mobile/modules/login/providers/authentication.provider.dart';
import 'package:immich_mobile/shared/services/server_info.service.dart';
import 'package:immich_mobile/shared/models/backup_state.model.dart';
import 'package:immich_mobile/shared/models/server_info.model.dart';
import 'package:immich_mobile/shared/services/backup.service.dart';
import 'package:photo_manager/photo_manager.dart';

class BackupNotifier extends StateNotifier<BackUpState> {
BackupNotifier()
BackupNotifier(this.ref)
: super(
BackUpState(
backupProgress: BackUpProgressEnum.idle,
Expand All @@ -29,6 +32,7 @@ class BackupNotifier extends StateNotifier<BackUpState> {
),
);

final Ref ref;
final BackupService _backupService = BackupService();
final ServerInfoService _serverInfoService = ServerInfoService();

Expand Down Expand Up @@ -96,7 +100,7 @@ class BackupNotifier extends StateNotifier<BackUpState> {

void cancelBackup() {
state.cancelToken.cancel('Cancel Backup');
state = state.copyWith(backupProgress: BackUpProgressEnum.idle);
state = state.copyWith(backupProgress: BackUpProgressEnum.idle, progressInPercentage: 0.0);
}

void _onAssetUploaded() {
Expand Down Expand Up @@ -130,8 +134,38 @@ class BackupNotifier extends StateNotifier<BackUpState> {
),
);
}

void resumeBackup() {
debugPrint("[resumeBackup]");
var authState = ref.read(authenticationProvider);

// Check if user is login
var accessKey = Hive.box(userInfoBox).get(accessTokenKey);

// User has been logged out return
if (accessKey == null || !authState.isAuthenticated) {
debugPrint("[resumeBackup] not authenticated - abort");
return;
}

// Check if this device is enable backup by the user
if ((authState.deviceInfo.deviceId == authState.deviceId) && authState.deviceInfo.isAutoBackup) {
// check if backup is alreayd in process - then return
if (state.backupProgress == BackUpProgressEnum.inProgress) {
debugPrint("[resumeBackup] Backup is already in progress - abort");
return;
}

// Run backup
debugPrint("[resumeBackup] Start back up");
startBackupProcess();
}

debugPrint("[resumeBackup] User disables auto backup");
return;
}
}

final backupProvider = StateNotifierProvider<BackupNotifier, BackUpState>((ref) {
return BackupNotifier();
return BackupNotifier(ref);
});
Loading