TF-675 Fix mark as read multiple mailbox on web

This commit is contained in:
dab246
2022-07-06 00:36:59 +07:00
committed by Dat H. Pham
parent 737555eb14
commit 68444a0f10
@@ -3,6 +3,7 @@ import 'dart:async';
import 'package:core/presentation/state/failure.dart'; import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart'; import 'package:core/presentation/state/success.dart';
import 'package:core/utils/app_logger.dart'; import 'package:core/utils/app_logger.dart';
import 'package:core/utils/build_utils.dart';
import 'package:dartz/dartz.dart'; import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/properties/properties.dart'; import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
@@ -38,26 +39,31 @@ class MailboxIsolateWorker {
int totalEmailUnread, int totalEmailUnread,
StreamController<Either<Failure, Success>> onProgressController StreamController<Either<Failure, Success>> onProgressController
) async { ) async {
final result = await _isolateExecutor.execute( if (BuildUtils.isWeb) {
arg1: MailboxMarkAsReadArguments( return _handleMarkAsMailboxReadActionOnWeb(
_threadApi, accountId,
_emailApi, mailboxId,
accountId, totalEmailUnread,
mailboxId), onProgressController);
fun1: _handleMarkAsMailboxReadAction, } else {
notification: (value) { final result = await _isolateExecutor.execute(
if (value is List<Email>) { arg1: MailboxMarkAsReadArguments(
log('MailboxIsolateWorker::markAsMailboxRead(): onUpdateProgress: COUNT ${value.length}'); _threadApi,
log('MailboxIsolateWorker::markAsMailboxRead(): onUpdateProgress: TOTAL $totalEmailUnread'); _emailApi,
log('MailboxIsolateWorker::markAsMailboxRead(): onUpdateProgress: PERCENT ${value.length / totalEmailUnread}'); accountId,
log('MailboxIsolateWorker::markAsMailboxRead(): onUpdateProgress: ${((value.length / totalEmailUnread) * 100).toInt()}%'); mailboxId),
onProgressController.add(Right(UpdatingMarkAsMailboxReadState( fun1: _handleMarkAsMailboxReadAction,
mailboxId: mailboxId, notification: (value) {
totalUnread: totalEmailUnread, if (value is List<Email>) {
countRead: value.length))); log('MailboxIsolateWorker::markAsMailboxRead(): onUpdateProgress: PERCENT ${value.length / totalEmailUnread}');
} onProgressController.add(Right(UpdatingMarkAsMailboxReadState(
}); mailboxId: mailboxId,
return result; totalUnread: totalEmailUnread,
countRead: value.length)));
}
});
return result;
}
} }
static Future<List<Email>> _handleMarkAsMailboxReadAction( static Future<List<Email>> _handleMarkAsMailboxReadAction(
@@ -65,7 +71,6 @@ class MailboxIsolateWorker {
TypeSendPort sendPort TypeSendPort sendPort
) async { ) async {
List<Email> emailListCompleted = List.empty(growable: true); List<Email> emailListCompleted = List.empty(growable: true);
try { try {
bool mailboxHasEmails = true; bool mailboxHasEmails = true;
UTCDate? lastReceivedDate; UTCDate? lastReceivedDate;
@@ -122,4 +127,69 @@ class MailboxIsolateWorker {
log('MailboxIsolateWorker::_handleMarkAsMailboxRead(): TOTAL_READ: ${emailListCompleted.length}'); log('MailboxIsolateWorker::_handleMarkAsMailboxRead(): TOTAL_READ: ${emailListCompleted.length}');
return emailListCompleted; return emailListCompleted;
} }
Future<List<Email>> _handleMarkAsMailboxReadActionOnWeb(
AccountId accountId,
MailboxId mailboxId,
int totalEmailUnread,
StreamController<Either<Failure, Success>> onProgressController
) async {
List<Email> emailListCompleted = List.empty(growable: true);
try {
bool mailboxHasEmails = true;
UTCDate? lastReceivedDate;
EmailId? lastEmailId;
while (mailboxHasEmails) {
final emailResponse = await _threadApi
.getAllEmail(accountId,
limit: UnsignedInt(30),
filter: EmailFilterCondition(
inMailbox: mailboxId,
notKeyword: KeyWordIdentifier.emailSeen.value,
before: lastReceivedDate),
sort: <Comparator>{}..add(
EmailComparator(EmailComparatorProperty.receivedAt)
..setIsAscending(false)),
properties: Properties({
EmailProperty.id,
EmailProperty.keywords,
EmailProperty.receivedAt,
}))
.then((response) {
var listEmails = response.emailList;
if (listEmails != null && listEmails.isNotEmpty && lastEmailId != null) {
listEmails = listEmails
.where((email) => email.id != lastEmailId)
.toList();
}
return EmailsResponse(emailList: listEmails, state: response.state);
});
final listEmailUnread = emailResponse.emailList;
log('MailboxIsolateWorker::_handleMarkAsMailboxReadActionOnWeb(): listEmailUnread: ${listEmailUnread?.length}');
if (listEmailUnread == null || listEmailUnread.isEmpty) {
mailboxHasEmails = false;
} else {
lastEmailId = listEmailUnread.last.id;
lastReceivedDate = listEmailUnread.last.receivedAt;
final result = await _emailApi.markAsRead(
accountId, listEmailUnread, ReadActions.markAsRead);
log('MailboxIsolateWorker::_handleMarkAsMailboxReadActionOnWeb(): MARK_READ: ${result.length}');
emailListCompleted.addAll(result);
onProgressController.add(Right(UpdatingMarkAsMailboxReadState(
mailboxId: mailboxId,
totalUnread: totalEmailUnread,
countRead: emailListCompleted.length)));
}
}
} catch (e) {
log('MailboxIsolateWorker::_handleMarkAsMailboxReadActionOnWeb(): ERROR: $e');
}
log('MailboxIsolateWorker::_handleMarkAsMailboxReadActionOnWeb(): TOTAL_READ: ${emailListCompleted.length}');
return emailListCompleted;
}
} }