backup_section_widget.dart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. final duplicates =
  132. await DeduplicationService.instance.getDuplicateFiles();
  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. int cleanedItemCount =
  139. await routeToPage(context, DeduplicatePage(duplicates));
  140. if (cleanedItemCount != null) {
  141. _showDuplicateFilesDeletedDialog(cleanedItemCount);
  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. if (Platform.isAndroid) {
  169. launch(
  170. "https://play.google.com/store/apps/details?id=io.ente.photos");
  171. } else {
  172. launch("https://apps.apple.com/in/app/ente-photos/id1542026904");
  173. }
  174. },
  175. ),
  176. TextButton(
  177. child: Text(
  178. "ok",
  179. style: TextStyle(
  180. color: Colors.white,
  181. ),
  182. ),
  183. onPressed: () {
  184. if (Platform.isIOS) {
  185. showToast(
  186. "also empty \"Recently Deleted\" from \"Settings\" -> \"Storage\" to claim the freed space");
  187. }
  188. Navigator.of(context, rootNavigator: true).pop('dialog');
  189. },
  190. ),
  191. ],
  192. );
  193. showConfettiDialog(
  194. context: context,
  195. builder: (BuildContext context) {
  196. return alert;
  197. },
  198. barrierColor: Colors.black87,
  199. confettiAlignment: Alignment.topCenter,
  200. useRootNavigator: true,
  201. );
  202. }
  203. void _showDuplicateFilesDeletedDialog(int itemCount) {
  204. AlertDialog alert = AlertDialog(
  205. title: Text("✨ success"),
  206. content: Text(
  207. "you have cleaned up " + itemCount.toString() + " duplicate files!"),
  208. actions: [
  209. TextButton(
  210. child: Text(
  211. "rate us",
  212. style: TextStyle(
  213. color: Theme.of(context).buttonColor,
  214. ),
  215. ),
  216. onPressed: () {
  217. Navigator.of(context, rootNavigator: true).pop('dialog');
  218. if (Platform.isAndroid) {
  219. launch(
  220. "https://play.google.com/store/apps/details?id=io.ente.photos");
  221. } else {
  222. launch("https://apps.apple.com/in/app/ente-photos/id1542026904");
  223. }
  224. },
  225. ),
  226. TextButton(
  227. child: Text(
  228. "ok",
  229. style: TextStyle(
  230. color: Colors.white,
  231. ),
  232. ),
  233. onPressed: () {
  234. showToast(
  235. "also empty your \"Trash\" from to claim the freed space");
  236. Navigator.of(context, rootNavigator: true).pop('dialog');
  237. },
  238. ),
  239. ],
  240. );
  241. showConfettiDialog(
  242. context: context,
  243. builder: (BuildContext context) {
  244. return alert;
  245. },
  246. barrierColor: Colors.black87,
  247. confettiAlignment: Alignment.topCenter,
  248. useRootNavigator: true,
  249. );
  250. }
  251. }