backup_folder_selection_page.dart 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import 'dart:ui';
  2. import 'package:flutter/material.dart';
  3. import 'package:implicitly_animated_reorderable_list/implicitly_animated_reorderable_list.dart';
  4. import 'package:implicitly_animated_reorderable_list/transitions.dart';
  5. import 'package:logging/logging.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/events/backup_folders_updated_event.dart';
  10. import 'package:photos/models/file.dart';
  11. import 'package:photos/ui/common_elements.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 = Set<String>();
  28. Set<String> _selectedFolders = Set<String>();
  29. List<File> _latestFiles;
  30. @override
  31. void initState() {
  32. _selectedFolders = Configuration.instance.getPathsToBackUp();
  33. FilesDB.instance.getLatestLocalFiles().then((files) {
  34. setState(() {
  35. _latestFiles = files;
  36. for (final file in _latestFiles) {
  37. _allFolders.add(file.deviceFolder);
  38. }
  39. if (widget.shouldSelectAll) {
  40. _selectedFolders.addAll(_allFolders);
  41. }
  42. });
  43. });
  44. super.initState();
  45. }
  46. @override
  47. Widget build(BuildContext context) {
  48. return Scaffold(
  49. appBar: AppBar(title: Text("select folders to backup")),
  50. body: Column(
  51. mainAxisAlignment: MainAxisAlignment.center,
  52. children: [
  53. Padding(
  54. padding: EdgeInsets.all(12),
  55. ),
  56. Padding(
  57. padding: const EdgeInsets.only(left: 32, right: 32),
  58. child: Text(
  59. "the selected folders will be end-to-end encrypted and backed up",
  60. style: TextStyle(
  61. color: Colors.white.withOpacity(0.36),
  62. fontSize: 14,
  63. height: 1.3,
  64. ),
  65. textAlign: TextAlign.center,
  66. ),
  67. ),
  68. Padding(
  69. padding: EdgeInsets.all(6),
  70. ),
  71. GestureDetector(
  72. behavior: HitTestBehavior.translucent,
  73. child: Padding(
  74. padding: const EdgeInsets.fromLTRB(6, 6, 64, 6),
  75. child: Align(
  76. alignment: Alignment.centerRight,
  77. child: Text(
  78. _selectedFolders.length == _allFolders.length
  79. ? "unselect all"
  80. : "select all",
  81. textAlign: TextAlign.right,
  82. style: TextStyle(
  83. fontSize: 12,
  84. color: Colors.white.withOpacity(0.8),
  85. ),
  86. ),
  87. ),
  88. ),
  89. onTap: () {
  90. final hasSelectedAll =
  91. _selectedFolders.length == _allFolders.length;
  92. // Flip selection
  93. if (hasSelectedAll) {
  94. _selectedFolders.clear();
  95. } else {
  96. _selectedFolders.addAll(_allFolders);
  97. }
  98. _latestFiles.sort((first, second) {
  99. return first.deviceFolder.compareTo(second.deviceFolder);
  100. });
  101. setState(() {});
  102. }),
  103. Expanded(child: _getFolders()),
  104. Padding(
  105. padding: EdgeInsets.all(20),
  106. ),
  107. Hero(
  108. tag: "select_folders",
  109. child: Container(
  110. padding: EdgeInsets.only(left: 60, right: 60, bottom: 32),
  111. child: button(
  112. widget.buttonText,
  113. fontSize: 18,
  114. onPressed: _selectedFolders.length == 0
  115. ? null
  116. : () async {
  117. await Configuration.instance
  118. .setPathsToBackUp(_selectedFolders);
  119. Bus.instance.fire(BackupFoldersUpdatedEvent());
  120. Navigator.of(context).pop();
  121. },
  122. padding: EdgeInsets.fromLTRB(60, 20, 60, 20),
  123. ),
  124. ),
  125. ),
  126. ],
  127. ),
  128. );
  129. }
  130. Widget _getFolderList() {
  131. Widget child;
  132. if (_latestFiles != null) {
  133. final foldersWidget = _getFolders();
  134. final scrollController = ScrollController();
  135. child = Scrollbar(
  136. isAlwaysShown: true,
  137. controller: scrollController,
  138. child: SingleChildScrollView(
  139. controller: scrollController,
  140. child: Container(
  141. padding: EdgeInsets.only(right: 4),
  142. child: foldersWidget,
  143. ),
  144. ),
  145. );
  146. } else {
  147. child = loadWidget;
  148. }
  149. return Container(
  150. padding: EdgeInsets.only(left: 40, right: 40),
  151. child: child,
  152. );
  153. }
  154. Widget _getFolders() {
  155. if (_latestFiles == null) {
  156. return Container();
  157. }
  158. _sortFiles();
  159. final scrollController = ScrollController();
  160. return Container(
  161. padding: EdgeInsets.only(left: 40, right: 40),
  162. child: Scrollbar(
  163. controller: scrollController,
  164. isAlwaysShown: true,
  165. child: Padding(
  166. padding: const EdgeInsets.only(right: 4),
  167. child: ImplicitlyAnimatedReorderableList<File>(
  168. controller: scrollController,
  169. items: _latestFiles,
  170. areItemsTheSame: (oldItem, newItem) =>
  171. oldItem.deviceFolder == newItem.deviceFolder,
  172. onReorderFinished: (item, from, to, newItems) {
  173. setState(() {
  174. _latestFiles
  175. ..clear()
  176. ..addAll(newItems);
  177. });
  178. },
  179. itemBuilder: (context, itemAnimation, file, index) {
  180. return Reorderable(
  181. key: ValueKey(file),
  182. builder: (context, dragAnimation, inDrag) {
  183. final t = dragAnimation.value;
  184. final elevation = lerpDouble(0, 8, t);
  185. final color = Color.lerp(
  186. Colors.white, Colors.white.withOpacity(0.8), t);
  187. return SizeFadeTransition(
  188. sizeFraction: 0.7,
  189. curve: Curves.easeInOut,
  190. animation: itemAnimation,
  191. child: Material(
  192. color: color,
  193. elevation: elevation,
  194. type: MaterialType.transparency,
  195. child: _getFileItem(file),
  196. ),
  197. );
  198. },
  199. );
  200. },
  201. ),
  202. ),
  203. ),
  204. );
  205. }
  206. Padding _getFileItem(File file) {
  207. final isSelected = _selectedFolders.contains(file.deviceFolder);
  208. return Padding(
  209. padding: const EdgeInsets.only(bottom: 1, right: 4),
  210. child: Container(
  211. decoration: BoxDecoration(
  212. border: Border.all(
  213. color: Colors.black,
  214. ),
  215. borderRadius: BorderRadius.all(
  216. Radius.circular(10),
  217. ),
  218. color: isSelected
  219. ? Color.fromRGBO(16, 32, 32, 1)
  220. : Color.fromRGBO(8, 18, 18, 0.4),
  221. ),
  222. padding: EdgeInsets.fromLTRB(20, 16, 20, 16),
  223. child: InkWell(
  224. child: Row(
  225. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  226. children: [
  227. Container(
  228. child: Expanded(
  229. child: Row(
  230. children: [
  231. _getThumbnail(file),
  232. Padding(padding: EdgeInsets.all(10)),
  233. Expanded(
  234. child: Text(
  235. file.deviceFolder,
  236. style: TextStyle(fontSize: 16, height: 1.5),
  237. overflow: TextOverflow.clip,
  238. ),
  239. ),
  240. ],
  241. ),
  242. ),
  243. ),
  244. Checkbox(
  245. value: isSelected,
  246. onChanged: (value) {
  247. if (value) {
  248. _selectedFolders.add(file.deviceFolder);
  249. } else {
  250. _selectedFolders.remove(file.deviceFolder);
  251. }
  252. setState(() {});
  253. },
  254. ),
  255. ],
  256. ),
  257. onTap: () {
  258. final value = !_selectedFolders.contains(file.deviceFolder);
  259. if (value) {
  260. _selectedFolders.add(file.deviceFolder);
  261. } else {
  262. _selectedFolders.remove(file.deviceFolder);
  263. }
  264. setState(() {});
  265. },
  266. ),
  267. ),
  268. );
  269. }
  270. void _sortFiles() {
  271. _latestFiles.sort((first, second) {
  272. if (_selectedFolders.contains(first) &&
  273. _selectedFolders.contains(second)) {
  274. return first.deviceFolder.compareTo(second.deviceFolder);
  275. } else if (_selectedFolders.contains(first.deviceFolder)) {
  276. return -1;
  277. } else if (_selectedFolders.contains(second.deviceFolder)) {
  278. return 1;
  279. }
  280. return first.deviceFolder.compareTo(second.deviceFolder);
  281. });
  282. Logger("Test").info(
  283. "Post sort: " + _latestFiles.map((e) => e.deviceFolder).toString());
  284. }
  285. Widget _getThumbnail(File file) {
  286. return ClipRRect(
  287. borderRadius: BorderRadius.circular(4.0),
  288. child: Container(
  289. child: ThumbnailWidget(
  290. file,
  291. shouldShowSyncStatus: false,
  292. key: Key("backup_selection_widget" + file.tag()),
  293. ),
  294. height: 60,
  295. width: 60,
  296. ),
  297. );
  298. }
  299. }