ente/lib/ui/viewer/file/fading_bottom_bar.dart

293 lines
9 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/models/file.dart';
import 'package:photos/models/file_type.dart';
import 'package:photos/models/magic_metadata.dart';
import 'package:photos/models/selected_files.dart';
import 'package:photos/models/trash_file.dart';
import 'package:photos/services/collections_service.dart';
import 'package:photos/theme/colors.dart';
import 'package:photos/theme/ente_theme.dart';
2023-01-25 04:57:40 +00:00
import 'package:photos/ui/create_collection_sheet.dart';
import 'package:photos/ui/viewer/file/file_info_widget.dart';
import 'package:photos/utils/delete_file_util.dart';
import 'package:photos/utils/magic_util.dart';
import 'package:photos/utils/share_util.dart';
class FadingBottomBar extends StatefulWidget {
final File file;
final Function(File) onEditRequested;
2021-09-15 20:40:08 +00:00
final bool showOnlyInfoButton;
const FadingBottomBar(
2021-07-11 08:01:47 +00:00
this.file,
2021-09-15 20:40:08 +00:00
this.onEditRequested,
this.showOnlyInfoButton, {
Key? key,
}) : super(key: key);
@override
FadingBottomBarState createState() => FadingBottomBarState();
}
class FadingBottomBarState extends State<FadingBottomBar> {
bool _shouldHide = false;
final GlobalKey shareButtonKey = GlobalKey();
@override
Widget build(BuildContext context) {
return _getBottomBar();
}
void hide() {
setState(() {
_shouldHide = true;
});
}
void show() {
setState(() {
_shouldHide = false;
});
}
void safeRefresh() {
if (mounted) {
setState(() {});
}
}
Widget _getBottomBar() {
2022-08-29 14:43:31 +00:00
final List<Widget> children = [];
children.add(
2021-10-03 21:58:33 +00:00
Tooltip(
2022-05-17 11:38:21 +00:00
message: "Info",
2021-10-03 21:58:33 +00:00
child: Padding(
padding: const EdgeInsets.only(top: 12, bottom: 12),
child: IconButton(
icon: Icon(
Platform.isAndroid ? Icons.info_outline : CupertinoIcons.info,
color: Colors.white,
),
onPressed: () async {
await _displayInfo(widget.file);
safeRefresh(); //to instantly show the new caption if keypad is closed after pressing 'done' - here the caption will be updated before the bottom sheet is closed
await Future.delayed(
const Duration(milliseconds: 500),
); //Waiting for some time till the caption gets updated in db if the user closes the bottom sheet without pressing 'done'
safeRefresh();
2021-10-03 21:58:33 +00:00
},
),
),
),
);
if (widget.file is TrashFile) {
_addTrashOptions(children);
}
2022-11-06 08:34:06 +00:00
final bool isUploadedByUser = widget.file.uploadedFileID != null &&
widget.file.ownerID == Configuration.instance.getUserID();
bool isFileHidden = false;
if (isUploadedByUser) {
isFileHidden = CollectionsService.instance
.getCollectionByID(widget.file.collectionID!)
?.isHidden() ??
false;
}
if (!widget.showOnlyInfoButton && widget.file is! TrashFile) {
2021-09-15 20:40:08 +00:00
if (widget.file.fileType == FileType.image ||
widget.file.fileType == FileType.livePhoto) {
children.add(
2021-10-03 21:58:33 +00:00
Tooltip(
2022-05-17 11:38:21 +00:00
message: "Edit",
2021-10-03 21:58:33 +00:00
child: Padding(
padding: const EdgeInsets.only(top: 12, bottom: 12),
child: IconButton(
2022-07-04 06:02:17 +00:00
icon: const Icon(
Icons.tune_outlined,
color: Colors.white,
),
2021-10-03 21:58:33 +00:00
onPressed: () {
widget.onEditRequested(widget.file);
},
),
2021-09-15 20:40:08 +00:00
),
),
);
}
if (isUploadedByUser && !isFileHidden) {
2022-08-29 14:43:31 +00:00
final bool isArchived =
widget.file.magicMetadata.visibility == visibilityArchive;
children.add(
Tooltip(
2022-10-31 09:56:59 +00:00
message: isArchived ? "Unarchive" : "Archive",
child: Padding(
padding: const EdgeInsets.only(top: 12, bottom: 12),
child: IconButton(
icon: Icon(
2022-10-31 09:56:59 +00:00
isArchived ? Icons.unarchive : Icons.archive_outlined,
color: Colors.white,
),
onPressed: () async {
await changeVisibility(
context,
[widget.file],
isArchived ? visibilityVisible : visibilityArchive,
);
safeRefresh();
},
2021-10-03 21:57:09 +00:00
),
),
),
);
}
children.add(
2021-10-03 21:58:33 +00:00
Tooltip(
2022-05-17 11:38:21 +00:00
message: "Share",
2021-10-03 21:58:33 +00:00
child: Padding(
padding: const EdgeInsets.only(top: 12, bottom: 12),
child: IconButton(
key: shareButtonKey,
icon: Icon(
Platform.isAndroid
? Icons.share_outlined
: CupertinoIcons.share,
color: Colors.white,
),
2021-10-03 21:58:33 +00:00
onPressed: () {
share(context, [widget.file], shareButtonKey: shareButtonKey);
2021-10-03 21:58:33 +00:00
},
),
),
),
);
}
2022-08-29 14:43:31 +00:00
final safeAreaBottomPadding = MediaQuery.of(context).padding.bottom * .5;
return IgnorePointer(
ignoring: _shouldHide,
child: AnimatedOpacity(
2022-07-03 09:45:00 +00:00
opacity: _shouldHide ? 0 : 1,
2022-07-04 06:02:17 +00:00
duration: const Duration(milliseconds: 150),
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.transparent,
Colors.black.withOpacity(0.6),
Colors.black.withOpacity(0.72),
],
stops: const [0, 0.8, 1],
),
),
child: Padding(
padding: EdgeInsets.only(bottom: safeAreaBottomPadding),
2022-11-05 04:41:21 +00:00
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
2022-11-05 12:01:25 +00:00
widget.file.caption?.isNotEmpty ?? false
2022-11-05 04:41:21 +00:00
? Padding(
padding: const EdgeInsets.fromLTRB(
16,
28,
16,
12,
),
child: Text(
widget.file.caption!,
overflow: TextOverflow.ellipsis,
maxLines: 4,
2022-11-05 04:41:21 +00:00
style: getEnteTextTheme(context)
.small
.copyWith(color: textBaseDark),
2022-11-05 05:58:11 +00:00
textAlign: TextAlign.center,
2022-11-05 04:41:21 +00:00
),
)
: const SizedBox.shrink(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: children,
),
],
),
),
2021-09-15 20:40:08 +00:00
),
),
),
);
}
void _addTrashOptions(List<Widget> children) {
children.add(
Tooltip(
2022-05-17 11:38:21 +00:00
message: "Restore",
child: Padding(
padding: const EdgeInsets.only(top: 12, bottom: 12),
child: IconButton(
2022-07-04 06:02:17 +00:00
icon: const Icon(
Icons.restore_outlined,
color: Colors.white,
),
onPressed: () {
final selectedFiles = SelectedFiles();
selectedFiles.toggleSelection(widget.file);
2023-01-25 04:57:40 +00:00
createCollectionSheet(
selectedFiles,
null,
2022-06-11 08:23:52 +00:00
context,
2023-01-25 04:57:40 +00:00
actionType: CollectionActionType.restoreFiles,
2022-06-11 08:23:52 +00:00
);
},
),
),
),
);
children.add(
Tooltip(
2022-05-17 11:38:21 +00:00
message: "Delete",
child: Padding(
padding: const EdgeInsets.only(top: 12, bottom: 12),
child: IconButton(
2022-07-04 06:02:17 +00:00
icon: const Icon(
Icons.delete_forever_outlined,
color: Colors.white,
),
onPressed: () async {
final trashedFile = <TrashFile>[];
trashedFile.add(widget.file as TrashFile);
if (await deleteFromTrash(context, trashedFile) == true) {
Navigator.pop(context);
}
},
),
),
),
);
}
Future<void> _displayInfo(File file) async {
final colorScheme = getEnteColorScheme(context);
return showBarModalBottomSheet(
topControl: const SizedBox.shrink(),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(0)),
2022-11-08 09:27:39 +00:00
backgroundColor: colorScheme.backgroundElevated,
2022-12-17 05:18:33 +00:00
barrierColor: backdropFaintDark,
context: context,
builder: (BuildContext context) {
2022-11-04 07:07:58 +00:00
return Padding(
padding:
EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
child: FileInfoWidget(file),
);
},
);
}
}