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/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: Theme.of(context).textTheme.caption!.copyWith(height: 1.3),
  99. ),
  100. ),
  101. const Padding(
  102. padding: EdgeInsets.all(10),
  103. ),
  104. _deviceCollections == null
  105. ? Container()
  106. : GestureDetector(
  107. behavior: HitTestBehavior.translucent,
  108. child: Padding(
  109. padding: const EdgeInsets.fromLTRB(24, 6, 64, 12),
  110. child: Align(
  111. alignment: Alignment.centerLeft,
  112. child: Text(
  113. _selectedDevicePathIDs.length ==
  114. _allDevicePathIDs.length
  115. ? S.of(context).unselectAll
  116. : S.of(context).selectAll,
  117. textAlign: TextAlign.right,
  118. style: const TextStyle(
  119. decoration: TextDecoration.underline,
  120. fontSize: 16,
  121. ),
  122. ),
  123. ),
  124. ),
  125. onTap: () {
  126. final hasSelectedAll = _selectedDevicePathIDs.length ==
  127. _allDevicePathIDs.length;
  128. // Flip selection
  129. if (hasSelectedAll) {
  130. _selectedDevicePathIDs.clear();
  131. } else {
  132. _selectedDevicePathIDs.addAll(_allDevicePathIDs);
  133. }
  134. _deviceCollections!.sort((first, second) {
  135. return first.name
  136. .toLowerCase()
  137. .compareTo(second.name.toLowerCase());
  138. });
  139. setState(() {});
  140. },
  141. ),
  142. Expanded(child: _getFolders()),
  143. Column(
  144. children: [
  145. Container(
  146. width: double.infinity,
  147. decoration: BoxDecoration(
  148. boxShadow: [
  149. BoxShadow(
  150. color: Theme.of(context).backgroundColor,
  151. blurRadius: 24,
  152. offset: const Offset(0, -8),
  153. spreadRadius: 4,
  154. )
  155. ],
  156. ),
  157. padding: widget.isOnboarding
  158. ? const EdgeInsets.only(left: 20, right: 20)
  159. : EdgeInsets.only(
  160. top: 16,
  161. left: 20,
  162. right: 20,
  163. bottom: Platform.isIOS ? 60 : 32,
  164. ),
  165. child: OutlinedButton(
  166. onPressed: _selectedDevicePathIDs.isEmpty
  167. ? null
  168. : () async {
  169. await updateFolderSettings();
  170. },
  171. child: Text(widget.buttonText),
  172. ),
  173. ),
  174. widget.isOnboarding
  175. ? Padding(
  176. padding: EdgeInsets.only(
  177. top: 16,
  178. bottom: Platform.isIOS ? 48 : 32,
  179. ),
  180. child: GestureDetector(
  181. key: const ValueKey("skipBackupButton"),
  182. onTap: () {
  183. Navigator.of(context).pop();
  184. },
  185. child: Text(
  186. S.of(context).skip,
  187. style: Theme.of(context).textTheme.caption!.copyWith(
  188. decoration: TextDecoration.underline,
  189. ),
  190. ),
  191. ),
  192. )
  193. : const SizedBox.shrink(),
  194. ],
  195. ),
  196. ],
  197. ),
  198. );
  199. }
  200. Future<void> updateFolderSettings() async {
  201. final dialog = createProgressDialog(
  202. context,
  203. S.of(context).updatingFolderSelection,
  204. );
  205. await dialog.show();
  206. try {
  207. final Map<String, bool> syncStatus = {};
  208. for (String pathID in _allDevicePathIDs) {
  209. syncStatus[pathID] = _selectedDevicePathIDs.contains(pathID);
  210. }
  211. await Configuration.instance.setHasSelectedAnyBackupFolder(
  212. _selectedDevicePathIDs.isNotEmpty,
  213. );
  214. await Configuration.instance.setSelectAllFoldersForBackup(
  215. _allDevicePathIDs.length == _selectedDevicePathIDs.length,
  216. );
  217. await RemoteSyncService.instance.updateDeviceFolderSyncStatus(syncStatus);
  218. dialog.hide();
  219. Navigator.of(context).pop();
  220. } catch (e, s) {
  221. _logger.severe("Failed to updated backup folder", e, s);
  222. await dialog.hide();
  223. showGenericErrorDialog(context: context);
  224. }
  225. }
  226. Widget _getFolders() {
  227. if (_deviceCollections == null) {
  228. return const EnteLoadingWidget();
  229. }
  230. _sortFiles();
  231. final scrollController = ScrollController();
  232. return Container(
  233. padding: const EdgeInsets.symmetric(horizontal: 20),
  234. child: Scrollbar(
  235. controller: scrollController,
  236. thumbVisibility: true,
  237. child: Padding(
  238. padding: const EdgeInsets.only(right: 4),
  239. child: ImplicitlyAnimatedReorderableList<DeviceCollection>(
  240. controller: scrollController,
  241. items: _deviceCollections!,
  242. areItemsTheSame: (oldItem, newItem) => oldItem.id == newItem.id,
  243. onReorderFinished: (item, from, to, newItems) {
  244. setState(() {
  245. _deviceCollections!
  246. ..clear()
  247. ..addAll(newItems);
  248. });
  249. },
  250. itemBuilder: (context, itemAnimation, file, index) {
  251. return Reorderable(
  252. key: ValueKey(file),
  253. builder: (context, dragAnimation, inDrag) {
  254. final t = dragAnimation.value;
  255. final elevation = lerpDouble(0, 8, t)!;
  256. final themeColor = Theme.of(context).colorScheme.onSurface;
  257. final color =
  258. Color.lerp(themeColor, themeColor.withOpacity(0.8), t);
  259. return SizeFadeTransition(
  260. sizeFraction: 0.7,
  261. curve: Curves.easeInOut,
  262. animation: itemAnimation,
  263. child: Material(
  264. color: color,
  265. elevation: elevation,
  266. type: MaterialType.transparency,
  267. child: _getFileItem(file),
  268. ),
  269. );
  270. },
  271. );
  272. },
  273. ),
  274. ),
  275. ),
  276. );
  277. }
  278. Widget _getFileItem(DeviceCollection deviceCollection) {
  279. final isSelected = _selectedDevicePathIDs.contains(deviceCollection.id);
  280. final importedCount = _pathIDToItemCount != null
  281. ? _pathIDToItemCount![deviceCollection.id] ?? 0
  282. : -1;
  283. return Padding(
  284. padding: const EdgeInsets.only(bottom: 1, right: 1),
  285. child: Container(
  286. decoration: BoxDecoration(
  287. border: Border.all(
  288. color: Theme.of(context).colorScheme.boxUnSelectColor,
  289. ),
  290. borderRadius: const BorderRadius.all(
  291. Radius.circular(12),
  292. ),
  293. // color: isSelected
  294. // ? Theme.of(context).colorScheme.boxSelectColor
  295. // : Theme.of(context).colorScheme.boxUnSelectColor,
  296. gradient: isSelected
  297. ? const LinearGradient(
  298. colors: [Color(0xFF00DD4D), Color(0xFF43BA6C)],
  299. ) //same for both themes
  300. : LinearGradient(
  301. colors: [
  302. Theme.of(context).colorScheme.boxUnSelectColor,
  303. Theme.of(context).colorScheme.boxUnSelectColor
  304. ],
  305. ),
  306. ),
  307. padding: const EdgeInsets.fromLTRB(8, 4, 4, 4),
  308. child: InkWell(
  309. child: Row(
  310. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  311. children: [
  312. Row(
  313. children: [
  314. Checkbox(
  315. checkColor: Colors.green,
  316. activeColor: Colors.white,
  317. value: isSelected,
  318. onChanged: (value) {
  319. if (value!) {
  320. _selectedDevicePathIDs.add(deviceCollection.id);
  321. } else {
  322. _selectedDevicePathIDs.remove(deviceCollection.id);
  323. }
  324. setState(() {});
  325. },
  326. ),
  327. Column(
  328. crossAxisAlignment: CrossAxisAlignment.start,
  329. children: [
  330. Container(
  331. constraints: const BoxConstraints(maxWidth: 180),
  332. child: Text(
  333. deviceCollection.name,
  334. textAlign: TextAlign.left,
  335. style: TextStyle(
  336. fontFamily: 'Inter-Medium',
  337. fontSize: 18,
  338. fontWeight: FontWeight.w600,
  339. color: isSelected
  340. ? Colors.white
  341. : Theme.of(context)
  342. .colorScheme
  343. .onSurface
  344. .withOpacity(0.7),
  345. ),
  346. overflow: TextOverflow.ellipsis,
  347. maxLines: 2,
  348. ),
  349. ),
  350. const Padding(padding: EdgeInsets.only(top: 2)),
  351. Text(
  352. (kDebugMode ? 'inApp: $importedCount : device ' : '') +
  353. S.of(context).itemCount(deviceCollection.count),
  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. }