backup_section_widget.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/sync_service.dart';
  6. import 'package:photos/ui/backup_folder_selection_page.dart';
  7. import 'package:photos/ui/free_space_page.dart';
  8. import 'package:photos/ui/settings/settings_section_title.dart';
  9. import 'package:photos/ui/settings/settings_text_item.dart';
  10. import 'package:photos/utils/data_util.dart';
  11. import 'package:photos/utils/dialog_util.dart';
  12. import 'package:photos/utils/navigation_util.dart';
  13. import 'package:photos/utils/toast_util.dart';
  14. import 'package:url_launcher/url_launcher.dart';
  15. class BackupSectionWidget extends StatefulWidget {
  16. BackupSectionWidget({Key key}) : super(key: key);
  17. @override
  18. BackupSectionWidgetState createState() => BackupSectionWidgetState();
  19. }
  20. class BackupSectionWidgetState extends State<BackupSectionWidget> {
  21. @override
  22. Widget build(BuildContext context) {
  23. return Container(
  24. child: Column(
  25. children: [
  26. SettingsSectionTitle("backup"),
  27. Padding(
  28. padding: EdgeInsets.all(4),
  29. ),
  30. GestureDetector(
  31. behavior: HitTestBehavior.translucent,
  32. onTap: () async {
  33. routeToPage(
  34. context,
  35. BackupFolderSelectionPage(
  36. buttonText: "backup",
  37. ),
  38. );
  39. },
  40. child: SettingsTextItem(
  41. text: "backed up folders", icon: Icons.navigate_next),
  42. ),
  43. Platform.isIOS
  44. ? Padding(padding: EdgeInsets.all(2))
  45. : Padding(padding: EdgeInsets.all(2)),
  46. Divider(height: 4),
  47. Platform.isIOS
  48. ? Padding(padding: EdgeInsets.all(2))
  49. : Padding(padding: EdgeInsets.all(4)),
  50. Container(
  51. height: 36,
  52. child: Row(
  53. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  54. children: [
  55. Text("backup over mobile data"),
  56. Switch(
  57. value: Configuration.instance.shouldBackupOverMobileData(),
  58. onChanged: (value) async {
  59. Configuration.instance.setBackupOverMobileData(value);
  60. setState(() {});
  61. },
  62. ),
  63. ],
  64. ),
  65. ),
  66. Platform.isIOS
  67. ? Padding(padding: EdgeInsets.all(2))
  68. : Padding(padding: EdgeInsets.all(4)),
  69. Divider(height: 4),
  70. Platform.isIOS
  71. ? Padding(padding: EdgeInsets.all(2))
  72. : Padding(padding: EdgeInsets.all(2)),
  73. GestureDetector(
  74. behavior: HitTestBehavior.translucent,
  75. onTap: () async {
  76. final dialog = createProgressDialog(context, "calculating...");
  77. await dialog.show();
  78. final status = await SyncService.instance.getBackupStatus();
  79. await dialog.hide();
  80. if (status.localIDs.isEmpty) {
  81. showErrorDialog(context, "✨ all clear",
  82. "you've no files on this device that can be deleted");
  83. } else {
  84. bool result = await routeToPage(context, FreeSpacePage(status));
  85. if (result == true) {
  86. _showSpaceFreedDialog(status);
  87. }
  88. }
  89. },
  90. child: SettingsTextItem(
  91. text: "free up space",
  92. icon: Icons.navigate_next,
  93. ),
  94. ),
  95. ],
  96. ),
  97. );
  98. }
  99. void _showSpaceFreedDialog(BackupStatus status) {
  100. AlertDialog alert = AlertDialog(
  101. title: Text("success"),
  102. content: Text(
  103. "you have successfully freed up " + formatBytes(status.size) + "!"),
  104. actions: [
  105. TextButton(
  106. child: Text(
  107. "rate us",
  108. style: TextStyle(
  109. color: Theme.of(context).buttonColor,
  110. ),
  111. ),
  112. onPressed: () {
  113. Navigator.of(context, rootNavigator: true).pop('dialog');
  114. if (Platform.isAndroid) {
  115. launch(
  116. "https://play.google.com/store/apps/details?id=io.ente.photos");
  117. } else {
  118. launch("https://apps.apple.com/in/app/ente-photos/id1542026904");
  119. }
  120. },
  121. ),
  122. TextButton(
  123. child: Text(
  124. "ok",
  125. style: TextStyle(
  126. color: Colors.white,
  127. ),
  128. ),
  129. onPressed: () {
  130. if (Platform.isIOS) {
  131. showToast(
  132. "also empty \"Recently Deleted\" from \"Settings\" -> \"Storage\" to claim the freed space");
  133. }
  134. Navigator.of(context, rootNavigator: true).pop('dialog');
  135. },
  136. ),
  137. ],
  138. );
  139. showConfettiDialog(
  140. context: context,
  141. builder: (BuildContext context) {
  142. return alert;
  143. },
  144. barrierColor: Colors.black87,
  145. confettiAlignment: Alignment.topCenter,
  146. useRootNavigator: true,
  147. );
  148. }
  149. }