ente/lib/utils/magic_util.dart

157 lines
4 KiB
Dart
Raw Normal View History

// @dart=2.9
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/rename_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,
newVisibility == visibilityArchive ? "Hiding..." : "Unhiding...",
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-06-11 08:23:52 +00:00
? "Successfully hidden"
: "Successfully unhidden",
);
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,
newVisibility == visibilityArchive ? "Hiding..." : "Unhiding...",
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
Bus.instance.fire(ForceReloadHomeGalleryEvent());
2022-06-10 14:29:56 +00:00
showShortToast(
2022-06-11 08:23:52 +00:00
context,
newVisibility == visibilityArchive
2022-06-11 08:23:52 +00:00
? "Successfully hidden"
: "Successfully unhidden",
);
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) {
2022-06-10 14:29:56 +00:00
showToast(context, 'something went wrong');
2021-10-26 10:37:14 +00:00
return false;
}
}
Future<bool> editFilename(
BuildContext context,
File file,
) async {
try {
2022-09-23 01:48:25 +00:00
final fileName = file.displayName;
final nameWithoutExt = basenameWithoutExtension(fileName);
final extName = extension(fileName);
var result = await showDialog<String>(
context: context,
builder: (BuildContext context) {
return RenameDialog(nameWithoutExt, 'file', maxLength: 50);
},
barrierColor: Colors.black.withOpacity(0.85),
);
if (result == null || result.trim() == nameWithoutExt.trim()) {
return true;
}
result = result + extName;
await _updatePublicMetadata(
2022-06-11 08:23:52 +00:00
context,
List.of([file]),
pubMagicKeyEditedName,
2022-06-11 08:23:52 +00:00
result,
);
return true;
2022-07-03 10:09:01 +00:00
} catch (e) {
2022-06-10 14:29:56 +00:00
showToast(context, 'something went wrong');
return false;
}
}
2021-10-26 10:37:14 +00:00
Future<void> _updatePublicMetadata(
2022-06-11 08:23:52 +00:00
BuildContext context,
List<File> files,
String key,
dynamic value,
) async {
2021-10-26 10:37:14 +00:00
if (files.isEmpty) {
return;
}
2021-10-27 07:21:40 +00:00
final dialog = createProgressDialog(context, 'please wait...');
2021-10-26 10:37:14 +00:00
await dialog.show();
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);
2022-06-10 14:29:56 +00:00
showShortToast(context, 'done');
2021-10-26 10:37:14 +00:00
await dialog.hide();
if (_shouldReloadGallery(key)) {
Bus.instance.fire(ForceReloadHomeGalleryEvent());
}
} catch (e, s) {
_logger.severe("failed to update $key = $value", e, s);
await dialog.hide();
rethrow;
}
}
bool _shouldReloadGallery(String key) {
return key == pubMagicKeyEditedTime;
2021-10-26 10:37:14 +00:00
}