backup_section_widget.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:photos/models/backup_status.dart';
  4. import 'package:photos/models/duplicate_files.dart';
  5. import 'package:photos/services/deduplication_service.dart';
  6. import 'package:photos/services/sync_service.dart';
  7. import 'package:photos/services/update_service.dart';
  8. import 'package:photos/theme/ente_theme.dart';
  9. import 'package:photos/ui/backup_folder_selection_page.dart';
  10. import 'package:photos/ui/backup_settings_screen.dart';
  11. import 'package:photos/ui/components/captioned_text_widget.dart';
  12. import 'package:photos/ui/components/dialog_widget.dart';
  13. import 'package:photos/ui/components/expandable_menu_item_widget.dart';
  14. import 'package:photos/ui/components/menu_item_widget.dart';
  15. import 'package:photos/ui/components/models/button_type.dart';
  16. import 'package:photos/ui/settings/common_settings.dart';
  17. import 'package:photos/ui/tools/deduplicate_page.dart';
  18. import 'package:photos/ui/tools/free_space_page.dart';
  19. import 'package:photos/utils/data_util.dart';
  20. import 'package:photos/utils/dialog_util.dart';
  21. import 'package:photos/utils/navigation_util.dart';
  22. import 'package:photos/utils/toast_util.dart';
  23. import 'package:url_launcher/url_launcher_string.dart';
  24. class BackupSectionWidget extends StatefulWidget {
  25. const BackupSectionWidget({Key? key}) : super(key: key);
  26. @override
  27. BackupSectionWidgetState createState() => BackupSectionWidgetState();
  28. }
  29. class BackupSectionWidgetState extends State<BackupSectionWidget> {
  30. @override
  31. Widget build(BuildContext context) {
  32. return ExpandableMenuItemWidget(
  33. title: "Backup",
  34. selectionOptionsWidget: _getSectionOptions(context),
  35. leadingIcon: Icons.backup_outlined,
  36. );
  37. }
  38. Widget _getSectionOptions(BuildContext context) {
  39. final List<Widget> sectionOptions = [
  40. sectionOptionSpacing,
  41. MenuItemWidget(
  42. captionedTextWidget: const CaptionedTextWidget(
  43. title: "Backed up folders",
  44. ),
  45. pressedColor: getEnteColorScheme(context).fillFaint,
  46. trailingIcon: Icons.chevron_right_outlined,
  47. trailingIconIsMuted: true,
  48. onTap: () {
  49. routeToPage(
  50. context,
  51. const BackupFolderSelectionPage(
  52. buttonText: "Backup",
  53. ),
  54. );
  55. },
  56. ),
  57. sectionOptionSpacing,
  58. MenuItemWidget(
  59. captionedTextWidget: const CaptionedTextWidget(
  60. title: "Backup settings",
  61. ),
  62. pressedColor: getEnteColorScheme(context).fillFaint,
  63. trailingIcon: Icons.chevron_right_outlined,
  64. trailingIconIsMuted: true,
  65. onTap: () {
  66. routeToPage(
  67. context,
  68. const BackupSettingsScreen(),
  69. );
  70. },
  71. ),
  72. sectionOptionSpacing,
  73. ];
  74. sectionOptions.addAll(
  75. [
  76. MenuItemWidget(
  77. captionedTextWidget: const CaptionedTextWidget(
  78. title: "Free up device space",
  79. ),
  80. pressedColor: getEnteColorScheme(context).fillFaint,
  81. trailingIcon: Icons.chevron_right_outlined,
  82. trailingIconIsMuted: true,
  83. onTap: () async {
  84. final dialog = createProgressDialog(context, "Calculating...");
  85. await dialog.show();
  86. BackupStatus status;
  87. try {
  88. status = await SyncService.instance.getBackupStatus();
  89. } catch (e) {
  90. await dialog.hide();
  91. showGenericErrorDialog(context: context);
  92. return;
  93. }
  94. await dialog.hide();
  95. if (status.localIDs.isEmpty) {
  96. showErrorDialog(
  97. context,
  98. "✨ All clear",
  99. "You've no files on this device that can be deleted",
  100. );
  101. } else {
  102. final bool? result =
  103. await routeToPage(context, FreeSpacePage(status));
  104. if (result == true) {
  105. _showSpaceFreedDialog(status);
  106. }
  107. }
  108. },
  109. ),
  110. sectionOptionSpacing,
  111. MenuItemWidget(
  112. captionedTextWidget: const CaptionedTextWidget(
  113. title: "Remove duplicates",
  114. ),
  115. pressedColor: getEnteColorScheme(context).fillFaint,
  116. trailingIcon: Icons.chevron_right_outlined,
  117. trailingIconIsMuted: true,
  118. onTap: () async {
  119. final dialog = createProgressDialog(context, "Calculating...");
  120. await dialog.show();
  121. List<DuplicateFiles> duplicates;
  122. try {
  123. duplicates =
  124. await DeduplicationService.instance.getDuplicateFiles();
  125. } catch (e) {
  126. await dialog.hide();
  127. showGenericErrorDialog(context: context);
  128. return;
  129. }
  130. await dialog.hide();
  131. if (duplicates.isEmpty) {
  132. showErrorDialog(
  133. context,
  134. "✨ No duplicates",
  135. "You've no duplicate files that can be cleared",
  136. );
  137. } else {
  138. final DeduplicationResult? result =
  139. await routeToPage(context, DeduplicatePage(duplicates));
  140. if (result != null) {
  141. _showDuplicateFilesDeletedDialog(result);
  142. }
  143. }
  144. },
  145. ),
  146. sectionOptionSpacing,
  147. ],
  148. );
  149. return Column(
  150. children: sectionOptions,
  151. );
  152. }
  153. void _showSpaceFreedDialog(BackupStatus status) {
  154. final DialogWidget dialog = choiceDialog(
  155. title: "Success",
  156. body: "You have successfully freed up " + formatBytes(status.size) + "!",
  157. firstButtonLabel: "Rate us",
  158. firstButtonOnTap: () async {
  159. final url = UpdateService.instance.getRateDetails().item2;
  160. launchUrlString(url);
  161. },
  162. firstButtonType: ButtonType.primary,
  163. secondButtonLabel: "OK",
  164. secondButtonOnTap: () async {
  165. if (Platform.isIOS) {
  166. showToast(
  167. context,
  168. "Also empty \"Recently Deleted\" from \"Settings\" -> \"Storage\" to claim the freed space",
  169. );
  170. }
  171. },
  172. );
  173. showConfettiDialog(
  174. context: context,
  175. dialogBuilder: (BuildContext context) {
  176. return dialog;
  177. },
  178. barrierColor: Colors.black87,
  179. confettiAlignment: Alignment.topCenter,
  180. useRootNavigator: true,
  181. );
  182. }
  183. void _showDuplicateFilesDeletedDialog(DeduplicationResult result) {
  184. final String countText = result.count.toString() +
  185. " duplicate file" +
  186. (result.count == 1 ? "" : "s");
  187. final DialogWidget dialog = choiceDialog(
  188. title: "✨ Success",
  189. body: "You have cleaned up " +
  190. countText +
  191. ", saving " +
  192. formatBytes(result.size) +
  193. "!",
  194. firstButtonLabel: "Rate us",
  195. firstButtonOnTap: () async {
  196. // TODO: Replace with https://pub.dev/packages/in_app_review
  197. final url = UpdateService.instance.getRateDetails().item2;
  198. launchUrlString(url);
  199. },
  200. firstButtonType: ButtonType.primary,
  201. secondButtonLabel: "OK",
  202. secondButtonOnTap: () async {
  203. showShortToast(
  204. context,
  205. "Also empty your \"Trash\" to claim the freed up space",
  206. );
  207. },
  208. );
  209. showConfettiDialog(
  210. context: context,
  211. dialogBuilder: (BuildContext context) {
  212. return dialog;
  213. },
  214. barrierColor: Colors.black87,
  215. confettiAlignment: Alignment.topCenter,
  216. useRootNavigator: true,
  217. );
  218. }
  219. }