Skip to content

Commit

Permalink
TF-3385 Fix loading forever when empty <20 items mailbox
Browse files Browse the repository at this point in the history
  • Loading branch information
tddang-linagora authored and hoangdat committed Jan 7, 2025
1 parent 9ceb56c commit 2b1e3b3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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<MailboxId,List<EmailId>> originalMailboxIdsWithEmailIds,
required MailboxId destinationMailboxId,
required MoveAction moveAction,
}) {
if (destinationMailboxId == selectedMailbox.value?.id) return;

final currentEmails = List<PresentationEmail>.from(
emailsInCurrentMailbox,
);
Expand All @@ -23,9 +24,18 @@ 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) {
int newTotalEmails = currentTotalEmails.value.value.toInt() - movedEmailIds.length;
if (newTotalEmails < 0) newTotalEmails = 0;
selectedMailbox.value = currentMailbox.copyWith(
totalEmails: TotalEmails(UnsignedInt(newTotalEmails)),
);
}
}
}
48 changes: 35 additions & 13 deletions lib/features/thread/presentation/thread_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -356,32 +356,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();
}
});
}
Expand All @@ -397,6 +403,22 @@ 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) {
int newTotalEmails = currentTotalEmails.value.value.toInt() - deletedEmailsCount;
if (newTotalEmails < 0) newTotalEmails = 0;
mailboxDashBoardController.selectedMailbox.value = currentMailbox.copyWith(
totalEmails: TotalEmails(UnsignedInt(newTotalEmails)),
);
}
}

void _checkIfCurrentMailboxCanLoadMore() {
final currentMailbox = mailboxDashBoardController.selectedMailbox.value;
if (currentMailbox == null) return;
Expand Down

0 comments on commit 2b1e3b3

Please sign in to comment.