backup_folder_selection_page.dart 9.7 KB

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