backup_section_widget.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. showOnlyLoadingState: true,
  83. onTap: () async {
  84. BackupStatus status;
  85. try {
  86. status = await SyncService.instance.getBackupStatus();
  87. } catch (e) {
  88. showGenericErrorDialog(context: context);
  89. return;
  90. }
  91. if (status.localIDs.isEmpty) {
  92. showErrorDialog(
  93. context,
  94. "✨ All clear",
  95. "You've no files on this device that can be deleted",
  96. );
  97. } else {
  98. final bool? result =
  99. await routeToPage(context, FreeSpacePage(status));
  100. if (result == true) {
  101. _showSpaceFreedDialog(status);
  102. }
  103. }
  104. },
  105. ),
  106. sectionOptionSpacing,
  107. MenuItemWidget(
  108. captionedTextWidget: const CaptionedTextWidget(
  109. title: "Remove duplicates",
  110. ),
  111. pressedColor: getEnteColorScheme(context).fillFaint,
  112. trailingIcon: Icons.chevron_right_outlined,
  113. trailingIconIsMuted: true,
  114. showOnlyLoadingState: true,
  115. onTap: () async {
  116. List<DuplicateFiles> duplicates;
  117. try {
  118. duplicates =
  119. await DeduplicationService.instance.getDuplicateFiles();
  120. } catch (e) {
  121. showGenericErrorDialog(context: context);
  122. return;
  123. }
  124. if (duplicates.isEmpty) {
  125. showErrorDialog(
  126. context,
  127. "✨ No duplicates",
  128. "You've no duplicate files that can be cleared",
  129. );
  130. } else {
  131. final DeduplicationResult? result =
  132. await routeToPage(context, DeduplicatePage(duplicates));
  133. if (result != null) {
  134. _showDuplicateFilesDeletedDialog(result);
  135. }
  136. }
  137. },
  138. ),
  139. sectionOptionSpacing,
  140. ],
  141. );
  142. return Column(
  143. children: sectionOptions,
  144. );
  145. }
  146. void _showSpaceFreedDialog(BackupStatus status) {
  147. final DialogWidget dialog = choiceDialog(
  148. title: "Success",
  149. body: "You have successfully freed up " + formatBytes(status.size) + "!",
  150. firstButtonLabel: "Rate us",
  151. firstButtonOnTap: () async {
  152. UpdateService.instance.launchReviewUrl();
  153. },
  154. firstButtonType: ButtonType.primary,
  155. secondButtonLabel: "OK",
  156. secondButtonOnTap: () async {
  157. if (Platform.isIOS) {
  158. showToast(
  159. context,
  160. "Also empty \"Recently Deleted\" from \"Settings\" -> \"Storage\" to claim the freed space",
  161. );
  162. }
  163. },
  164. );
  165. showConfettiDialog(
  166. context: context,
  167. dialogBuilder: (BuildContext context) {
  168. return dialog;
  169. },
  170. barrierColor: Colors.black87,
  171. confettiAlignment: Alignment.topCenter,
  172. useRootNavigator: true,
  173. );
  174. }
  175. void _showDuplicateFilesDeletedDialog(DeduplicationResult result) {
  176. final String countText = result.count.toString() +
  177. " duplicate file" +
  178. (result.count == 1 ? "" : "s");
  179. final DialogWidget dialog = choiceDialog(
  180. title: "✨ Success",
  181. body: "You have cleaned up " +
  182. countText +
  183. ", saving " +
  184. formatBytes(result.size) +
  185. "!",
  186. firstButtonLabel: "Rate us",
  187. firstButtonOnTap: () async {
  188. UpdateService.instance.launchReviewUrl();
  189. },
  190. firstButtonType: ButtonType.primary,
  191. secondButtonLabel: "OK",
  192. secondButtonOnTap: () async {
  193. showShortToast(
  194. context,
  195. "Also empty your \"Trash\" to claim the freed up space",
  196. );
  197. },
  198. );
  199. showConfettiDialog(
  200. context: context,
  201. dialogBuilder: (BuildContext context) {
  202. return dialog;
  203. },
  204. barrierColor: Colors.black87,
  205. confettiAlignment: Alignment.topCenter,
  206. useRootNavigator: true,
  207. );
  208. }
  209. }