TF-1190 Handle SetError when save email as drafts

This commit is contained in:
dab246
2023-02-03 11:29:04 +07:00
committed by Dat Vu
parent f41baf5250
commit e56e8fde27
10 changed files with 138 additions and 45 deletions
@@ -25,14 +25,13 @@ class SaveEmailAsDraftsInteractor {
final currentEmailState = listState.last;
final emailAsDrafts = await _emailRepository.saveEmailAsDrafts(accountId, email);
if (emailAsDrafts != null) {
yield Right<Failure, Success>(SaveEmailAsDraftsSuccess(
emailAsDrafts,
currentEmailState: currentEmailState,
currentMailboxState: currentMailboxState));
} else {
yield Left<Failure, Success>(SaveEmailAsDraftsFailure(null));
}
yield Right<Failure, Success>(
SaveEmailAsDraftsSuccess(
emailAsDrafts,
currentEmailState: currentEmailState,
currentMailboxState: currentMailboxState
)
);
} catch (e) {
yield Left<Failure, Success>(SaveEmailAsDraftsFailure(e));
}
@@ -50,7 +50,7 @@ abstract class EmailDataSource {
MarkStarAction markStarAction
);
Future<Email?> saveEmailAsDrafts(AccountId accountId, Email email);
Future<Email> saveEmailAsDrafts(AccountId accountId, Email email);
Future<bool> removeEmailDrafts(AccountId accountId, EmailId emailId);
@@ -98,7 +98,7 @@ class EmailDataSourceImpl extends EmailDataSource {
}
@override
Future<Email?> saveEmailAsDrafts(AccountId accountId, Email email) {
Future<Email> saveEmailAsDrafts(AccountId accountId, Email email) {
return Future.sync(() async {
return await emailAPI.saveEmailAsDrafts(accountId, email);
}).catchError((error) {
+13 -8
View File
@@ -432,7 +432,7 @@ class EmailAPI with HandleSetErrorMixin {
});
}
Future<Email?> saveEmailAsDrafts(AccountId accountId, Email email) async {
Future<Email> saveEmailAsDrafts(AccountId accountId, Email email) async {
final setEmailMethod = SetEmailMethod(accountId)
..addCreate(email.id.id, email);
@@ -446,14 +446,19 @@ class EmailAPI with HandleSetErrorMixin {
.execute();
final setEmailResponse = response.parse<SetEmailResponse>(
setEmailInvocation.methodCallId,
SetEmailResponse.deserialize);
setEmailInvocation.methodCallId,
SetEmailResponse.deserialize
);
return Future.sync(() async {
return setEmailResponse?.created?[email.id.id];
}).catchError((error) {
throw error;
});
final emailCreated = setEmailResponse?.created?[email.id.id];
final listEntriesErrors = _handleSetEmailResponse(response: setEmailResponse,);
final mapErrors = Map.fromEntries(listEntriesErrors);
if (emailCreated != null && mapErrors.isEmpty) {
return emailCreated;
} else {
throw SetEmailMethodException(mapErrors);
}
}
Future<bool> removeEmailDrafts(AccountId accountId, EmailId emailId) async {
@@ -104,7 +104,7 @@ class EmailRepositoryImpl extends EmailRepository {
}
@override
Future<Email?> saveEmailAsDrafts(AccountId accountId, Email email) {
Future<Email> saveEmailAsDrafts(AccountId accountId, Email email) {
return emailDataSource.saveEmailAsDrafts(accountId, email);
}
@@ -60,7 +60,7 @@ abstract class EmailRepository {
Future<List<EmailContent>> addTooltipWhenHoverOnLink(List<EmailContent> emailContents);
Future<Email?> saveEmailAsDrafts(AccountId accountId, Email email);
Future<Email> saveEmailAsDrafts(AccountId accountId, Email email);
Future<bool> removeEmailDrafts(AccountId accountId, EmailId emailId);
@@ -61,6 +61,7 @@ import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/download/download_controller.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/search_controller.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/spam_report_controller.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/set_error_extension.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/model/composer_overlay_state.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/model/dashboard_routes.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/model/download/download_task_state.dart';
@@ -277,9 +278,9 @@ class MailboxDashBoardController extends ReloadableController {
(failure) {
if (failure is SendEmailFailure) {
_handleSendEmailFailure(failure);
} else if (failure is SaveEmailAsDraftsFailure
|| failure is RemoveEmailDraftsFailure
|| failure is UpdateEmailDraftsFailure) {
} else if (failure is SaveEmailAsDraftsFailure) {
_handleSaveEmailAsDraftsFailure(failure);
} else if (failure is RemoveEmailDraftsFailure || failure is UpdateEmailDraftsFailure) {
clearState();
} else if (failure is MarkAsMailboxReadAllFailure ||
failure is MarkAsMailboxReadFailure) {
@@ -1555,22 +1556,23 @@ class MailboxDashBoardController extends ReloadableController {
final listErrors = exception.mapErrors.values.toList();
final toastSuccess = _handleSetErrors(listErrors);
if (!toastSuccess) {
_showToastSendMessageFailure(AppLocalizations.of(currentContext!).message_has_been_sent_failure);
_showToastSendMessageFailure(AppLocalizations.of(currentContext!).sendMessageFailure);
}
} else {
_showToastSendMessageFailure(AppLocalizations.of(currentContext!).message_has_been_sent_failure);
_showToastSendMessageFailure(AppLocalizations.of(currentContext!).sendMessageFailure);
}
clearState();
}
bool _handleSetErrors(List<SetError> listErrors) {
bool _handleSetErrors(List<SetError> listErrors, {bool isDrafts = false}) {
for (var error in listErrors) {
if (error.type == SetError.tooLarge) {
_showToastSendMessageFailure(AppLocalizations.of(currentContext!).sendMessageFailureWithSetErrorTypeTooLarge);
return true;
} else if (error.type == SetError.overQuota) {
_showToastSendMessageFailure(AppLocalizations.of(currentContext!).sendMessageFailureWithSetErrorTypeOverQuota);
if (error.type == SetError.tooLarge || error.type == SetError.overQuota) {
if (isDrafts) {
_showToastSendMessageFailure(error.toastMessageForSaveEmailAsDraftFailure(currentContext!));
} else {
_showToastSendMessageFailure(error.toastMessageForSendEmailFailure(currentContext!));
}
return true;
}
}
@@ -1587,6 +1589,27 @@ class MailboxDashBoardController extends ReloadableController {
);
}
}
void _handleSaveEmailAsDraftsFailure(SaveEmailAsDraftsFailure failure) {
logError('MailboxDashBoardController::_handleSaveEmailAsDraftsFailure():failure: $failure');
if (currentContext == null) {
clearState();
return;
}
final exception = failure.exception;
logError('MailboxDashBoardController::_handleSaveEmailAsDraftsFailure():exception: $exception');
if (exception is SetEmailMethodException) {
final listErrors = exception.mapErrors.values.toList();
final toastSuccess = _handleSetErrors(listErrors);
if (!toastSuccess) {
_showToastSendMessageFailure(AppLocalizations.of(currentContext!).saveEmailAsDraftFailure);
}
} else {
_showToastSendMessageFailure(AppLocalizations.of(currentContext!).saveEmailAsDraftFailure);
}
clearState();
}
@override
void onClose() {
@@ -0,0 +1,27 @@
import 'package:flutter/widgets.dart';
import 'package:jmap_dart_client/jmap/core/error/set_error.dart';
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
extension SetErrorExtension on SetError {
String toastMessageForSendEmailFailure(BuildContext context) {
if (type == SetError.tooLarge) {
return AppLocalizations.of(context).sendMessageFailureWithSetErrorTypeTooLarge;
} else if (type == SetError.overQuota) {
return AppLocalizations.of(context).sendMessageFailureWithSetErrorTypeOverQuota;
} else {
return AppLocalizations.of(context).sendMessageFailure;
}
}
String toastMessageForSaveEmailAsDraftFailure(BuildContext context) {
if (type == SetError.tooLarge) {
return AppLocalizations.of(context).saveEmailAsDraftFailureWithSetErrorTypeTooLarge;
} else if (type == SetError.overQuota) {
return AppLocalizations.of(context).saveEmailAsDraftFailureWithSetErrorTypeOverQuota;
} else {
return AppLocalizations.of(context).saveEmailAsDraftFailure;
}
}
}
+25 -7
View File
@@ -1,5 +1,5 @@
{
"@@last_modified": "2023-02-02T19:29:42.134695",
"@@last_modified": "2023-02-03T11:24:09.273116",
"initializing_data": "Initializing data...",
"@initializing_data": {
"type": "text",
@@ -588,12 +588,6 @@
"placeholders_order": [],
"placeholders": {}
},
"message_has_been_sent_failure": "Message has been sent failure",
"@message_has_been_sent_failure": {
"type": "text",
"placeholders_order": [],
"placeholders": {}
},
"done": "Done",
"@done": {
"type": "text",
@@ -2650,6 +2644,12 @@
"placeholders_order": [],
"placeholders": {}
},
"sendMessageFailure": "Failure to send your message.",
"@sendMessageFailure": {
"type": "text",
"placeholders_order": [],
"placeholders": {}
},
"sendMessageFailureWithSetErrorTypeTooLarge": "Failure to send your message, because it is too large.",
"@sendMessageFailureWithSetErrorTypeTooLarge": {
"type": "text",
@@ -2661,5 +2661,23 @@
"type": "text",
"placeholders_order": [],
"placeholders": {}
},
"saveEmailAsDraftFailure": "Failure to save your message as drafts.",
"@saveEmailAsDraftFailure": {
"type": "text",
"placeholders_order": [],
"placeholders": {}
},
"saveEmailAsDraftFailureWithSetErrorTypeTooLarge": "Failure to save your message as drafts, because it is too large.",
"@saveEmailAsDraftFailureWithSetErrorTypeTooLarge": {
"type": "text",
"placeholders_order": [],
"placeholders": {}
},
"saveEmailAsDraftFailureWithSetErrorTypeOverQuota": "Failure to save your message as drafts, because it is over quota.",
"@saveEmailAsDraftFailureWithSetErrorTypeOverQuota": {
"type": "text",
"placeholders_order": [],
"placeholders": {}
}
}
+28 -7
View File
@@ -602,13 +602,6 @@ class AppLocalizations {
);
}
String get message_has_been_sent_failure {
return Intl.message(
'Message has been sent failure',
name: 'message_has_been_sent_failure',
);
}
String get done {
return Intl.message(
'Done',
@@ -2720,6 +2713,13 @@ class AppLocalizations {
name: 'reduceSomeFiltersAndTryAgain');
}
String get sendMessageFailure {
return Intl.message(
'Failure to send your message.',
name: 'sendMessageFailure',
);
}
String get sendMessageFailureWithSetErrorTypeTooLarge {
return Intl.message(
'Failure to send your message, because it is too large.',
@@ -2733,4 +2733,25 @@ class AppLocalizations {
name: 'sendMessageFailureWithSetErrorTypeOverQuota',
);
}
String get saveEmailAsDraftFailure {
return Intl.message(
'Failure to save your message as drafts.',
name: 'saveEmailAsDraftFailure',
);
}
String get saveEmailAsDraftFailureWithSetErrorTypeTooLarge {
return Intl.message(
'Failure to save your message as drafts, because it is too large.',
name: 'saveEmailAsDraftFailureWithSetErrorTypeTooLarge',
);
}
String get saveEmailAsDraftFailureWithSetErrorTypeOverQuota {
return Intl.message(
'Failure to save your message as drafts, because it is over quota.',
name: 'saveEmailAsDraftFailureWithSetErrorTypeOverQuota',
);
}
}