backup_folder_selection_page.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. await Configuration.instance
  133. .setSelectAllFoldersForBackup(
  134. _selectedFolders.length == _allFolders.length);
  135. Bus.instance.fire(BackupFoldersUpdatedEvent());
  136. Navigator.of(context).pop();
  137. },
  138. padding: EdgeInsets.fromLTRB(60, 20, 60, 20),
  139. ),
  140. ),
  141. ),
  142. ],
  143. ),
  144. );
  145. }
  146. Widget _getFolders() {
  147. if (_latestFiles == null) {
  148. return loadWidget;
  149. }
  150. _sortFiles();
  151. final scrollController = ScrollController();
  152. return Container(
  153. padding: EdgeInsets.only(left: 40, right: 40),
  154. child: Scrollbar(
  155. controller: scrollController,
  156. isAlwaysShown: true,
  157. child: Padding(
  158. padding: const EdgeInsets.only(right: 4),
  159. child: ImplicitlyAnimatedReorderableList<File>(
  160. controller: scrollController,
  161. items: _latestFiles,
  162. areItemsTheSame: (oldItem, newItem) =>
  163. oldItem.deviceFolder == newItem.deviceFolder,
  164. onReorderFinished: (item, from, to, newItems) {
  165. setState(() {
  166. _latestFiles
  167. ..clear()
  168. ..addAll(newItems);
  169. });
  170. },
  171. itemBuilder: (context, itemAnimation, file, index) {
  172. return Reorderable(
  173. key: ValueKey(file),
  174. builder: (context, dragAnimation, inDrag) {
  175. final t = dragAnimation.value;
  176. final elevation = lerpDouble(0, 8, t);
  177. final color = Color.lerp(
  178. Colors.white, Colors.white.withOpacity(0.8), t);
  179. return SizeFadeTransition(
  180. sizeFraction: 0.7,
  181. curve: Curves.easeInOut,
  182. animation: itemAnimation,
  183. child: Material(
  184. color: color,
  185. elevation: elevation,
  186. type: MaterialType.transparency,
  187. child: _getFileItem(file),
  188. ),
  189. );
  190. },
  191. );
  192. },
  193. ),
  194. ),
  195. ),
  196. );
  197. }
  198. Widget _getFileItem(File file) {
  199. final isSelected = _selectedFolders.contains(file.deviceFolder);
  200. return Padding(
  201. padding: const EdgeInsets.only(bottom: 1, right: 4),
  202. child: Container(
  203. decoration: BoxDecoration(
  204. border: Border.all(
  205. color: Colors.black,
  206. ),
  207. borderRadius: BorderRadius.all(
  208. Radius.circular(10),
  209. ),
  210. color: isSelected
  211. ? Color.fromRGBO(16, 32, 32, 1)
  212. : Color.fromRGBO(8, 18, 18, 0.4),
  213. ),
  214. padding: EdgeInsets.fromLTRB(20, 16, 20, 16),
  215. child: InkWell(
  216. child: Row(
  217. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  218. children: [
  219. Row(
  220. children: [
  221. _getThumbnail(file),
  222. Padding(padding: EdgeInsets.all(10)),
  223. Column(
  224. crossAxisAlignment: CrossAxisAlignment.start,
  225. children: [
  226. Container(
  227. constraints: BoxConstraints(maxWidth: 140),
  228. child: Text(
  229. file.deviceFolder,
  230. style: TextStyle(
  231. fontSize: 14,
  232. height: 1.5,
  233. color: isSelected
  234. ? Colors.white
  235. : Colors.white.withOpacity(0.7),
  236. ),
  237. overflow: TextOverflow.ellipsis,
  238. maxLines: 2,
  239. ),
  240. ),
  241. Padding(padding: EdgeInsets.all(2)),
  242. Text(
  243. _itemCount[file.deviceFolder].toString() +
  244. " item" +
  245. (_itemCount[file.deviceFolder] == 1 ? "" : "s"),
  246. textAlign: TextAlign.left,
  247. style: TextStyle(
  248. fontSize: 12,
  249. color: isSelected
  250. ? Colors.white.withOpacity(0.65)
  251. : Colors.white.withOpacity(0.4),
  252. ),
  253. ),
  254. ],
  255. ),
  256. ],
  257. ),
  258. Checkbox(
  259. value: isSelected,
  260. onChanged: (value) {
  261. if (value) {
  262. _selectedFolders.add(file.deviceFolder);
  263. } else {
  264. _selectedFolders.remove(file.deviceFolder);
  265. }
  266. setState(() {});
  267. },
  268. ),
  269. ],
  270. ),
  271. onTap: () {
  272. final value = !_selectedFolders.contains(file.deviceFolder);
  273. if (value) {
  274. _selectedFolders.add(file.deviceFolder);
  275. } else {
  276. _selectedFolders.remove(file.deviceFolder);
  277. }
  278. setState(() {});
  279. },
  280. ),
  281. ),
  282. );
  283. }
  284. void _sortFiles() {
  285. _latestFiles.sort((first, second) {
  286. if (_selectedFolders.contains(first.deviceFolder) &&
  287. _selectedFolders.contains(second.deviceFolder)) {
  288. return first.deviceFolder
  289. .toLowerCase()
  290. .compareTo(second.deviceFolder.toLowerCase());
  291. } else if (_selectedFolders.contains(first.deviceFolder)) {
  292. return -1;
  293. } else if (_selectedFolders.contains(second.deviceFolder)) {
  294. return 1;
  295. }
  296. return first.deviceFolder
  297. .toLowerCase()
  298. .compareTo(second.deviceFolder.toLowerCase());
  299. });
  300. }
  301. Widget _getThumbnail(File file) {
  302. return ClipRRect(
  303. borderRadius: BorderRadius.circular(4.0),
  304. child: SizedBox(
  305. child: ThumbnailWidget(
  306. file,
  307. shouldShowSyncStatus: false,
  308. key: Key("backup_selection_widget" + file.tag()),
  309. ),
  310. height: 60,
  311. width: 60,
  312. ),
  313. );
  314. }
  315. }