backup_section_widget.dart 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // @dart=2.9
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:photos/ente_theme_data.dart';
  5. import 'package:photos/models/backup_status.dart';
  6. import 'package:photos/models/duplicate_files.dart';
  7. import 'package:photos/services/deduplication_service.dart';
  8. import 'package:photos/services/sync_service.dart';
  9. import 'package:photos/theme/ente_theme.dart';
  10. import 'package:photos/ui/backup_folder_selection_page.dart';
  11. import 'package:photos/ui/backup_settings_screen.dart';
  12. import 'package:photos/ui/components/captioned_text_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/settings/common_settings.dart';
  16. import 'package:photos/ui/tools/deduplicate_page.dart';
  17. import 'package:photos/ui/tools/free_space_page.dart';
  18. import 'package:photos/utils/data_util.dart';
  19. import 'package:photos/utils/dialog_util.dart';
  20. import 'package:photos/utils/navigation_util.dart';
  21. import 'package:photos/utils/toast_util.dart';
  22. import 'package:url_launcher/url_launcher_string.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: () {
  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: () {
  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);
  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);
  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 AlertDialog alert = AlertDialog(
  154. title: const Text("Success"),
  155. content: Text(
  156. "You have successfully freed up " + formatBytes(status.size) + "!",
  157. ),
  158. actions: [
  159. TextButton(
  160. child: Text(
  161. "Rate us",
  162. style: TextStyle(
  163. color: Theme.of(context).colorScheme.greenAlternative,
  164. ),
  165. ),
  166. onPressed: () {
  167. Navigator.of(context, rootNavigator: true).pop('dialog');
  168. // TODO: Replace with https://pub.dev/packages/in_app_review
  169. if (Platform.isAndroid) {
  170. launchUrlString(
  171. "https://play.google.com/store/apps/details?id=io.ente.photos",
  172. );
  173. } else {
  174. launchUrlString(
  175. "https://apps.apple.com/in/app/ente-photos/id1542026904",
  176. );
  177. }
  178. },
  179. ),
  180. TextButton(
  181. child: const Text(
  182. "Ok",
  183. ),
  184. onPressed: () {
  185. if (Platform.isIOS) {
  186. showToast(
  187. context,
  188. "Also empty \"Recently Deleted\" from \"Settings\" -> \"Storage\" to claim the freed space",
  189. );
  190. }
  191. Navigator.of(context, rootNavigator: true).pop('dialog');
  192. },
  193. ),
  194. ],
  195. );
  196. showConfettiDialog(
  197. context: context,
  198. builder: (BuildContext context) {
  199. return alert;
  200. },
  201. barrierColor: Colors.black87,
  202. confettiAlignment: Alignment.topCenter,
  203. useRootNavigator: true,
  204. );
  205. }
  206. void _showDuplicateFilesDeletedDialog(DeduplicationResult result) {
  207. final String countText = result.count.toString() +
  208. " duplicate file" +
  209. (result.count == 1 ? "" : "s");
  210. final AlertDialog alert = AlertDialog(
  211. title: const Text("✨ Success"),
  212. content: Text(
  213. "You have cleaned up " +
  214. countText +
  215. ", saving " +
  216. formatBytes(result.size) +
  217. "!",
  218. ),
  219. actions: [
  220. TextButton(
  221. child: Text(
  222. "Rate us",
  223. style: TextStyle(
  224. color: Theme.of(context).colorScheme.greenAlternative,
  225. ),
  226. ),
  227. onPressed: () {
  228. Navigator.of(context, rootNavigator: true).pop('dialog');
  229. // TODO: Replace with https://pub.dev/packages/in_app_review
  230. if (Platform.isAndroid) {
  231. launchUrlString(
  232. "https://play.google.com/store/apps/details?id=io.ente.photos",
  233. );
  234. } else {
  235. launchUrlString(
  236. "https://apps.apple.com/in/app/ente-photos/id1542026904",
  237. );
  238. }
  239. },
  240. ),
  241. TextButton(
  242. child: const Text(
  243. "Ok",
  244. ),
  245. onPressed: () {
  246. showToast(
  247. context,
  248. "Also empty your \"Trash\" to claim the freed up space",
  249. );
  250. Navigator.of(context, rootNavigator: true).pop('dialog');
  251. },
  252. ),
  253. ],
  254. );
  255. showConfettiDialog(
  256. context: context,
  257. builder: (BuildContext context) {
  258. return alert;
  259. },
  260. barrierColor: Colors.black87,
  261. confettiAlignment: Alignment.topCenter,
  262. useRootNavigator: true,
  263. );
  264. }
  265. }