ente/lib/ui/settings/backup_section_widget.dart

335 lines
10 KiB
Dart
Raw Normal View History

// @dart=2.9
import 'dart:io';
import 'package:expandable/expandable.dart';
import 'package:flutter/material.dart';
import 'package:photos/core/configuration.dart';
2022-07-12 06:30:02 +00:00
import 'package:photos/ente_theme_data.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';
2021-07-11 18:35:16 +00:00
import 'package:photos/ui/backup_folder_selection_page.dart';
import 'package:photos/ui/common/dialogs.dart';
import 'package:photos/ui/components/captioned_text_widget.dart';
import 'package:photos/ui/components/menu_item_widget.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';
import 'package:url_launcher/url_launcher_string.dart';
class BackupSectionWidget extends StatefulWidget {
const BackupSectionWidget({Key key}) : super(key: key);
@override
BackupSectionWidgetState createState() => BackupSectionWidgetState();
}
class BackupSectionWidgetState extends State<BackupSectionWidget> {
final expandableController = ExpandableController(initialExpanded: false);
@override
void dispose() {
expandableController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return ExpandablePanel(
header: MenuItemWidget(
captionedTextWidget: const CaptionedTextWidget(
text: "Backup",
),
isHeaderOfExpansion: true,
leadingIcon: Icons.backup_outlined,
trailingIcon: Icons.expand_more,
menuItemColor:
Theme.of(context).colorScheme.enteTheme.colorScheme.fillFaint,
expandableController: expandableController,
),
collapsed: const SizedBox.shrink(),
expanded: _getSectionOptions(context),
theme: getExpandableTheme(context),
controller: expandableController,
);
}
Widget _getSectionOptions(BuildContext context) {
final bodyTextTheme =
Theme.of(context).colorScheme.enteTheme.textTheme.body;
final List<Widget> sectionOptions = [
sectionOptionDivider,
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
text: "Backed up folders",
textStyle: bodyTextTheme,
),
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () {
routeToPage(
context,
2022-07-04 06:02:17 +00:00
const BackupFolderSelectionPage(
buttonText: "Backup",
),
);
},
),
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
text: "Backup over mobile data",
textStyle: bodyTextTheme,
),
trailingSwitch: Switch.adaptive(
value: Configuration.instance.shouldBackupOverMobileData(),
onChanged: (value) async {
Configuration.instance.setBackupOverMobileData(value);
setState(() {});
},
2021-07-28 14:28:12 +00:00
),
),
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
text: "Backup videos",
textStyle: bodyTextTheme,
),
trailingSwitch: Switch.adaptive(
value: Configuration.instance.shouldBackupVideos(),
onChanged: (value) async {
Configuration.instance.setShouldBackupVideos(value);
setState(() {});
},
2021-07-28 14:28:12 +00:00
),
),
];
if (Platform.isIOS) {
sectionOptions.addAll([
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
text: "Disable auto lock",
textStyle: bodyTextTheme,
),
trailingSwitch: Switch.adaptive(
value: Configuration.instance.shouldKeepDeviceAwake(),
onChanged: (value) async {
if (value) {
final choice = await showChoiceDialog(
context,
"Disable automatic screen lock when ente is running?",
"This will ensure faster uploads by ensuring your device does not sleep when uploads are in progress.",
firstAction: "No",
secondAction: "Yes",
);
if (choice != DialogUserChoice.secondChoice) {
return;
}
}
await Configuration.instance.setShouldKeepDeviceAwake(value);
setState(() {});
},
2022-06-18 19:39:08 +00:00
),
),
]);
}
sectionOptions.addAll(
[
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
text: "Free up space",
textStyle: bodyTextTheme,
),
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
2021-07-28 14:28:12 +00:00
onTap: () async {
2022-05-30 10:52:46 +00:00
final dialog = createProgressDialog(context, "Calculating...");
2021-07-28 14:28:12 +00:00
await dialog.show();
BackupStatus status;
try {
status = await SyncService.instance.getBackupStatus();
2022-07-03 10:09:01 +00:00
} catch (e) {
await dialog.hide();
showGenericErrorDialog(context);
return;
}
2021-07-28 14:28:12 +00:00
await dialog.hide();
if (status.localIDs.isEmpty) {
2022-06-11 08:23:52 +00:00
showErrorDialog(
context,
"✨ All clear",
"You've no files on this device that can be deleted",
);
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
}
},
),
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
text: "Deduplicate files",
textStyle: bodyTextTheme,
),
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
2022-05-30 10:52:46 +00:00
final dialog = createProgressDialog(context, "Calculating...");
await dialog.show();
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) {
await dialog.hide();
showGenericErrorDialog(context);
2021-09-28 12:51:20 +00:00
return;
}
await dialog.hide();
if (duplicates.isEmpty) {
2022-06-11 08:23:52 +00:00
showErrorDialog(
context,
"✨ No duplicates",
"You've no duplicate files that can be cleared",
);
2021-09-28 12:51:20 +00:00
} else {
2022-08-29 14:43:31 +00:00
final DeduplicationResult result =
2021-09-28 12:51:20 +00:00
await routeToPage(context, DeduplicatePage(duplicates));
if (result != null) {
_showDuplicateFilesDeletedDialog(result);
}
}
},
),
2021-07-28 14:28:12 +00:00
],
);
return Column(
children: sectionOptions,
);
}
void _showSpaceFreedDialog(BackupStatus status) {
2022-08-29 14:43:31 +00:00
final AlertDialog alert = AlertDialog(
2022-07-04 06:02:17 +00:00
title: const Text("Success"),
content: Text(
2022-06-11 08:23:52 +00:00
"You have successfully freed up " + formatBytes(status.size) + "!",
),
actions: [
TextButton(
child: Text(
2022-05-30 10:52:46 +00:00
"Rate us",
style: TextStyle(
2022-07-12 06:30:02 +00:00
color: Theme.of(context).colorScheme.greenAlternative,
),
),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop('dialog');
2021-09-23 07:04:42 +00:00
// TODO: Replace with https://pub.dev/packages/in_app_review
if (Platform.isAndroid) {
launchUrlString(
2022-06-11 08:23:52 +00:00
"https://play.google.com/store/apps/details?id=io.ente.photos",
);
} else {
launchUrlString(
"https://apps.apple.com/in/app/ente-photos/id1542026904",
);
}
},
),
TextButton(
2022-07-04 06:02:17 +00:00
child: const Text(
2022-05-30 10:52:46 +00:00
"Ok",
),
onPressed: () {
2021-06-28 18:47:48 +00:00
if (Platform.isIOS) {
2022-06-11 08:23:52 +00:00
showToast(
context,
"Also empty \"Recently Deleted\" from \"Settings\" -> \"Storage\" to claim the freed space",
);
2021-06-28 18:47:48 +00:00
}
Navigator.of(context, rootNavigator: true).pop('dialog');
},
),
],
);
showConfettiDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
barrierColor: Colors.black87,
confettiAlignment: Alignment.topCenter,
useRootNavigator: true,
);
}
2021-09-22 08:26:44 +00:00
void _showDuplicateFilesDeletedDialog(DeduplicationResult result) {
2022-08-29 14:43:31 +00:00
final String countText = result.count.toString() +
2021-09-22 08:26:44 +00:00
" duplicate file" +
(result.count == 1 ? "" : "s");
2022-08-29 14:43:31 +00:00
final AlertDialog alert = AlertDialog(
2022-07-04 06:02:17 +00:00
title: const Text("✨ Success"),
2022-06-11 08:23:52 +00:00
content: Text(
"You have cleaned up " +
countText +
", saving " +
formatBytes(result.size) +
"!",
),
actions: [
TextButton(
child: Text(
2022-05-30 10:52:46 +00:00
"Rate us",
style: TextStyle(
2022-07-12 06:30:02 +00:00
color: Theme.of(context).colorScheme.greenAlternative,
),
),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop('dialog');
2021-09-23 07:04:42 +00:00
// TODO: Replace with https://pub.dev/packages/in_app_review
if (Platform.isAndroid) {
launchUrlString(
2022-06-11 08:23:52 +00:00
"https://play.google.com/store/apps/details?id=io.ente.photos",
);
} else {
launchUrlString(
"https://apps.apple.com/in/app/ente-photos/id1542026904",
);
}
},
),
TextButton(
2022-07-04 06:02:17 +00:00
child: const Text(
2022-05-30 10:52:46 +00:00
"Ok",
),
onPressed: () {
2022-06-11 08:23:52 +00:00
showToast(
context,
"Also empty your \"Trash\" to claim the freed up space",
);
Navigator.of(context, rootNavigator: true).pop('dialog');
},
),
],
);
showConfettiDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
barrierColor: Colors.black87,
confettiAlignment: Alignment.topCenter,
useRootNavigator: true,
);
}
}