backup_folder_selection_page.dart 11 KB

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