upload_icon_widget.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import "dart:async";
  2. import "package:flutter/foundation.dart";
  3. import "package:flutter/material.dart";
  4. import "package:flutter_animate/flutter_animate.dart";
  5. import "package:photos/core/event_bus.dart";
  6. import "package:photos/db/files_db.dart";
  7. import "package:photos/events/collection_updated_event.dart";
  8. import "package:photos/events/files_updated_event.dart";
  9. import "package:photos/models/file.dart";
  10. import "package:photos/services/collections_service.dart";
  11. import "package:photos/services/hidden_service.dart";
  12. import "package:photos/services/ignored_files_service.dart";
  13. import "package:photos/services/remote_sync_service.dart";
  14. import "package:photos/services/sync_service.dart";
  15. import "package:photos/ui/common/loading_widget.dart";
  16. import "package:photos/utils/toast_util.dart";
  17. class UploadIconWidget extends StatefulWidget {
  18. final EnteFile file;
  19. const UploadIconWidget({super.key, required this.file});
  20. @override
  21. State<StatefulWidget> createState() {
  22. return _UpdateIconWidgetState();
  23. }
  24. }
  25. class _UpdateIconWidgetState extends State<UploadIconWidget> {
  26. late StreamSubscription<CollectionUpdatedEvent> _firstImportEvent;
  27. late IgnoredFilesService ignoreService;
  28. bool isUploadedNow = false;
  29. bool isBeingUploaded = false;
  30. @override
  31. void initState() {
  32. super.initState();
  33. ignoreService = IgnoredFilesService.instance;
  34. _firstImportEvent =
  35. Bus.instance.on<CollectionUpdatedEvent>().listen((event) {
  36. if (mounted &&
  37. event.type == EventType.addedOrUpdated &&
  38. event.updatedFiles.isNotEmpty &&
  39. event.updatedFiles.first.localID == widget.file.localID &&
  40. event.updatedFiles.first.isUploaded) {
  41. setState(() {
  42. isUploadedNow = true;
  43. });
  44. }
  45. });
  46. }
  47. @override
  48. void dispose() {
  49. _firstImportEvent.cancel();
  50. super.dispose();
  51. }
  52. @override
  53. Widget build(BuildContext context) {
  54. if (SyncService.instance.isSyncInProgress()) {
  55. return const SizedBox.shrink();
  56. }
  57. if (widget.file.isUploaded || isUploadedNow) {
  58. if (isUploadedNow) {
  59. return Padding(
  60. padding: const EdgeInsets.all(8.0),
  61. child: const Icon(
  62. Icons.cloud_done_outlined,
  63. color: Colors.white,
  64. )
  65. .animate()
  66. .fadeIn(
  67. duration: 500.ms,
  68. curve: Curves.easeInOutCubic,
  69. )
  70. .fadeOut(
  71. delay: const Duration(seconds: 3),
  72. duration: 500.ms,
  73. curve: Curves.easeInOutCubic,
  74. ),
  75. );
  76. }
  77. return const SizedBox.shrink();
  78. }
  79. return FutureBuilder<Map<String, String>>(
  80. future: ignoreService.idToIgnoreReasonMap,
  81. builder: (context, snapshot) {
  82. if (snapshot.hasData) {
  83. final Map<String, String> idsToReasonMap = snapshot.data!;
  84. final ignoreReason =
  85. ignoreService.getUploadSkipReason(idsToReasonMap, widget.file);
  86. final bool isIgnored = ignoreReason != null;
  87. final bool isQueuedForUpload =
  88. !isIgnored && widget.file.collectionID != null;
  89. if (isQueuedForUpload && isBeingUploaded) {
  90. return const EnteLoadingWidget();
  91. }
  92. if (isIgnored && kDebugMode) {
  93. showToast(
  94. context,
  95. 'Upload is ignored due to $ignoreReason',
  96. );
  97. }
  98. return Tooltip(
  99. message: isIgnored
  100. ? "Tap to upload, upload is currently ignored due "
  101. "to $ignoreReason"
  102. : "Tap to upload",
  103. child: IconButton(
  104. icon: const Icon(
  105. Icons.upload_rounded,
  106. color: Colors.white,
  107. ),
  108. onPressed: () async {
  109. if (isIgnored) {
  110. await IgnoredFilesService.instance
  111. .removeIgnoredMappings([widget.file]);
  112. }
  113. if (widget.file.collectionID == null) {
  114. widget.file.collectionID = (await CollectionsService.instance
  115. .getUncategorizedCollection())
  116. .id;
  117. FilesDB.instance.insert(widget.file);
  118. }
  119. RemoteSyncService.instance.sync().ignore();
  120. if (mounted) {
  121. setState(() {
  122. isBeingUploaded = true;
  123. });
  124. }
  125. },
  126. ),
  127. );
  128. } else {
  129. return const SizedBox.shrink();
  130. }
  131. },
  132. );
  133. }
  134. }