ente/lib/ui/settings/backup_section_widget.dart

325 lines
10 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'package:expandable/expandable.dart';
import 'package:flutter/material.dart';
import 'package:photos/core/configuration.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/settings/common_settings.dart';
import 'package:photos/ui/settings/settings_section_title.dart';
import 'package:photos/ui/settings/settings_text_item.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.dart';
class BackupSectionWidget extends StatefulWidget {
BackupSectionWidget({Key key}) : super(key: key);
@override
BackupSectionWidgetState createState() => BackupSectionWidgetState();
}
class BackupSectionWidgetState extends State<BackupSectionWidget> {
@override
Widget build(BuildContext context) {
return ExpandablePanel(
2022-07-04 06:02:17 +00:00
header: const SettingsSectionTitle("Backup"),
collapsed: Container(),
expanded: _getSectionOptions(context),
theme: getExpandableTheme(context),
);
}
Widget _getSectionOptions(BuildContext context) {
final List<Widget> sectionOptions = [
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () async {
routeToPage(
context,
2022-07-04 06:02:17 +00:00
const BackupFolderSelectionPage(
buttonText: "Backup",
),
);
},
2022-07-04 06:02:17 +00:00
child: const SettingsTextItem(
text: "Backed up folders",
icon: Icons.navigate_next,
2021-07-28 14:28:12 +00:00
),
),
sectionOptionDivider,
SizedBox(
height: 48,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Backup over mobile data",
style: Theme.of(context).textTheme.subtitle1,
),
Switch.adaptive(
value: Configuration.instance.shouldBackupOverMobileData(),
onChanged: (value) async {
Configuration.instance.setBackupOverMobileData(value);
setState(() {});
},
),
],
2021-07-28 14:28:12 +00:00
),
),
sectionOptionDivider,
SizedBox(
height: 48,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Backup videos",
style: Theme.of(context).textTheme.subtitle1,
),
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([
sectionOptionDivider,
2022-06-18 19:39:08 +00:00
SizedBox(
height: 48,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
2022-06-19 10:57:54 +00:00
"Disable auto lock",
2022-06-18 19:39:08 +00:00
style: Theme.of(context).textTheme.subtitle1,
),
Switch.adaptive(
value: Configuration.instance.shouldKeepDeviceAwake(),
onChanged: (value) async {
if (value) {
var choice = await showChoiceDialog(
context,
2022-06-19 10:57:54 +00:00
"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.",
2022-06-18 20:55:05 +00:00
firstAction: "No",
secondAction: "Yes",
);
if (choice != DialogUserChoice.secondChoice) {
return;
}
}
await Configuration.instance.setShouldKeepDeviceAwake(value);
2022-06-18 19:39:08 +00:00
setState(() {});
},
),
],
),
),
]);
}
sectionOptions.addAll(
[
sectionOptionDivider,
2021-07-28 14:28:12 +00:00
GestureDetector(
behavior: HitTestBehavior.translucent,
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 {
bool result = await routeToPage(context, FreeSpacePage(status));
if (result == true) {
_showSpaceFreedDialog(status);
}
2021-07-28 14:28:12 +00:00
}
},
2022-07-04 06:02:17 +00:00
child: const SettingsTextItem(
text: "Free up space",
2021-07-28 14:28:12 +00:00
icon: Icons.navigate_next,
2021-06-28 10:12:21 +00:00
),
2021-07-28 14:28:12 +00:00
),
sectionOptionDivider,
GestureDetector(
behavior: HitTestBehavior.translucent,
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 {
DeduplicationResult result =
await routeToPage(context, DeduplicatePage(duplicates));
if (result != null) {
_showDuplicateFilesDeletedDialog(result);
}
}
},
2022-07-04 06:02:17 +00:00
child: const SettingsTextItem(
text: "Deduplicate files",
icon: Icons.navigate_next,
),
),
2021-07-28 14:28:12 +00:00
],
);
return Column(
children: sectionOptions,
);
}
void _showSpaceFreedDialog(BackupStatus status) {
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(
color: Theme.of(context).buttonColor,
),
),
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) {
launch(
2022-06-11 08:23:52 +00:00
"https://play.google.com/store/apps/details?id=io.ente.photos",
);
} else {
launch("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) {
String countText = result.count.toString() +
" duplicate file" +
(result.count == 1 ? "" : "s");
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(
color: Theme.of(context).buttonColor,
),
),
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) {
launch(
2022-06-11 08:23:52 +00:00
"https://play.google.com/store/apps/details?id=io.ente.photos",
);
} else {
launch("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,
);
}
}