backup_folder_selection_page.dart 11 KB

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