TF-1897 Implement resend sending email when failed

(cherry picked from commit f508b65688503e9ae8ece5a1924e84481b98a39e)
This commit is contained in:
dab246
2023-06-10 19:20:30 +07:00
committed by Dat Vu
parent b6528b5bf5
commit c5bd77f800
16 changed files with 338 additions and 129 deletions
@@ -1,8 +1,8 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/utils/app_logger.dart';
import 'package:core/presentation/extensions/color_extension.dart';
import 'package:core/presentation/resources/image_paths.dart';
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:core/presentation/utils/app_toast.dart';
import 'package:flutter/material.dart';
@@ -17,9 +17,14 @@ import 'package:tmail_ui_user/features/caching/utils/cache_utils.dart';
import 'package:tmail_ui_user/features/email/presentation/model/composer_arguments.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart';
import 'package:tmail_ui_user/features/network_status_handle/presentation/network_connnection_controller.dart';
import 'package:tmail_ui_user/features/offline_mode/manager/sending_email_cache_manager.dart';
import 'package:tmail_ui_user/features/offline_mode/model/sending_state.dart';
import 'package:tmail_ui_user/features/sending_queue/domain/extensions/list_sending_email_extension.dart';
import 'package:tmail_ui_user/features/sending_queue/domain/extensions/sending_email_extension.dart';
import 'package:tmail_ui_user/features/sending_queue/domain/model/sending_email.dart';
import 'package:tmail_ui_user/features/sending_queue/domain/state/update_multiple_sending_email_state.dart';
import 'package:tmail_ui_user/features/sending_queue/domain/state/update_sending_email_state.dart';
import 'package:tmail_ui_user/features/sending_queue/domain/usecases/update_multiple_sending_email_interactor.dart';
import 'package:tmail_ui_user/features/sending_queue/domain/usecases/update_sending_email_interactor.dart';
import 'package:tmail_ui_user/features/sending_queue/presentation/utils/sending_queue_isolate_manager.dart';
import 'package:tmail_ui_user/features/offline_mode/controller/work_scheduler_controller.dart';
@@ -34,10 +39,12 @@ class SendingQueueController extends BaseController with MessageDialogActionMixi
final DeleteMultipleSendingEmailInteractor _deleteMultipleSendingEmailInteractor;
final UpdateSendingEmailInteractor _updateSendingEmailInteractor;
final UpdateMultipleSendingEmailInteractor _updateMultipleSendingEmailInteractor;
final dashboardController = getBinding<MailboxDashBoardController>();
final _networkConnectionController = getBinding<NetworkConnectionController>();
final _sendingQueueIsolateManager = getBinding<SendingQueueIsolateManager>();
final _sendingEmailCacheManager = getBinding<SendingEmailCacheManager>();
final _imagePaths = getBinding<ImagePaths>();
final _appToast = getBinding<AppToast>();
@@ -48,6 +55,7 @@ class SendingQueueController extends BaseController with MessageDialogActionMixi
SendingQueueController(
this._deleteMultipleSendingEmailInteractor,
this._updateSendingEmailInteractor,
this._updateMultipleSendingEmailInteractor,
);
@override
@@ -61,7 +69,7 @@ class SendingQueueController extends BaseController with MessageDialogActionMixi
);
}
void _handleSendingQueueEvent(Object? event) {
void _handleSendingQueueEvent(Object? event) async {
log('SendingQueueController::_handleSendingQueueEvent():event: $event');
try {
if (event is String) {
@@ -69,16 +77,20 @@ class SendingQueueController extends BaseController with MessageDialogActionMixi
final sendingId = tupleKey.parts[0];
final sendingState = SendingState.values.firstWhereOrNull((state) => state.name == tupleKey.parts[1]);
log('SendingQueueController::_handleSendingQueueEvent():sendingId: $sendingId | sendingState: $sendingState');
if (sendingState != null) {
await _sendingEmailCacheManager?.closeSendingEmailHiveCacheBox();
switch(sendingState) {
case SendingState.waiting:
_handleSendingStateRunningEvent(sendingId);
_updatingSendingStateToRunningAction(sendingId);
break;
case SendingState.running:
case SendingState.success:
refreshSendingQueue();
break;
case SendingState.error:
refreshSendingQueue(needToReopen: true);
await WorkSchedulerController().cancelByUniqueId(sendingId);
_updatingSendingStateToErrorAction(sendingId);
break;
}
}
@@ -88,11 +100,21 @@ class SendingQueueController extends BaseController with MessageDialogActionMixi
}
}
void _handleSendingStateRunningEvent(String sendingId) {
log('SendingQueueController::_handleSendingStateRunningEvent():sendingId: $sendingId');
void _updatingSendingStateToRunningAction(String sendingId) {
log('SendingQueueController::_updatingSendingStateToRunningAction():sendingId: $sendingId');
final matchedSendingEmail = dashboardController!.listSendingEmails.firstWhereOrNull((sendingEmail) => sendingEmail.sendingId == sendingId);
if (matchedSendingEmail != null) {
_updateSendingEmailAction(matchedSendingEmail);
final newSendingEmail = matchedSendingEmail.updatingSendingState(SendingState.running);
_updateSendingEmailAction(newSendingEmail);
}
}
void _updatingSendingStateToErrorAction(String sendingId) {
log('SendingQueueController::_updatingSendingStateToErrorAction():sendingId: $sendingId');
final matchedSendingEmail = dashboardController!.listSendingEmails.firstWhereOrNull((sendingEmail) => sendingEmail.sendingId == sendingId);
if (matchedSendingEmail != null) {
final newSendingEmail = matchedSendingEmail.updatingSendingState(SendingState.error);
_updateSendingEmailAction(newSendingEmail);
}
}
@@ -118,8 +140,8 @@ class SendingQueueController extends BaseController with MessageDialogActionMixi
bool get isConnectedNetwork => _networkConnectionController?.isNetworkConnectionAvailable() == true;
void refreshSendingQueue({bool needToReopen = false}) {
dashboardController!.getAllSendingEmails(needToReopen: needToReopen);
void refreshSendingQueue() {
dashboardController!.getAllSendingEmails();
}
void openMailboxMenu() {
@@ -148,6 +170,9 @@ class SendingQueueController extends BaseController with MessageDialogActionMixi
break;
case SendingEmailActionType.create:
break;
case SendingEmailActionType.resend:
_resendSendingEmailAction(listSendingEmails);
break;
}
}
@@ -185,32 +210,31 @@ class SendingQueueController extends BaseController with MessageDialogActionMixi
}
void _handleDeleteSendingEmail(List<SendingEmail> listSendingEmails) async {
disableSelectionMode();
final accountId = dashboardController!.accountId.value;
final session = dashboardController!.sessionCurrent;
if (accountId != null && session != null) {
final sendingIds = listSendingEmails.map((sendingEmail) => sendingEmail.sendingId).toSet().toList();
await Future.wait(
sendingIds.map((sendingId) => WorkSchedulerController().cancelByUniqueId(sendingId)),
eagerError: true
consumeState(
_deleteMultipleSendingEmailInteractor.execute(
accountId,
session.username,
listSendingEmails.sendingIds
)
);
consumeState(_deleteMultipleSendingEmailInteractor.execute(accountId, session.username, sendingIds));
}
}
void _handleDeleteSendingEmailSuccess() {
if (currentContext != null && currentOverlayContext != null) {
selectionState.value = SelectMode.INACTIVE;
void _handleDeleteSendingEmailSuccess(DeleteMultipleSendingEmailSuccess success) async {
await Future.wait(success.sendingIds.map(WorkSchedulerController().cancelByUniqueId));
if (currentContext != null && currentOverlayContext != null) {
_appToast!.showToastSuccessMessage(
currentOverlayContext!,
AppLocalizations.of(currentContext!).messageHaveBeenDeletedSuccessfully
);
refreshSendingQueue();
AppLocalizations.of(currentContext!).messageHaveBeenDeletedSuccessfully);
}
refreshSendingQueue();
}
void _editSendingEmailAction(SendingEmail sendingEmail) {
@@ -227,10 +251,6 @@ class SendingQueueController extends BaseController with MessageDialogActionMixi
dashboardController?.goToComposer(arguments);
}
void _handleDeleteSendingEmailFailure() {
selectionState.value = SelectMode.INACTIVE;
}
void _updateSendingEmailAction(SendingEmail newSendingEmail) {
final accountId = dashboardController!.accountId.value;
final session = dashboardController!.sessionCurrent;
@@ -238,29 +258,62 @@ class SendingQueueController extends BaseController with MessageDialogActionMixi
consumeState(_updateSendingEmailInteractor.execute(
accountId,
session.username,
newSendingEmail,
needToReopen: true
newSendingEmail
));
}
}
void _resendSendingEmailAction(List<SendingEmail> listSendingEmails) async {
disableSelectionMode();
final accountId = dashboardController!.accountId.value;
final session = dashboardController!.sessionCurrent;
if (accountId != null && session != null) {
consumeState(
_updateMultipleSendingEmailInteractor.execute(
accountId,
session.username,
listSendingEmails.toSendingStateWaiting()
)
);
}
}
void _handleResendSendingEmailSuccess(List<SendingEmail> newListSendingEmails) async {
await Future.forEach<SendingEmail>(newListSendingEmails, (sendingEmail) async {
await WorkSchedulerController().cancelByUniqueId(sendingEmail.sendingId);
dashboardController!.addSendingEmailToSendingQueue(sendingEmail);
});
if (currentContext != null && currentOverlayContext != null) {
_appToast!.showToastSuccessMessage(
currentOverlayContext!,
AppLocalizations.of(currentContext!).messagesHaveBeenResent);
}
refreshSendingQueue();
}
@override
void handleSuccessViewState(Success success) {
super.handleSuccessViewState(success);
if (success is DeleteMultipleSendingEmailAllSuccess ||
success is DeleteMultipleSendingEmailHasSomeSuccess) {
_handleDeleteSendingEmailSuccess();
if (success is DeleteMultipleSendingEmailSuccess) {
_handleDeleteSendingEmailSuccess(success);
} else if (success is UpdateSendingEmailSuccess) {
refreshSendingQueue(needToReopen: true);
refreshSendingQueue();
} else if (success is UpdateMultipleSendingEmailAllSuccess) {
_handleResendSendingEmailSuccess(success.newSendingEmails);
} else if (success is UpdateMultipleSendingEmailHasSomeSuccess) {
_handleResendSendingEmailSuccess(success.newSendingEmails);
}
}
@override
void handleFailureViewState(Failure failure) {
super.handleFailureViewState(failure);
if (failure is DeleteMultipleSendingEmailAllFailure ||
failure is DeleteMultipleSendingEmailFailure) {
_handleDeleteSendingEmailFailure();
if (failure is UpdateSendingEmailFailure) {
refreshSendingQueue();
}
}