ente/lib/ui/settings/backup_section_widget.dart

219 lines
6.9 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'package:flutter/material.dart';
import "package:photos/generated/l10n.dart";
import 'package:photos/models/backup_status.dart';
2021-09-28 12:51:20 +00:00
import 'package:photos/models/duplicate_files.dart';
import 'package:photos/services/deduplication_service.dart';
import 'package:photos/services/sync_service.dart';
import 'package:photos/services/update_service.dart';
2022-10-21 13:37:57 +00:00
import 'package:photos/theme/ente_theme.dart';
2021-07-11 18:35:16 +00:00
import 'package:photos/ui/backup_folder_selection_page.dart';
2022-10-28 06:19:49 +00:00
import 'package:photos/ui/backup_settings_screen.dart';
import 'package:photos/ui/components/captioned_text_widget.dart';
import 'package:photos/ui/components/dialog_widget.dart';
import 'package:photos/ui/components/expandable_menu_item_widget.dart';
2023-01-31 12:21:57 +00:00
import 'package:photos/ui/components/menu_item_widget/menu_item_widget.dart';
import 'package:photos/ui/components/models/button_type.dart';
import 'package:photos/ui/settings/common_settings.dart';
2022-07-01 14:39:02 +00:00
import 'package:photos/ui/tools/deduplicate_page.dart';
import 'package:photos/ui/tools/free_space_page.dart';
import 'package:photos/utils/data_util.dart';
import 'package:photos/utils/dialog_util.dart';
2021-06-28 10:12:21 +00:00
import 'package:photos/utils/navigation_util.dart';
2021-06-28 18:47:48 +00:00
import 'package:photos/utils/toast_util.dart';
class BackupSectionWidget extends StatefulWidget {
const BackupSectionWidget({Key? key}) : super(key: key);
@override
BackupSectionWidgetState createState() => BackupSectionWidgetState();
}
class BackupSectionWidgetState extends State<BackupSectionWidget> {
@override
Widget build(BuildContext context) {
return ExpandableMenuItemWidget(
title: S.of(context).backup,
selectionOptionsWidget: _getSectionOptions(context),
leadingIcon: Icons.backup_outlined,
);
}
Widget _getSectionOptions(BuildContext context) {
final List<Widget> sectionOptions = [
sectionOptionSpacing,
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: S.of(context).backedUpFolders,
),
2022-10-21 13:37:57 +00:00
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
routeToPage(
context,
BackupFolderSelectionPage(
buttonText: S.of(context).backup,
),
);
},
),
sectionOptionSpacing,
2022-10-28 06:19:49 +00:00
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: S.of(context).backupSettings,
2022-10-28 06:19:49 +00:00
),
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
2022-10-28 06:19:49 +00:00
routeToPage(
context,
const BackupSettingsScreen(),
);
},
),
sectionOptionSpacing,
];
sectionOptions.addAll(
[
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: S.of(context).freeUpDeviceSpace,
),
2022-10-21 13:37:57 +00:00
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
showOnlyLoadingState: true,
2021-07-28 14:28:12 +00:00
onTap: () async {
BackupStatus status;
try {
status = await SyncService.instance.getBackupStatus();
2022-07-03 10:09:01 +00:00
} catch (e) {
showGenericErrorDialog(context: context);
return;
}
2021-07-28 14:28:12 +00:00
if (status.localIDs.isEmpty) {
2022-06-11 08:23:52 +00:00
showErrorDialog(
context,
S.of(context).allClear,
S.of(context).noDeviceThatCanBeDeleted,
2022-06-11 08:23:52 +00:00
);
2021-07-28 14:28:12 +00:00
} else {
final bool? result =
await routeToPage(context, FreeSpacePage(status));
2021-07-28 14:28:12 +00:00
if (result == true) {
_showSpaceFreedDialog(status);
}
2021-07-28 14:28:12 +00:00
}
},
),
sectionOptionSpacing,
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: S.of(context).removeDuplicates,
),
2022-10-21 13:37:57 +00:00
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
showOnlyLoadingState: true,
onTap: () async {
2021-09-28 12:51:20 +00:00
List<DuplicateFiles> duplicates;
2021-09-28 12:16:15 +00:00
try {
2021-09-28 12:51:20 +00:00
duplicates =
2021-09-28 12:16:15 +00:00
await DeduplicationService.instance.getDuplicateFiles();
} catch (e) {
showGenericErrorDialog(context: context);
2021-09-28 12:51:20 +00:00
return;
}
if (duplicates.isEmpty) {
2022-06-11 08:23:52 +00:00
showErrorDialog(
context,
S.of(context).noDuplicates,
S.of(context).youveNoDuplicateFilesThatCanBeCleared,
2022-06-11 08:23:52 +00:00
);
2021-09-28 12:51:20 +00:00
} else {
final DeduplicationResult? result =
2021-09-28 12:51:20 +00:00
await routeToPage(context, DeduplicatePage(duplicates));
if (result != null) {
_showDuplicateFilesDeletedDialog(result);
}
}
},
),
sectionOptionSpacing,
2021-07-28 14:28:12 +00:00
],
);
return Column(
children: sectionOptions,
);
}
void _showSpaceFreedDialog(BackupStatus status) {
final DialogWidget dialog = choiceDialog(
title: S.of(context).success,
body: S.of(context).youHaveSuccessfullyFreedUp(formatBytes(status.size)),
firstButtonLabel: S.of(context).rateUs,
firstButtonOnTap: () async {
UpdateService.instance.launchReviewUrl();
},
firstButtonType: ButtonType.primary,
secondButtonLabel: S.of(context).ok,
secondButtonOnTap: () async {
if (Platform.isIOS) {
showToast(
context,
S.of(context).remindToEmptyDeviceTrash,
);
}
},
);
showConfettiDialog(
context: context,
2022-12-30 01:29:50 +00:00
dialogBuilder: (BuildContext context) {
return dialog;
},
barrierColor: Colors.black87,
confettiAlignment: Alignment.topCenter,
useRootNavigator: true,
);
}
2021-09-22 08:26:44 +00:00
void _showDuplicateFilesDeletedDialog(DeduplicationResult result) {
final DialogWidget dialog = choiceDialog(
title: S.of(context).sparkleSuccess,
body: S.of(context).duplicateFileCountWithStorageSaved(
result.count,
formatBytes(result.size),
),
firstButtonLabel: S.of(context).rateUs,
firstButtonOnTap: () async {
UpdateService.instance.launchReviewUrl();
},
firstButtonType: ButtonType.primary,
secondButtonLabel: S.of(context).ok,
secondButtonOnTap: () async {
showShortToast(
context,
S.of(context).remindToEmptyEnteTrash,
);
},
);
showConfettiDialog(
context: context,
2022-12-30 01:29:50 +00:00
dialogBuilder: (BuildContext context) {
return dialog;
},
barrierColor: Colors.black87,
confettiAlignment: Alignment.topCenter,
useRootNavigator: true,
);
}
}