backup_folder_selection_page.dart 13 KB

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