magic_util.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:logging/logging.dart';
  4. import 'package:path/path.dart';
  5. import 'package:photos/core/event_bus.dart';
  6. import 'package:photos/events/force_reload_home_gallery_event.dart';
  7. import 'package:photos/models/file.dart';
  8. import 'package:photos/models/magic_metadata.dart';
  9. import 'package:photos/services/file_magic_service.dart';
  10. import 'package:photos/ui/rename_dialog.dart';
  11. import 'package:photos/utils/dialog_util.dart';
  12. import 'package:photos/utils/toast_util.dart';
  13. final _logger = Logger('MagicUtil');
  14. Future<void> changeVisibility(
  15. BuildContext context, List<File> files, int newVisibility) async {
  16. final dialog = createProgressDialog(context,
  17. newVisibility == kVisibilityArchive ? "archiving..." : "unarchiving...");
  18. await dialog.show();
  19. try {
  20. await FileMagicService.instance.changeVisibility(files, newVisibility);
  21. showShortToast(newVisibility == kVisibilityArchive
  22. ? "successfully archived"
  23. : "successfully unarchived");
  24. await dialog.hide();
  25. } catch (e, s) {
  26. _logger.severe("failed to update file visibility", e, s);
  27. await dialog.hide();
  28. rethrow;
  29. }
  30. }
  31. Future<bool> editTime(
  32. BuildContext context, List<File> files, int editedTime) async {
  33. try {
  34. await _updatePublicMetadata(
  35. context, files, kPubMagicKeyEditedTime, editedTime);
  36. return true;
  37. } catch (e, s) {
  38. showToast('something went wrong');
  39. return false;
  40. }
  41. }
  42. Future<bool> editFilename(
  43. BuildContext context,
  44. File file,
  45. ) async {
  46. try {
  47. final fileName = file.getDisplayName();
  48. final nameWithoutExt = basenameWithoutExtension(fileName);
  49. final extName = extension(fileName);
  50. var result = await showDialog<String>(
  51. context: context,
  52. builder: (BuildContext context) {
  53. return RenameDialog(nameWithoutExt, 'file', maxLength: 50);
  54. },
  55. barrierColor: Colors.black.withOpacity(0.85),
  56. );
  57. if (result == null || result.trim() == nameWithoutExt.trim()) {
  58. return true;
  59. }
  60. result = result + extName;
  61. await _updatePublicMetadata(
  62. context, List.of([file]), kPubMagicKeyEditedName, result);
  63. return true;
  64. } catch (e, s) {
  65. showToast('something went wrong');
  66. return false;
  67. }
  68. }
  69. Future<void> _updatePublicMetadata(
  70. BuildContext context, List<File> files, String key, dynamic value) async {
  71. if (files.isEmpty) {
  72. return;
  73. }
  74. final dialog = createProgressDialog(context, 'please wait...');
  75. await dialog.show();
  76. try {
  77. Map<String, dynamic> update = {key: value};
  78. await FileMagicService.instance.updatePublicMagicMetadata(files, update);
  79. showShortToast('done');
  80. await dialog.hide();
  81. if (_shouldReloadGallery(key)) {
  82. Bus.instance.fire(ForceReloadHomeGalleryEvent());
  83. }
  84. } catch (e, s) {
  85. _logger.severe("failed to update $key = $value", e, s);
  86. await dialog.hide();
  87. rethrow;
  88. }
  89. }
  90. bool _shouldReloadGallery(String key) {
  91. return key == kPubMagicKeyEditedTime;
  92. }