Skip to content

Commit

Permalink
TF-3385 Update mark as read of MailboxController
Browse files Browse the repository at this point in the history
  • Loading branch information
tddang-linagora committed Dec 30, 2024
1 parent 54e4bef commit fe4276f
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 14 deletions.
43 changes: 43 additions & 0 deletions lib/features/base/base_mailbox_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -578,4 +578,47 @@ abstract class BaseMailboxController extends BaseController {
return;
}
}

void updateUnreadCountOfMailboxById(
MailboxId mailboxId, {
int? readCount,
int? unreadCount,
}) {
int unreadChanges = 0;
if (readCount != null) {
unreadChanges -= readCount;
}
if (unreadCount != null) {
unreadChanges += unreadCount;
}

MailboxNode? selectedNode;

selectedNode = defaultMailboxTree.value.findNode((node) => node.item.id == mailboxId);
if (selectedNode != null) {
defaultMailboxTree.value.updateMailboxUnreadCountById(
mailboxId,
unreadChanges);
defaultMailboxTree.refresh();
return;
}

selectedNode = personalMailboxTree.value.findNode((node) => node.item.id == mailboxId);
if (selectedNode != null) {
personalMailboxTree.value.updateMailboxUnreadCountById(
mailboxId,
unreadChanges);
personalMailboxTree.refresh();
return;
}

selectedNode = teamMailboxesTree.value.findNode((node) => node.item.id == mailboxId);
if (selectedNode != null) {
teamMailboxesTree.value.updateMailboxUnreadCountById(
mailboxId,
unreadChanges);
teamMailboxesTree.refresh();
return;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.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/read_actions.dart';
import 'package:tmail_ui_user/features/email/domain/model/mark_read_action.dart';

class MarkAsEmailReadSuccess extends UIState {
final EmailId emailId;
final ReadActions readActions;
final MarkReadAction markReadAction;
final MailboxId? mailboxId;

MarkAsEmailReadSuccess(
this.emailId,
this.readActions,
this.markReadAction,
this.mailboxId,
);

@override
List<Object?> get props => [emailId, readActions, markReadAction];
List<Object?> get props => [emailId, readActions, markReadAction, mailboxId];
}

class MarkAsEmailReadFailure extends FeatureFailure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/session/session.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/read_actions.dart';
import 'package:tmail_ui_user/features/email/domain/model/mark_read_action.dart';
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
Expand All @@ -20,6 +21,7 @@ class MarkAsEmailReadInteractor {
EmailId emailId,
ReadActions readAction,
MarkReadAction markReadAction,
MailboxId? mailboxId,
) async* {
try {
final result = await _emailRepository.markAsRead(
Expand All @@ -33,6 +35,7 @@ class MarkAsEmailReadInteractor {
result.first,
readAction,
markReadAction,
mailboxId,
));
} catch (e) {
yield Left(MarkAsEmailReadFailure(readAction, exception: e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
presentationEmail.id!,
readActions,
markReadAction,
presentationEmail.mailboxContain?.mailboxId,
));
}
}
Expand Down
51 changes: 51 additions & 0 deletions lib/features/mailbox/presentation/mailbox_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'package:tmail_ui_user/features/base/base_mailbox_controller.dart';
import 'package:tmail_ui_user/features/base/mixin/contact_support_mixin.dart';
import 'package:tmail_ui_user/features/base/mixin/mailbox_action_handler_mixin.dart';
import 'package:tmail_ui_user/features/email/domain/model/move_action.dart';
import 'package:tmail_ui_user/features/email/domain/state/mark_as_email_read_state.dart';
import 'package:tmail_ui_user/features/email/presentation/model/composer_arguments.dart';
import 'package:tmail_ui_user/features/home/data/exceptions/session_exceptions.dart';
import 'package:tmail_ui_user/features/home/domain/extensions/session_extensions.dart';
Expand Down Expand Up @@ -70,6 +71,7 @@ import 'package:tmail_ui_user/features/push_notification/presentation/websocket/
import 'package:tmail_ui_user/features/push_notification/presentation/websocket/web_socket_queue_handler.dart';
import 'package:tmail_ui_user/features/search/mailbox/presentation/search_mailbox_bindings.dart';
import 'package:tmail_ui_user/features/thread/domain/model/search_query.dart';
import 'package:tmail_ui_user/features/thread/domain/state/mark_as_multiple_email_read_state.dart';
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
import 'package:tmail_ui_user/main/routes/app_routes.dart';
import 'package:tmail_ui_user/main/routes/dialog_router.dart';
Expand Down Expand Up @@ -255,6 +257,55 @@ class MailboxController extends BaseMailboxController
mailboxDashBoardController.clearMailboxUIAction();
}
});

ever(mailboxDashBoardController.viewState, (viewState) {
final reactionState = viewState.getOrElse(() => UIState.idle);
if (reactionState is MarkAsEmailReadSuccess) {
_handleMarkEmailsAsReadOrUnread(
affectedMailboxId: reactionState.mailboxId,
readCount: reactionState.readActions == ReadActions.markAsRead
? 1
: null,
unreadCount: reactionState.readActions == ReadActions.markAsUnread
? 1
: null,
);
} else if (reactionState is MarkAsMultipleEmailReadAllSuccess) {
_handleMarkEmailsAsReadOrUnread(
affectedMailboxId: reactionState.mailboxId,
readCount: reactionState.readActions == ReadActions.markAsRead
? reactionState.emailIds.length
: null,
unreadCount: reactionState.readActions == ReadActions.markAsUnread
? reactionState.emailIds.length
: null,
);
} else if (reactionState is MarkAsMultipleEmailReadHasSomeEmailFailure) {
_handleMarkEmailsAsReadOrUnread(
affectedMailboxId: reactionState.mailboxId,
readCount: reactionState.readActions == ReadActions.markAsRead
? reactionState.successEmailIds.length
: null,
unreadCount: reactionState.readActions == ReadActions.markAsUnread
? reactionState.successEmailIds.length
: null,
);
}
});
}

