ente/lib/ui/backup_folder_selection_page.dart

438 lines
15 KiB
Dart
Raw Normal View History

// @dart=2.9
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/foundation.dart';
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:logging/logging.dart';
2022-08-23 13:19:20 +00:00
import 'package:photos/core/configuration.dart';
import 'package:photos/db/device_files_db.dart';
import 'package:photos/db/files_db.dart';
2022-04-08 05:59:20 +00:00
import 'package:photos/ente_theme_data.dart';
import 'package:photos/models/device_collection.dart';
import 'package:photos/models/file.dart';
import 'package:photos/services/remote_sync_service.dart';
import 'package:photos/ui/common/loading_widget.dart';
import 'package:photos/ui/viewer/file/thumbnail_widget.dart';
import 'package:photos/utils/dialog_util.dart';
class BackupFolderSelectionPage extends StatefulWidget {
final bool isOnboarding;
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,
this.isOnboarding = false,
2021-07-11 18:00:32 +00:00
Key key,
}) : super(key: key);
@override
2022-07-03 09:45:00 +00:00
State<BackupFolderSelectionPage> createState() =>
_BackupFolderSelectionPageState();
}
class _BackupFolderSelectionPageState extends State<BackupFolderSelectionPage> {
final Logger _logger = Logger((_BackupFolderSelectionPageState).toString());
final Set<String> _allDevicePathIDs = <String>{};
final Set<String> _selectedDevicePathIDs = <String>{};
List<DeviceCollection> _deviceCollections;
Map<String, int> _pathIDToItemCount;
@override
void initState() {
FilesDB.instance
.getDeviceCollections(includeCoverThumbnail: true)
.then((files) async {
_pathIDToItemCount =
await FilesDB.instance.getDevicePathIDToImportedFileCount();
2021-07-11 18:00:32 +00:00
setState(() {
_deviceCollections = files;
_deviceCollections.sort((first, second) {
return first.name.toLowerCase().compareTo(second.name.toLowerCase());
2021-07-12 14:02:56 +00:00
});
for (final file in _deviceCollections) {
_allDevicePathIDs.add(file.id);
if (file.shouldBackup) {
_selectedDevicePathIDs.add(file.id);
}
2021-07-11 18:00:32 +00:00
}
if (widget.isOnboarding) {
_selectedDevicePathIDs.addAll(_allDevicePathIDs);
2021-07-11 18:00:32 +00:00
}
_selectedDevicePathIDs
.removeWhere((folder) => !_allDevicePathIDs.contains(folder));
2021-07-11 18:00:32 +00:00
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: widget.isOnboarding
2022-05-16 20:57:41 +00:00
? null
: AppBar(
elevation: 0,
2022-07-04 06:02:17 +00:00
title: const Text(""),
2022-05-16 20:57:41 +00:00
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
2022-07-04 06:02:17 +00:00
const SizedBox(
height: 0,
),
SafeArea(
child: Container(
2022-07-04 06:02:17 +00:00
padding: const EdgeInsets.fromLTRB(24, 32, 24, 8),
child: Text(
'Select folders for backup',
textAlign: TextAlign.left,
style: TextStyle(
color: Theme.of(context).colorScheme.onSurface,
fontFamily: 'Inter-Bold',
fontSize: 32,
fontWeight: FontWeight.bold,
),
),
2022-03-12 14:03:30 +00:00
),
),
Padding(
2022-03-12 14:03:30 +00:00
padding: const EdgeInsets.only(left: 24, right: 48),
child: Text(
"Selected folders will be encrypted and backed up",
2022-03-05 20:52:00 +00:00
style: Theme.of(context).textTheme.caption.copyWith(height: 1.3),
2021-05-29 17:01:59 +00:00
),
),
2022-07-04 06:02:17 +00:00
const Padding(
2021-07-13 07:55:20 +00:00
padding: EdgeInsets.all(10),
),
_deviceCollections == null
? Container()
: GestureDetector(
behavior: HitTestBehavior.translucent,
child: Padding(
2022-03-12 14:03:30 +00:00
padding: const EdgeInsets.fromLTRB(24, 6, 64, 12),
child: Align(
2022-03-12 14:03:30 +00:00
alignment: Alignment.centerLeft,
child: Text(
_selectedDevicePathIDs.length ==
_allDevicePathIDs.length
2022-03-12 14:03:30 +00:00
? "Unselect all"
: "Select all",
textAlign: TextAlign.right,
2022-07-04 06:02:17 +00:00
style: const TextStyle(
decoration: TextDecoration.underline,
fontSize: 16,
),
),
2021-07-10 09:55:07 +00:00
),
),
onTap: () {
final hasSelectedAll = _selectedDevicePathIDs.length ==
_allDevicePathIDs.length;
// Flip selection
if (hasSelectedAll) {
_selectedDevicePathIDs.clear();
} else {
_selectedDevicePathIDs.addAll(_allDevicePathIDs);
}
_deviceCollections.sort((first, second) {
return first.name
2021-07-12 14:02:56 +00:00
.toLowerCase()
.compareTo(second.name.toLowerCase());
});
setState(() {});
2022-06-11 08:23:52 +00:00
},
),
2021-07-11 21:24:28 +00:00
Expanded(child: _getFolders()),
Column(
children: [
2022-10-14 08:45:38 +00:00
Container(
width: double.infinity,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Theme.of(context).backgroundColor,
blurRadius: 24,
offset: const Offset(0, -8),
spreadRadius: 4,
)
],
),
padding: widget.isOnboarding
? const EdgeInsets.only(left: 20, right: 20)
: EdgeInsets.only(
top: 16,
left: 20,
right: 20,
bottom: Platform.isIOS ? 60 : 32,
),
child: OutlinedButton(
onPressed: _selectedDevicePathIDs.isEmpty
? null
: () async {
await updateFolderSettings();
},
child: Text(widget.buttonText),
),
),
widget.isOnboarding
? Padding(
padding: EdgeInsets.only(
top: 16,
bottom: Platform.isIOS ? 48 : 32,
),
child: GestureDetector(
onTap: () {
Navigator.of(context).pop();
},
child: Text(
"Skip",
style: Theme.of(context).textTheme.caption.copyWith(
decoration: TextDecoration.underline,
),
),
),
)
: const SizedBox.shrink(),
],
),
],
),
);
}
Future<void> updateFolderSettings() async {
final dialog = createProgressDialog(
context,
"Updating folder selection...",
);
await dialog.show();
try {
final Map<String, bool> syncStatus = {};
for (String pathID in _allDevicePathIDs) {
syncStatus[pathID] = _selectedDevicePathIDs.contains(pathID);
}
await Configuration.instance.setHasSelectedAnyBackupFolder(
_selectedDevicePathIDs.isNotEmpty,
);
await Configuration.instance.setSelectAllFoldersForBackup(
_allDevicePathIDs.length == _selectedDevicePathIDs.length,
);
await RemoteSyncService.instance.updateDeviceFolderSyncStatus(syncStatus);
dialog.hide();
Navigator.of(context).pop();
} catch (e, s) {
_logger.severe("Failed to updated backup folder", e, s);
await dialog.hide();
showGenericErrorDialog(context);
}
}
2021-07-11 21:24:28 +00:00
Widget _getFolders() {
if (_deviceCollections == null) {
return const EnteLoadingWidget();
2021-07-11 21:24:28 +00:00
}
_sortFiles();
final scrollController = ScrollController();
return Container(
2022-07-04 06:02:17 +00:00
padding: const EdgeInsets.symmetric(horizontal: 20),
2021-07-11 21:24:28 +00:00
child: Scrollbar(
controller: scrollController,
2022-06-03 12:10:14 +00:00
thumbVisibility: true,
2021-07-11 21:53:06 +00:00
child: Padding(
padding: const EdgeInsets.only(right: 4),
child: ImplicitlyAnimatedReorderableList<DeviceCollection>(
2021-07-11 21:53:06 +00:00
controller: scrollController,
items: _deviceCollections,
areItemsTheSame: (oldItem, newItem) => oldItem.id == newItem.id,
2021-07-11 21:53:06 +00:00
onReorderFinished: (item, from, to, newItems) {
setState(() {
_deviceCollections
2021-07-11 21:53:06 +00:00
..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);
2022-03-05 20:52:00 +00:00
final themeColor = Theme.of(context).colorScheme.onSurface;
final color =
Color.lerp(themeColor, themeColor.withOpacity(0.8), t);
2021-07-11 21:53:06 +00:00
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
),
),
);
}
Widget _getFileItem(DeviceCollection deviceCollection) {
final isSelected = _selectedDevicePathIDs.contains(deviceCollection.id);
final importedCount = _pathIDToItemCount != null
? _pathIDToItemCount[deviceCollection.id] ?? 0
: -1;
2021-07-11 21:24:28 +00:00
return Padding(
2022-03-12 14:03:30 +00:00
padding: const EdgeInsets.only(bottom: 1, right: 1),
2021-07-11 21:24:28 +00:00
child: Container(
decoration: BoxDecoration(
2022-06-11 08:23:52 +00:00
border: Border.all(
color: Theme.of(context).colorScheme.boxUnSelectColor,
),
2022-07-04 06:02:17 +00:00
borderRadius: const BorderRadius.all(
2022-06-11 08:23:52 +00:00
Radius.circular(12),
),
// color: isSelected
// ? Theme.of(context).colorScheme.boxSelectColor
// : Theme.of(context).colorScheme.boxUnSelectColor,
gradient: isSelected
2022-07-04 06:02:17 +00:00
? const LinearGradient(
colors: [Color(0xFF00DD4D), Color(0xFF43BA6C)],
2022-06-11 08:23:52 +00:00
) //same for both themes
: LinearGradient(
colors: [
2022-04-21 13:04:22 +00:00
Theme.of(context).colorScheme.boxUnSelectColor,
Theme.of(context).colorScheme.boxUnSelectColor
2022-06-11 08:23:52 +00:00
],
),
),
2022-07-04 06:02:17 +00:00
padding: const EdgeInsets.fromLTRB(8, 4, 4, 4),
2021-07-11 21:24:28 +00:00
child: InkWell(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2021-07-12 10:24:30 +00:00
Row(
children: [
2022-03-12 14:03:30 +00:00
Checkbox(
checkColor: Colors.green,
activeColor: Colors.white,
value: isSelected,
onChanged: (value) {
if (value) {
_selectedDevicePathIDs.add(deviceCollection.id);
2022-03-12 14:03:30 +00:00
} else {
_selectedDevicePathIDs.remove(deviceCollection.id);
2022-03-12 14:03:30 +00:00
}
setState(() {});
},
),
2021-07-22 18:47:41 +00:00
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
2022-07-04 06:02:17 +00:00
constraints: const BoxConstraints(maxWidth: 180),
2021-07-22 18:47:41 +00:00
child: Text(
deviceCollection.name,
2022-03-12 14:03:30 +00:00
textAlign: TextAlign.left,
2021-07-12 10:24:30 +00:00
style: TextStyle(
2022-03-12 14:03:30 +00:00
fontFamily: 'Inter-Medium',
fontSize: 18,
fontWeight: FontWeight.w600,
2021-07-13 07:55:20 +00:00
color: isSelected
2021-07-22 18:47:41 +00:00
? Colors.white
2022-03-12 14:03:30 +00:00
: Theme.of(context)
.colorScheme
.onSurface
.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
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.only(top: 2)),
2021-07-22 18:47:41 +00:00
Text(
(kDebugMode ? 'inApp: $importedCount : device ' : '') +
(deviceCollection.count ?? 0).toString() +
2021-07-22 18:47:41 +00:00
" item" +
((deviceCollection.count ?? 0) == 1 ? "" : "s"),
2021-07-22 18:47:41 +00:00
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 12,
color: isSelected
2022-03-12 14:03:30 +00:00
? Colors.white
: Theme.of(context).colorScheme.onSurface,
2021-07-22 18:47:41 +00:00
),
),
],
2021-07-11 21:24:28 +00:00
),
2021-07-12 10:24:30 +00:00
],
2021-07-11 21:24:28 +00:00
),
_getThumbnail(deviceCollection.thumbnail, isSelected),
2021-07-11 21:24:28 +00:00
],
),
onTap: () {
final value = !_selectedDevicePathIDs.contains(deviceCollection.id);
2021-07-11 21:24:28 +00:00
if (value) {
_selectedDevicePathIDs.add(deviceCollection.id);
2021-07-11 21:24:28 +00:00
} else {
_selectedDevicePathIDs.remove(deviceCollection.id);
2021-07-11 21:24:28 +00:00
}
setState(() {});
},
),
),
);
}
void _sortFiles() {
_deviceCollections.sort((first, second) {
if (_selectedDevicePathIDs.contains(first.id) &&
_selectedDevicePathIDs.contains(second.id)) {
return first.name.toLowerCase().compareTo(second.name.toLowerCase());
} else if (_selectedDevicePathIDs.contains(first.id)) {
2021-07-11 21:24:28 +00:00
return -1;
} else if (_selectedDevicePathIDs.contains(second.id)) {
2021-07-11 21:24:28 +00:00
return 1;
}
return first.name.toLowerCase().compareTo(second.name.toLowerCase());
2021-07-11 21:24:28 +00:00
});
}
Widget _getThumbnail(File file, bool isSelected) {
return ClipRRect(
borderRadius: BorderRadius.circular(8),
2021-07-22 18:47:41 +00:00
child: SizedBox(
2022-07-03 09:45:00 +00:00
height: 88,
width: 88,
2022-06-11 08:23:52 +00:00
child: Stack(
alignment: AlignmentDirectional.bottomEnd,
children: [
ThumbnailWidget(
file,
shouldShowSyncStatus: false,
2022-09-23 01:48:25 +00:00
key: Key("backup_selection_widget" + file.tag),
2022-06-11 08:23:52 +00:00
),
Padding(
padding: const EdgeInsets.all(9),
child: isSelected
2022-07-04 06:02:17 +00:00
? const Icon(
Icons.local_police,
color: Colors.white,
)
2022-06-11 08:23:52 +00:00
: null,
),
],
),
),
);
}
}