backup_section_widget.dart 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:logging/logging.dart';
  4. import 'package:photos/core/configuration.dart';
  5. import 'package:photos/models/backup_status.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 = await SyncService.instance.getDuplicateFiles();
  132. await dialog.hide();
  133. if (duplicates.isEmpty) {
  134. showErrorDialog(context, "✨ no duplicates",
  135. "you've no duplicate files that can be cleared");
  136. } else {
  137. bool result =
  138. await routeToPage(context, DeduplicatePage(duplicates));
  139. if (result == true) {
  140. _showDuplicateFilesDeletedDialog(0); // todo
  141. }
  142. }
  143. },
  144. child: SettingsTextItem(
  145. text: "deduplicate files",
  146. icon: Icons.navigate_next,
  147. ),
  148. ),
  149. ],
  150. );
  151. }
  152. void _showSpaceFreedDialog(BackupStatus status) {
  153. AlertDialog alert = AlertDialog(
  154. title: Text("success"),
  155. content: Text(
  156. "you have successfully freed up " + formatBytes(status.size) + "!"),
  157. actions: [
  158. TextButton(
  159. child: Text(
  160. "rate us",
  161. style: TextStyle(
  162. color: Theme.of(context).buttonColor,
  163. ),
  164. ),
  165. onPressed: () {
  166. Navigator.of(context, rootNavigator: true).pop('dialog');
  167. if (Platform.isAndroid) {
  168. launch(
  169. "https://play.google.com/store/apps/details?id=io.ente.photos");
  170. } else {
  171. launch("https://apps.apple.com/in/app/ente-photos/id1542026904");
  172. }
  173. },
  174. ),
  175. TextButton(
  176. child: Text(
  177. "ok",
  178. style: TextStyle(
  179. color: Colors.white,
  180. ),
  181. ),
  182. onPressed: () {
  183. if (Platform.isIOS) {
  184. showToast(
  185. "also empty \"Recently Deleted\" from \"Settings\" -> \"Storage\" to claim the freed space");
  186. }
  187. Navigator.of(context, rootNavigator: true).pop('dialog');
  188. },
  189. ),
  190. ],
  191. );
  192. showConfettiDialog(
  193. context: context,
  194. builder: (BuildContext context) {
  195. return alert;
  196. },
  197. barrierColor: Colors.black87,
  198. confettiAlignment: Alignment.topCenter,
  199. useRootNavigator: true,
  200. );
  201. }
  202. void _showDuplicateFilesDeletedDialog(int spaceFreed) {
  203. AlertDialog alert = AlertDialog(
  204. title: Text("success"),
  205. content: Text(
  206. "you have successfully freed up " + formatBytes(spaceFreed) + "!"),
  207. actions: [
  208. TextButton(
  209. child: Text(
  210. "rate us",
  211. style: TextStyle(
  212. color: Theme.of(context).buttonColor,
  213. ),
  214. ),
  215. onPressed: () {
  216. Navigator.of(context, rootNavigator: true).pop('dialog');
  217. if (Platform.isAndroid) {
  218. launch(
  219. "https://play.google.com/store/apps/details?id=io.ente.photos");
  220. } else {
  221. launch("https://apps.apple.com/in/app/ente-photos/id1542026904");
  222. }
  223. },
  224. ),
  225. TextButton(
  226. child: Text(
  227. "ok",
  228. style: TextStyle(
  229. color: Colors.white,
  230. ),
  231. ),
  232. onPressed: () {
  233. showToast(
  234. "also empty your \"Trash\" from to claim the freed space");
  235. Navigator.of(context, rootNavigator: true).pop('dialog');
  236. },
  237. ),
  238. ],
  239. );
  240. showConfettiDialog(
  241. context: context,
  242. builder: (BuildContext context) {
  243. return alert;
  244. },
  245. barrierColor: Colors.black87,
  246. confettiAlignment: Alignment.topCenter,
  247. useRootNavigator: true,
  248. );
  249. }
  250. }