void _handleMarkEmailsAsReadOrUnread({
required MailboxId? affectedMailboxId,
int? readCount,
int? unreadCount,
}) {
if (affectedMailboxId == null) return;

updateUnreadCountOfMailboxById(
affectedMailboxId,
readCount: readCount,
unreadCount: unreadCount,
);
}

void _initWebSocketQueueHandler() {
Expand Down
13 changes: 13 additions & 0 deletions lib/features/mailbox/presentation/model/mailbox_tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:collection';

import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart';
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:model/mailbox/expand_mode.dart';
import 'package:model/mailbox/presentation_mailbox.dart';
Expand Down Expand Up @@ -90,6 +91,18 @@ class MailboxTree with EquatableMixin {
}
}

void updateMailboxUnreadCountById(MailboxId mailboxId, int unreadCount) {
final matchedNode = findNode((node) => node.item.id == mailboxId);
if (matchedNode != null) {
final currentUnreadCount = matchedNode.item.unreadEmails?.value.value ?? 0;
final updatedUnreadCount = currentUnreadCount + unreadCount;
if (updatedUnreadCount < 0) return;
matchedNode.item = matchedNode.item.copyWith(
unreadEmails: UnreadEmails(UnsignedInt(updatedUnreadCount)),
);
}
}

String? getNodePath(MailboxId mailboxId) {
final matchedNode = findNode((node) => node.item.id == mailboxId);
if (matchedNode == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,7 @@ class MailboxDashBoardController extends ReloadableController
EmailId emailId,
ReadActions readActions,
MarkReadAction markReadAction,
MailboxId? mailboxId,
) {
if (accountId.value != null && sessionCurrent != null) {
consumeState(_markAsEmailReadInteractor.execute(
Expand All @@ -889,6 +890,7 @@ class MailboxDashBoardController extends ReloadableController
emailId,
readActions,
markReadAction,
mailboxId,
));
}
}
Expand Down Expand Up @@ -920,6 +922,7 @@ class MailboxDashBoardController extends ReloadableController
accountId.value!,
listEmailNeedMarkAsRead.listEmailIds,
readActions,
listPresentationEmail.firstOrNull?.mailboxContain?.mailboxId,
));
}
}
Expand Down Expand Up @@ -957,7 +960,7 @@ class MailboxDashBoardController extends ReloadableController
message,
actionName: AppLocalizations.of(currentContext!).undo,
onActionClick: () {
markAsEmailRead(success.emailId, undoAction, MarkReadAction.undo);
markAsEmailRead(success.emailId, undoAction, MarkReadAction.undo, success.mailboxId);
},
leadingSVGIcon: imagePaths.icToastSuccessMessage,
backgroundColor: AppColor.toastSuccessBackgroundColor,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.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/read_actions.dart';

class LoadingMarkAsMultipleEmailReadAll extends UIState {}

class MarkAsMultipleEmailReadAllSuccess extends UIState {
final int countMarkAsReadSuccess;
final List<EmailId> emailIds;
final ReadActions readActions;
final MailboxId? mailboxId;

MarkAsMultipleEmailReadAllSuccess(
this.countMarkAsReadSuccess,
this.readActions,
this.emailIds,
this.readActions,
this.mailboxId,
);

@override
List<Object?> get props => [countMarkAsReadSuccess, readActions];
List<Object?> get props => [emailIds, readActions, mailboxId];
}

class MarkAsMultipleEmailReadAllFailure extends FeatureFailure {
Expand All @@ -27,16 +31,18 @@ class MarkAsMultipleEmailReadAllFailure extends FeatureFailure {
}

class MarkAsMultipleEmailReadHasSomeEmailFailure extends UIState {
final int countMarkAsReadSuccess;
final List<EmailId> successEmailIds;
final ReadActions readActions;
final MailboxId? mailboxId;

MarkAsMultipleEmailReadHasSomeEmailFailure(
this.countMarkAsReadSuccess,
this.successEmailIds,
this.readActions,
this.mailboxId,
);

@override
List<Object?> get props => [countMarkAsReadSuccess, readActions];
List<Object?> get props => [successEmailIds, readActions, mailboxId];
}

class MarkAsMultipleEmailReadFailure extends FeatureFailure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:model/model.dart';
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
import 'package:tmail_ui_user/features/thread/domain/state/mark_as_multiple_email_read_state.dart';
Expand All @@ -17,7 +18,8 @@ class MarkAsMultipleEmailReadInteractor {
Session session,
AccountId accountId,
List<EmailId> emailIds,
ReadActions readAction
ReadActions readAction,
MailboxId? mailboxId,
) async* {
try {
yield Right(LoadingMarkAsMultipleEmailReadAll());
Expand All @@ -31,15 +33,17 @@ class MarkAsMultipleEmailReadInteractor {

if (emailIds.length == result.length) {
yield Right(MarkAsMultipleEmailReadAllSuccess(
result.length,
readAction,
result,
readAction,
mailboxId,
));
} else if (result.isEmpty) {
yield Left(MarkAsMultipleEmailReadAllFailure(readAction));
} else {
yield Right(MarkAsMultipleEmailReadHasSomeEmailFailure(
result.length,
readAction,
result,
readAction,
mailboxId,
));
}
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ mixin EmailActionController {
presentationEmail.id!,
readActions,
markReadAction,
presentationEmail.mailboxContain?.mailboxId,
);
}

Expand Down

0 comments on commit fe4276f

Please sign in to comment.