2022-08-18 16:41:59 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-06-18 14:36:58 +02:00
|
|
|
import 'package:cancellation_token_http/http.dart';
|
2022-05-06 14:22:23 +02:00
|
|
|
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/backup/models/available_album.model.dart';
|
2022-06-22 07:23:35 +02:00
|
|
|
import 'package:immich_mobile/modules/backup/models/backup_state.model.dart';
|
2022-07-06 23:12:55 +02:00
|
|
|
import 'package:immich_mobile/modules/backup/models/current_upload_asset.model.dart';
|
|
|
|
import 'package:immich_mobile/modules/backup/models/error_upload_asset.model.dart';
|
2022-05-06 14:22:23 +02:00
|
|
|
import 'package:immich_mobile/modules/backup/models/hive_backup_albums.model.dart';
|
2022-10-25 16:51:03 +02:00
|
|
|
import 'package:immich_mobile/modules/backup/models/hive_duplicated_assets.model.dart';
|
2022-07-06 23:12:55 +02:00
|
|
|
import 'package:immich_mobile/modules/backup/providers/error_backup_list.provider.dart';
|
2022-08-18 16:41:59 +02:00
|
|
|
import 'package:immich_mobile/modules/backup/background_service/background.service.dart';
|
2022-06-22 07:23:35 +02:00
|
|
|
import 'package:immich_mobile/modules/backup/services/backup.service.dart';
|
2022-06-25 20:46:51 +02:00
|
|
|
import 'package:immich_mobile/modules/login/models/authentication_state.model.dart';
|
2022-05-06 14:22:23 +02:00
|
|
|
import 'package:immich_mobile/modules/login/providers/authentication.provider.dart';
|
2022-08-18 16:41:59 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/app_state.provider.dart';
|
2022-06-22 07:23:35 +02:00
|
|
|
import 'package:immich_mobile/shared/services/server_info.service.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:openapi/api.dart';
|
2022-05-06 14:22:23 +02:00
|
|
|
import 'package:photo_manager/photo_manager.dart';
|
|
|
|
|
|
|
|
class BackupNotifier extends StateNotifier<BackUpState> {
|
2022-07-06 23:12:55 +02:00
|
|
|
BackupNotifier(
|
|
|
|
this._backupService,
|
|
|
|
this._serverInfoService,
|
|
|
|
this._authState,
|
2022-08-18 16:41:59 +02:00
|
|
|
this._backgroundService,
|
2022-07-06 23:12:55 +02:00
|
|
|
this.ref,
|
|
|
|
) : super(
|
2022-05-06 14:22:23 +02:00
|
|
|
BackUpState(
|
|
|
|
backupProgress: BackUpProgressEnum.idle,
|
2022-06-25 22:12:47 +02:00
|
|
|
allAssetsInDatabase: const [],
|
2022-05-06 14:22:23 +02:00
|
|
|
progressInPercentage: 0,
|
2022-06-18 14:36:58 +02:00
|
|
|
cancelToken: CancellationToken(),
|
2022-08-18 16:41:59 +02:00
|
|
|
backgroundBackup: false,
|
|
|
|
backupRequireWifi: true,
|
|
|
|
backupRequireCharging: false,
|
2022-07-13 14:23:48 +02:00
|
|
|
serverInfo: ServerInfoResponseDto(
|
2022-05-06 14:22:23 +02:00
|
|
|
diskAvailable: "0",
|
|
|
|
diskAvailableRaw: 0,
|
|
|
|
diskSize: "0",
|
|
|
|
diskSizeRaw: 0,
|
2022-07-13 14:23:48 +02:00
|
|
|
diskUsagePercentage: 0,
|
2022-05-06 14:22:23 +02:00
|
|
|
diskUse: "0",
|
|
|
|
diskUseRaw: 0,
|
|
|
|
),
|
|
|
|
availableAlbums: const [],
|
|
|
|
selectedBackupAlbums: const {},
|
|
|
|
excludedBackupAlbums: const {},
|
|
|
|
allUniqueAssets: const {},
|
|
|
|
selectedAlbumsBackupAssetsIds: const {},
|
2022-07-06 23:12:55 +02:00
|
|
|
currentUploadAsset: CurrentUploadAsset(
|
|
|
|
id: '...',
|
|
|
|
createdAt: DateTime.parse('2020-10-04'),
|
|
|
|
fileName: '...',
|
|
|
|
fileType: '...',
|
|
|
|
),
|
2022-05-06 14:22:23 +02:00
|
|
|
),
|
2022-06-25 22:12:47 +02:00
|
|
|
) {
|
|
|
|
getBackupInfo();
|
|
|
|
}
|
2022-05-06 14:22:23 +02:00
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
final BackupService _backupService;
|
|
|
|
final ServerInfoService _serverInfoService;
|
|
|
|
final AuthenticationState _authState;
|
2022-08-18 16:41:59 +02:00
|
|
|
final BackgroundService _backgroundService;
|
2022-07-06 23:12:55 +02:00
|
|
|
final Ref ref;
|
2022-05-06 14:22:23 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// UI INTERACTION
|
|
|
|
///
|
|
|
|
/// Album selection
|
|
|
|
/// Due to the overlapping assets across multiple albums on the device
|
|
|
|
/// We have method to include and exclude albums
|
|
|
|
/// The total unique assets will be used for backing mechanism
|
|
|
|
///
|
2022-08-18 16:41:59 +02:00
|
|
|
void addAlbumForBackup(AvailableAlbum album) {
|
2022-05-06 14:22:23 +02:00
|
|
|
if (state.excludedBackupAlbums.contains(album)) {
|
|
|
|
removeExcludedAlbumForBackup(album);
|
|
|
|
}
|
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
state = state
|
|
|
|
.copyWith(selectedBackupAlbums: {...state.selectedBackupAlbums, album});
|
2022-05-06 14:22:23 +02:00
|
|
|
_updateBackupAssetCount();
|
|
|
|
}
|
|
|
|
|
2022-08-18 16:41:59 +02:00
|
|
|
void addExcludedAlbumForBackup(AvailableAlbum album) {
|
2022-05-06 14:22:23 +02:00
|
|
|
if (state.selectedBackupAlbums.contains(album)) {
|
|
|
|
removeAlbumForBackup(album);
|
|
|
|
}
|
2022-06-22 07:23:35 +02:00
|
|
|
state = state
|
|
|
|
.copyWith(excludedBackupAlbums: {...state.excludedBackupAlbums, album});
|
2022-05-06 14:22:23 +02:00
|
|
|
_updateBackupAssetCount();
|
|
|
|
}
|
|
|
|
|
2022-08-18 16:41:59 +02:00
|
|
|
void removeAlbumForBackup(AvailableAlbum album) {
|
|
|
|
Set<AvailableAlbum> currentSelectedAlbums = state.selectedBackupAlbums;
|
2022-05-06 14:22:23 +02:00
|
|
|
|
|
|
|
currentSelectedAlbums.removeWhere((a) => a == album);
|
|
|
|
|
|
|
|
state = state.copyWith(selectedBackupAlbums: currentSelectedAlbums);
|
|
|
|
_updateBackupAssetCount();
|
|
|
|
}
|
|
|
|
|
2022-08-18 16:41:59 +02:00
|
|
|
void removeExcludedAlbumForBackup(AvailableAlbum album) {
|
|
|
|
Set<AvailableAlbum> currentExcludedAlbums = state.excludedBackupAlbums;
|
2022-05-06 14:22:23 +02:00
|
|
|
|
|
|
|
currentExcludedAlbums.removeWhere((a) => a == album);
|
|
|
|
|
|
|
|
state = state.copyWith(excludedBackupAlbums: currentExcludedAlbums);
|
|
|
|
_updateBackupAssetCount();
|
|
|
|
}
|
|
|
|
|
2022-08-18 16:41:59 +02:00
|
|
|
void configureBackgroundBackup({
|
|
|
|
bool? enabled,
|
|
|
|
bool? requireWifi,
|
|
|
|
bool? requireCharging,
|
|
|
|
required void Function(String msg) onError,
|
2022-08-31 15:08:40 +02:00
|
|
|
required void Function() onBatteryInfo,
|
2022-08-18 16:41:59 +02:00
|
|
|
}) async {
|
|
|
|
assert(enabled != null || requireWifi != null || requireCharging != null);
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
final bool wasEnabled = state.backgroundBackup;
|
|
|
|
final bool wasWifi = state.backupRequireWifi;
|
|
|
|
final bool wasCharing = state.backupRequireCharging;
|
|
|
|
state = state.copyWith(
|
|
|
|
backgroundBackup: enabled,
|
|
|
|
backupRequireWifi: requireWifi,
|
|
|
|
backupRequireCharging: requireCharging,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (state.backgroundBackup) {
|
2022-09-08 15:36:08 +02:00
|
|
|
bool success = true;
|
2022-08-18 16:41:59 +02:00
|
|
|
if (!wasEnabled) {
|
2022-08-31 15:08:40 +02:00
|
|
|
if (!await _backgroundService.isIgnoringBatteryOptimizations()) {
|
|
|
|
onBatteryInfo();
|
|
|
|
}
|
2022-09-08 15:36:08 +02:00
|
|
|
success &= await _backgroundService.enableService(immediate: true);
|
2022-08-18 16:41:59 +02:00
|
|
|
}
|
2022-09-08 15:36:08 +02:00
|
|
|
success &= success &&
|
|
|
|
await _backgroundService.configureService(
|
2022-08-18 16:41:59 +02:00
|
|
|
requireUnmetered: state.backupRequireWifi,
|
|
|
|
requireCharging: state.backupRequireCharging,
|
|
|
|
);
|
2022-08-30 16:09:19 +02:00
|
|
|
if (success) {
|
|
|
|
await Hive.box(backgroundBackupInfoBox)
|
|
|
|
.put(backupRequireWifi, state.backupRequireWifi);
|
|
|
|
await Hive.box(backgroundBackupInfoBox)
|
|
|
|
.put(backupRequireCharging, state.backupRequireCharging);
|
|
|
|
} else {
|
2022-08-18 16:41:59 +02:00
|
|
|
state = state.copyWith(
|
|
|
|
backgroundBackup: wasEnabled,
|
|
|
|
backupRequireWifi: wasWifi,
|
|
|
|
backupRequireCharging: wasCharing,
|
|
|
|
);
|
|
|
|
onError("backup_controller_page_background_configure_error");
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-08 15:36:08 +02:00
|
|
|
final bool success = await _backgroundService.disableService();
|
2022-08-18 16:41:59 +02:00
|
|
|
if (!success) {
|
|
|
|
state = state.copyWith(backgroundBackup: wasEnabled);
|
|
|
|
onError("backup_controller_page_background_configure_error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-06 14:22:23 +02:00
|
|
|
///
|
|
|
|
/// Get all album on the device
|
|
|
|
/// Get all selected and excluded album from the user's persistent storage
|
|
|
|
/// If this is the first time performing backup - set the default selected album to be
|
|
|
|
/// the one that has all assets (Recent on Android, Recents on iOS)
|
|
|
|
///
|
2022-06-25 22:12:47 +02:00
|
|
|
Future<void> _getBackupAlbumsInfo() async {
|
2022-05-06 14:22:23 +02:00
|
|
|
// Get all albums on the device
|
|
|
|
List<AvailableAlbum> availableAlbums = [];
|
2022-06-22 07:23:35 +02:00
|
|
|
List<AssetPathEntity> albums = await PhotoManager.getAssetPathList(
|
2022-07-13 14:23:48 +02:00
|
|
|
hasAll: true,
|
|
|
|
type: RequestType.common,
|
|
|
|
);
|
2022-05-06 14:22:23 +02:00
|
|
|
|
|
|
|
for (AssetPathEntity album in albums) {
|
|
|
|
AvailableAlbum availableAlbum = AvailableAlbum(albumEntity: album);
|
|
|
|
|
2022-09-16 23:46:23 +02:00
|
|
|
var assetCountInAlbum = await album.assetCountAsync;
|
|
|
|
if (assetCountInAlbum > 0) {
|
|
|
|
var assetList =
|
|
|
|
await album.getAssetListRange(start: 0, end: assetCountInAlbum);
|
|
|
|
|
|
|
|
if (assetList.isNotEmpty) {
|
|
|
|
var thumbnailAsset = assetList.first;
|
|
|
|
var thumbnailData = await thumbnailAsset
|
|
|
|
.thumbnailDataWithSize(const ThumbnailSize(512, 512));
|
|
|
|
availableAlbum =
|
|
|
|
availableAlbum.copyWith(thumbnailData: thumbnailData);
|
|
|
|
}
|
2022-05-06 14:22:23 +02:00
|
|
|
|
2022-09-16 23:46:23 +02:00
|
|
|
availableAlbums.add(availableAlbum);
|
|
|
|
}
|
2022-05-06 14:22:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state = state.copyWith(availableAlbums: availableAlbums);
|
|
|
|
|
|
|
|
// Put persistent storage info into local state of the app
|
|
|
|
// Get local storage on selected backup album
|
2022-06-22 07:23:35 +02:00
|
|
|
Box<HiveBackupAlbums> backupAlbumInfoBox =
|
|
|
|
Hive.box<HiveBackupAlbums>(hiveBackupInfoBox);
|
2022-05-06 14:22:23 +02:00
|
|
|
HiveBackupAlbums? backupAlbumInfo = backupAlbumInfoBox.get(
|
|
|
|
backupInfoKey,
|
|
|
|
defaultValue: HiveBackupAlbums(
|
|
|
|
selectedAlbumIds: [],
|
|
|
|
excludedAlbumsIds: [],
|
2022-08-18 16:41:59 +02:00
|
|
|
lastSelectedBackupTime: [],
|
|
|
|
lastExcludedBackupTime: [],
|
2022-05-06 14:22:23 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (backupAlbumInfo == null) {
|
|
|
|
debugPrint("[ERROR] getting Hive backup album infomation");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First time backup - set isAll album is the default one for backup.
|
|
|
|
if (backupAlbumInfo.selectedAlbumIds.isEmpty) {
|
|
|
|
debugPrint("First time backup setup recent album as default");
|
|
|
|
|
|
|
|
// Get album that contains all assets
|
2022-06-22 07:23:35 +02:00
|
|
|
var list = await PhotoManager.getAssetPathList(
|
2022-07-13 14:23:48 +02:00
|
|
|
hasAll: true,
|
|
|
|
onlyAll: true,
|
|
|
|
type: RequestType.common,
|
|
|
|
);
|
2022-08-03 22:36:12 +02:00
|
|
|
|
|
|
|
if (list.isEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-06 14:22:23 +02:00
|
|
|
AssetPathEntity albumHasAllAssets = list.first;
|
|
|
|
|
|
|
|
backupAlbumInfoBox.put(
|
|
|
|
backupInfoKey,
|
|
|
|
HiveBackupAlbums(
|
|
|
|
selectedAlbumIds: [albumHasAllAssets.id],
|
|
|
|
excludedAlbumsIds: [],
|
2022-08-18 16:41:59 +02:00
|
|
|
lastSelectedBackupTime: [
|
|
|
|
DateTime.fromMillisecondsSinceEpoch(0, isUtc: true)
|
|
|
|
],
|
|
|
|
lastExcludedBackupTime: [],
|
2022-05-06 14:22:23 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
backupAlbumInfo = backupAlbumInfoBox.get(backupInfoKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate AssetPathEntity from id to add to local state
|
|
|
|
try {
|
2022-08-18 16:41:59 +02:00
|
|
|
Set<AvailableAlbum> selectedAlbums = {};
|
|
|
|
for (var i = 0; i < backupAlbumInfo!.selectedAlbumIds.length; i++) {
|
|
|
|
var albumAsset =
|
|
|
|
await AssetPathEntity.fromId(backupAlbumInfo.selectedAlbumIds[i]);
|
|
|
|
selectedAlbums.add(
|
|
|
|
AvailableAlbum(
|
|
|
|
albumEntity: albumAsset,
|
|
|
|
lastBackup: backupAlbumInfo.lastSelectedBackupTime.length > i
|
|
|
|
? backupAlbumInfo.lastSelectedBackupTime[i]
|
|
|
|
: DateTime.fromMillisecondsSinceEpoch(0, isUtc: true),
|
|
|
|
),
|
2022-07-13 14:23:48 +02:00
|
|
|
);
|
2022-05-06 14:22:23 +02:00
|
|
|
}
|
|
|
|
|
2022-08-18 16:41:59 +02:00
|
|
|
Set<AvailableAlbum> excludedAlbums = {};
|
|
|
|
for (var i = 0; i < backupAlbumInfo.excludedAlbumsIds.length; i++) {
|
|
|
|
var albumAsset =
|
|
|
|
await AssetPathEntity.fromId(backupAlbumInfo.excludedAlbumsIds[i]);
|
|
|
|
excludedAlbums.add(
|
|
|
|
AvailableAlbum(
|
|
|
|
albumEntity: albumAsset,
|
|
|
|
lastBackup: backupAlbumInfo.lastExcludedBackupTime.length > i
|
|
|
|
? backupAlbumInfo.lastExcludedBackupTime[i]
|
|
|
|
: DateTime.fromMillisecondsSinceEpoch(0, isUtc: true),
|
|
|
|
),
|
2022-07-13 14:23:48 +02:00
|
|
|
);
|
2022-05-06 14:22:23 +02:00
|
|
|
}
|
2022-08-18 16:41:59 +02:00
|
|
|
state = state.copyWith(
|
|
|
|
selectedBackupAlbums: selectedAlbums,
|
|
|
|
excludedBackupAlbums: excludedAlbums,
|
|
|
|
);
|
2022-05-06 14:22:23 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("[ERROR] Failed to generate album from id $e");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// From all the selected and albums assets
|
|
|
|
/// Find the assets that are not overlapping between the two sets
|
|
|
|
/// Those assets are unique and are used as the total assets
|
|
|
|
///
|
2022-06-25 22:12:47 +02:00
|
|
|
Future<void> _updateBackupAssetCount() async {
|
2022-10-25 16:51:03 +02:00
|
|
|
Set<String> duplicatedAssetIds = _backupService.getDuplicatedAssetIds();
|
2022-05-06 14:22:23 +02:00
|
|
|
Set<AssetEntity> assetsFromSelectedAlbums = {};
|
|
|
|
Set<AssetEntity> assetsFromExcludedAlbums = {};
|
|
|
|
|
|
|
|
for (var album in state.selectedBackupAlbums) {
|
2022-09-16 23:46:23 +02:00
|
|
|
var assets = await album.albumEntity.getAssetListRange(
|
|
|
|
start: 0,
|
|
|
|
end: await album.albumEntity.assetCountAsync,
|
|
|
|
);
|
2022-05-06 14:22:23 +02:00
|
|
|
assetsFromSelectedAlbums.addAll(assets);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var album in state.excludedBackupAlbums) {
|
2022-09-16 23:46:23 +02:00
|
|
|
var assets = await album.albumEntity.getAssetListRange(
|
|
|
|
start: 0,
|
|
|
|
end: await album.albumEntity.assetCountAsync,
|
|
|
|
);
|
2022-05-06 14:22:23 +02:00
|
|
|
assetsFromExcludedAlbums.addAll(assets);
|
|
|
|
}
|
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
Set<AssetEntity> allUniqueAssets =
|
|
|
|
assetsFromSelectedAlbums.difference(assetsFromExcludedAlbums);
|
2022-07-13 14:23:48 +02:00
|
|
|
var allAssetsInDatabase = await _backupService.getDeviceBackupAsset();
|
|
|
|
|
|
|
|
if (allAssetsInDatabase == null) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-06 14:22:23 +02:00
|
|
|
|
|
|
|
// Find asset that were backup from selected albums
|
2022-06-22 07:23:35 +02:00
|
|
|
Set<String> selectedAlbumsBackupAssets =
|
|
|
|
Set.from(allUniqueAssets.map((e) => e.id));
|
2022-10-25 16:51:03 +02:00
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
selectedAlbumsBackupAssets
|
2022-06-25 22:12:47 +02:00
|
|
|
.removeWhere((assetId) => !allAssetsInDatabase.contains(assetId));
|
2022-05-06 14:22:23 +02:00
|
|
|
|
2022-10-25 16:51:03 +02:00
|
|
|
// Remove duplicated asset from all unique assets
|
|
|
|
allUniqueAssets.removeWhere(
|
|
|
|
(asset) => duplicatedAssetIds.contains(asset.id),
|
|
|
|
);
|
|
|
|
|
2022-05-06 14:22:23 +02:00
|
|
|
if (allUniqueAssets.isEmpty) {
|
|
|
|
debugPrint("No Asset On Device");
|
|
|
|
state = state.copyWith(
|
|
|
|
backupProgress: BackUpProgressEnum.idle,
|
2022-06-25 22:12:47 +02:00
|
|
|
allAssetsInDatabase: allAssetsInDatabase,
|
2022-05-06 14:22:23 +02:00
|
|
|
allUniqueAssets: {},
|
|
|
|
selectedAlbumsBackupAssetsIds: selectedAlbumsBackupAssets,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
state = state.copyWith(
|
2022-06-25 22:12:47 +02:00
|
|
|
allAssetsInDatabase: allAssetsInDatabase,
|
2022-05-06 14:22:23 +02:00
|
|
|
allUniqueAssets: allUniqueAssets,
|
|
|
|
selectedAlbumsBackupAssetsIds: selectedAlbumsBackupAssets,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save to persistent storage
|
|
|
|
_updatePersistentAlbumsSelection();
|
2022-06-25 22:12:47 +02:00
|
|
|
|
|
|
|
return;
|
2022-05-06 14:22:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Get all necessary information for calculating the available albums,
|
|
|
|
/// which albums are selected or excluded
|
|
|
|
/// and then update the UI according to those information
|
|
|
|
///
|
2022-06-25 22:12:47 +02:00
|
|
|
Future<void> getBackupInfo() async {
|
2022-08-18 16:41:59 +02:00
|
|
|
final bool isEnabled = await _backgroundService.isBackgroundBackupEnabled();
|
|
|
|
state = state.copyWith(backgroundBackup: isEnabled);
|
|
|
|
if (state.backupProgress != BackUpProgressEnum.inBackground) {
|
2022-09-16 23:46:23 +02:00
|
|
|
await _getBackupAlbumsInfo();
|
|
|
|
await _updateServerInfo();
|
2022-08-18 16:41:59 +02:00
|
|
|
await _updateBackupAssetCount();
|
|
|
|
}
|
2022-05-06 14:22:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Save user selection of selected albums and excluded albums to
|
|
|
|
/// Hive database
|
|
|
|
///
|
|
|
|
void _updatePersistentAlbumsSelection() {
|
2022-08-18 16:41:59 +02:00
|
|
|
final epoch = DateTime.fromMillisecondsSinceEpoch(0, isUtc: true);
|
2022-06-22 07:23:35 +02:00
|
|
|
Box<HiveBackupAlbums> backupAlbumInfoBox =
|
|
|
|
Hive.box<HiveBackupAlbums>(hiveBackupInfoBox);
|
2022-05-06 14:22:23 +02:00
|
|
|
backupAlbumInfoBox.put(
|
|
|
|
backupInfoKey,
|
|
|
|
HiveBackupAlbums(
|
|
|
|
selectedAlbumIds: state.selectedBackupAlbums.map((e) => e.id).toList(),
|
|
|
|
excludedAlbumsIds: state.excludedBackupAlbums.map((e) => e.id).toList(),
|
2022-08-18 16:41:59 +02:00
|
|
|
lastSelectedBackupTime: state.selectedBackupAlbums
|
|
|
|
.map((e) => e.lastBackup ?? epoch)
|
|
|
|
.toList(),
|
|
|
|
lastExcludedBackupTime: state.excludedBackupAlbums
|
|
|
|
.map((e) => e.lastBackup ?? epoch)
|
|
|
|
.toList(),
|
2022-05-06 14:22:23 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Invoke backup process
|
|
|
|
///
|
2022-08-18 16:41:59 +02:00
|
|
|
Future<void> startBackupProcess() async {
|
|
|
|
assert(state.backupProgress == BackUpProgressEnum.idle);
|
2022-05-06 14:22:23 +02:00
|
|
|
state = state.copyWith(backupProgress: BackUpProgressEnum.inProgress);
|
|
|
|
|
2022-06-25 22:12:47 +02:00
|
|
|
await getBackupInfo();
|
|
|
|
|
2022-05-06 14:22:23 +02:00
|
|
|
var authResult = await PhotoManager.requestPermissionExtend();
|
|
|
|
if (authResult.isAuth) {
|
|
|
|
await PhotoManager.clearFileCache();
|
|
|
|
|
|
|
|
if (state.allUniqueAssets.isEmpty) {
|
|
|
|
debugPrint("No Asset On Device - Abort Backup Process");
|
|
|
|
state = state.copyWith(backupProgress: BackUpProgressEnum.idle);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-25 22:12:47 +02:00
|
|
|
Set<AssetEntity> assetsWillBeBackup = Set.from(state.allUniqueAssets);
|
2022-05-06 14:22:23 +02:00
|
|
|
|
|
|
|
// Remove item that has already been backed up
|
2022-06-25 22:12:47 +02:00
|
|
|
for (var assetId in state.allAssetsInDatabase) {
|
2022-05-06 14:22:23 +02:00
|
|
|
assetsWillBeBackup.removeWhere((e) => e.id == assetId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (assetsWillBeBackup.isEmpty) {
|
|
|
|
state = state.copyWith(backupProgress: BackUpProgressEnum.idle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform Backup
|
2022-06-18 14:36:58 +02:00
|
|
|
state = state.copyWith(cancelToken: CancellationToken());
|
2022-08-18 16:41:59 +02:00
|
|
|
await _backupService.backupAsset(
|
2022-07-06 23:12:55 +02:00
|
|
|
assetsWillBeBackup,
|
|
|
|
state.cancelToken,
|
|
|
|
_onAssetUploaded,
|
|
|
|
_onUploadProgress,
|
|
|
|
_onSetCurrentBackupAsset,
|
|
|
|
_onBackupError,
|
|
|
|
);
|
2022-08-18 16:41:59 +02:00
|
|
|
await _notifyBackgroundServiceCanRun();
|
2022-05-06 14:22:23 +02:00
|
|
|
} else {
|
|
|
|
PhotoManager.openSetting();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:12:55 +02:00
|
|
|
void _onBackupError(ErrorUploadAsset errorAssetInfo) {
|
|
|
|
ref.watch(errorBackupListProvider.notifier).add(errorAssetInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSetCurrentBackupAsset(CurrentUploadAsset currentUploadAsset) {
|
|
|
|
state = state.copyWith(currentUploadAsset: currentUploadAsset);
|
|
|
|
}
|
|
|
|
|
2022-05-06 14:22:23 +02:00
|
|
|
void cancelBackup() {
|
2022-08-18 16:41:59 +02:00
|
|
|
if (state.backupProgress != BackUpProgressEnum.inProgress) {
|
|
|
|
_notifyBackgroundServiceCanRun();
|
|
|
|
}
|
2022-06-18 14:36:58 +02:00
|
|
|
state.cancelToken.cancel();
|
2022-06-22 07:23:35 +02:00
|
|
|
state = state.copyWith(
|
2022-07-13 14:23:48 +02:00
|
|
|
backupProgress: BackUpProgressEnum.idle,
|
|
|
|
progressInPercentage: 0.0,
|
|
|
|
);
|
2022-05-06 14:22:23 +02:00
|
|
|
}
|
|
|
|
|
2022-10-25 16:51:03 +02:00
|
|
|
void _onAssetUploaded(
|
|
|
|
String deviceAssetId,
|
|
|
|
String deviceId,
|
|
|
|
bool isDuplicated,
|
|
|
|
) {
|
|
|
|
if (isDuplicated) {
|
|
|
|
state = state.copyWith(
|
|
|
|
allUniqueAssets: state.allUniqueAssets
|
|
|
|
.where((asset) => asset.id != deviceAssetId)
|
|
|
|
.toSet(),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
state = state.copyWith(
|
|
|
|
selectedAlbumsBackupAssetsIds: {
|
|
|
|
...state.selectedAlbumsBackupAssetsIds,
|
|
|
|
deviceAssetId
|
|
|
|
},
|
|
|
|
allAssetsInDatabase: [...state.allAssetsInDatabase, deviceAssetId],
|
|
|
|
);
|
|
|
|
}
|
2022-06-22 07:23:35 +02:00
|
|
|
|
|
|
|
if (state.allUniqueAssets.length -
|
|
|
|
state.selectedAlbumsBackupAssetsIds.length ==
|
|
|
|
0) {
|
2022-08-18 16:41:59 +02:00
|
|
|
final latestAssetBackup =
|
|
|
|
state.allUniqueAssets.map((e) => e.modifiedDateTime).reduce(
|
|
|
|
(v, e) => e.isAfter(v) ? e : v,
|
|
|
|
);
|
2022-06-22 07:23:35 +02:00
|
|
|
state = state.copyWith(
|
2022-08-18 16:41:59 +02:00
|
|
|
selectedBackupAlbums: state.selectedBackupAlbums
|
|
|
|
.map((e) => e.copyWith(lastBackup: latestAssetBackup))
|
|
|
|
.toSet(),
|
|
|
|
excludedBackupAlbums: state.excludedBackupAlbums
|
|
|
|
.map((e) => e.copyWith(lastBackup: latestAssetBackup))
|
|
|
|
.toSet(),
|
2022-07-13 14:23:48 +02:00
|
|
|
backupProgress: BackUpProgressEnum.done,
|
|
|
|
progressInPercentage: 0.0,
|
|
|
|
);
|
2022-08-18 16:41:59 +02:00
|
|
|
_updatePersistentAlbumsSelection();
|
2022-05-06 14:22:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_updateServerInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onUploadProgress(int sent, int total) {
|
2022-06-22 07:23:35 +02:00
|
|
|
state = state.copyWith(
|
2022-07-13 14:23:48 +02:00
|
|
|
progressInPercentage: (sent.toDouble() / total.toDouble() * 100),
|
|
|
|
);
|
2022-05-06 14:22:23 +02:00
|
|
|
}
|
|
|
|
|
2022-06-25 22:12:47 +02:00
|
|
|
Future<void> _updateServerInfo() async {
|
2022-05-06 14:22:23 +02:00
|
|
|
var serverInfo = await _serverInfoService.getServerInfo();
|
|
|
|
|
|
|
|
// Update server info
|
2022-07-13 14:23:48 +02:00
|
|
|
if (serverInfo != null) {
|
|
|
|
state = state.copyWith(
|
|
|
|
serverInfo: serverInfo,
|
|
|
|
);
|
|
|
|
}
|
2022-05-06 14:22:23 +02:00
|
|
|
}
|
|
|
|
|
2022-08-18 16:41:59 +02:00
|
|
|
Future<void> _resumeBackup() async {
|
2022-05-06 14:22:23 +02:00
|
|
|
// Check if user is login
|
|
|
|
var accessKey = Hive.box(userInfoBox).get(accessTokenKey);
|
|
|
|
|
|
|
|
// User has been logged out return
|
2022-06-25 20:46:51 +02:00
|
|
|
if (accessKey == null || !_authState.isAuthenticated) {
|
|
|
|
debugPrint("[resumeBackup] not authenticated - abort");
|
|
|
|
return;
|
|
|
|
}
|
2022-05-06 14:22:23 +02:00
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
// 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;
|
2022-05-06 14:22:23 +02:00
|
|
|
}
|
|
|
|
|
2022-08-18 16:41:59 +02:00
|
|
|
if (state.backupProgress == BackUpProgressEnum.inBackground) {
|
|
|
|
debugPrint("[resumeBackup] Background backup is running - abort");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
// Run backup
|
|
|
|
debugPrint("[resumeBackup] Start back up");
|
2022-08-18 16:41:59 +02:00
|
|
|
await startBackupProcess();
|
2022-05-06 14:22:23 +02:00
|
|
|
}
|
2022-06-25 20:46:51 +02:00
|
|
|
|
|
|
|
return;
|
2022-05-06 14:22:23 +02:00
|
|
|
}
|
2022-08-18 16:41:59 +02:00
|
|
|
|
|
|
|
Future<void> resumeBackup() async {
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
// assumes the background service is currently running
|
|
|
|
// if true, waits until it has stopped to update the app state from HiveDB
|
|
|
|
// before actually resuming backup by calling the internal `_resumeBackup`
|
|
|
|
final BackUpProgressEnum previous = state.backupProgress;
|
|
|
|
state = state.copyWith(backupProgress: BackUpProgressEnum.inBackground);
|
|
|
|
final bool hasLock = await _backgroundService.acquireLock();
|
|
|
|
if (!hasLock) {
|
2022-11-08 19:00:24 +02:00
|
|
|
debugPrint("WARNING [resumeBackup] failed to acquireLock");
|
2022-08-18 16:41:59 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-11-08 19:00:24 +02:00
|
|
|
await Future.wait([
|
|
|
|
Hive.openBox<HiveBackupAlbums>(hiveBackupInfoBox),
|
|
|
|
Hive.openBox<HiveDuplicatedAssets>(duplicatedAssetsBox),
|
|
|
|
Hive.openBox(backgroundBackupInfoBox),
|
|
|
|
]);
|
|
|
|
final HiveBackupAlbums? albums =
|
|
|
|
Hive.box<HiveBackupAlbums>(hiveBackupInfoBox).get(backupInfoKey);
|
2022-08-18 16:41:59 +02:00
|
|
|
Set<AvailableAlbum> selectedAlbums = state.selectedBackupAlbums;
|
|
|
|
Set<AvailableAlbum> excludedAlbums = state.excludedBackupAlbums;
|
|
|
|
if (albums != null) {
|
|
|
|
selectedAlbums = _updateAlbumsBackupTime(
|
|
|
|
selectedAlbums,
|
|
|
|
albums.selectedAlbumIds,
|
|
|
|
albums.lastSelectedBackupTime,
|
|
|
|
);
|
|
|
|
excludedAlbums = _updateAlbumsBackupTime(
|
|
|
|
excludedAlbums,
|
|
|
|
albums.excludedAlbumsIds,
|
|
|
|
albums.lastExcludedBackupTime,
|
|
|
|
);
|
|
|
|
}
|
2022-11-08 19:00:24 +02:00
|
|
|
final Box backgroundBox = Hive.box(backgroundBackupInfoBox);
|
2022-08-18 16:41:59 +02:00
|
|
|
state = state.copyWith(
|
|
|
|
backupProgress: previous,
|
|
|
|
selectedBackupAlbums: selectedAlbums,
|
|
|
|
excludedBackupAlbums: excludedAlbums,
|
2022-08-30 16:09:19 +02:00
|
|
|
backupRequireWifi: backgroundBox.get(backupRequireWifi),
|
|
|
|
backupRequireCharging: backgroundBox.get(backupRequireCharging),
|
2022-08-18 16:41:59 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return _resumeBackup();
|
|
|
|
}
|
|
|
|
|
|
|
|
Set<AvailableAlbum> _updateAlbumsBackupTime(
|
|
|
|
Set<AvailableAlbum> albums,
|
|
|
|
List<String> ids,
|
|
|
|
List<DateTime> times,
|
|
|
|
) {
|
|
|
|
Set<AvailableAlbum> result = {};
|
|
|
|
for (int i = 0; i < ids.length; i++) {
|
|
|
|
try {
|
|
|
|
AvailableAlbum a = albums.firstWhere((e) => e.id == ids[i]);
|
|
|
|
result.add(a.copyWith(lastBackup: times[i]));
|
|
|
|
} on StateError {
|
|
|
|
debugPrint("[_updateAlbumBackupTime] failed to find album in state");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _notifyBackgroundServiceCanRun() async {
|
|
|
|
const allowedStates = [
|
|
|
|
AppStateEnum.inactive,
|
|
|
|
AppStateEnum.paused,
|
|
|
|
AppStateEnum.detached,
|
|
|
|
];
|
|
|
|
if (Platform.isAndroid &&
|
|
|
|
allowedStates.contains(ref.read(appStateProvider.notifier).state)) {
|
|
|
|
try {
|
|
|
|
if (Hive.isBoxOpen(hiveBackupInfoBox)) {
|
|
|
|
await Hive.box<HiveBackupAlbums>(hiveBackupInfoBox).close();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
debugPrint("[_notifyBackgroundServiceCanRun] failed to close box");
|
|
|
|
}
|
2022-10-25 16:51:03 +02:00
|
|
|
try {
|
|
|
|
if (Hive.isBoxOpen(duplicatedAssetsBox)) {
|
|
|
|
await Hive.box<HiveDuplicatedAssets>(duplicatedAssetsBox).close();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
debugPrint("[_notifyBackgroundServiceCanRun] failed to close box");
|
|
|
|
}
|
2022-08-30 16:09:19 +02:00
|
|
|
try {
|
|
|
|
if (Hive.isBoxOpen(backgroundBackupInfoBox)) {
|
|
|
|
await Hive.box(backgroundBackupInfoBox).close();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
debugPrint("[_notifyBackgroundServiceCanRun] failed to close box");
|
|
|
|
}
|
2022-08-18 16:41:59 +02:00
|
|
|
_backgroundService.releaseLock();
|
|
|
|
}
|
|
|
|
}
|
2022-05-06 14:22:23 +02:00
|
|
|
}
|
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
final backupProvider =
|
|
|
|
StateNotifierProvider<BackupNotifier, BackUpState>((ref) {
|
2022-06-25 20:46:51 +02:00
|
|
|
return BackupNotifier(
|
|
|
|
ref.watch(backupServiceProvider),
|
|
|
|
ref.watch(serverInfoServiceProvider),
|
|
|
|
ref.watch(authenticationProvider),
|
2022-08-18 16:41:59 +02:00
|
|
|
ref.watch(backgroundServiceProvider),
|
2022-07-06 23:12:55 +02:00
|
|
|
ref,
|
2022-06-25 20:46:51 +02:00
|
|
|
);
|
2022-05-06 14:22:23 +02:00
|
|
|
});
|