backup_folder_selection_page.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. 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. }