backup_folder_selection_page.dart 15 KB

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