diff --git a/lib/features/mailbox_dashboard/presentation/extensions/move_emails_to_mailbox_extension.dart b/lib/features/mailbox_dashboard/presentation/extensions/move_emails_to_mailbox_extension.dart index 468d3f5236..f05bf6cffc 100644 --- a/lib/features/mailbox_dashboard/presentation/extensions/move_emails_to_mailbox_extension.dart +++ b/lib/features/mailbox_dashboard/presentation/extensions/move_emails_to_mailbox_extension.dart @@ -1,15 +1,16 @@ +import 'package:jmap_dart_client/jmap/core/unsigned_int.dart'; import 'package:jmap_dart_client/jmap/mail/email/email.dart'; import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart'; import 'package:model/email/presentation_email.dart'; -import 'package:tmail_ui_user/features/email/domain/model/move_action.dart'; import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart'; extension MoveEmailsToMailboxExtension on MailboxDashBoardController { void handleMoveEmailsToMailbox({ required Map> originalMailboxIdsWithEmailIds, required MailboxId destinationMailboxId, - required MoveAction moveAction, }) { + if (destinationMailboxId == selectedMailbox.value?.id) return; + final currentEmails = List.from( emailsInCurrentMailbox, ); @@ -23,9 +24,17 @@ extension MoveEmailsToMailboxExtension on MailboxDashBoardController { final currentEmailsToBeMoved = currentEmails .where((email) => movedEmailIds.contains(email.id)) .toList(); - if (currentEmailsToBeMoved.isNotEmpty && destinationMailboxId != selectedMailbox.value?.id) { + if (currentEmailsToBeMoved.isNotEmpty) { currentEmails.removeWhere(currentEmailsToBeMoved.contains); updateEmailList(currentEmails); } + final currentMailbox = selectedMailbox.value; + final currentTotalEmails = currentMailbox?.totalEmails; + if (currentMailbox != null && currentTotalEmails != null) { + final newTotalEmails = currentTotalEmails.value.value.toInt() - movedEmailIds.length; + selectedMailbox.value = currentMailbox.copyWith( + totalEmails: TotalEmails(UnsignedInt(newTotalEmails)), + ); + } } } \ No newline at end of file diff --git a/lib/features/thread/presentation/thread_controller.dart b/lib/features/thread/presentation/thread_controller.dart index 0fbb495016..467852e057 100644 --- a/lib/features/thread/presentation/thread_controller.dart +++ b/lib/features/thread/presentation/thread_controller.dart @@ -353,32 +353,38 @@ class ThreadController extends BaseController with EmailActionController { mailboxDashBoardController.handleMoveEmailsToMailbox( originalMailboxIdsWithEmailIds: reactionState.originalMailboxIdsWithEmailIds, destinationMailboxId: reactionState.destinationMailboxId, - moveAction: reactionState.moveAction, ); _checkIfCurrentMailboxCanLoadMore(); } else if (reactionState is MoveMultipleEmailToMailboxAllSuccess) { mailboxDashBoardController.handleMoveEmailsToMailbox( originalMailboxIdsWithEmailIds: reactionState.originalMailboxIdsWithEmailIds, destinationMailboxId: reactionState.destinationMailboxId, - moveAction: reactionState.moveAction, ); _checkIfCurrentMailboxCanLoadMore(); } else if (reactionState is MoveMultipleEmailToMailboxHasSomeEmailFailure) { mailboxDashBoardController.handleMoveEmailsToMailbox( originalMailboxIdsWithEmailIds: reactionState.originalMailboxIdsWithMoveSucceededEmailIds, destinationMailboxId: reactionState.destinationMailboxId, - moveAction: reactionState.moveAction, ); - WidgetsBinding.instance.addPostFrameCallback((_) { - _checkIfCurrentMailboxCanLoadMore(); - }); - } else if (reactionState is DeleteEmailPermanentlySuccess - || reactionState is DeleteMultipleEmailsPermanentlyAllSuccess - || reactionState is DeleteMultipleEmailsPermanentlyHasSomeEmailFailure - ) { - WidgetsBinding.instance.addPostFrameCallback((_) { - _checkIfCurrentMailboxCanLoadMore(); - }); + _checkIfCurrentMailboxCanLoadMore(); + } else if (reactionState is DeleteEmailPermanentlySuccess) { + _handleDeleteEmailsPermanentlyFromMailboxId( + reactionState.mailboxId, + deletedEmailsCount: 1, + ); + _checkIfCurrentMailboxCanLoadMore(); + } else if (reactionState is DeleteMultipleEmailsPermanentlyAllSuccess) { + _handleDeleteEmailsPermanentlyFromMailboxId( + reactionState.mailboxId, + deletedEmailsCount: reactionState.emailIds.length, + ); + _checkIfCurrentMailboxCanLoadMore(); + } else if (reactionState is DeleteMultipleEmailsPermanentlyHasSomeEmailFailure) { + _handleDeleteEmailsPermanentlyFromMailboxId( + reactionState.mailboxId, + deletedEmailsCount: reactionState.emailIds.length, + ); + _checkIfCurrentMailboxCanLoadMore(); } }); } @@ -394,6 +400,21 @@ class ThreadController extends BaseController with EmailActionController { mailboxDashBoardController.emailsInCurrentMailbox.refresh(); } + void _handleDeleteEmailsPermanentlyFromMailboxId( + MailboxId? mailboxId, { + required int deletedEmailsCount, + }) { + if (mailboxDashBoardController.selectedMailbox.value?.id != mailboxId) return; + final currentMailbox = mailboxDashBoardController.selectedMailbox.value; + final currentTotalEmails = currentMailbox?.totalEmails; + if (currentMailbox != null && currentTotalEmails != null) { + final newTotalEmails = currentTotalEmails.value.value.toInt() - deletedEmailsCount; + mailboxDashBoardController.selectedMailbox.value = currentMailbox.copyWith( + totalEmails: TotalEmails(UnsignedInt(newTotalEmails)), + ); + } + } + void _checkIfCurrentMailboxCanLoadMore() { final currentMailbox = mailboxDashBoardController.selectedMailbox.value; if (currentMailbox == null) return;