magic_util.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. final result = await showTextInputDialog(
  97. context,
  98. title: "Rename file",
  99. submitButtonLabel: "Rename",
  100. initialValue: nameWithoutExt,
  101. message: extName.toUpperCase(),
  102. alignMessage: Alignment.centerRight,
  103. hintText: "Enter file name",
  104. maxLength: 50,
  105. alwaysShowSuccessState: true,
  106. onSubmit: (String text) async {
  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. showProgressDialogs: false,
  117. showDoneToast: false,
  118. );
  119. },
  120. );
  121. if (result is Exception) {
  122. _logger.severe("Failed to rename file");
  123. showGenericErrorDialog(context: context);
  124. }
  125. }
  126. Future<bool> editFileCaption(
  127. BuildContext? context,
  128. File file,
  129. String caption,
  130. ) async {
  131. try {
  132. await _updatePublicMetadata(
  133. context,
  134. [file],
  135. pubMagicKeyCaption,
  136. caption,
  137. showDoneToast: false,
  138. );
  139. return true;
  140. } catch (e) {
  141. if (context != null) {
  142. showShortToast(context, "Something went wrong");
  143. }
  144. return false;
  145. }
  146. }
  147. Future<void> _updatePublicMetadata(
  148. BuildContext? context,
  149. List<File> files,
  150. String key,
  151. dynamic value, {
  152. bool showDoneToast = true,
  153. bool showProgressDialogs = true,
  154. }) async {
  155. if (files.isEmpty) {
  156. return;
  157. }
  158. ProgressDialog? dialog;
  159. if (context != null && showProgressDialogs) {
  160. dialog = createProgressDialog(context, 'Please wait...');
  161. await dialog.show();
  162. }
  163. try {
  164. final Map<String, dynamic> update = {key: value};
  165. await FileMagicService.instance.updatePublicMagicMetadata(files, update);
  166. if (context != null) {
  167. if (showDoneToast) {
  168. showShortToast(context, 'Done');
  169. }
  170. await dialog?.hide();
  171. }
  172. if (_shouldReloadGallery(key)) {
  173. Bus.instance.fire(ForceReloadHomeGalleryEvent("FileMetadataChange-$key"));
  174. }
  175. } catch (e, s) {
  176. _logger.severe("failed to update $key = $value", e, s);
  177. if (context != null) {
  178. await dialog?.hide();
  179. }
  180. rethrow;
  181. }
  182. }
  183. bool _shouldReloadGallery(String key) {
  184. return key == pubMagicKeyEditedTime;
  185. }