magic_util.dart 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import 'package:flutter/widgets.dart';
  2. import 'package:fluttertoast/fluttertoast.dart';
  3. import 'package:logging/logging.dart';
  4. import 'package:photos/core/event_bus.dart';
  5. import 'package:photos/events/force_reload_home_gallery_event.dart';
  6. import 'package:photos/models/file.dart';
  7. import 'package:photos/models/magic_metadata.dart';
  8. import 'package:photos/services/file_magic_service.dart';
  9. import 'package:photos/utils/dialog_util.dart';
  10. import 'package:photos/utils/toast_util.dart';
  11. final _logger = Logger('MagicUtil');
  12. Future<void> changeVisibility(
  13. BuildContext context, List<File> files, int newVisibility) async {
  14. final dialog = createProgressDialog(context,
  15. newVisibility == kVisibilityArchive ? "archiving..." : "unarchiving...");
  16. await dialog.show();
  17. try {
  18. await FileMagicService.instance.changeVisibility(files, newVisibility);
  19. showShortToast(newVisibility == kVisibilityArchive
  20. ? "successfully archived"
  21. : "successfully unarchived");
  22. await dialog.hide();
  23. } catch (e, s) {
  24. _logger.severe("failed to update file visibility", e, s);
  25. await dialog.hide();
  26. rethrow;
  27. }
  28. }
  29. Future<bool> editTime(
  30. BuildContext context, List<File> files, int editedTime) async {
  31. try {
  32. await _updatePublicMetadata(
  33. context, files, kPubMagicKeyEditedTime, editedTime);
  34. return true;
  35. } catch (e, s) {
  36. showToast('something went wrong');
  37. return false;
  38. }
  39. }
  40. Future<void> _updatePublicMetadata(
  41. BuildContext context, List<File> files, String key, dynamic value) async {
  42. if (files.isEmpty) {
  43. return;
  44. }
  45. final dialog = createProgressDialog(context, 'please wait...');
  46. await dialog.show();
  47. try {
  48. Map<String, dynamic> update = {key: value};
  49. await FileMagicService.instance.updatePublicMagicMetadata(files, update);
  50. showShortToast('done');
  51. await dialog.hide();
  52. if (_shouldReloadGallery(key)) {
  53. Bus.instance.fire(ForceReloadHomeGalleryEvent());
  54. }
  55. } catch (e, s) {
  56. _logger.severe("failed to update $key = $value", e, s);
  57. await dialog.hide();
  58. rethrow;
  59. }
  60. }
  61. bool _shouldReloadGallery(String key) {
  62. return key == kPubMagicKeyEditedTime;
  63. }