magic_util.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import 'package:flutter/material.dart';
  2. import 'package:logging/logging.dart';
  3. import 'package:path/path.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/collection.dart';
  7. import 'package:photos/models/file.dart';
  8. import 'package:photos/models/magic_metadata.dart';
  9. import 'package:photos/services/collections_service.dart';
  10. import 'package:photos/services/file_magic_service.dart';
  11. import 'package:photos/ui/common/progress_dialog.dart';
  12. import 'package:photos/utils/dialog_util.dart';
  13. import 'package:photos/utils/toast_util.dart';
  14. final _logger = Logger('MagicUtil');
  15. Future<void> changeVisibility(
  16. BuildContext context,
  17. List<File> files,
  18. int newVisibility,
  19. ) async {
  20. final dialog = createProgressDialog(
  21. context,
  22. newVisibility == visibilityArchive
  23. ? "Archiving..."
  24. : "Unarchiving..."
  25. "...",
  26. );
  27. await dialog.show();
  28. try {
  29. await FileMagicService.instance.changeVisibility(files, newVisibility);
  30. showShortToast(
  31. context,
  32. newVisibility == visibilityArchive
  33. ? "Successfully archived"
  34. : "Successfully unarchived",
  35. );
  36. await dialog.hide();
  37. } catch (e, s) {
  38. _logger.severe("failed to update file visibility", e, s);
  39. await dialog.hide();
  40. rethrow;
  41. }
  42. }
  43. Future<void> changeCollectionVisibility(
  44. BuildContext context,
  45. Collection collection,
  46. int newVisibility,
  47. ) async {
  48. final dialog = createProgressDialog(
  49. context,
  50. newVisibility == visibilityArchive ? "Archiving..." : "Unarchiving...",
  51. );
  52. await dialog.show();
  53. try {
  54. final Map<String, dynamic> update = {magicKeyVisibility: newVisibility};
  55. await CollectionsService.instance.updateMagicMetadata(collection, update);
  56. // Force reload home gallery to pull in the now unarchived files
  57. Bus.instance.fire(ForceReloadHomeGalleryEvent("CollectionArchiveChange"));
  58. showShortToast(
  59. context,
  60. newVisibility == visibilityArchive
  61. ? "Successfully archived"
  62. : "Successfully unarchived",
  63. );
  64. await dialog.hide();
  65. } catch (e, s) {
  66. _logger.severe("failed to update collection visibility", e, s);
  67. await dialog.hide();
  68. rethrow;
  69. }
  70. }
  71. Future<bool> editTime(
  72. BuildContext context,
  73. List<File> files,
  74. int editedTime,
  75. ) async {
  76. try {
  77. await _updatePublicMetadata(
  78. context,
  79. files,
  80. pubMagicKeyEditedTime,
  81. editedTime,
  82. );
  83. return true;
  84. } catch (e) {
  85. showShortToast(context, 'something went wrong');
  86. return false;
  87. }
  88. }
  89. Future<void> editFilename(
  90. BuildContext context,
  91. File file,
  92. ) async {
  93. final fileName = file.displayName;
  94. final nameWithoutExt = basenameWithoutExtension(fileName);
  95. final extName = extension(fileName);
  96. await showTextInputDialog(
  97. context,
  98. title: "Rename file",
  99. confirmationButtonLabel: "Rename",
  100. initialValue: nameWithoutExt,
  101. message: extName,
  102. alignMessage: Alignment.centerRight,
  103. hintText: "Enter file name",
  104. maxLength: 50,
  105. onConfirm: (String text) async {
  106. try {
  107. if (text.isEmpty || text.trim() == nameWithoutExt.trim()) {
  108. return;
  109. }
  110. final newName = text + extName;
  111. await _updatePublicMetadata(
  112. context,
  113. List.of([file]),
  114. pubMagicKeyEditedName,
  115. newName,
  116. );
  117. } catch (e) {
  118. showShortToast(context, 'Something went wrong');
  119. }
  120. },
  121. );
  122. }
  123. Future<bool> editFileCaption(
  124. BuildContext? context,
  125. File file,
  126. String caption,
  127. ) async {
  128. try {
  129. await _updatePublicMetadata(
  130. context,
  131. [file],
  132. pubMagicKeyCaption,
  133. caption,
  134. showDoneToast: false,
  135. );
  136. return true;
  137. } catch (e) {
  138. if (context != null) {
  139. showShortToast(context, "Something went wrong");
  140. }
  141. return false;
  142. }
  143. }
  144. Future<void> _updatePublicMetadata(
  145. BuildContext? context,
  146. List<File> files,
  147. String key,
  148. dynamic value, {
  149. bool showDoneToast = true,
  150. }) async {
  151. if (files.isEmpty) {
  152. return;
  153. }
  154. ProgressDialog? dialog;
  155. if (context != null) {
  156. dialog = createProgressDialog(context, 'Please wait...');
  157. await dialog.show();
  158. }
  159. try {
  160. final Map<String, dynamic> update = {key: value};
  161. await FileMagicService.instance.updatePublicMagicMetadata(files, update);
  162. if (context != null) {
  163. if (showDoneToast) {
  164. showShortToast(context, 'Done');
  165. }
  166. await dialog?.hide();
  167. }
  168. if (_shouldReloadGallery(key)) {
  169. Bus.instance.fire(ForceReloadHomeGalleryEvent("FileMetadataChange-$key"));
  170. }
  171. } catch (e, s) {
  172. _logger.severe("failed to update $key = $value", e, s);
  173. if (context != null) {
  174. await dialog?.hide();
  175. }
  176. rethrow;
  177. }
  178. }
  179. bool _shouldReloadGallery(String key) {
  180. return key == pubMagicKeyEditedTime;
  181. }