backup_section_widget.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // @dart=2.9
  2. import 'dart:io';
  3. import 'package:expandable/expandable.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:photos/core/configuration.dart';
  6. import 'package:photos/ente_theme_data.dart';
  7. import 'package:photos/models/backup_status.dart';
  8. import 'package:photos/models/duplicate_files.dart';
  9. import 'package:photos/services/deduplication_service.dart';
  10. import 'package:photos/services/sync_service.dart';
  11. import 'package:photos/ui/backup_folder_selection_page.dart';
  12. import 'package:photos/ui/common/dialogs.dart';
  13. import 'package:photos/ui/components/captioned_text_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. final expandableController = ExpandableController(initialExpanded: false);
  30. @override
  31. void dispose() {
  32. expandableController.dispose();
  33. super.dispose();
  34. }
  35. @override
  36. Widget build(BuildContext context) {
  37. return ExpandablePanel(
  38. header: MenuItemWidget(
  39. captionedTextWidget: const CaptionedTextWidget(
  40. text: "Backup",
  41. makeTextBold: true,
  42. ),
  43. isHeaderOfExpansion: true,
  44. leadingIcon: Icons.backup_outlined,
  45. trailingIcon: Icons.expand_more,
  46. menuItemColor:
  47. Theme.of(context).colorScheme.enteTheme.colorScheme.fillFaint,
  48. expandableController: expandableController,
  49. ),
  50. collapsed: const SizedBox.shrink(),
  51. expanded: _getSectionOptions(context),
  52. theme: getExpandableTheme(context),
  53. controller: expandableController,
  54. );
  55. }
  56. Widget _getSectionOptions(BuildContext context) {
  57. final List<Widget> sectionOptions = [
  58. sectionOptionDivider,
  59. MenuItemWidget(
  60. captionedTextWidget: const CaptionedTextWidget(
  61. text: "Backed up folders",
  62. ),
  63. trailingIcon: Icons.chevron_right_outlined,
  64. trailingIconIsMuted: true,
  65. onTap: () {
  66. routeToPage(
  67. context,
  68. const BackupFolderSelectionPage(
  69. buttonText: "Backup",
  70. ),
  71. );
  72. },
  73. ),
  74. MenuItemWidget(
  75. captionedTextWidget: const CaptionedTextWidget(
  76. text: "Backup over mobile data",
  77. ),
  78. trailingSwitch: Switch.adaptive(
  79. value: Configuration.instance.shouldBackupOverMobileData(),
  80. onChanged: (value) async {
  81. Configuration.instance.setBackupOverMobileData(value);
  82. setState(() {});
  83. },
  84. ),
  85. ),
  86. MenuItemWidget(
  87. captionedTextWidget: const CaptionedTextWidget(
  88. text: "Backup videos",
  89. ),
  90. trailingSwitch: Switch.adaptive(
  91. value: Configuration.instance.shouldBackupVideos(),
  92. onChanged: (value) async {
  93. Configuration.instance.setShouldBackupVideos(value);
  94. setState(() {});
  95. },
  96. ),
  97. ),
  98. ];
  99. if (Platform.isIOS) {
  100. sectionOptions.addAll([
  101. MenuItemWidget(
  102. captionedTextWidget: const CaptionedTextWidget(
  103. text: "Disable auto lock",
  104. ),
  105. trailingSwitch: Switch.adaptive(
  106. value: Configuration.instance.shouldKeepDeviceAwake(),
  107. onChanged: (value) async {
  108. if (value) {
  109. final choice = await showChoiceDialog(
  110. context,
  111. "Disable automatic screen lock when ente is running?",
  112. "This will ensure faster uploads by ensuring your device does not sleep when uploads are in progress.",
  113. firstAction: "No",
  114. secondAction: "Yes",
  115. );
  116. if (choice != DialogUserChoice.secondChoice) {
  117. return;
  118. }
  119. }
  120. await Configuration.instance.setShouldKeepDeviceAwake(value);
  121. setState(() {});
  122. },
  123. ),
  124. ),
  125. ]);
  126. }
  127. sectionOptions.addAll(
  128. [
  129. MenuItemWidget(
  130. captionedTextWidget: const CaptionedTextWidget(
  131. text: "Free up space",
  132. ),
  133. trailingIcon: Icons.chevron_right_outlined,
  134. trailingIconIsMuted: true,
  135. onTap: () async {
  136. final dialog = createProgressDialog(context, "Calculating...");
  137. await dialog.show();
  138. BackupStatus status;
  139. try {
  140. status = await SyncService.instance.getBackupStatus();
  141. } catch (e) {
  142. await dialog.hide();
  143. showGenericErrorDialog(context);
  144. return;
  145. }
  146. await dialog.hide();
  147. if (status.localIDs.isEmpty) {
  148. showErrorDialog(
  149. context,
  150. "✨ All clear",
  151. "You've no files on this device that can be deleted",
  152. );
  153. } else {
  154. final bool result =
  155. await routeToPage(context, FreeSpacePage(status));
  156. if (result == true) {
  157. _showSpaceFreedDialog(status);
  158. }
  159. }
  160. },
  161. ),
  162. MenuItemWidget(
  163. captionedTextWidget: const CaptionedTextWidget(
  164. text: "Deduplicate files",
  165. ),
  166. trailingIcon: Icons.chevron_right_outlined,
  167. trailingIconIsMuted: true,
  168. onTap: () async {
  169. final dialog = createProgressDialog(context, "Calculating...");
  170. await dialog.show();
  171. List<DuplicateFiles> duplicates;
  172. try {
  173. duplicates =
  174. await DeduplicationService.instance.getDuplicateFiles();
  175. } catch (e) {
  176. await dialog.hide();
  177. showGenericErrorDialog(context);
  178. return;
  179. }
  180. await dialog.hide();
  181. if (duplicates.isEmpty) {
  182. showErrorDialog(
  183. context,
  184. "✨ No duplicates",
  185. "You've no duplicate files that can be cleared",
  186. );
  187. } else {
  188. final DeduplicationResult result =
  189. await routeToPage(context, DeduplicatePage(duplicates));
  190. if (result != null) {
  191. _showDuplicateFilesDeletedDialog(result);
  192. }
  193. }
  194. },
  195. ),
  196. ],
  197. );
  198. return Column(
  199. children: sectionOptions,
  200. );
  201. }
  202. void _showSpaceFreedDialog(BackupStatus status) {
  203. final AlertDialog alert = AlertDialog(
  204. title: const Text("Success"),
  205. content: Text(
  206. "You have successfully freed up " + formatBytes(status.size) + "!",
  207. ),
  208. actions: [
  209. TextButton(
  210. child: Text(
  211. "Rate us",
  212. style: TextStyle(
  213. color: Theme.of(context).colorScheme.greenAlternative,
  214. ),
  215. ),
  216. onPressed: () {
  217. Navigator.of(context, rootNavigator: true).pop('dialog');
  218. // TODO: Replace with https://pub.dev/packages/in_app_review
  219. if (Platform.isAndroid) {
  220. launchUrlString(
  221. "https://play.google.com/store/apps/details?id=io.ente.photos",
  222. );
  223. } else {
  224. launchUrlString(
  225. "https://apps.apple.com/in/app/ente-photos/id1542026904",
  226. );
  227. }
  228. },
  229. ),
  230. TextButton(
  231. child: const Text(
  232. "Ok",
  233. ),
  234. onPressed: () {
  235. if (Platform.isIOS) {
  236. showToast(
  237. context,
  238. "Also empty \"Recently Deleted\" from \"Settings\" -> \"Storage\" to claim the freed space",
  239. );
  240. }
  241. Navigator.of(context, rootNavigator: true).pop('dialog');
  242. },
  243. ),
  244. ],
  245. );
  246. showConfettiDialog(
  247. context: context,
  248. builder: (BuildContext context) {
  249. return alert;
  250. },
  251. barrierColor: Colors.black87,
  252. confettiAlignment: Alignment.topCenter,
  253. useRootNavigator: true,
  254. );
  255. }
  256. void _showDuplicateFilesDeletedDialog(DeduplicationResult result) {
  257. final String countText = result.count.toString() +
  258. " duplicate file" +
  259. (result.count == 1 ? "" : "s");
  260. final AlertDialog alert = AlertDialog(
  261. title: const Text("✨ Success"),
  262. content: Text(
  263. "You have cleaned up " +
  264. countText +
  265. ", saving " +
  266. formatBytes(result.size) +
  267. "!",
  268. ),
  269. actions: [
  270. TextButton(
  271. child: Text(
  272. "Rate us",
  273. style: TextStyle(
  274. color: Theme.of(context).colorScheme.greenAlternative,
  275. ),
  276. ),
  277. onPressed: () {
  278. Navigator.of(context, rootNavigator: true).pop('dialog');
  279. // TODO: Replace with https://pub.dev/packages/in_app_review
  280. if (Platform.isAndroid) {
  281. launchUrlString(
  282. "https://play.google.com/store/apps/details?id=io.ente.photos",
  283. );
  284. } else {
  285. launchUrlString(
  286. "https://apps.apple.com/in/app/ente-photos/id1542026904",
  287. );
  288. }
  289. },
  290. ),
  291. TextButton(
  292. child: const Text(
  293. "Ok",
  294. ),
  295. onPressed: () {
  296. showToast(
  297. context,
  298. "Also empty your \"Trash\" to claim the freed up space",
  299. );
  300. Navigator.of(context, rootNavigator: true).pop('dialog');
  301. },
  302. ),
  303. ],
  304. );
  305. showConfettiDialog(
  306. context: context,
  307. builder: (BuildContext context) {
  308. return alert;
  309. },
  310. barrierColor: Colors.black87,
  311. confettiAlignment: Alignment.topCenter,
  312. useRootNavigator: true,
  313. );
  314. }
  315. }