ente/lib/ui/backup_folder_selection_page.dart

324 lines
11 KiB
Dart
Raw Normal View History

2021-07-27 20:21:59 +00:00
import 'dart:io';
2021-07-11 21:24:28 +00:00
import 'dart:ui';
import 'package:flutter/material.dart';
2021-07-11 21:24:28 +00:00
import 'package:implicitly_animated_reorderable_list/implicitly_animated_reorderable_list.dart';
import 'package:implicitly_animated_reorderable_list/transitions.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/core/event_bus.dart';
import 'package:photos/db/files_db.dart';
import 'package:photos/events/backup_folders_updated_event.dart';
import 'package:photos/models/file.dart';
2021-05-12 15:59:07 +00:00
import 'package:photos/ui/common_elements.dart';
import 'package:photos/ui/loading_widget.dart';
import 'package:photos/ui/thumbnail_widget.dart';
class BackupFolderSelectionPage extends StatefulWidget {
2021-07-11 18:00:32 +00:00
final bool shouldSelectAll;
2021-07-11 18:05:07 +00:00
final String buttonText;
2021-07-11 18:00:32 +00:00
2021-07-11 18:05:07 +00:00
const BackupFolderSelectionPage({
@required this.buttonText,
2021-07-11 18:35:16 +00:00
this.shouldSelectAll = false,
2021-07-11 18:00:32 +00:00
Key key,
}) : super(key: key);
@override
_BackupFolderSelectionPageState createState() =>
_BackupFolderSelectionPageState();
}
class _BackupFolderSelectionPageState extends State<BackupFolderSelectionPage> {
2021-07-22 18:47:41 +00:00
final Set<String> _allFolders = <String>{};
Set<String> _selectedFolders = <String>{};
2021-07-11 18:00:32 +00:00
List<File> _latestFiles;
2021-07-12 10:24:30 +00:00
Map<String, int> _itemCount;
@override
void initState() {
2021-07-11 21:24:28 +00:00
_selectedFolders = Configuration.instance.getPathsToBackUp();
2021-07-12 10:24:30 +00:00
FilesDB.instance.getLatestLocalFiles().then((files) async {
_itemCount = await FilesDB.instance.getFileCountInDeviceFolders();
2021-07-11 18:00:32 +00:00
setState(() {
_latestFiles = files;
2021-07-12 14:02:56 +00:00
_latestFiles.sort((first, second) {
return first.deviceFolder
.toLowerCase()
.compareTo(second.deviceFolder.toLowerCase());
});
2021-07-11 18:00:32 +00:00
for (final file in _latestFiles) {
_allFolders.add(file.deviceFolder);
}
if (widget.shouldSelectAll) {
2021-07-11 21:24:28 +00:00
_selectedFolders.addAll(_allFolders);
2021-07-11 18:00:32 +00:00
}
_selectedFolders.removeWhere((folder) => !_allFolders.contains(folder));
2021-07-11 18:00:32 +00:00
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
2021-07-10 09:31:40 +00:00
appBar: AppBar(title: Text("select folders to backup")),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.all(12),
),
Padding(
padding: const EdgeInsets.only(left: 32, right: 32),
child: Text(
"the selected folders will be end-to-end encrypted and backed up",
style: TextStyle(
2021-07-13 07:55:20 +00:00
color: Colors.white.withOpacity(0.4),
fontSize: 14,
height: 1.3,
2021-05-29 17:01:59 +00:00
),
textAlign: TextAlign.center,
2021-05-29 17:01:59 +00:00
),
),
Padding(
2021-07-13 07:55:20 +00:00
padding: EdgeInsets.all(10),
),
_latestFiles == null
? Container()
: GestureDetector(
behavior: HitTestBehavior.translucent,
child: Padding(
padding: const EdgeInsets.fromLTRB(6, 6, 64, 6),
child: Align(
alignment: Alignment.centerRight,
child: Text(
_selectedFolders.length == _allFolders.length
? "unselect all"
: "select all",
textAlign: TextAlign.right,
style: TextStyle(
fontSize: 12,
color: Colors.white.withOpacity(0.8),
),
),
2021-07-10 09:55:07 +00:00
),
),
onTap: () {
final hasSelectedAll =
_selectedFolders.length == _allFolders.length;
// Flip selection
if (hasSelectedAll) {
_selectedFolders.clear();
} else {
_selectedFolders.addAll(_allFolders);
}
_latestFiles.sort((first, second) {
2021-07-12 14:02:56 +00:00
return first.deviceFolder
.toLowerCase()
.compareTo(second.deviceFolder.toLowerCase());
});
setState(() {});
}),
2021-07-11 21:24:28 +00:00
Expanded(child: _getFolders()),
Padding(
2021-07-10 09:30:08 +00:00
padding: EdgeInsets.all(20),
),
Hero(
tag: "select_folders",
child: Container(
2021-07-27 20:21:59 +00:00
padding: EdgeInsets.only(
left: 60, right: 60, bottom: Platform.isIOS ? 60 : 32),
child: button(
2021-07-11 18:05:07 +00:00
widget.buttonText,
fontSize: 18,
2021-07-22 18:47:41 +00:00
onPressed: _selectedFolders.isEmpty
? null
: () async {
await Configuration.instance
2021-07-11 21:24:28 +00:00
.setPathsToBackUp(_selectedFolders);
Bus.instance.fire(BackupFoldersUpdatedEvent());
Navigator.of(context).pop();
},
2021-07-10 09:30:08 +00:00
padding: EdgeInsets.fromLTRB(60, 20, 60, 20),
),
),
),
],
),
);
}
2021-07-11 21:24:28 +00:00
Widget _getFolders() {
if (_latestFiles == null) {
return loadWidget;
2021-07-11 21:24:28 +00:00
}
_sortFiles();
final scrollController = ScrollController();
return Container(
padding: EdgeInsets.only(left: 40, right: 40),
child: Scrollbar(
controller: scrollController,
isAlwaysShown: true,
2021-07-11 21:53:06 +00:00
child: Padding(
padding: const EdgeInsets.only(right: 4),
child: ImplicitlyAnimatedReorderableList<File>(
controller: scrollController,
items: _latestFiles,
areItemsTheSame: (oldItem, newItem) =>
oldItem.deviceFolder == newItem.deviceFolder,
onReorderFinished: (item, from, to, newItems) {
setState(() {
_latestFiles
..clear()
..addAll(newItems);
});
},
itemBuilder: (context, itemAnimation, file, index) {
return Reorderable(
key: ValueKey(file),
builder: (context, dragAnimation, inDrag) {
final t = dragAnimation.value;
final elevation = lerpDouble(0, 8, t);
final color = Color.lerp(
Colors.white, Colors.white.withOpacity(0.8), t);
return SizeFadeTransition(
sizeFraction: 0.7,
curve: Curves.easeInOut,
animation: itemAnimation,
child: Material(
color: color,
elevation: elevation,
type: MaterialType.transparency,
child: _getFileItem(file),
),
);
},
);
},
),
2021-07-11 21:24:28 +00:00
),
),
);
}
2021-07-12 10:24:30 +00:00
Widget _getFileItem(File file) {
2021-07-11 21:24:28 +00:00
final isSelected = _selectedFolders.contains(file.deviceFolder);
return Padding(
padding: const EdgeInsets.only(bottom: 1, right: 4),
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
borderRadius: BorderRadius.all(
Radius.circular(10),
),
color: isSelected
? Color.fromRGBO(16, 32, 32, 1)
: Color.fromRGBO(8, 18, 18, 0.4),
),
padding: EdgeInsets.fromLTRB(20, 16, 20, 16),
child: InkWell(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2021-07-12 10:24:30 +00:00
Row(
children: [
_getThumbnail(file),
Padding(padding: EdgeInsets.all(10)),
2021-07-22 18:47:41 +00:00
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
constraints: BoxConstraints(maxWidth: 140),
child: Text(
file.deviceFolder,
2021-07-12 10:24:30 +00:00
style: TextStyle(
2021-07-22 18:47:41 +00:00
fontSize: 14,
height: 1.5,
2021-07-13 07:55:20 +00:00
color: isSelected
2021-07-22 18:47:41 +00:00
? Colors.white
: Colors.white.withOpacity(0.7),
2021-07-12 10:24:30 +00:00
),
2021-07-22 18:47:41 +00:00
overflow: TextOverflow.ellipsis,
maxLines: 2,
2021-07-12 10:24:30 +00:00
),
2021-07-22 18:47:41 +00:00
),
Padding(padding: EdgeInsets.all(2)),
Text(
_itemCount[file.deviceFolder].toString() +
" item" +
(_itemCount[file.deviceFolder] == 1 ? "" : "s"),
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 12,
color: isSelected
? Colors.white.withOpacity(0.65)
: Colors.white.withOpacity(0.4),
),
),
],
2021-07-11 21:24:28 +00:00
),
2021-07-12 10:24:30 +00:00
],
2021-07-11 21:24:28 +00:00
),
Checkbox(
value: isSelected,
onChanged: (value) {
if (value) {
_selectedFolders.add(file.deviceFolder);
} else {
_selectedFolders.remove(file.deviceFolder);
}
setState(() {});
},
),
],
),
onTap: () {
final value = !_selectedFolders.contains(file.deviceFolder);
if (value) {
_selectedFolders.add(file.deviceFolder);
} else {
_selectedFolders.remove(file.deviceFolder);
}
setState(() {});
},
),
),
);
}
void _sortFiles() {
_latestFiles.sort((first, second) {
2021-07-22 18:47:41 +00:00
if (_selectedFolders.contains(first.deviceFolder) &&
_selectedFolders.contains(second.deviceFolder)) {
2021-07-12 14:02:56 +00:00
return first.deviceFolder
.toLowerCase()
.compareTo(second.deviceFolder.toLowerCase());
2021-07-11 21:24:28 +00:00
} else if (_selectedFolders.contains(first.deviceFolder)) {
return -1;
} else if (_selectedFolders.contains(second.deviceFolder)) {
return 1;
}
2021-07-12 14:02:56 +00:00
return first.deviceFolder
.toLowerCase()
.compareTo(second.deviceFolder.toLowerCase());
2021-07-11 21:24:28 +00:00
});
}
Widget _getThumbnail(File file) {
return ClipRRect(
borderRadius: BorderRadius.circular(4.0),
2021-07-22 18:47:41 +00:00
child: SizedBox(
child: ThumbnailWidget(
file,
shouldShowSyncStatus: false,
key: Key("backup_selection_widget" + file.tag()),
),
height: 60,
width: 60,
),
);
}
}