TF-695: implement isolate for empty trash
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/filter/filter.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
|
||||
@@ -9,6 +13,7 @@ import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/model/email_change_response.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/model/email_response.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/model/filter_message_option.dart';
|
||||
import 'package:dartz/dartz.dart' as dartz;
|
||||
|
||||
abstract class ThreadDataSource {
|
||||
Future<EmailsResponse> getAllEmail(
|
||||
@@ -33,4 +38,11 @@ abstract class ThreadDataSource {
|
||||
Future<List<Email>> getAllEmailCache({MailboxId? inMailboxId, Set<Comparator>? sort, FilterMessageOption? filterOption});
|
||||
|
||||
Future<void> update({List<Email>? updated, List<Email>? created, List<EmailId>? destroyed});
|
||||
|
||||
Future<List<EmailId>> emptyTrashFolder(
|
||||
AccountId accountId,
|
||||
MailboxId mailboxId,
|
||||
Future<void> Function(State state) updateState,
|
||||
Future<void> Function(List<EmailId>? newDestroyed) updateDestroyedEmailCache,
|
||||
);
|
||||
}
|
||||
@@ -1,3 +1,8 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:dartz/dartz.dart' as dartz;
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/filter/filter.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
|
||||
@@ -60,4 +65,9 @@ class LocalThreadDataSourceImpl extends ThreadDataSource {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<EmailId>> emptyTrashFolder(AccountId accountId, MailboxId mailboxId, Future<void> Function(State state) updateState, Future<void> Function(List<EmailId>? newDestroyed) updateDestroyedEmailCache) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,8 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:dartz/dartz.dart' as dartz;
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/filter/filter.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
|
||||
@@ -8,6 +13,7 @@ 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/thread/data/datasource/thread_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/model/email_change_response.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/network/thread_isolate_worker.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/model/email_response.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/model/filter_message_option.dart';
|
||||
@@ -15,8 +21,9 @@ import 'package:tmail_ui_user/features/thread/domain/model/filter_message_option
|
||||
class ThreadDataSourceImpl extends ThreadDataSource {
|
||||
|
||||
final ThreadAPI threadAPI;
|
||||
final ThreadIsolateWorker _threadIsolateWorker;
|
||||
|
||||
ThreadDataSourceImpl(this.threadAPI);
|
||||
ThreadDataSourceImpl(this.threadAPI, this._threadIsolateWorker);
|
||||
|
||||
@override
|
||||
Future<EmailsResponse> getAllEmail(
|
||||
@@ -69,4 +76,18 @@ class ThreadDataSourceImpl extends ThreadDataSource {
|
||||
Future<void> update({List<Email>? updated, List<Email>? created, List<EmailId>? destroyed}) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<EmailId>> emptyTrashFolder(AccountId accountId, MailboxId mailboxId, Future<void> Function(State state) updateState, Future<void> Function(List<EmailId>? newDestroyed) updateDestroyedEmailCache) {
|
||||
return Future.sync(() async {
|
||||
return await _threadIsolateWorker.emptyTrashFolder(
|
||||
accountId,
|
||||
mailboxId,
|
||||
updateState,
|
||||
updateDestroyedEmailCache,
|
||||
);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -9,17 +9,10 @@ import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart';
|
||||
class EmptyTrashFolderArguments with EquatableMixin {
|
||||
AccountId accountId;
|
||||
MailboxId trashMailboxId;
|
||||
Future<void> Function(State state) updateState;
|
||||
Future<void> Function({
|
||||
List<Email>? newUpdated,
|
||||
List<Email>? newCreated,
|
||||
List<EmailId>? newDestroyed,
|
||||
}) updateEmailCache;
|
||||
final ThreadAPI threadAPI;
|
||||
final EmailAPI emailAPI;
|
||||
|
||||
EmptyTrashFolderArguments(this.threadAPI, this.emailAPI, this.accountId,
|
||||
this.trashMailboxId, this.updateEmailCache, this.updateState);
|
||||
EmptyTrashFolderArguments(this.threadAPI, this.emailAPI, this.accountId, this.trashMailboxId);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [accountId, trashMailboxId];
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:core/utils/build_utils.dart';
|
||||
import 'package:dartz/dartz.dart' as dartz;
|
||||
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/state.dart';
|
||||
@@ -18,7 +15,6 @@ import 'package:model/email/email_property.dart';
|
||||
import 'package:tmail_ui_user/features/email/data/network/email_api.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/model/empty_trash_folder_arguments.dart';
|
||||
import 'package:tmail_ui_user/features/thread/data/network/thread_api.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/state/empty_trash_folder_state.dart';
|
||||
import 'package:worker_manager/worker_manager.dart';
|
||||
|
||||
class ThreadIsolateWorker {
|
||||
@@ -31,28 +27,22 @@ class ThreadIsolateWorker {
|
||||
Future<List<EmailId>> emptyTrashFolder(
|
||||
AccountId accountId,
|
||||
MailboxId mailboxId,
|
||||
int totalEmailUnread,
|
||||
Future<void> Function(State state) updateState,
|
||||
Future<void> Function({
|
||||
List<Email>? newUpdated,
|
||||
List<Email>? newCreated,
|
||||
List<EmailId>? newDestroyed,
|
||||
}) updateEmailCache,
|
||||
StreamController<dartz.Either<Failure, Success>> onProgressController,
|
||||
Future<void> Function(List<EmailId>? newDestroyed) updateDestroyedEmailCache,
|
||||
) async {
|
||||
if (BuildUtils.isWeb) {
|
||||
return _emptyTrashFolderOnWeb(accountId, mailboxId, updateState, updateEmailCache, onProgressController);
|
||||
return _emptyTrashFolderOnWeb(accountId, mailboxId, updateState, updateDestroyedEmailCache);
|
||||
} else {
|
||||
final result = await _isolateExecutor.execute(
|
||||
arg1: EmptyTrashFolderArguments(_threadAPI, _emailAPI, accountId, mailboxId, updateEmailCache, updateState),
|
||||
arg1: EmptyTrashFolderArguments(_threadAPI, _emailAPI, accountId, mailboxId),
|
||||
fun1: _emptyTrashFolderAction,
|
||||
notification: (value) {
|
||||
if (value is List<Email>) {
|
||||
log('ThreadIsolateWorker::markAsThreadRead(): onUpdateProgress: PERCENT ${value.length / totalEmailUnread}');
|
||||
onProgressController.add(dartz.Right(UpdatingEmptyTrashFolderState(
|
||||
mailboxId: mailboxId,
|
||||
countRemove: value.length,
|
||||
)));
|
||||
if(value is State) {
|
||||
updateState.call(value);
|
||||
}
|
||||
if (value is List<EmailId>) {
|
||||
updateDestroyedEmailCache.call(value);
|
||||
log('ThreadIsolateWorker::emptyTrashFolder(): onUpdateProgress: PERCENT ${value.length}');
|
||||
}
|
||||
});
|
||||
return result;
|
||||
@@ -75,7 +65,7 @@ class ThreadIsolateWorker {
|
||||
properties: Properties({EmailProperty.id}));
|
||||
|
||||
if (emailsResponse.state != null) {
|
||||
await args.updateState.call(emailsResponse.state!);
|
||||
sendPort.send(emailsResponse.state!);
|
||||
}
|
||||
|
||||
var newEmailList = emailsResponse.emailList ?? <Email>[];
|
||||
@@ -83,7 +73,7 @@ class ThreadIsolateWorker {
|
||||
newEmailList = newEmailList.where((email) => email.id != lastEmail!.id).toList();
|
||||
}
|
||||
|
||||
log('ThreadRepositoryImpl::emptyTrashFolder(): ${newEmailList.length}');
|
||||
log('ThreadIsolateWorker::_emptyTrashFolderAction(): ${newEmailList.length}');
|
||||
|
||||
if (newEmailList.isNotEmpty == true) {
|
||||
lastEmail = newEmailList.last;
|
||||
@@ -93,7 +83,7 @@ class ThreadIsolateWorker {
|
||||
final listEmailIdDeleted = await args.emailAPI.deleteMultipleEmailsPermanently(args.accountId, emailIds);
|
||||
|
||||
if (listEmailIdDeleted.isNotEmpty && listEmailIdDeleted.length == emailIds.length) {
|
||||
await args.updateEmailCache(newDestroyed: listEmailIdDeleted);
|
||||
sendPort.send(listEmailIdDeleted);
|
||||
}
|
||||
emailListCompleted.addAll(listEmailIdDeleted);
|
||||
|
||||
@@ -103,9 +93,9 @@ class ThreadIsolateWorker {
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
log('ThreadIsolateWorker::_emptyTrashFolderOnWeb(): ERROR: $e');
|
||||
log('ThreadIsolateWorker::_emptyTrashFolderAction(): ERROR: $e');
|
||||
}
|
||||
log('ThreadIsolateWorker::_emptyTrashFolderOnWeb(): TOTAL_REMOVE: ${emailListCompleted.length}');
|
||||
log('ThreadIsolateWorker::_emptyTrashFolderAction(): TOTAL_REMOVE: ${emailListCompleted.length}');
|
||||
return emailListCompleted;
|
||||
}
|
||||
|
||||
@@ -113,12 +103,7 @@ class ThreadIsolateWorker {
|
||||
AccountId accountId,
|
||||
MailboxId trashMailboxId,
|
||||
Future<void> Function(State state) updateState,
|
||||
Future<void> Function({
|
||||
List<Email>? newUpdated,
|
||||
List<Email>? newCreated,
|
||||
List<EmailId>? newDestroyed,
|
||||
}) updateEmailCache,
|
||||
StreamController<dartz.Either<Failure, Success>> onProgressController,
|
||||
Future<void> Function(List<EmailId> newDestroyed) updateDestroyedEmailCache,
|
||||
) async {
|
||||
List<EmailId> emailListCompleted = List.empty(growable: true);
|
||||
try {
|
||||
@@ -143,7 +128,7 @@ class ThreadIsolateWorker {
|
||||
newEmailList = newEmailList.where((email) => email.id != lastEmail!.id).toList();
|
||||
}
|
||||
|
||||
log('ThreadRepositoryImpl::emptyTrashFolder(): ${newEmailList.length}');
|
||||
log('ThreadIsolateWorker::_emptyTrashFolderOnWeb(): ${newEmailList.length}');
|
||||
|
||||
if (newEmailList.isNotEmpty == true) {
|
||||
lastEmail = newEmailList.last;
|
||||
@@ -153,13 +138,10 @@ class ThreadIsolateWorker {
|
||||
final listEmailIdDeleted = await _emailAPI.deleteMultipleEmailsPermanently(accountId, emailIds);
|
||||
|
||||
if (listEmailIdDeleted.isNotEmpty && listEmailIdDeleted.length == emailIds.length) {
|
||||
await updateEmailCache(newDestroyed: listEmailIdDeleted);
|
||||
await updateDestroyedEmailCache(listEmailIdDeleted);
|
||||
}
|
||||
emailListCompleted.addAll(listEmailIdDeleted);
|
||||
|
||||
onProgressController.add(dartz.Right(UpdatingEmptyTrashFolderState(
|
||||
mailboxId: trashMailboxId,
|
||||
countRemove: emailListCompleted.length)));
|
||||
} else {
|
||||
hasEmails = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user