backup_section_widget.dart 8.5 KB

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