backup_section_widget.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/common/dialogs.dart';
  11. import 'package:photos/ui/deduplicate_page.dart';
  12. import 'package:photos/ui/free_space_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. BackupFolderSelectionPage(
  45. buttonText: "Backup",
  46. ),
  47. );
  48. },
  49. child: SettingsTextItem(
  50. text: "Backed up folders",
  51. icon: Icons.navigate_next,
  52. ),
  53. ),
  54. SectionOptionDivider,
  55. SizedBox(
  56. height: 48,
  57. child: Row(
  58. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  59. children: [
  60. Text(
  61. "Backup over mobile data",
  62. style: Theme.of(context).textTheme.subtitle1,
  63. ),
  64. Switch.adaptive(
  65. value: Configuration.instance.shouldBackupOverMobileData(),
  66. onChanged: (value) async {
  67. Configuration.instance.setBackupOverMobileData(value);
  68. setState(() {});
  69. },
  70. ),
  71. ],
  72. ),
  73. ),
  74. SectionOptionDivider,
  75. SizedBox(
  76. height: 48,
  77. child: Row(
  78. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  79. children: [
  80. Text(
  81. "Backup videos",
  82. style: Theme.of(context).textTheme.subtitle1,
  83. ),
  84. Switch.adaptive(
  85. value: Configuration.instance.shouldBackupVideos(),
  86. onChanged: (value) async {
  87. Configuration.instance.setShouldBackupVideos(value);
  88. setState(() {});
  89. },
  90. ),
  91. ],
  92. ),
  93. ),
  94. SectionOptionDivider,
  95. SizedBox(
  96. height: 48,
  97. child: Row(
  98. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  99. children: [
  100. Text(
  101. "Wakelock",
  102. style: Theme.of(context).textTheme.subtitle1,
  103. ),
  104. Switch.adaptive(
  105. value: Configuration.instance.shouldKeepDeviceAwake(),
  106. onChanged: (value) async {
  107. if (value) {
  108. var choice = await showChoiceDialog(
  109. context,
  110. "Enable Wakelock?",
  111. "This will ensure faster uploads by keeping your device awake while uploads are in progress.",
  112. firstAction: "Cancel",
  113. secondAction: "Enable",
  114. );
  115. if (choice != DialogUserChoice.secondChoice) {
  116. return;
  117. }
  118. }
  119. await Configuration.instance.setShouldKeepDeviceAwake(value);
  120. showShortToast(context,
  121. value ? "Wakelock enabled" : "Wakelock disabled");
  122. setState(() {});
  123. },
  124. ),
  125. ],
  126. ),
  127. ),
  128. SectionOptionDivider,
  129. GestureDetector(
  130. behavior: HitTestBehavior.translucent,
  131. onTap: () async {
  132. final dialog = createProgressDialog(context, "Calculating...");
  133. await dialog.show();
  134. BackupStatus status;
  135. try {
  136. status = await SyncService.instance.getBackupStatus();
  137. } catch (e, s) {
  138. await dialog.hide();
  139. showGenericErrorDialog(context);
  140. return;
  141. }
  142. await dialog.hide();
  143. if (status.localIDs.isEmpty) {
  144. showErrorDialog(
  145. context,
  146. "✨ All clear",
  147. "You've no files on this device that can be deleted",
  148. );
  149. } else {
  150. bool result = await routeToPage(context, FreeSpacePage(status));
  151. if (result == true) {
  152. _showSpaceFreedDialog(status);
  153. }
  154. }
  155. },
  156. child: SettingsTextItem(
  157. text: "Free up space",
  158. icon: Icons.navigate_next,
  159. ),
  160. ),
  161. SectionOptionDivider,
  162. GestureDetector(
  163. behavior: HitTestBehavior.translucent,
  164. onTap: () async {
  165. final dialog = createProgressDialog(context, "Calculating...");
  166. await dialog.show();
  167. List<DuplicateFiles> duplicates;
  168. try {
  169. duplicates =
  170. await DeduplicationService.instance.getDuplicateFiles();
  171. } catch (e) {
  172. await dialog.hide();
  173. showGenericErrorDialog(context);
  174. return;
  175. }
  176. await dialog.hide();
  177. if (duplicates.isEmpty) {
  178. showErrorDialog(
  179. context,
  180. "✨ No duplicates",
  181. "You've no duplicate files that can be cleared",
  182. );
  183. } else {
  184. DeduplicationResult result =
  185. await routeToPage(context, DeduplicatePage(duplicates));
  186. if (result != null) {
  187. _showDuplicateFilesDeletedDialog(result);
  188. }
  189. }
  190. },
  191. child: SettingsTextItem(
  192. text: "Deduplicate files",
  193. icon: Icons.navigate_next,
  194. ),
  195. ),
  196. ],
  197. );
  198. }
  199. void _showSpaceFreedDialog(BackupStatus status) {
  200. AlertDialog alert = AlertDialog(
  201. title: Text("Success"),
  202. content: Text(
  203. "You have successfully freed up " + formatBytes(status.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. );
  220. } else {
  221. launch("https://apps.apple.com/in/app/ente-photos/id1542026904");
  222. }
  223. },
  224. ),
  225. TextButton(
  226. child: Text(
  227. "Ok",
  228. ),
  229. onPressed: () {
  230. if (Platform.isIOS) {
  231. showToast(
  232. context,
  233. "Also empty \"Recently Deleted\" from \"Settings\" -> \"Storage\" to claim the freed space",
  234. );
  235. }
  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. void _showDuplicateFilesDeletedDialog(DeduplicationResult result) {
  252. String countText = result.count.toString() +
  253. " duplicate file" +
  254. (result.count == 1 ? "" : "s");
  255. AlertDialog alert = AlertDialog(
  256. title: Text("✨ Success"),
  257. content: Text(
  258. "You have cleaned up " +
  259. countText +
  260. ", saving " +
  261. formatBytes(result.size) +
  262. "!",
  263. ),
  264. actions: [
  265. TextButton(
  266. child: Text(
  267. "Rate us",
  268. style: TextStyle(
  269. color: Theme.of(context).buttonColor,
  270. ),
  271. ),
  272. onPressed: () {
  273. Navigator.of(context, rootNavigator: true).pop('dialog');
  274. // TODO: Replace with https://pub.dev/packages/in_app_review
  275. if (Platform.isAndroid) {
  276. launch(
  277. "https://play.google.com/store/apps/details?id=io.ente.photos",
  278. );
  279. } else {
  280. launch("https://apps.apple.com/in/app/ente-photos/id1542026904");
  281. }
  282. },
  283. ),
  284. TextButton(
  285. child: Text(
  286. "Ok",
  287. style: TextStyle(
  288. color: Colors.white,
  289. ),
  290. ),
  291. onPressed: () {
  292. showToast(
  293. context,
  294. "Also empty your \"Trash\" to claim the freed up space",
  295. );
  296. Navigator.of(context, rootNavigator: true).pop('dialog');
  297. },
  298. ),
  299. ],
  300. );
  301. showConfettiDialog(
  302. context: context,
  303. builder: (BuildContext context) {
  304. return alert;
  305. },
  306. barrierColor: Colors.black87,
  307. confettiAlignment: Alignment.topCenter,
  308. useRootNavigator: true,
  309. );
  310. }
  311. }