ente/lib/utils/magic_util.dart

196 lines
4.9 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2021-10-26 10:37:14 +00:00
import 'package:logging/logging.dart';
import 'package:path/path.dart';
2021-10-26 10:37:14 +00:00
import 'package:photos/core/event_bus.dart';
import 'package:photos/events/force_reload_home_gallery_event.dart';
2022-03-21 09:32:24 +00:00
import 'package:photos/models/collection.dart';
2021-10-26 10:37:14 +00:00
import 'package:photos/models/file.dart';
import 'package:photos/models/magic_metadata.dart';
2022-03-21 09:32:24 +00:00
import 'package:photos/services/collections_service.dart';
2021-10-26 10:37:14 +00:00
import 'package:photos/services/file_magic_service.dart';
import 'package:photos/ui/common/progress_dialog.dart';
2021-10-26 10:37:14 +00:00
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/toast_util.dart';
final _logger = Logger('MagicUtil');
Future<void> changeVisibility(
2022-06-11 08:23:52 +00:00
BuildContext context,
List<File> files,
int newVisibility,
) async {
final dialog = createProgressDialog(
context,
2022-10-31 09:56:59 +00:00
newVisibility == visibilityArchive
? "Archiving..."
: "Unarchiving..."
"...",
2022-06-11 08:23:52 +00:00
);
2021-10-26 10:37:14 +00:00
await dialog.show();
try {
await FileMagicService.instance.changeVisibility(files, newVisibility);
2022-06-10 14:29:56 +00:00
showShortToast(
2022-06-11 08:23:52 +00:00
context,
newVisibility == visibilityArchive
2022-10-31 09:56:59 +00:00
? "Successfully archived"
: "Successfully unarchived",
2022-06-11 08:23:52 +00:00
);
2021-10-26 10:37:14 +00:00
await dialog.hide();
} catch (e, s) {
_logger.severe("failed to update file visibility", e, s);
await dialog.hide();
rethrow;
}
}
2022-03-21 10:07:02 +00:00
Future<void> changeCollectionVisibility(
2022-06-11 08:23:52 +00:00
BuildContext context,
Collection collection,
int newVisibility,
) async {
final dialog = createProgressDialog(
context,
2022-11-01 08:26:08 +00:00
newVisibility == visibilityArchive ? "Archiving..." : "Unarchiving...",
2022-06-11 08:23:52 +00:00
);
2022-03-21 09:32:24 +00:00
await dialog.show();
try {
final Map<String, dynamic> update = {magicKeyVisibility: newVisibility};
2022-03-21 09:32:24 +00:00
await CollectionsService.instance.updateMagicMetadata(collection, update);
// Force reload home gallery to pull in the now unarchived files
2022-11-11 10:23:35 +00:00
Bus.instance.fire(ForceReloadHomeGalleryEvent("CollectionArchiveChange"));
2022-06-10 14:29:56 +00:00
showShortToast(
2022-06-11 08:23:52 +00:00
context,
newVisibility == visibilityArchive
2022-11-01 08:26:08 +00:00
? "Successfully archived"
: "Successfully unarchived",
2022-06-11 08:23:52 +00:00
);
2022-03-21 09:32:24 +00:00
await dialog.hide();
} catch (e, s) {
_logger.severe("failed to update collection visibility", e, s);
await dialog.hide();
rethrow;
}
}
2021-10-26 10:37:14 +00:00
Future<bool> editTime(
2022-06-11 08:23:52 +00:00
BuildContext context,
List<File> files,
int editedTime,
) async {
2021-10-26 10:37:14 +00:00
try {
await _updatePublicMetadata(
2022-06-11 08:23:52 +00:00
context,
files,
pubMagicKeyEditedTime,
2022-06-11 08:23:52 +00:00
editedTime,
);
2021-10-26 10:37:14 +00:00
return true;
2022-07-03 10:09:01 +00:00
} catch (e) {
showShortToast(context, 'something went wrong');
2021-10-26 10:37:14 +00:00
return false;
}
}
Future<void> editFilename(
BuildContext context,
File file,
) async {
final fileName = file.displayName;
final nameWithoutExt = basenameWithoutExtension(fileName);
final extName = extension(fileName);
final result = await showTextInputDialog(
context,
title: "Rename file",
submitButtonLabel: "Rename",
initialValue: nameWithoutExt,
message: extName.toUpperCase(),
alignMessage: Alignment.centerRight,
hintText: "Enter file name",
maxLength: 50,
onSubmit: (String text) async {
if (text.isEmpty || text.trim() == nameWithoutExt.trim()) {
return;
}
final newName = text + extName;
await _updatePublicMetadata(
context,
List.of([file]),
pubMagicKeyEditedName,
newName,
showProgressDialogs: false,
showDoneToast: false,
);
},
);
if (result is Exception) {
_logger.severe("Failed to rename file");
showGenericErrorDialog(context: context);
}
}
Future<bool> editFileCaption(
2022-12-30 09:08:46 +00:00
BuildContext? context,
File file,
String caption,
) async {
try {
await _updatePublicMetadata(
context,
[file],
pubMagicKeyCaption,
caption,
showDoneToast: false,
);
return true;
} catch (e) {
if (context != null) {
showShortToast(context, "Something went wrong");
}
return false;
}
}
2021-10-26 10:37:14 +00:00
Future<void> _updatePublicMetadata(
2022-12-30 09:08:46 +00:00
BuildContext? context,
2022-06-11 08:23:52 +00:00
List<File> files,
String key,
dynamic value, {
bool showDoneToast = true,
bool showProgressDialogs = true,
}) async {
2021-10-26 10:37:14 +00:00
if (files.isEmpty) {
return;
}
2022-12-30 09:08:46 +00:00
ProgressDialog? dialog;
if (context != null && showProgressDialogs) {
2022-11-05 18:01:47 +00:00
dialog = createProgressDialog(context, 'Please wait...');
await dialog.show();
}
2021-10-26 10:37:14 +00:00
try {
2022-08-29 14:43:31 +00:00
final Map<String, dynamic> update = {key: value};
2021-10-26 10:37:14 +00:00
await FileMagicService.instance.updatePublicMagicMetadata(files, update);
if (context != null) {
if (showDoneToast) {
showShortToast(context, 'Done');
}
2022-12-30 09:08:46 +00:00
await dialog?.hide();
}
2021-10-26 10:37:14 +00:00
if (_shouldReloadGallery(key)) {
2022-11-11 10:23:35 +00:00
Bus.instance.fire(ForceReloadHomeGalleryEvent("FileMetadataChange-$key"));
2021-10-26 10:37:14 +00:00
}
} catch (e, s) {
_logger.severe("failed to update $key = $value", e, s);
if (context != null) {
2022-12-30 09:08:46 +00:00
await dialog?.hide();
}
2021-10-26 10:37:14 +00:00
rethrow;
}
}
bool _shouldReloadGallery(String key) {
return key == pubMagicKeyEditedTime;
2021-10-26 10:37:14 +00:00
}