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

424 lines
13 KiB
Dart
Raw Normal View History

// @dart=2.9
import 'dart:io';
2021-08-05 16:18:07 +00:00
import 'dart:io' as io;
2021-10-26 14:46:58 +00:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:like_button/like_button.dart';
import 'package:logging/logging.dart';
import 'package:media_extension/media_extension.dart';
2022-08-17 12:18:53 +00:00
import 'package:path/path.dart' as file_path;
import 'package:photo_manager/photo_manager.dart';
import 'package:photos/core/event_bus.dart';
import 'package:photos/db/files_db.dart';
import 'package:photos/events/local_photos_updated_event.dart';
import 'package:photos/models/file.dart';
2021-08-03 14:39:43 +00:00
import 'package:photos/models/file_type.dart';
import 'package:photos/models/ignored_file.dart';
import 'package:photos/models/trash_file.dart';
2022-11-02 04:20:34 +00:00
import 'package:photos/services/collections_service.dart';
import 'package:photos/services/favorites_service.dart';
2022-11-02 04:20:34 +00:00
import 'package:photos/services/hidden_service.dart';
import 'package:photos/services/ignored_files_service.dart';
import 'package:photos/services/local_sync_service.dart';
import 'package:photos/ui/common/progress_dialog.dart';
import 'package:photos/ui/viewer/file/custom_app_bar.dart';
import 'package:photos/utils/delete_file_util.dart';
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/file_util.dart';
import 'package:photos/utils/toast_util.dart';
class FadingAppBar extends StatefulWidget implements PreferredSizeWidget {
final File file;
final Function(File) onFileDeleted;
final double height;
2021-09-15 20:40:08 +00:00
final bool shouldShowActions;
final int userID;
const FadingAppBar(
this.file,
this.onFileDeleted,
this.userID,
2021-09-15 20:40:08 +00:00
this.height,
this.shouldShowActions, {
Key key,
}) : super(key: key);
@override
Size get preferredSize => Size.fromHeight(height);
@override
FadingAppBarState createState() => FadingAppBarState();
}
class FadingAppBarState extends State<FadingAppBar> {
final _logger = Logger("FadingAppBar");
bool _shouldHide = false;
@override
Widget build(BuildContext context) {
return CustomAppBar(
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: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.black.withOpacity(0.72),
Colors.black.withOpacity(0.6),
Colors.transparent,
],
stops: const [0, 0.2, 1],
),
),
child: _buildAppBar(),
),
),
),
2022-06-17 10:29:43 +00:00
height: Platform.isAndroid ? 80 : 96,
);
}
void hide() {
setState(() {
_shouldHide = true;
});
}
void show() {
if (mounted) {
setState(() {
_shouldHide = false;
});
}
}
AppBar _buildAppBar() {
debugPrint("building app bar");
final List<Widget> actions = [];
2021-10-26 14:46:58 +00:00
final isTrashedFile = widget.file is TrashFile;
final shouldShowActions = widget.shouldShowActions && !isTrashedFile;
// only show fav option for files owned by the user
if (widget.file.ownerID == null || widget.file.ownerID == widget.userID) {
actions.add(_getFavoriteButton());
}
2022-06-11 08:23:52 +00:00
actions.add(
PopupMenuButton(
itemBuilder: (context) {
final List<PopupMenuItem> items = [];
2022-09-23 01:48:25 +00:00
if (widget.file.isRemoteFile) {
2021-10-26 10:37:14 +00:00
items.add(
PopupMenuItem(
2022-06-11 08:23:52 +00:00
value: 1,
2021-10-26 10:37:14 +00:00
child: Row(
children: [
Icon(
2022-07-03 09:49:33 +00:00
Platform.isAndroid
? Icons.download
: CupertinoIcons.cloud_download,
color: Theme.of(context).iconTheme.color,
),
2022-07-04 06:02:17 +00:00
const Padding(
2021-10-26 10:37:14 +00:00
padding: EdgeInsets.all(8),
),
2022-07-04 06:02:17 +00:00
const Text("Download"),
2021-10-26 10:37:14 +00:00
],
),
),
);
}
2022-06-11 08:23:52 +00:00
// options for files owned by the user
2022-07-03 09:49:33 +00:00
if (widget.file.ownerID == null ||
widget.file.ownerID == widget.userID) {
2022-06-11 08:23:52 +00:00
items.add(
PopupMenuItem(
value: 2,
2022-06-11 08:23:52 +00:00
child: Row(
children: [
Icon(
2022-07-03 09:49:33 +00:00
Platform.isAndroid
? Icons.delete_outline
: CupertinoIcons.delete,
2022-06-11 08:23:52 +00:00
color: Theme.of(context).iconTheme.color,
),
2022-07-04 06:02:17 +00:00
const Padding(
2022-06-11 08:23:52 +00:00
padding: EdgeInsets.all(8),
),
2022-07-04 06:02:17 +00:00
const Text("Delete"),
2022-06-11 08:23:52 +00:00
],
),
2021-10-26 11:07:34 +00:00
),
2022-06-11 08:23:52 +00:00
);
}
if ((widget.file.fileType == FileType.image ||
widget.file.fileType == FileType.livePhoto) &&
Platform.isAndroid) {
items.add(
PopupMenuItem(
value: 3,
child: Row(
children: [
Icon(
Icons.wallpaper_outlined,
color: Theme.of(context).iconTheme.color,
),
const Padding(
padding: EdgeInsets.all(8),
),
const Text("Set as"),
],
),
),
);
}
2022-11-02 04:20:34 +00:00
if (widget.file.ownerID != null &&
widget.file.ownerID == widget.userID) {
items.add(
PopupMenuItem(
value: 4,
child: Row(
2022-11-02 05:10:57 +00:00
children: [
Icon(
Icons.visibility_off,
color: Theme.of(context).iconTheme.color,
),
const Padding(
2022-11-02 04:20:34 +00:00
padding: EdgeInsets.all(8),
),
2022-11-02 05:10:57 +00:00
const Text("Hide"),
2022-11-02 04:20:34 +00:00
],
),
),
);
}
2022-06-11 08:23:52 +00:00
return items;
},
onSelected: (value) {
if (value == 1) {
_download(widget.file);
} else if (value == 2) {
_showDeleteSheet(widget.file);
} else if (value == 3) {
2022-10-12 06:13:25 +00:00
_setAs(widget.file);
2022-11-02 04:20:34 +00:00
} else if (value == 4) {
_handleHideRequest(context);
2022-06-11 08:23:52 +00:00
}
},
),
);
return AppBar(
2022-07-04 06:02:17 +00:00
iconTheme:
const IconThemeData(color: Colors.white), //same for both themes
actions: shouldShowActions ? actions : [],
elevation: 0,
2022-07-04 06:02:17 +00:00
backgroundColor: const Color(0x00000000),
);
}
2022-11-02 04:20:34 +00:00
Future<void> _handleHideRequest(BuildContext context) async {
try {
final hideResult =
await CollectionsService.instance.hideFiles(context, [widget.file]);
} catch (e, s) {
_logger.severe("failed to update file visibility", e, s);
await showGenericErrorDialog(context);
}
}
Widget _getFavoriteButton() {
return FutureBuilder(
future: FavoritesService.instance.isFavorite(widget.file),
builder: (context, snapshot) {
if (snapshot.hasData) {
return _getLikeButton(widget.file, snapshot.data);
} else {
return _getLikeButton(widget.file, false);
}
},
);
}
Widget _getLikeButton(File file, bool isLiked) {
return LikeButton(
isLiked: isLiked,
onTap: (oldValue) async {
final isLiked = !oldValue;
bool hasError = false;
if (isLiked) {
final shouldBlockUser = file.uploadedFileID == null;
ProgressDialog dialog;
if (shouldBlockUser) {
dialog = createProgressDialog(context, "Adding to favorites...");
await dialog.show();
}
try {
await FavoritesService.instance.addToFavorites(file);
} catch (e, s) {
_logger.severe(e, s);
hasError = true;
2022-06-10 14:29:56 +00:00
showToast(context, "Sorry, could not add this to favorites!");
} finally {
if (shouldBlockUser) {
await dialog.hide();
}
}
} else {
try {
await FavoritesService.instance.removeFromFavorites(file);
} catch (e, s) {
_logger.severe(e, s);
hasError = true;
2022-06-10 14:29:56 +00:00
showToast(context, "Sorry, could not remove this from favorites!");
}
}
return hasError ? oldValue : isLiked;
},
likeBuilder: (isLiked) {
return Icon(
2022-06-10 12:31:42 +00:00
isLiked ? Icons.favorite_rounded : Icons.favorite_border_rounded,
2022-07-03 09:49:33 +00:00
color:
isLiked ? Colors.pinkAccent : Colors.white, //same for both themes
size: 24,
);
},
);
}
void _showDeleteSheet(File file) {
2021-07-11 08:23:17 +00:00
final List<Widget> actions = [];
if (file.uploadedFileID == null || file.localID == null) {
2022-06-11 08:23:52 +00:00
actions.add(
CupertinoActionSheetAction(
isDestructiveAction: true,
onPressed: () async {
await deleteFilesFromEverywhere(context, [file]);
Navigator.of(context, rootNavigator: true).pop();
widget.onFileDeleted(file);
},
2022-07-04 06:02:17 +00:00
child: const Text("Everywhere"),
2022-06-11 08:23:52 +00:00
),
);
} else {
// uploaded file which is present locally too
2022-06-11 08:23:52 +00:00
actions.add(
CupertinoActionSheetAction(
isDestructiveAction: true,
onPressed: () async {
await deleteFilesOnDeviceOnly(context, [file]);
showToast(context, "File deleted from device");
Navigator.of(context, rootNavigator: true).pop();
// TODO: Fix behavior when inside a device folder
},
2022-07-04 06:02:17 +00:00
child: const Text("Device"),
2022-06-11 08:23:52 +00:00
),
);
2022-06-11 08:23:52 +00:00
actions.add(
CupertinoActionSheetAction(
isDestructiveAction: true,
onPressed: () async {
await deleteFilesFromRemoteOnly(context, [file]);
showShortToast(context, "Moved to trash");
Navigator.of(context, rootNavigator: true).pop();
// TODO: Fix behavior when inside a collection
},
2022-07-04 06:02:17 +00:00
child: const Text("ente"),
2022-06-11 08:23:52 +00:00
),
);
2022-06-11 08:23:52 +00:00
actions.add(
CupertinoActionSheetAction(
isDestructiveAction: true,
onPressed: () async {
await deleteFilesFromEverywhere(context, [file]);
Navigator.of(context, rootNavigator: true).pop();
widget.onFileDeleted(file);
},
2022-07-04 06:02:17 +00:00
child: const Text("Everywhere"),
2022-06-11 08:23:52 +00:00
),
);
}
final action = CupertinoActionSheet(
2022-07-04 06:02:17 +00:00
title: const Text("Delete file?"),
actions: actions,
cancelButton: CupertinoActionSheetAction(
2022-07-04 06:02:17 +00:00
child: const Text("Cancel"),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
},
),
);
showCupertinoModalPopup(context: context, builder: (_) => action);
}
Future<void> _download(File file) async {
2022-06-06 16:13:29 +00:00
final dialog = createProgressDialog(context, "Downloading...");
await dialog.show();
2022-08-29 14:43:31 +00:00
final FileType type = file.fileType;
2021-08-05 16:18:07 +00:00
// save and track image for livePhoto/image and video for FileType.video
2022-08-29 14:43:31 +00:00
final io.File fileToSave = await getFile(file);
2021-08-05 16:18:07 +00:00
final savedAsset = type == FileType.video
? (await PhotoManager.editor.saveVideo(fileToSave, title: file.title))
2022-07-03 09:49:33 +00:00
: (await PhotoManager.editor
.saveImageWithPath(fileToSave.path, title: file.title));
2021-08-05 16:18:07 +00:00
// immediately track assetID to avoid duplicate upload
await LocalSyncService.instance.trackDownloadedFile(savedAsset.id);
file.localID = savedAsset.id;
await FilesDB.instance.insert(file);
2021-08-05 16:18:07 +00:00
if (type == FileType.livePhoto) {
2022-08-29 14:43:31 +00:00
final io.File liveVideo = await getFileFromServer(file, liveVideo: true);
2021-08-05 16:18:07 +00:00
if (liveVideo == null) {
2022-09-23 01:48:25 +00:00
_logger.warning("Failed to find live video" + file.tag);
2021-08-05 16:18:07 +00:00
} else {
2022-08-17 12:18:53 +00:00
final videoTitle = file_path.basenameWithoutExtension(file.title) +
file_path.extension(liveVideo.path);
final savedAsset = (await PhotoManager.editor.saveVideo(
liveVideo,
title: videoTitle,
));
final ignoreVideoFile = IgnoredFile(
savedAsset.id,
savedAsset.title ?? videoTitle,
savedAsset.relativePath ?? 'remoteDownload',
"remoteDownload",
);
debugPrint("IgnoreFile for auto-upload ${ignoreVideoFile.toString()}");
await IgnoredFilesService.instance.cacheAndInsert([ignoreVideoFile]);
}
2021-08-03 14:39:43 +00:00
}
2021-08-05 16:18:07 +00:00
Bus.instance.fire(LocalPhotosUpdatedEvent([file]));
await dialog.hide();
if (file.fileType == FileType.livePhoto) {
2022-06-10 14:29:56 +00:00
showToast(context, "Photo and video saved to gallery");
2021-08-04 13:07:37 +00:00
} else {
2022-06-10 14:29:56 +00:00
showToast(context, "File saved to gallery");
2021-08-04 13:07:37 +00:00
}
}
2022-10-12 06:13:25 +00:00
Future<void> _setAs(File file) async {
final dialog = createProgressDialog(context, "Please wait...");
await dialog.show();
try {
final io.File fileToSave = await getFile(file);
var m = MediaExtension();
2022-10-12 06:13:25 +00:00
final bool result = await m.setAs("file://${fileToSave.path}", "image/*");
if (result == false) {
showShortToast(context, "Something went wrong");
}
dialog.hide();
} catch (e) {
dialog.hide();
_logger.severe("Failed to use as", e);
showGenericErrorDialog(context);
}
}
}