backup_folder_selection_page.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // @dart=2.9
  2. import 'dart:io';
  3. import 'dart:ui';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:implicitly_animated_reorderable_list/implicitly_animated_reorderable_list.dart';
  7. import 'package:implicitly_animated_reorderable_list/transitions.dart';
  8. import 'package:logging/logging.dart';
  9. import 'package:photos/core/configuration.dart';
  10. import 'package:photos/db/device_files_db.dart';
  11. import 'package:photos/db/files_db.dart';
  12. import 'package:photos/ente_theme_data.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. 'Select folders for backup',
  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. "Selected folders will be encrypted and backed up",
  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. ? "Unselect all"
  116. : "Select all",
  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. Hero(
  146. tag: "select_folders",
  147. child: Container(
  148. width: double.infinity,
  149. decoration: BoxDecoration(
  150. boxShadow: [
  151. BoxShadow(
  152. color: Theme.of(context).backgroundColor,
  153. blurRadius: 24,
  154. offset: const Offset(0, -8),
  155. spreadRadius: 4,
  156. )
  157. ],
  158. ),
  159. padding: widget.isOnboarding
  160. ? const EdgeInsets.only(left: 20, right: 20)
  161. : EdgeInsets.only(
  162. top: 16,
  163. left: 20,
  164. right: 20,
  165. bottom: Platform.isIOS ? 60 : 32,
  166. ),
  167. child: OutlinedButton(
  168. onPressed: _selectedDevicePathIDs.isEmpty
  169. ? null
  170. : () async {
  171. await updateFolderSettings();
  172. },
  173. child: Text(widget.buttonText),
  174. ),
  175. ),
  176. ),
  177. widget.isOnboarding
  178. ? Padding(
  179. padding: EdgeInsets.only(
  180. top: 16,
  181. bottom: Platform.isIOS ? 48 : 32,
  182. ),
  183. child: GestureDetector(
  184. onTap: () {
  185. Navigator.of(context).pop();
  186. },
  187. child: Text(
  188. "Skip",
  189. style: Theme.of(context).textTheme.caption.copyWith(
  190. decoration: TextDecoration.underline,
  191. ),
  192. ),
  193. ),
  194. )
  195. : const SizedBox.shrink(),
  196. ],
  197. ),
  198. ],
  199. ),
  200. );
  201. }
  202. Future<void> updateFolderSettings() async {
  203. final dialog = createProgressDialog(
  204. context,
  205. "Updating folder selection...",
  206. );
  207. await dialog.show();
  208. try {
  209. final Map<String, bool> syncStatus = {};
  210. for (String pathID in _allDevicePathIDs) {
  211. syncStatus[pathID] = _selectedDevicePathIDs.contains(pathID);
  212. }
  213. await Configuration.instance.setHasSelectedAnyBackupFolder(
  214. _selectedDevicePathIDs.isNotEmpty,
  215. );
  216. await Configuration.instance.setSelectAllFoldersForBackup(
  217. _allDevicePathIDs.length == _selectedDevicePathIDs.length,
  218. );
  219. await RemoteSyncService.instance.updateDeviceFolderSyncStatus(syncStatus);
  220. dialog.hide();
  221. Navigator.of(context).pop();
  222. } catch (e, s) {
  223. _logger.severe("Failed to updated backup folder", e, s);
  224. await dialog.hide();
  225. showGenericErrorDialog(context);
  226. }
  227. }
  228. Widget _getFolders() {
  229. if (_deviceCollections == null) {
  230. return const EnteLoadingWidget();
  231. }
  232. _sortFiles();
  233. final scrollController = ScrollController();
  234. return Container(
  235. padding: const EdgeInsets.symmetric(horizontal: 20),
  236. child: Scrollbar(
  237. controller: scrollController,
  238. thumbVisibility: true,
  239. child: Padding(
  240. padding: const EdgeInsets.only(right: 4),
  241. child: ImplicitlyAnimatedReorderableList<DeviceCollection>(
  242. controller: scrollController,
  243. items: _deviceCollections,
  244. areItemsTheSame: (oldItem, newItem) => oldItem.id == newItem.id,
  245. onReorderFinished: (item, from, to, newItems) {
  246. setState(() {
  247. _deviceCollections
  248. ..clear()
  249. ..addAll(newItems);
  250. });
  251. },
  252. itemBuilder: (context, itemAnimation, file, index) {
  253. return Reorderable(
  254. key: ValueKey(file),
  255. builder: (context, dragAnimation, inDrag) {
  256. final t = dragAnimation.value;
  257. final elevation = lerpDouble(0, 8, t);
  258. final themeColor = Theme.of(context).colorScheme.onSurface;
  259. final color =
  260. Color.lerp(themeColor, themeColor.withOpacity(0.8), t);
  261. return SizeFadeTransition(
  262. sizeFraction: 0.7,
  263. curve: Curves.easeInOut,
  264. animation: itemAnimation,
  265. child: Material(
  266. color: color,
  267. elevation: elevation,
  268. type: MaterialType.transparency,
  269. child: _getFileItem(file),
  270. ),
  271. );
  272. },
  273. );
  274. },
  275. ),
  276. ),
  277. ),
  278. );
  279. }
  280. Widget _getFileItem(DeviceCollection deviceCollection) {
  281. final isSelected = _selectedDevicePathIDs.contains(deviceCollection.id);
  282. final importedCount = _pathIDToItemCount != null
  283. ? _pathIDToItemCount[deviceCollection.id] ?? 0
  284. : -1;
  285. return Padding(
  286. padding: const EdgeInsets.only(bottom: 1, right: 1),
  287. child: Container(
  288. decoration: BoxDecoration(
  289. border: Border.all(
  290. color: Theme.of(context).colorScheme.boxUnSelectColor,
  291. ),
  292. borderRadius: const BorderRadius.all(
  293. Radius.circular(12),
  294. ),
  295. // color: isSelected
  296. // ? Theme.of(context).colorScheme.boxSelectColor
  297. // : Theme.of(context).colorScheme.boxUnSelectColor,
  298. gradient: isSelected
  299. ? const LinearGradient(
  300. colors: [Color(0xFF00DD4D), Color(0xFF43BA6C)],
  301. ) //same for both themes
  302. : LinearGradient(
  303. colors: [
  304. Theme.of(context).colorScheme.boxUnSelectColor,
  305. Theme.of(context).colorScheme.boxUnSelectColor
  306. ],
  307. ),
  308. ),
  309. padding: const EdgeInsets.fromLTRB(8, 4, 4, 4),
  310. child: InkWell(
  311. child: Row(
  312. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  313. children: [
  314. Row(
  315. children: [
  316. Checkbox(
  317. checkColor: Colors.green,
  318. activeColor: Colors.white,
  319. value: isSelected,
  320. onChanged: (value) {
  321. if (value) {
  322. _selectedDevicePathIDs.add(deviceCollection.id);
  323. } else {
  324. _selectedDevicePathIDs.remove(deviceCollection.id);
  325. }
  326. setState(() {});
  327. },
  328. ),
  329. Column(
  330. crossAxisAlignment: CrossAxisAlignment.start,
  331. children: [
  332. Container(
  333. constraints: const BoxConstraints(maxWidth: 180),
  334. child: Text(
  335. deviceCollection.name,
  336. textAlign: TextAlign.left,
  337. style: TextStyle(
  338. fontFamily: 'Inter-Medium',
  339. fontSize: 18,
  340. fontWeight: FontWeight.w600,
  341. color: isSelected
  342. ? Colors.white
  343. : Theme.of(context)
  344. .colorScheme
  345. .onSurface
  346. .withOpacity(0.7),
  347. ),
  348. overflow: TextOverflow.ellipsis,
  349. maxLines: 2,
  350. ),
  351. ),
  352. const Padding(padding: EdgeInsets.only(top: 2)),
  353. Text(
  354. (kDebugMode ? 'inApp: $importedCount : device ' : '') +
  355. (deviceCollection.count ?? 0).toString() +
  356. " item" +
  357. ((deviceCollection.count ?? 0) == 1 ? "" : "s"),
  358. textAlign: TextAlign.left,
  359. style: TextStyle(
  360. fontSize: 12,
  361. color: isSelected
  362. ? Colors.white
  363. : Theme.of(context).colorScheme.onSurface,
  364. ),
  365. ),
  366. ],
  367. ),
  368. ],
  369. ),
  370. _getThumbnail(deviceCollection.thumbnail, isSelected),
  371. ],
  372. ),
  373. onTap: () {
  374. final value = !_selectedDevicePathIDs.contains(deviceCollection.id);
  375. if (value) {
  376. _selectedDevicePathIDs.add(deviceCollection.id);
  377. } else {
  378. _selectedDevicePathIDs.remove(deviceCollection.id);
  379. }
  380. setState(() {});
  381. },
  382. ),
  383. ),
  384. );
  385. }
  386. void _sortFiles() {
  387. _deviceCollections.sort((first, second) {
  388. if (_selectedDevicePathIDs.contains(first.id) &&
  389. _selectedDevicePathIDs.contains(second.id)) {
  390. return first.name.toLowerCase().compareTo(second.name.toLowerCase());
  391. } else if (_selectedDevicePathIDs.contains(first.id)) {
  392. return -1;
  393. } else if (_selectedDevicePathIDs.contains(second.id)) {
  394. return 1;
  395. }
  396. return first.name.toLowerCase().compareTo(second.name.toLowerCase());
  397. });
  398. }
  399. Widget _getThumbnail(File file, bool isSelected) {
  400. return ClipRRect(
  401. borderRadius: BorderRadius.circular(8),
  402. child: SizedBox(
  403. height: 88,
  404. width: 88,
  405. child: Stack(
  406. alignment: AlignmentDirectional.bottomEnd,
  407. children: [
  408. ThumbnailWidget(
  409. file,
  410. shouldShowSyncStatus: false,
  411. key: Key("backup_selection_widget" + file.tag),
  412. ),
  413. Padding(
  414. padding: const EdgeInsets.all(9),
  415. child: isSelected
  416. ? const Icon(
  417. Icons.local_police,
  418. color: Colors.white,
  419. )
  420. : null,
  421. ),
  422. ],
  423. ),
  424. ),
  425. );
  426. }
  427. }