Skip to content

Commit

Permalink
TF-3150 Remove Cancel button and add timeout for saving & sending email
Browse files Browse the repository at this point in the history
  • Loading branch information
dab246 committed Oct 25, 2024
1 parent cce2e45 commit 030a01b
Show file tree
Hide file tree
Showing 19 changed files with 440 additions and 166 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class SendingEmailCanceledException implements Exception {}

class SavingEmailToDraftsCanceledException implements Exception {}
class SavingEmailToDraftsCanceledException implements Exception {}

class SendingEmailTimeoutException implements Exception {}

class SavingEmailToDraftsTimeoutException implements Exception {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:core/utils/app_logger.dart';
Expand Down Expand Up @@ -32,6 +34,7 @@ class CreateNewAndSaveEmailToDraftsInteractor {
Stream<dartz.Either<Failure, Success>> execute({
required CreateEmailRequest createEmailRequest,
CancelToken? cancelToken,
Duration? timeout,
}) async* {
try {
yield dartz.Right<Failure, Success>(GenerateEmailLoading());
Expand All @@ -43,45 +46,47 @@ class CreateNewAndSaveEmailToDraftsInteractor {

final emailCreated = await _createEmailObject(createEmailRequest);

if (emailCreated != null) {
if (createEmailRequest.draftsEmailId == null) {
yield dartz.Right<Failure, Success>(SaveEmailAsDraftsLoading());

final emailDraftSaved = await _emailRepository.saveEmailAsDrafts(
createEmailRequest.session,
createEmailRequest.accountId,
emailCreated,
cancelToken: cancelToken
);

yield dartz.Right<Failure, Success>(
SaveEmailAsDraftsSuccess(
emailDraftSaved.id!,
currentMailboxState: listCurrentState?.value1,
currentEmailState: listCurrentState?.value2
)
);
} else {
yield dartz.Right<Failure, Success>(UpdatingEmailDrafts());

final emailDraftSaved = await _emailRepository.updateEmailDrafts(
createEmailRequest.session,
createEmailRequest.accountId,
emailCreated,
createEmailRequest.draftsEmailId!,
cancelToken: cancelToken
);

yield dartz.Right<Failure, Success>(
UpdateEmailDraftsSuccess(
emailDraftSaved.id!,
currentMailboxState: listCurrentState?.value1,
currentEmailState: listCurrentState?.value2
)
);
}
} else {
if (emailCreated == null) {
yield dartz.Left<Failure, Success>(GenerateEmailFailure(CannotCreateEmailObjectException()));
return;
}

if (createEmailRequest.draftsEmailId == null) {
yield dartz.Right<Failure, Success>(SaveEmailAsDraftsLoading());

final emailDraftSaved = await _emailRepository.saveEmailAsDrafts(
createEmailRequest.session,
createEmailRequest.accountId,
emailCreated,
cancelToken: cancelToken,
timeout: timeout,
);

yield dartz.Right<Failure, Success>(
SaveEmailAsDraftsSuccess(
emailDraftSaved.id!,
currentMailboxState: listCurrentState?.value1,
currentEmailState: listCurrentState?.value2
)
);
} else {
yield dartz.Right<Failure, Success>(UpdatingEmailDrafts());

final emailDraftSaved = await _emailRepository.updateEmailDrafts(
createEmailRequest.session,
createEmailRequest.accountId,
emailCreated,
createEmailRequest.draftsEmailId!,
cancelToken: cancelToken
);

yield dartz.Right<Failure, Success>(
UpdateEmailDraftsSuccess(
emailDraftSaved.id!,
currentMailboxState: listCurrentState?.value1,
currentEmailState: listCurrentState?.value2
)
);
}
} catch (e) {
logError('CreateNewAndSaveEmailToDraftsInteractor::execute: Exception: $e');
Expand All @@ -91,6 +96,10 @@ class CreateNewAndSaveEmailToDraftsInteractor {
} else {
yield dartz.Left<Failure, Success>(UpdateEmailDraftsFailure(SavingEmailToDraftsCanceledException()));
}
} else if (e is TimeoutException) {
yield dartz.Left<Failure, Success>(createEmailRequest.draftsEmailId == null
? SaveEmailAsDraftsFailure(SavingEmailToDraftsTimeoutException())
: UpdateEmailDraftsFailure(SavingEmailToDraftsTimeoutException()));
} else {
if (createEmailRequest.draftsEmailId == null) {
yield dartz.Left<Failure, Success>(SaveEmailAsDraftsFailure(e));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:core/utils/app_logger.dart';
Expand Down Expand Up @@ -32,7 +34,8 @@ class CreateNewAndSendEmailInteractor {

Stream<dartz.Either<Failure, Success>> execute({
required CreateEmailRequest createEmailRequest,
CancelToken? cancelToken
CancelToken? cancelToken,
Duration? timeout,
}) async* {
SendingEmailArguments? sendingEmailArguments;
try {
Expand All @@ -45,36 +48,37 @@ class CreateNewAndSendEmailInteractor {

sendingEmailArguments = await _createEmailObject(createEmailRequest);

if (sendingEmailArguments != null) {
yield dartz.Right<Failure, Success>(SendEmailLoading());
if (sendingEmailArguments == null) {
yield dartz.Left<Failure, Success>(GenerateEmailFailure(CannotCreateEmailObjectException()));
return;
}

await _emailRepository.sendEmail(
sendingEmailArguments.session,
sendingEmailArguments.accountId,
sendingEmailArguments.emailRequest,
mailboxRequest: sendingEmailArguments.mailboxRequest,
cancelToken: cancelToken
);
yield dartz.Right<Failure, Success>(SendEmailLoading());

if (sendingEmailArguments.emailRequest.emailIdDestroyed != null) {
await _deleteOldDraftsEmail(
session: sendingEmailArguments.session,
accountId: sendingEmailArguments.accountId,
draftEmailId: sendingEmailArguments.emailRequest.emailIdDestroyed!,
cancelToken: cancelToken
);
}

yield dartz.Right<Failure, Success>(
SendEmailSuccess(
currentMailboxState: listCurrentState?.value1,
currentEmailState: listCurrentState?.value2,
emailRequest: sendingEmailArguments.emailRequest
)
await _emailRepository.sendEmail(
sendingEmailArguments.session,
sendingEmailArguments.accountId,
sendingEmailArguments.emailRequest,
mailboxRequest: sendingEmailArguments.mailboxRequest,
cancelToken: cancelToken,
timeout: timeout,
);

if (sendingEmailArguments.emailRequest.emailIdDestroyed != null) {
await _deleteOldDraftsEmail(
session: sendingEmailArguments.session,
accountId: sendingEmailArguments.accountId,
draftEmailId: sendingEmailArguments.emailRequest.emailIdDestroyed!,
);
} else {
yield dartz.Left<Failure, Success>(GenerateEmailFailure(CannotCreateEmailObjectException()));
}

yield dartz.Right<Failure, Success>(
SendEmailSuccess(
currentMailboxState: listCurrentState?.value1,
currentEmailState: listCurrentState?.value2,
emailRequest: sendingEmailArguments.emailRequest
)
);
} catch (e) {
logError('CreateNewAndSendEmailInteractor::execute: Exception: $e');
if (e is UnknownError && e.message is List<SendingEmailCanceledException>) {
Expand All @@ -85,6 +89,14 @@ class CreateNewAndSendEmailInteractor {
emailRequest: sendingEmailArguments?.emailRequest,
mailboxRequest: sendingEmailArguments?.mailboxRequest,
));
} else if (e is TimeoutException) {
yield dartz.Left<Failure, Success>(SendEmailFailure(
exception: SendingEmailTimeoutException(),
session: sendingEmailArguments?.session,
accountId: sendingEmailArguments?.accountId,
emailRequest: sendingEmailArguments?.emailRequest,
mailboxRequest: sendingEmailArguments?.mailboxRequest,
));
} else {
yield dartz.Left<Failure, Success>(SendEmailFailure(
exception: e,
Expand Down Expand Up @@ -132,14 +144,12 @@ class CreateNewAndSendEmailInteractor {
required Session session,
required AccountId accountId,
required EmailId draftEmailId,
CancelToken? cancelToken
}) async {
try {
await _emailRepository.deleteEmailPermanently(
session,
accountId,
draftEmailId,
cancelToken: cancelToken
);
} catch (e) {
logError('CreateNewAndSendEmailInteractor::_deleteOldDraftsEmail: Exception: $e');
Expand Down
Loading

0 comments on commit 030a01b

Please sign in to comment.