backup_section_widget.dart 8.8 KB

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