backup_section_widget.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/ente_theme_data.dart';
  6. import 'package:photos/models/backup_status.dart';
  7. import 'package:photos/models/duplicate_files.dart';
  8. import 'package:photos/services/deduplication_service.dart';
  9. import 'package:photos/services/sync_service.dart';
  10. import 'package:photos/ui/backup_folder_selection_page.dart';
  11. import 'package:photos/ui/common/dialogs.dart';
  12. import 'package:photos/ui/settings/common_settings.dart';
  13. import 'package:photos/ui/settings/settings_section_title.dart';
  14. import 'package:photos/ui/settings/settings_text_item.dart';
  15. import 'package:photos/ui/tools/deduplicate_page.dart';
  16. import 'package:photos/ui/tools/free_space_page.dart';
  17. import 'package:photos/utils/data_util.dart';
  18. import 'package:photos/utils/dialog_util.dart';
  19. import 'package:photos/utils/navigation_util.dart';
  20. import 'package:photos/utils/toast_util.dart';
  21. import 'package:url_launcher/url_launcher_string.dart';
  22. class BackupSectionWidget extends StatefulWidget {
  23. const BackupSectionWidget({Key key}) : super(key: key);
  24. @override
  25. BackupSectionWidgetState createState() => BackupSectionWidgetState();
  26. }
  27. class BackupSectionWidgetState extends State<BackupSectionWidget> {
  28. @override
  29. Widget build(BuildContext context) {
  30. return ExpandablePanel(
  31. header: const SettingsSectionTitle("Backup"),
  32. collapsed: Container(),
  33. expanded: _getSectionOptions(context),
  34. theme: getExpandableTheme(context),
  35. );
  36. }
  37. Widget _getSectionOptions(BuildContext context) {
  38. final List<Widget> sectionOptions = [
  39. GestureDetector(
  40. behavior: HitTestBehavior.translucent,
  41. onTap: () async {
  42. routeToPage(
  43. context,
  44. const BackupFolderSelectionPage(
  45. buttonText: "Backup",
  46. ),
  47. );
  48. },
  49. child: const 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. ];
  95. if (Platform.isIOS) {
  96. sectionOptions.addAll([
  97. sectionOptionDivider,
  98. SizedBox(
  99. height: 48,
  100. child: Row(
  101. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  102. children: [
  103. Text(
  104. "Disable auto lock",
  105. style: Theme.of(context).textTheme.subtitle1,
  106. ),
  107. Switch.adaptive(
  108. value: Configuration.instance.shouldKeepDeviceAwake(),
  109. onChanged: (value) async {
  110. if (value) {
  111. final choice = await showChoiceDialog(
  112. context,
  113. "Disable automatic screen lock when ente is running?",
  114. "This will ensure faster uploads by ensuring your device does not sleep when uploads are in progress.",
  115. firstAction: "No",
  116. secondAction: "Yes",
  117. );
  118. if (choice != DialogUserChoice.secondChoice) {
  119. return;
  120. }
  121. }
  122. await Configuration.instance.setShouldKeepDeviceAwake(value);
  123. setState(() {});
  124. },
  125. ),
  126. ],
  127. ),
  128. ),
  129. ]);
  130. }
  131. sectionOptions.addAll(
  132. [
  133. sectionOptionDivider,
  134. GestureDetector(
  135. behavior: HitTestBehavior.translucent,
  136. onTap: () async {
  137. final dialog = createProgressDialog(context, "Calculating...");
  138. await dialog.show();
  139. BackupStatus status;
  140. try {
  141. status = await SyncService.instance.getBackupStatus();
  142. } catch (e) {
  143. await dialog.hide();
  144. showGenericErrorDialog(context);
  145. return;
  146. }
  147. await dialog.hide();
  148. if (status.localIDs.isEmpty) {
  149. showErrorDialog(
  150. context,
  151. "✨ All clear",
  152. "You've no files on this device that can be deleted",
  153. );
  154. } else {
  155. final bool result = await routeToPage(context, FreeSpacePage(status));
  156. if (result == true) {
  157. _showSpaceFreedDialog(status);
  158. }
  159. }
  160. },
  161. child: const SettingsTextItem(
  162. text: "Free up space",
  163. icon: Icons.navigate_next,
  164. ),
  165. ),
  166. sectionOptionDivider,
  167. GestureDetector(
  168. behavior: HitTestBehavior.translucent,
  169. onTap: () async {
  170. final dialog = createProgressDialog(context, "Calculating...");
  171. await dialog.show();
  172. List<DuplicateFiles> duplicates;
  173. try {
  174. duplicates =
  175. await DeduplicationService.instance.getDuplicateFiles();
  176. } catch (e) {
  177. await dialog.hide();
  178. showGenericErrorDialog(context);
  179. return;
  180. }
  181. await dialog.hide();
  182. if (duplicates.isEmpty) {
  183. showErrorDialog(
  184. context,
  185. "✨ No duplicates",
  186. "You've no duplicate files that can be cleared",
  187. );
  188. } else {
  189. final DeduplicationResult result =
  190. await routeToPage(context, DeduplicatePage(duplicates));
  191. if (result != null) {
  192. _showDuplicateFilesDeletedDialog(result);
  193. }
  194. }
  195. },
  196. child: const SettingsTextItem(
  197. text: "Deduplicate files",
  198. icon: Icons.navigate_next,
  199. ),
  200. ),
  201. ],
  202. );
  203. return Column(
  204. children: sectionOptions,
  205. );
  206. }
  207. void _showSpaceFreedDialog(BackupStatus status) {
  208. final AlertDialog alert = AlertDialog(
  209. title: const Text("Success"),
  210. content: Text(
  211. "You have successfully freed up " + formatBytes(status.size) + "!",
  212. ),
  213. actions: [
  214. TextButton(
  215. child: Text(
  216. "Rate us",
  217. style: TextStyle(
  218. color: Theme.of(context).colorScheme.greenAlternative,
  219. ),
  220. ),
  221. onPressed: () {
  222. Navigator.of(context, rootNavigator: true).pop('dialog');
  223. // TODO: Replace with https://pub.dev/packages/in_app_review
  224. if (Platform.isAndroid) {
  225. launchUrlString(
  226. "https://play.google.com/store/apps/details?id=io.ente.photos",
  227. );
  228. } else {
  229. launchUrlString(
  230. "https://apps.apple.com/in/app/ente-photos/id1542026904",
  231. );
  232. }
  233. },
  234. ),
  235. TextButton(
  236. child: const Text(
  237. "Ok",
  238. ),
  239. onPressed: () {
  240. if (Platform.isIOS) {
  241. showToast(
  242. context,
  243. "Also empty \"Recently Deleted\" from \"Settings\" -> \"Storage\" to claim the freed space",
  244. );
  245. }
  246. Navigator.of(context, rootNavigator: true).pop('dialog');
  247. },
  248. ),
  249. ],
  250. );
  251. showConfettiDialog(
  252. context: context,
  253. builder: (BuildContext context) {
  254. return alert;
  255. },
  256. barrierColor: Colors.black87,
  257. confettiAlignment: Alignment.topCenter,
  258. useRootNavigator: true,
  259. );
  260. }
  261. void _showDuplicateFilesDeletedDialog(DeduplicationResult result) {
  262. final String countText = result.count.toString() +
  263. " duplicate file" +
  264. (result.count == 1 ? "" : "s");
  265. final AlertDialog alert = AlertDialog(
  266. title: const Text("✨ Success"),
  267. content: Text(
  268. "You have cleaned up " +
  269. countText +
  270. ", saving " +
  271. formatBytes(result.size) +
  272. "!",
  273. ),
  274. actions: [
  275. TextButton(
  276. child: Text(
  277. "Rate us",
  278. style: TextStyle(
  279. color: Theme.of(context).colorScheme.greenAlternative,
  280. ),
  281. ),
  282. onPressed: () {
  283. Navigator.of(context, rootNavigator: true).pop('dialog');
  284. // TODO: Replace with https://pub.dev/packages/in_app_review
  285. if (Platform.isAndroid) {
  286. launchUrlString(
  287. "https://play.google.com/store/apps/details?id=io.ente.photos",
  288. );
  289. } else {
  290. launchUrlString(
  291. "https://apps.apple.com/in/app/ente-photos/id1542026904",
  292. );
  293. }
  294. },
  295. ),
  296. TextButton(
  297. child: const Text(
  298. "Ok",
  299. ),
  300. onPressed: () {
  301. showToast(
  302. context,
  303. "Also empty your \"Trash\" to claim the freed up space",
  304. );
  305. Navigator.of(context, rootNavigator: true).pop('dialog');
  306. },
  307. ),
  308. ],
  309. );
  310. showConfettiDialog(
  311. context: context,
  312. builder: (BuildContext context) {
  313. return alert;
  314. },
  315. barrierColor: Colors.black87,
  316. confettiAlignment: Alignment.topCenter,
  317. useRootNavigator: true,
  318. );
  319. }
  320. }