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

472 lines
15 KiB
Dart
Raw Normal View History

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';
2023-04-07 05:41:42 +00:00
import "package:photos/generated/l10n.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/magic_metadata.dart";
2022-11-02 10:25:32 +00:00
import 'package:photos/models/selected_files.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';
2023-02-14 10:32:56 +00:00
import 'package:photos/ui/collection_action_sheet.dart';
import 'package:photos/ui/common/progress_dialog.dart';
import 'package:photos/ui/viewer/file/custom_app_bar.dart';
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/file_util.dart';
import "package:photos/utils/magic_util.dart";
import 'package:photos/utils/toast_util.dart';
class FadingAppBar extends StatefulWidget implements PreferredSizeWidget {
final File file;
2022-11-12 11:28:34 +00:00
final Function(File) onFileRemoved;
final double height;
2021-09-15 20:40:08 +00:00
final bool shouldShowActions;
final int? userID;
const FadingAppBar(
this.file,
2022-11-12 11:28:34 +00:00
this.onFileRemoved,
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(),
),
),
),
Size.fromHeight(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;
final bool isOwnedByUser =
widget.file.ownerID == null || widget.file.ownerID == widget.userID;
final bool isFileUploaded = widget.file.isUploaded;
bool isFileHidden = false;
if (isOwnedByUser && isFileUploaded) {
isFileHidden = CollectionsService.instance
.getCollectionByID(widget.file.collectionID!)
?.isHidden() ??
false;
}
// only show fav option for files owned by the user
2022-11-11 06:24:43 +00:00
if (isOwnedByUser && !isFileHidden && isFileUploaded) {
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),
),
2023-04-07 05:41:42 +00:00
Text(S.of(context).download),
2021-10-26 10:37:14 +00:00
],
),
),
);
}
2022-06-11 08:23:52 +00:00
// options for files owned by the user
if (isOwnedByUser && !isFileHidden) {
final bool isArchived =
widget.file.magicMetadata.visibility == visibilityArchive;
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(
isArchived ? Icons.unarchive : Icons.archive_outlined,
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),
),
2023-04-07 05:41:42 +00:00
Text(isArchived
? S.of(context).unarchive
: S.of(context).archive),
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),
),
2023-04-07 05:41:42 +00:00
Text(S.of(context).setAs),
],
),
),
);
}
if (isOwnedByUser && widget.file.isUploaded) {
if (!isFileHidden) {
2022-11-02 10:25:32 +00:00
items.add(
PopupMenuItem(
value: 4,
child: Row(
children: [
Icon(
Icons.visibility_off,
color: Theme.of(context).iconTheme.color,
),
const Padding(
padding: EdgeInsets.all(8),
),
2023-04-07 05:41:42 +00:00
Text(S.of(context).hide),
2022-11-02 10:25:32 +00:00
],
),
2022-11-02 04:20:34 +00:00
),
2022-11-02 10:25:32 +00:00
);
} else {
items.add(
PopupMenuItem(
value: 5,
child: Row(
children: [
Icon(
Icons.visibility,
color: Theme.of(context).iconTheme.color,
),
const Padding(
padding: EdgeInsets.all(8),
),
2023-04-07 05:41:42 +00:00
Text(S.of(context).unhide),
2022-11-02 10:25:32 +00:00
],
),
),
);
}
2022-11-02 04:20:34 +00:00
}
2022-06-11 08:23:52 +00:00
return items;
},
onSelected: (dynamic value) async {
2022-06-11 08:23:52 +00:00
if (value == 1) {
_download(widget.file);
} else if (value == 2) {
await _toggleFileArchiveStatus(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-11-02 10:25:32 +00:00
} else if (value == 5) {
_handleUnHideRequest(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]);
2022-11-02 10:25:32 +00:00
if (hideResult) {
2022-11-12 11:28:34 +00:00
widget.onFileRemoved(widget.file);
2022-11-02 10:25:32 +00:00
}
2022-11-02 04:20:34 +00:00
} catch (e, s) {
_logger.severe("failed to update file visibility", e, s);
await showGenericErrorDialog(context: context);
2022-11-02 04:20:34 +00:00
}
}
2022-11-02 10:25:32 +00:00
Future<void> _handleUnHideRequest(BuildContext context) async {
2023-02-14 10:13:34 +00:00
final selectedFiles = SelectedFiles();
selectedFiles.files.add(widget.file);
showCollectionActionSheet(
2022-11-02 10:25:32 +00:00
context,
2023-02-14 10:13:34 +00:00
selectedFiles: selectedFiles,
2023-01-25 04:57:40 +00:00
actionType: CollectionActionType.unHide,
2022-11-02 10:25:32 +00:00
);
}
Widget _getFavoriteButton() {
return FutureBuilder<bool>(
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;
late ProgressDialog dialog;
if (shouldBlockUser) {
2023-04-07 05:41:42 +00:00
dialog =
createProgressDialog(context, S.of(context).addingToFavorites);
await dialog.show();
}
try {
await FavoritesService.instance.addToFavorites(context, file);
} catch (e, s) {
_logger.severe(e, s);
hasError = true;
2023-04-07 05:41:42 +00:00
showToast(context, S.of(context).sorryCouldNotAddToFavorites);
} finally {
if (shouldBlockUser) {
await dialog.hide();
}
}
} else {
try {
await FavoritesService.instance.removeFromFavorites(context, file);
} catch (e, s) {
_logger.severe(e, s);
hasError = true;
2023-04-07 05:41:42 +00:00
showToast(context, S.of(context).sorryCouldNotRemoveFromFavorites);
}
}
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,
);
},
);
}
Future<void> _toggleFileArchiveStatus(File file) async {
final bool isArchived =
widget.file.magicMetadata.visibility == visibilityArchive;
await changeVisibility(
context,
[widget.file],
isArchived ? visibilityVisible : visibilityArchive,
);
if (mounted) {
setState(() {});
}
}
Future<void> _download(File file) async {
2022-06-06 16:13:29 +00:00
final dialog = createProgressDialog(context, "Downloading...");
await dialog.show();
2022-11-05 10:41:50 +00:00
try {
final FileType type = file.fileType;
final bool downloadLivePhotoOnDroid =
type == FileType.livePhoto && Platform.isAndroid;
AssetEntity? savedAsset;
final io.File? fileToSave = await getFile(file);
//Disabling notifications for assets changing to insert the file into
//files db before triggering a sync.
PhotoManager.stopChangeNotify();
2022-11-05 10:41:50 +00:00
if (type == FileType.image) {
savedAsset = await PhotoManager.editor
.saveImageWithPath(fileToSave!.path, title: file.title!);
2022-11-05 10:41:50 +00:00
} else if (type == FileType.video) {
savedAsset = await PhotoManager.editor
.saveVideo(fileToSave!, title: file.title!);
2022-11-05 10:41:50 +00:00
} else if (type == FileType.livePhoto) {
final io.File? liveVideoFile =
2022-11-05 10:41:50 +00:00
await getFileFromServer(file, liveVideo: true);
if (liveVideoFile == null) {
throw AssertionError("Live video can not be null");
}
if (downloadLivePhotoOnDroid) {
await _saveLivePhotoOnDroid(fileToSave!, liveVideoFile, file);
2022-11-05 10:41:50 +00:00
} else {
savedAsset = await PhotoManager.editor.darwin.saveLivePhoto(
imageFile: fileToSave!,
2022-11-05 10:41:50 +00:00
videoFile: liveVideoFile,
title: file.title!,
2022-11-05 10:41:50 +00:00
);
}
}
2022-11-05 10:41:50 +00:00
if (savedAsset != null) {
file.localID = savedAsset.id;
await FilesDB.instance.insert(file);
2022-11-11 13:09:22 +00:00
Bus.instance.fire(
LocalPhotosUpdatedEvent(
[file],
source: "download",
),
);
2022-11-05 10:41:50 +00:00
} else if (!downloadLivePhotoOnDroid && savedAsset == null) {
_logger.severe('Failed to save assert of type $type');
}
2023-04-07 05:41:42 +00:00
showToast(context, S.of(context).fileSavedToGallery);
2022-11-05 10:41:50 +00:00
await dialog.hide();
} catch (e) {
_logger.warning("Failed to save file", e);
await dialog.hide();
showGenericErrorDialog(context: context);
} finally {
PhotoManager.startChangeNotify();
2023-01-05 04:14:11 +00:00
LocalSyncService.instance.checkAndSync().ignore();
2021-08-04 13:07:37 +00:00
}
}
2022-11-05 10:41:50 +00:00
Future<void> _saveLivePhotoOnDroid(
2022-11-06 08:22:54 +00:00
io.File image,
io.File video,
File enteFile,
) async {
2022-11-05 10:41:50 +00:00
debugPrint("Downloading LivePhoto on Droid");
AssetEntity? savedAsset = await (PhotoManager.editor
.saveImageWithPath(image.path, title: enteFile.title!));
if (savedAsset == null) {
throw Exception("Failed to save image of live photo");
}
2022-11-05 10:41:50 +00:00
IgnoredFile ignoreVideoFile = IgnoredFile(
savedAsset.id,
savedAsset.title ?? '',
savedAsset.relativePath ?? 'remoteDownload',
"remoteDownload",
);
await IgnoredFilesService.instance.cacheAndInsert([ignoreVideoFile]);
final videoTitle = file_path.basenameWithoutExtension(enteFile.title!) +
2022-11-05 10:41:50 +00:00
file_path.extension(video.path);
savedAsset = (await (PhotoManager.editor.saveVideo(
2022-11-05 10:41:50 +00:00
video,
title: videoTitle,
)));
if (savedAsset == null) {
throw Exception("Failed to save video of live photo");
}
2022-11-05 10:41:50 +00:00
ignoreVideoFile = IgnoredFile(
savedAsset.id,
savedAsset.title ?? videoTitle,
savedAsset.relativePath ?? 'remoteDownload',
"remoteDownload",
);
await IgnoredFilesService.instance.cacheAndInsert([ignoreVideoFile]);
}
2022-10-12 06:13:25 +00:00
Future<void> _setAs(File file) async {
2023-04-07 05:41:42 +00:00
final dialog = createProgressDialog(context, S.of(context).pleaseWait);
await dialog.show();
try {
final io.File? fileToSave = await (getFile(file));
if (fileToSave == null) {
throw Exception("Fail to get file for setAs operation");
}
2022-11-06 08:34:06 +00:00
final m = MediaExtension();
2022-10-12 06:13:25 +00:00
final bool result = await m.setAs("file://${fileToSave.path}", "image/*");
if (result == false) {
2023-04-07 05:41:42 +00:00
showShortToast(context, S.of(context).somethingWentWrong);
}
dialog.hide();
} catch (e) {
dialog.hide();
_logger.severe("Failed to use as", e);
showGenericErrorDialog(context: context);
}
}
}