backup_folder_selection_page.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. import 'dart:io';
  2. import 'dart:ui';
  3. import "package:animated_list_plus/animated_list_plus.dart";
  4. import "package:animated_list_plus/transitions.dart";
  5. import 'package:flutter/foundation.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:logging/logging.dart';
  8. import 'package:photos/core/configuration.dart';
  9. import 'package:photos/db/device_files_db.dart';
  10. import 'package:photos/db/files_db.dart';
  11. import 'package:photos/ente_theme_data.dart';
  12. import 'package:photos/models/device_collection.dart';
  13. import 'package:photos/models/file.dart';
  14. import 'package:photos/services/remote_sync_service.dart';
  15. import 'package:photos/ui/common/loading_widget.dart';
  16. import 'package:photos/ui/viewer/file/thumbnail_widget.dart';
  17. import 'package:photos/utils/dialog_util.dart';
  18. class BackupFolderSelectionPage extends StatefulWidget {
  19. final bool isOnboarding;
  20. final String buttonText;
  21. const BackupFolderSelectionPage({
  22. required this.buttonText,
  23. this.isOnboarding = false,
  24. Key? key,
  25. }) : super(key: key);
  26. @override
  27. State<BackupFolderSelectionPage> createState() =>
  28. _BackupFolderSelectionPageState();
  29. }
  30. class _BackupFolderSelectionPageState extends State<BackupFolderSelectionPage> {
  31. final Logger _logger = Logger((_BackupFolderSelectionPageState).toString());
  32. final Set<String> _allDevicePathIDs = <String>{};
  33. final Set<String> _selectedDevicePathIDs = <String>{};
  34. List<DeviceCollection>? _deviceCollections;
  35. Map<String, int>? _pathIDToItemCount;
  36. @override
  37. void initState() {
  38. FilesDB.instance
  39. .getDeviceCollections(includeCoverThumbnail: true)
  40. .then((files) async {
  41. _pathIDToItemCount =
  42. await FilesDB.instance.getDevicePathIDToImportedFileCount();
  43. setState(() {
  44. _deviceCollections = files;
  45. _deviceCollections!.sort((first, second) {
  46. return first.name.toLowerCase().compareTo(second.name.toLowerCase());
  47. });
  48. for (final file in _deviceCollections!) {
  49. _allDevicePathIDs.add(file.id);
  50. if (file.shouldBackup) {
  51. _selectedDevicePathIDs.add(file.id);
  52. }
  53. }
  54. if (widget.isOnboarding) {
  55. _selectedDevicePathIDs.addAll(_allDevicePathIDs);
  56. }
  57. _selectedDevicePathIDs
  58. .removeWhere((folder) => !_allDevicePathIDs.contains(folder));
  59. });
  60. });
  61. super.initState();
  62. }
  63. @override
  64. Widget build(BuildContext context) {
  65. return Scaffold(
  66. appBar: widget.isOnboarding
  67. ? null
  68. : AppBar(
  69. elevation: 0,
  70. title: const Text(""),
  71. ),
  72. body: Column(
  73. mainAxisAlignment: MainAxisAlignment.center,
  74. children: [
  75. const SizedBox(
  76. height: 0,
  77. ),
  78. SafeArea(
  79. child: Container(
  80. padding: const EdgeInsets.fromLTRB(24, 32, 24, 8),
  81. child: Text(
  82. 'Select folders for backup',
  83. textAlign: TextAlign.left,
  84. style: TextStyle(
  85. color: Theme.of(context).colorScheme.onSurface,
  86. fontFamily: 'Inter-Bold',
  87. fontSize: 32,
  88. fontWeight: FontWeight.bold,
  89. ),
  90. ),
  91. ),
  92. ),
  93. Padding(
  94. padding: const EdgeInsets.only(left: 24, right: 48),
  95. child: Text(
  96. "Selected folders will be encrypted and backed up",
  97. style: Theme.of(context).textTheme.caption!.copyWith(height: 1.3),
  98. ),
  99. ),
  100. const Padding(
  101. padding: EdgeInsets.all(10),
  102. ),
  103. _deviceCollections == null
  104. ? Container()
  105. : GestureDetector(
  106. behavior: HitTestBehavior.translucent,
  107. child: Padding(
  108. padding: const EdgeInsets.fromLTRB(24, 6, 64, 12),
  109. child: Align(
  110. alignment: Alignment.centerLeft,
  111. child: Text(
  112. _selectedDevicePathIDs.length ==
  113. _allDevicePathIDs.length
  114. ? "Unselect all"
  115. : "Select all",
  116. textAlign: TextAlign.right,
  117. style: const TextStyle(
  118. decoration: TextDecoration.underline,
  119. fontSize: 16,
  120. ),
  121. ),
  122. ),
  123. ),
  124. onTap: () {
  125. final hasSelectedAll = _selectedDevicePathIDs.length ==
  126. _allDevicePathIDs.length;
  127. // Flip selection
  128. if (hasSelectedAll) {
  129. _selectedDevicePathIDs.clear();
  130. } else {
  131. _selectedDevicePathIDs.addAll(_allDevicePathIDs);
  132. }
  133. _deviceCollections!.sort((first, second) {
  134. return first.name
  135. .toLowerCase()
  136. .compareTo(second.name.toLowerCase());
  137. });
  138. setState(() {});
  139. },
  140. ),
  141. Expanded(child: _getFolders()),
  142. Column(
  143. children: [
  144. Container(
  145. width: double.infinity,
  146. decoration: BoxDecoration(
  147. boxShadow: [
  148. BoxShadow(
  149. color: Theme.of(context).backgroundColor,
  150. blurRadius: 24,
  151. offset: const Offset(0, -8),
  152. spreadRadius: 4,
  153. )
  154. ],
  155. ),
  156. padding: widget.isOnboarding
  157. ? const EdgeInsets.only(left: 20, right: 20)
  158. : EdgeInsets.only(
  159. top: 16,
  160. left: 20,
  161. right: 20,
  162. bottom: Platform.isIOS ? 60 : 32,
  163. ),
  164. child: OutlinedButton(
  165. onPressed: _selectedDevicePathIDs.isEmpty
  166. ? null
  167. : () async {
  168. await updateFolderSettings();
  169. },
  170. child: Text(widget.buttonText),
  171. ),
  172. ),
  173. widget.isOnboarding
  174. ? Padding(
  175. padding: EdgeInsets.only(
  176. top: 16,
  177. bottom: Platform.isIOS ? 48 : 32,
  178. ),
  179. child: GestureDetector(
  180. onTap: () {
  181. Navigator.of(context).pop();
  182. },
  183. child: Text(
  184. "Skip",
  185. style: Theme.of(context).textTheme.caption!.copyWith(
  186. decoration: TextDecoration.underline,
  187. ),
  188. ),
  189. ),
  190. )
  191. : const SizedBox.shrink(),
  192. ],
  193. ),
  194. ],
  195. ),
  196. );
  197. }
  198. Future<void> updateFolderSettings() async {
  199. final dialog = createProgressDialog(
  200. context,
  201. "Updating folder selection...",
  202. );
  203. await dialog.show();
  204. try {
  205. final Map<String, bool> syncStatus = {};
  206. for (String pathID in _allDevicePathIDs) {
  207. syncStatus[pathID] = _selectedDevicePathIDs.contains(pathID);
  208. }
  209. await Configuration.instance.setHasSelectedAnyBackupFolder(
  210. _selectedDevicePathIDs.isNotEmpty,
  211. );
  212. await Configuration.instance.setSelectAllFoldersForBackup(
  213. _allDevicePathIDs.length == _selectedDevicePathIDs.length,
  214. );
  215. await RemoteSyncService.instance.updateDeviceFolderSyncStatus(syncStatus);
  216. dialog.hide();
  217. Navigator.of(context).pop();
  218. } catch (e, s) {
  219. _logger.severe("Failed to updated backup folder", e, s);
  220. await dialog.hide();
  221. showGenericErrorDialog(context: context);
  222. }
  223. }
  224. Widget _getFolders() {
  225. if (_deviceCollections == null) {
  226. return const EnteLoadingWidget();
  227. }
  228. _sortFiles();
  229. final scrollController = ScrollController();
  230. return Container(
  231. padding: const EdgeInsets.symmetric(horizontal: 20),
  232. child: Scrollbar(
  233. controller: scrollController,
  234. thumbVisibility: true,
  235. child: Padding(
  236. padding: const EdgeInsets.only(right: 4),
  237. child: ImplicitlyAnimatedReorderableList<DeviceCollection>(
  238. controller: scrollController,
  239. items: _deviceCollections!,
  240. areItemsTheSame: (oldItem, newItem) => oldItem.id == newItem.id,
  241. onReorderFinished: (item, from, to, newItems) {
  242. setState(() {
  243. _deviceCollections!
  244. ..clear()
  245. ..addAll(newItems);
  246. });
  247. },
  248. itemBuilder: (context, itemAnimation, file, index) {
  249. return Reorderable(
  250. key: ValueKey(file),
  251. builder: (context, dragAnimation, inDrag) {
  252. final t = dragAnimation.value;
  253. final elevation = lerpDouble(0, 8, t)!;
  254. final themeColor = Theme.of(context).colorScheme.onSurface;
  255. final color =
  256. Color.lerp(themeColor, themeColor.withOpacity(0.8), t);
  257. return SizeFadeTransition(
  258. sizeFraction: 0.7,
  259. curve: Curves.easeInOut,
  260. animation: itemAnimation,
  261. child: Material(
  262. color: color,
  263. elevation: elevation,
  264. type: MaterialType.transparency,
  265. child: _getFileItem(file),
  266. ),
  267. );
  268. },
  269. );
  270. },
  271. ),
  272. ),
  273. ),
  274. );
  275. }
  276. Widget _getFileItem(DeviceCollection deviceCollection) {
  277. final isSelected = _selectedDevicePathIDs.contains(deviceCollection.id);
  278. final importedCount = _pathIDToItemCount != null
  279. ? _pathIDToItemCount![deviceCollection.id] ?? 0
  280. : -1;
  281. return Padding(
  282. padding: const EdgeInsets.only(bottom: 1, right: 1),
  283. child: Container(
  284. decoration: BoxDecoration(
  285. border: Border.all(
  286. color: Theme.of(context).colorScheme.boxUnSelectColor,
  287. ),
  288. borderRadius: const BorderRadius.all(
  289. Radius.circular(12),
  290. ),
  291. // color: isSelected
  292. // ? Theme.of(context).colorScheme.boxSelectColor
  293. // : Theme.of(context).colorScheme.boxUnSelectColor,
  294. gradient: isSelected
  295. ? const LinearGradient(
  296. colors: [Color(0xFF00DD4D), Color(0xFF43BA6C)],
  297. ) //same for both themes
  298. : LinearGradient(
  299. colors: [
  300. Theme.of(context).colorScheme.boxUnSelectColor,
  301. Theme.of(context).colorScheme.boxUnSelectColor
  302. ],
  303. ),
  304. ),
  305. padding: const EdgeInsets.fromLTRB(8, 4, 4, 4),
  306. child: InkWell(
  307. child: Row(
  308. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  309. children: [
  310. Row(
  311. children: [
  312. Checkbox(
  313. checkColor: Colors.green,
  314. activeColor: Colors.white,
  315. value: isSelected,
  316. onChanged: (value) {
  317. if (value!) {
  318. _selectedDevicePathIDs.add(deviceCollection.id);
  319. } else {
  320. _selectedDevicePathIDs.remove(deviceCollection.id);
  321. }
  322. setState(() {});
  323. },
  324. ),
  325. Column(
  326. crossAxisAlignment: CrossAxisAlignment.start,
  327. children: [
  328. Container(
  329. constraints: const BoxConstraints(maxWidth: 180),
  330. child: Text(
  331. deviceCollection.name,
  332. textAlign: TextAlign.left,
  333. style: TextStyle(
  334. fontFamily: 'Inter-Medium',
  335. fontSize: 18,
  336. fontWeight: FontWeight.w600,
  337. color: isSelected
  338. ? Colors.white
  339. : Theme.of(context)
  340. .colorScheme
  341. .onSurface
  342. .withOpacity(0.7),
  343. ),
  344. overflow: TextOverflow.ellipsis,
  345. maxLines: 2,
  346. ),
  347. ),
  348. const Padding(padding: EdgeInsets.only(top: 2)),
  349. Text(
  350. (kDebugMode ? 'inApp: $importedCount : device ' : '') +
  351. (deviceCollection.count).toString() +
  352. " item" +
  353. ((deviceCollection.count) == 1 ? "" : "s"),
  354. textAlign: TextAlign.left,
  355. style: TextStyle(
  356. fontSize: 12,
  357. color: isSelected
  358. ? Colors.white
  359. : Theme.of(context).colorScheme.onSurface,
  360. ),
  361. ),
  362. ],
  363. ),
  364. ],
  365. ),
  366. _getThumbnail(deviceCollection.thumbnail!, isSelected),
  367. ],
  368. ),
  369. onTap: () {
  370. final value = !_selectedDevicePathIDs.contains(deviceCollection.id);
  371. if (value) {
  372. _selectedDevicePathIDs.add(deviceCollection.id);
  373. } else {
  374. _selectedDevicePathIDs.remove(deviceCollection.id);
  375. }
  376. setState(() {});
  377. },
  378. ),
  379. ),
  380. );
  381. }
  382. void _sortFiles() {
  383. _deviceCollections!.sort((first, second) {
  384. if (_selectedDevicePathIDs.contains(first.id) &&
  385. _selectedDevicePathIDs.contains(second.id)) {
  386. return first.name.toLowerCase().compareTo(second.name.toLowerCase());
  387. } else if (_selectedDevicePathIDs.contains(first.id)) {
  388. return -1;
  389. } else if (_selectedDevicePathIDs.contains(second.id)) {
  390. return 1;
  391. }
  392. return first.name.toLowerCase().compareTo(second.name.toLowerCase());
  393. });
  394. }
  395. Widget _getThumbnail(File file, bool isSelected) {
  396. return ClipRRect(
  397. borderRadius: BorderRadius.circular(8),
  398. child: SizedBox(
  399. height: 88,
  400. width: 88,
  401. child: Stack(
  402. alignment: AlignmentDirectional.bottomEnd,
  403. children: [
  404. ThumbnailWidget(
  405. file,
  406. shouldShowSyncStatus: false,
  407. key: Key("backup_selection_widget" + file.tag),
  408. ),
  409. Padding(
  410. padding: const EdgeInsets.all(9),
  411. child: isSelected
  412. ? const Icon(
  413. Icons.local_police,
  414. color: Colors.white,
  415. )
  416. : null,
  417. ),
  418. ],
  419. ),
  420. ),
  421. );
  422. }
  423. }