From e8a76542cf896837751ecf7da6f327edff67e7d0 Mon Sep 17 00:00:00 2001 From: dab246 Date: Tue, 16 Dec 2025 16:38:03 +0700 Subject: [PATCH] TF-4141 Auto refresh count emails in Action required folder when email changed --- .../handle_action_required_tab_extension.dart | 17 ++++++++++------- .../extensions/list_mailbox_node_extension.dart | 11 ++++++----- .../presentation/mailbox_controller.dart | 1 + .../extensions/handle_ai_action_extension.dart | 9 +++++++++ .../thread/presentation/thread_controller.dart | 2 ++ 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/features/mailbox/presentation/extensions/handle_action_required_tab_extension.dart b/lib/features/mailbox/presentation/extensions/handle_action_required_tab_extension.dart index 40d08bacc..46491beef 100644 --- a/lib/features/mailbox/presentation/extensions/handle_action_required_tab_extension.dart +++ b/lib/features/mailbox/presentation/extensions/handle_action_required_tab_extension.dart @@ -33,7 +33,7 @@ extension HandleActionRequiredTabExtension on BaseMailboxController { void _addActionRequiredFolder(int count) { final folder = _buildActionRequiredFolder(count); - _addToDefaultMailboxTree(folder); + _addToDefaultMailboxTree(folder, count); _addToAllMailboxes(folder); } @@ -43,20 +43,23 @@ extension HandleActionRequiredTabExtension on BaseMailboxController { return base.copyWith( displayName: currentContext != null ? base.getDisplayName(currentContext!) : null, - totalEmails: TotalEmails(UnsignedInt(count)), unreadEmails: UnreadEmails(UnsignedInt(count)), ); } - void _addToDefaultMailboxTree(PresentationMailbox folder) { + void _addToDefaultMailboxTree(PresentationMailbox folder, int count) { final root = defaultMailboxTree.value.root; final children = List.from(root.childrenItems ?? []); - children.insertAfterStarredOrInbox(MailboxNode(folder)); + final result = children.insertAfterStarredOrInbox(MailboxNode(folder)); - defaultMailboxTree.value = MailboxTree( - root.copyWith(children: children), - ); + if (result) { + defaultMailboxTree.value = MailboxTree( + root.copyWith(children: children), + ); + } else { + defaultMailboxTree.value.updateMailboxUnreadCountById(folder.id, count); + } } void _addToAllMailboxes(PresentationMailbox folder) { diff --git a/lib/features/mailbox/presentation/extensions/list_mailbox_node_extension.dart b/lib/features/mailbox/presentation/extensions/list_mailbox_node_extension.dart index 98d2a3ff8..2795e46b4 100644 --- a/lib/features/mailbox/presentation/extensions/list_mailbox_node_extension.dart +++ b/lib/features/mailbox/presentation/extensions/list_mailbox_node_extension.dart @@ -15,8 +15,8 @@ extension ListMailboxNodeExtension on List { /// Insert [newNode] after Starred if present, /// otherwise after Inbox, otherwise at the beginning. - void insertAfterStarredOrInbox(MailboxNode newNode) { - insertAfterByPriority( + bool insertAfterStarredOrInbox(MailboxNode newNode) { + return insertAfterByPriority( newNode, [ _isStarred, @@ -27,21 +27,22 @@ extension ListMailboxNodeExtension on List { /// Insert [newNode] after the first mailbox matching [priorities]. /// If none match, inserts at the beginning. - void insertAfterByPriority( + bool insertAfterByPriority( MailboxNode newNode, List priorities, ) { - if (_containsMailbox(newNode)) return; + if (_containsMailbox(newNode)) return false; for (final predicate in priorities) { final index = indexWhere(predicate); if (index != -1) { insert(index + 1, newNode); - return; + return true; } } insert(0, newNode); + return true; } bool _isInbox(MailboxNode node) { diff --git a/lib/features/mailbox/presentation/mailbox_controller.dart b/lib/features/mailbox/presentation/mailbox_controller.dart index 5af64b502..bb715a8b3 100644 --- a/lib/features/mailbox/presentation/mailbox_controller.dart +++ b/lib/features/mailbox/presentation/mailbox_controller.dart @@ -611,6 +611,7 @@ class MailboxController extends BaseMailboxController syncAllMailboxWithDisplayName(currentContext!); } addFavoriteFolderToMailboxList(); + setUpActionRequiredFolder(mailboxDashBoardController); _setMapMailbox(); _setOutboxMailbox(); _selectSelectedMailboxDefault(); diff --git a/lib/features/mailbox_dashboard/presentation/extensions/handle_ai_action_extension.dart b/lib/features/mailbox_dashboard/presentation/extensions/handle_ai_action_extension.dart index 9c6b71da4..4cd60763d 100644 --- a/lib/features/mailbox_dashboard/presentation/extensions/handle_ai_action_extension.dart +++ b/lib/features/mailbox_dashboard/presentation/extensions/handle_ai_action_extension.dart @@ -16,4 +16,13 @@ extension HandleAiActionExtension on MailboxDashBoardController { currentAccountId, ); } + + void autoRefreshCountEmailsInActionRequiredFolder() { + if (isAiCapabilitySupported) { + actionRequiredFolderController.getCountEmails( + session: sessionCurrent, + accountId: accountId.value, + ); + } + } } diff --git a/lib/features/thread/presentation/thread_controller.dart b/lib/features/thread/presentation/thread_controller.dart index c68b6c8bb..b349a7c7f 100644 --- a/lib/features/thread/presentation/thread_controller.dart +++ b/lib/features/thread/presentation/thread_controller.dart @@ -33,6 +33,7 @@ import 'package:tmail_ui_user/features/home/data/exceptions/session_exceptions.d import 'package:tmail_ui_user/features/mailbox/domain/state/mark_as_mailbox_read_state.dart'; import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/action/dashboard_action.dart'; import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/search_controller.dart' as search; +import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/handle_ai_action_extension.dart'; import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/move_emails_to_mailbox_extension.dart'; import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/open_and_close_composer_extension.dart'; import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/update_current_emails_flags_extension.dart'; @@ -788,6 +789,7 @@ class ThreadController extends BaseController with EmailActionController { } else { await _refreshChangeListEmail(); } + mailboxDashBoardController.autoRefreshCountEmailsInActionRequiredFolder(); } catch (e, stackTrace) { logWarning('ThreadController::_handleWebSocketMessage:Error processing state: $e'); onError(e, stackTrace);