backup_section_widget.dart 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import 'dart:io';
  2. import 'package:expandable/expandable.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:photos/core/configuration.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/ui/backup_folder_selection_page.dart';
  10. import 'package:photos/ui/deduplicate_page.dart';
  11. import 'package:photos/ui/free_space_page.dart';
  12. import 'package:photos/ui/settings/common_settings.dart';
  13. import 'package:photos/ui/settings/settings_section_title.dart';
  14. import 'package:photos/ui/settings/settings_text_item.dart';
  15. import 'package:photos/utils/data_util.dart';
  16. import 'package:photos/utils/dialog_util.dart';
  17. import 'package:photos/utils/navigation_util.dart';
  18. import 'package:photos/utils/toast_util.dart';
  19. import 'package:url_launcher/url_launcher.dart';
  20. class BackupSectionWidget extends StatefulWidget {
  21. BackupSectionWidget({Key key}) : super(key: key);
  22. @override
  23. BackupSectionWidgetState createState() => BackupSectionWidgetState();
  24. }
  25. class BackupSectionWidgetState extends State<BackupSectionWidget> {
  26. @override
  27. Widget build(BuildContext context) {
  28. return ExpandablePanel(
  29. header: SettingsSectionTitle("Backup"),
  30. collapsed: Container(),
  31. expanded: _getSectionOptions(context),
  32. theme: getExpandableTheme(context),
  33. );
  34. }
  35. Widget _getSectionOptions(BuildContext context) {
  36. return Column(
  37. children: [
  38. GestureDetector(
  39. behavior: HitTestBehavior.translucent,
  40. onTap: () async {
  41. routeToPage(
  42. context,
  43. BackupFolderSelectionPage(
  44. buttonText: "Backup",
  45. ),
  46. );
  47. },
  48. child: SettingsTextItem(
  49. text: "Backed up folders", icon: Icons.navigate_next),
  50. ),
  51. SectionOptionDivider,
  52. SizedBox(
  53. height: 48,
  54. child: Row(
  55. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  56. children: [
  57. Text(
  58. "Backup over mobile data",
  59. style: Theme.of(context).textTheme.subtitle1,
  60. ),
  61. Switch.adaptive(
  62. value: Configuration.instance.shouldBackupOverMobileData(),
  63. onChanged: (value) async {
  64. Configuration.instance.setBackupOverMobileData(value);
  65. setState(() {});
  66. },
  67. ),
  68. ],
  69. ),
  70. ),
  71. SectionOptionDivider,
  72. SizedBox(
  73. height: 48,
  74. child: Row(
  75. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  76. children: [
  77. Text(
  78. "Backup videos",
  79. style: Theme.of(context).textTheme.subtitle1,
  80. ),
  81. Switch.adaptive(
  82. value: Configuration.instance.shouldBackupVideos(),
  83. onChanged: (value) async {
  84. Configuration.instance.setShouldBackupVideos(value);
  85. setState(() {});
  86. },
  87. ),
  88. ],
  89. ),
  90. ),
  91. SectionOptionDivider,
  92. GestureDetector(
  93. behavior: HitTestBehavior.translucent,
  94. onTap: () async {
  95. final dialog = createProgressDialog(context, "Calculating...");
  96. await dialog.show();
  97. BackupStatus status;
  98. try {
  99. status = await SyncService.instance.getBackupStatus();
  100. } catch (e, s) {
  101. await dialog.hide();
  102. showGenericErrorDialog(context);
  103. return;
  104. }
  105. await dialog.hide();
  106. if (status.localIDs.isEmpty) {
  107. showErrorDialog(context, "✨ All clear",
  108. "You've no files on this device that can be deleted");
  109. } else {
  110. bool result = await routeToPage(context, FreeSpacePage(status));
  111. if (result == true) {
  112. _showSpaceFreedDialog(status);
  113. }
  114. }
  115. },
  116. child: SettingsTextItem(
  117. text: "Free up space",
  118. icon: Icons.navigate_next,
  119. ),
  120. ),
  121. SectionOptionDivider,
  122. GestureDetector(
  123. behavior: HitTestBehavior.translucent,
  124. onTap: () async {
  125. final dialog = createProgressDialog(context, "Calculating...");
  126. await dialog.show();
  127. List<DuplicateFiles> duplicates;
  128. try {
  129. duplicates =
  130. await DeduplicationService.instance.getDuplicateFiles();
  131. } catch (e) {
  132. await dialog.hide();
  133. showGenericErrorDialog(context);
  134. return;
  135. }
  136. await dialog.hide();
  137. if (duplicates.isEmpty) {
  138. showErrorDialog(context, "✨ No duplicates",
  139. "You've no duplicate files that can be cleared");
  140. } else {
  141. DeduplicationResult result =
  142. await routeToPage(context, DeduplicatePage(duplicates));
  143. if (result != null) {
  144. _showDuplicateFilesDeletedDialog(result);
  145. }
  146. }
  147. },
  148. child: SettingsTextItem(
  149. text: "Deduplicate files",
  150. icon: Icons.navigate_next,
  151. ),
  152. ),
  153. ],
  154. );
  155. }
  156. void _showSpaceFreedDialog(BackupStatus status) {
  157. AlertDialog alert = AlertDialog(
  158. title: Text("Success"),
  159. content: Text(
  160. "You have successfully freed up " + formatBytes(status.size) + "!"),
  161. actions: [
  162. TextButton(
  163. child: Text(
  164. "Rate us",
  165. style: TextStyle(
  166. color: Theme.of(context).buttonColor,
  167. ),
  168. ),
  169. onPressed: () {
  170. Navigator.of(context, rootNavigator: true).pop('dialog');
  171. // TODO: Replace with https://pub.dev/packages/in_app_review
  172. if (Platform.isAndroid) {
  173. launch(
  174. "https://play.google.com/store/apps/details?id=io.ente.photos");
  175. } else {
  176. launch("https://apps.apple.com/in/app/ente-photos/id1542026904");
  177. }
  178. },
  179. ),
  180. TextButton(
  181. child: Text(
  182. "Ok",
  183. style: TextStyle(
  184. color: Colors.white,
  185. ),
  186. ),
  187. onPressed: () {
  188. if (Platform.isIOS) {
  189. showToast(context,
  190. "Also empty \"Recently Deleted\" from \"Settings\" -> \"Storage\" to claim the freed space");
  191. }
  192. Navigator.of(context, rootNavigator: true).pop('dialog');
  193. },
  194. ),
  195. ],
  196. );
  197. showConfettiDialog(
  198. context: context,
  199. builder: (BuildContext context) {
  200. return alert;
  201. },
  202. barrierColor: Colors.black87,
  203. confettiAlignment: Alignment.topCenter,
  204. useRootNavigator: true,
  205. );
  206. }
  207. void _showDuplicateFilesDeletedDialog(DeduplicationResult result) {
  208. String countText = result.count.toString() +
  209. " duplicate file" +
  210. (result.count == 1 ? "" : "s");
  211. AlertDialog alert = AlertDialog(
  212. title: Text("✨ Success"),
  213. content: Text("You have cleaned up " +
  214. countText +
  215. ", saving " +
  216. formatBytes(result.size) +
  217. "!"),
  218. actions: [
  219. TextButton(
  220. child: Text(
  221. "Rate us",
  222. style: TextStyle(
  223. color: Theme.of(context).buttonColor,
  224. ),
  225. ),
  226. onPressed: () {
  227. Navigator.of(context, rootNavigator: true).pop('dialog');
  228. // TODO: Replace with https://pub.dev/packages/in_app_review
  229. if (Platform.isAndroid) {
  230. launch(
  231. "https://play.google.com/store/apps/details?id=io.ente.photos");
  232. } else {
  233. launch("https://apps.apple.com/in/app/ente-photos/id1542026904");
  234. }
  235. },
  236. ),
  237. TextButton(
  238. child: Text(
  239. "Ok",
  240. style: TextStyle(
  241. color: Colors.white,
  242. ),
  243. ),
  244. onPressed: () {
  245. showToast(context,
  246. "Also empty your \"Trash\" to claim the freed up space");
  247. Navigator.of(context, rootNavigator: true).pop('dialog');
  248. },
  249. ),
  250. ],
  251. );
  252. showConfettiDialog(
  253. context: context,
  254. builder: (BuildContext context) {
  255. return alert;
  256. },
  257. barrierColor: Colors.black87,
  258. confettiAlignment: Alignment.topCenter,
  259. useRootNavigator: true,
  260. );
  261. }
  262. }