Skip to content

Commit

Permalink
TF-3385 Update save and remove draft email
Browse files Browse the repository at this point in the history
  • Loading branch information
tddang-linagora committed Jan 2, 2025
1 parent 86e7316 commit 032e79d
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 9 deletions.
25 changes: 25 additions & 0 deletions lib/features/base/base_mailbox_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -655,4 +655,29 @@ abstract class BaseMailboxController extends BaseController {
return;
}
}

void updateMailboxTotalEmailsCountById(MailboxId mailboxId, int totalEmails) {
MailboxNode? selectedNode;

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

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

selectedNode = teamMailboxesTree.value.findNode((node) => node.item.id == mailboxId);
if (selectedNode != null) {
teamMailboxesTree.value.updateMailboxTotalEmailsCountById(mailboxId, totalEmails);
teamMailboxesTree.refresh();
return;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
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';

class SaveEmailAsDraftsLoading extends LoadingState {}

class SaveEmailAsDraftsSuccess extends UIState {

final EmailId emailId;
final MailboxId? draftMailboxId;

SaveEmailAsDraftsSuccess(this.emailId);
SaveEmailAsDraftsSuccess(this.emailId, this.draftMailboxId);

@override
List<Object?> get props => [emailId, ...super.props];
List<Object?> get props => [emailId, draftMailboxId, ...super.props];
}

class SaveEmailAsDraftsFailure extends FeatureFailure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ class CreateNewAndSaveEmailToDraftsInteractor {
);

yield dartz.Right<Failure, Success>(
SaveEmailAsDraftsSuccess(emailDraftSaved.id!)
SaveEmailAsDraftsSuccess(
emailDraftSaved.id!,
createEmailRequest.draftsMailboxId,
)
);
} else {
yield dartz.Right<Failure, Success>(UpdatingEmailDrafts());
Expand Down
24 changes: 24 additions & 0 deletions lib/features/mailbox/presentation/mailbox_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:rxdart/transformers.dart';
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/composer/domain/state/save_email_as_drafts_state.dart';
import 'package:tmail_ui_user/features/email/domain/model/move_action.dart';
import 'package:tmail_ui_user/features/email/domain/state/get_restored_deleted_message_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/mark_as_email_read_state.dart';
Expand Down Expand Up @@ -66,6 +67,7 @@ import 'package:tmail_ui_user/features/mailbox/presentation/utils/mailbox_utils.
import 'package:tmail_ui_user/features/mailbox_creator/domain/usecases/verify_name_interactor.dart';
import 'package:tmail_ui_user/features/mailbox_creator/presentation/model/mailbox_creator_arguments.dart';
import 'package:tmail_ui_user/features/mailbox_creator/presentation/model/new_mailbox_arguments.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/state/remove_email_drafts_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/mailbox_dashboard_controller.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/model/dashboard_routes.dart';
Expand Down Expand Up @@ -310,6 +312,16 @@ class MailboxController extends BaseMailboxController
?.value
.toInt() ?? 0,
);
} else if (reactionState is SaveEmailAsDraftsSuccess) {
_handleDraftSaved(
affectedMailboxId: reactionState.draftMailboxId,
totalEmailsChanged: 1,
);
} else if (reactionState is RemoveEmailDraftsSuccess) {
_handleDraftSaved(
affectedMailboxId: reactionState.draftMailboxId,
totalEmailsChanged: -1,
);
}
});
}
Expand All @@ -336,6 +348,18 @@ class MailboxController extends BaseMailboxController
clearUnreadCount(affectedMailboxId);
}

void _handleDraftSaved({
required MailboxId? affectedMailboxId,
required int totalEmailsChanged,
}) {
if (affectedMailboxId == null) return;

updateMailboxTotalEmailsCountById(
affectedMailboxId,
totalEmailsChanged,
);
}

void _initWebSocketQueueHandler() {
_webSocketQueueHandler = WebSocketQueueHandler(
processMessageCallback: _handleWebSocketMessage,
Expand Down
12 changes: 12 additions & 0 deletions lib/features/mailbox/presentation/model/mailbox_tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ class MailboxTree with EquatableMixin {
}
}

void updateMailboxTotalEmailsCountById(MailboxId mailboxId, int totalEmailsCount) {
final matchedNode = findNode((node) => node.item.id == mailboxId);
if (matchedNode != null) {
final currentTotalEmailsCount = matchedNode.item.totalEmails?.value.value ?? 0;
final updatedTotalEmailsCount = currentTotalEmailsCount + totalEmailsCount;
if (updatedTotalEmailsCount < 0) return;
matchedNode.item = matchedNode.item.copyWith(
totalEmails: TotalEmails(UnsignedInt(updatedTotalEmailsCount)),
);
}
}

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
@@ -1,7 +1,15 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';

class RemoveEmailDraftsSuccess extends UIState {}
class RemoveEmailDraftsSuccess extends UIState {
final MailboxId? draftMailboxId;

RemoveEmailDraftsSuccess(this.draftMailboxId);

@override
List<Object?> get props => [draftMailboxId];
}

class RemoveEmailDraftsFailure 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:tmail_ui_user/features/email/domain/repository/email_repository.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/state/remove_email_drafts_state.dart';

Expand All @@ -12,11 +13,16 @@ class RemoveEmailDraftsInteractor {

RemoveEmailDraftsInteractor(this._emailRepository);

Stream<Either<Failure, Success>> execute(Session session, AccountId accountId, EmailId emailId) async* {
Stream<Either<Failure, Success>> execute(
Session session,
AccountId accountId,
EmailId emailId,
MailboxId? draftMailboxId,
) async* {
try {
final result = await _emailRepository.removeEmailDrafts(session, accountId, emailId);
if (result) {
yield Right<Failure, Success>(RemoveEmailDraftsSuccess());
yield Right<Failure, Success>(RemoveEmailDraftsSuccess(draftMailboxId));
} else {
yield Left<Failure, Success>(RemoveEmailDraftsFailure(result));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ class MailboxDashBoardController extends ReloadableController
currentOverlayContext!,
AppLocalizations.of(currentContext!).drafts_saved,
actionName: AppLocalizations.of(currentContext!).discard,
onActionClick: () => _discardEmail(success.emailId),
onActionClick: () => _discardEmail(success.emailId, success.draftMailboxId),
leadingSVGIcon: imagePaths.icMailboxDrafts,
leadingSVGIconColor: Colors.white,
backgroundColor: AppColor.toastSuccessBackgroundColor,
Expand Down Expand Up @@ -851,11 +851,16 @@ class MailboxDashBoardController extends ReloadableController
}
}

void _discardEmail(EmailId emailId) {
void _discardEmail(EmailId emailId, MailboxId? draftMailboxId) {
final currentAccountId = accountId.value;
final session = sessionCurrent;
if (currentAccountId != null && session != null) {
consumeState(_removeEmailDraftsInteractor.execute(session, currentAccountId, emailId));
consumeState(_removeEmailDraftsInteractor.execute(
session,
currentAccountId,
emailId,
draftMailboxId,
));
}
}

Expand Down

0 comments on commit 032e79d

Please sign in to comment.