Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TF-3385 Fix loading forever when empty mailbox with less than 20 items #3402

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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();
}
});
}
Expand All @@ -394,6 +400,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
Loading