backup_section_widget.dart 7.2 KB

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