backup_section_widget.dart 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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: 36,
  54. child: Row(
  55. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  56. children: [
  57. Text("Backup over mobile data", style: Theme.of(context).textTheme.subtitle1,),
  58. Switch.adaptive(
  59. value: Configuration.instance.shouldBackupOverMobileData(),
  60. onChanged: (value) async {
  61. Configuration.instance.setBackupOverMobileData(value);
  62. setState(() {});
  63. },
  64. ),
  65. ],
  66. ),
  67. ),
  68. SectionOptionDivider,
  69. SizedBox(
  70. height: 36,
  71. child: Row(
  72. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  73. children: [
  74. Text("Backup videos", style: Theme.of(context).textTheme.subtitle1,),
  75. Switch.adaptive(
  76. value: Configuration.instance.shouldBackupVideos(),
  77. onChanged: (value) async {
  78. Configuration.instance.setShouldBackupVideos(value);
  79. setState(() {});
  80. },
  81. ),
  82. ],
  83. ),
  84. ),
  85. SectionOptionDivider,
  86. GestureDetector(
  87. behavior: HitTestBehavior.translucent,
  88. onTap: () async {
  89. final dialog = createProgressDialog(context, "calculating...");
  90. await dialog.show();
  91. final status = await SyncService.instance.getBackupStatus();
  92. await dialog.hide();
  93. if (status.localIDs.isEmpty) {
  94. showErrorDialog(context, "✨ all clear",
  95. "you've no files on this device that can be deleted");
  96. } else {
  97. bool result = await routeToPage(context, FreeSpacePage(status));
  98. if (result == true) {
  99. _showSpaceFreedDialog(status);
  100. }
  101. }
  102. },
  103. child: SettingsTextItem(
  104. text: "Free up space",
  105. icon: Icons.navigate_next,
  106. ),
  107. ),
  108. SectionOptionDivider,
  109. GestureDetector(
  110. behavior: HitTestBehavior.translucent,
  111. onTap: () async {
  112. final dialog = createProgressDialog(context, "calculating...");
  113. await dialog.show();
  114. List<DuplicateFiles> duplicates;
  115. try {
  116. duplicates =
  117. await DeduplicationService.instance.getDuplicateFiles();
  118. } catch (e) {
  119. await dialog.hide();
  120. showGenericErrorDialog(context);
  121. return;
  122. }
  123. await dialog.hide();
  124. if (duplicates.isEmpty) {
  125. showErrorDialog(context, "✨ no duplicates",
  126. "you've no duplicate files that can be cleared");
  127. } else {
  128. DeduplicationResult result =
  129. await routeToPage(context, DeduplicatePage(duplicates));
  130. if (result != null) {
  131. _showDuplicateFilesDeletedDialog(result);
  132. }
  133. }
  134. },
  135. child: SettingsTextItem(
  136. text: "Deduplicate files",
  137. icon: Icons.navigate_next,
  138. ),
  139. ),
  140. ],
  141. );
  142. }
  143. void _showSpaceFreedDialog(BackupStatus status) {
  144. AlertDialog alert = AlertDialog(
  145. title: Text("success"),
  146. content: Text(
  147. "you have successfully freed up " + formatBytes(status.size) + "!"),
  148. actions: [
  149. TextButton(
  150. child: Text(
  151. "rate us",
  152. style: TextStyle(
  153. color: Theme.of(context).buttonColor,
  154. ),
  155. ),
  156. onPressed: () {
  157. Navigator.of(context, rootNavigator: true).pop('dialog');
  158. // TODO: Replace with https://pub.dev/packages/in_app_review
  159. if (Platform.isAndroid) {
  160. launch(
  161. "https://play.google.com/store/apps/details?id=io.ente.photos");
  162. } else {
  163. launch("https://apps.apple.com/in/app/ente-photos/id1542026904");
  164. }
  165. },
  166. ),
  167. TextButton(
  168. child: Text(
  169. "ok",
  170. style: TextStyle(
  171. color: Colors.white,
  172. ),
  173. ),
  174. onPressed: () {
  175. if (Platform.isIOS) {
  176. showToast(
  177. "also empty \"Recently Deleted\" from \"Settings\" -> \"Storage\" to claim the freed space");
  178. }
  179. Navigator.of(context, rootNavigator: true).pop('dialog');
  180. },
  181. ),
  182. ],
  183. );
  184. showConfettiDialog(
  185. context: context,
  186. builder: (BuildContext context) {
  187. return alert;
  188. },
  189. barrierColor: Colors.black87,
  190. confettiAlignment: Alignment.topCenter,
  191. useRootNavigator: true,
  192. );
  193. }
  194. void _showDuplicateFilesDeletedDialog(DeduplicationResult result) {
  195. String countText = result.count.toString() +
  196. " duplicate file" +
  197. (result.count == 1 ? "" : "s");
  198. AlertDialog alert = AlertDialog(
  199. title: Text("✨ success"),
  200. content: Text("you have cleaned up " +
  201. countText +
  202. ", saving " +
  203. formatBytes(result.size) +
  204. "!"),
  205. actions: [
  206. TextButton(
  207. child: Text(
  208. "rate us",
  209. style: TextStyle(
  210. color: Theme.of(context).buttonColor,
  211. ),
  212. ),
  213. onPressed: () {
  214. Navigator.of(context, rootNavigator: true).pop('dialog');
  215. // TODO: Replace with https://pub.dev/packages/in_app_review
  216. if (Platform.isAndroid) {
  217. launch(
  218. "https://play.google.com/store/apps/details?id=io.ente.photos");
  219. } else {
  220. launch("https://apps.apple.com/in/app/ente-photos/id1542026904");
  221. }
  222. },
  223. ),
  224. TextButton(
  225. child: Text(
  226. "ok",
  227. style: TextStyle(
  228. color: Colors.white,
  229. ),
  230. ),
  231. onPressed: () {
  232. showToast("also empty your \"Trash\" to claim the freed up space");
  233. Navigator.of(context, rootNavigator: true).pop('dialog');
  234. },
  235. ),
  236. ],
  237. );
  238. showConfettiDialog(
  239. context: context,
  240. builder: (BuildContext context) {
  241. return alert;
  242. },
  243. barrierColor: Colors.black87,
  244. confettiAlignment: Alignment.topCenter,
  245. useRootNavigator: true,
  246. );
  247. }
  248. }