backup_folder_selection_page.dart 14 KB

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