backup_folder_selection_page.dart 15 KB

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