TF-56 Send email with attachment
This commit is contained in:
@@ -14,14 +14,13 @@ class ComposerAPI {
|
||||
Future<UploadResponse> uploadAttachment(UploadRequest uploadRequest) async {
|
||||
final resultJson = await _dioClient.post(
|
||||
Uri.decodeFull(uploadRequest.uploadUrl.toString()),
|
||||
options: Options(headers: _buildHeaderRequestParam(uploadRequest.accountRequest.basicAuth())),
|
||||
options: Options(headers: _buildHeaderRequestParam()),
|
||||
data: File(uploadRequest.fileInfo.filePath).readAsBytesSync());
|
||||
return UploadResponse.fromJson(resultJson);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _buildHeaderRequestParam(String basicAuth) {
|
||||
Map<String, dynamic> _buildHeaderRequestParam() {
|
||||
final headerParam = _dioClient.getHeaders();
|
||||
headerParam[HttpHeaders.authorizationHeader] = basicAuth;
|
||||
headerParam[HttpHeaders.acceptHeader] = DioClient.jmapHeader;
|
||||
return headerParam;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
import 'package:core/core.dart';
|
||||
|
||||
class SendingEmailState extends UIState {
|
||||
SendingEmailState();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class SendEmailSuccess extends UIState {
|
||||
|
||||
SendEmailSuccess();
|
||||
|
||||
@@ -2,8 +2,8 @@ import 'package:core/core.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:model/model.dart';
|
||||
|
||||
class UploadAttachmentLoadingState extends UIState {
|
||||
UploadAttachmentLoadingState();
|
||||
class UploadingAttachmentState extends UIState {
|
||||
UploadingAttachmentState();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
@@ -27,28 +27,37 @@ class UploadAttachmentFailure extends FeatureFailure {
|
||||
List<Object> get props => [exception];
|
||||
}
|
||||
|
||||
class UploadAttachmentAllSuccess extends UIState {
|
||||
class UploadMultipleAttachmentAllSuccess extends UIState {
|
||||
final List<Either<Failure, Success>> listResults;
|
||||
|
||||
UploadAttachmentAllSuccess(this.listResults);
|
||||
UploadMultipleAttachmentAllSuccess(this.listResults);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [listResults];
|
||||
}
|
||||
|
||||
class UploadAttachmentHasSomeFailure extends UIState {
|
||||
class UploadMultipleAttachmentHasSomeFailure extends UIState {
|
||||
final List<Either<Failure, Success>> listResults;
|
||||
|
||||
UploadAttachmentHasSomeFailure(this.listResults);
|
||||
UploadMultipleAttachmentHasSomeFailure(this.listResults);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [listResults];
|
||||
}
|
||||
|
||||
class UploadAttachmentAllFailure extends FeatureFailure {
|
||||
class UploadMultipleAttachmentAllFailure extends FeatureFailure {
|
||||
final List<Either<Failure, Success>> listResults;
|
||||
|
||||
UploadMultipleAttachmentAllFailure(this.listResults);
|
||||
|
||||
@override
|
||||
List<Object> get props => [listResults];
|
||||
}
|
||||
|
||||
class UploadMultipleAttachmentFailure extends FeatureFailure {
|
||||
final exception;
|
||||
|
||||
UploadAttachmentAllFailure(this.exception);
|
||||
UploadMultipleAttachmentFailure(this.exception);
|
||||
|
||||
@override
|
||||
List<Object> get props => [exception];
|
||||
|
||||
@@ -12,15 +12,15 @@ class SendEmailInteractor {
|
||||
|
||||
Stream<Either<Failure, Success>> execute(AccountId accountId, EmailRequest emailRequest) async* {
|
||||
try {
|
||||
yield Right<Failure, Success>(LoadingState());
|
||||
yield Right<Failure, Success>(SendingEmailState());
|
||||
final result = await emailRepository.sendEmail(accountId, emailRequest);
|
||||
if (result) {
|
||||
yield Right<Failure, Success>(SendEmailSuccess());
|
||||
} else {
|
||||
yield Left(SendEmailFailure(result));
|
||||
yield Left<Failure, Success>(SendEmailFailure(result));
|
||||
}
|
||||
} catch (e) {
|
||||
yield Left(SendEmailFailure(e));
|
||||
yield Left<Failure, Success>(SendEmailFailure(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,23 +4,16 @@ import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:model/model.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/repository/composer_repository.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/state/upload_attachment_state.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
|
||||
|
||||
class UploadAttachmentInteractor {
|
||||
final ComposerRepository _composerRepository;
|
||||
final CredentialRepository _credentialRepository;
|
||||
|
||||
UploadAttachmentInteractor(this._composerRepository, this._credentialRepository);
|
||||
UploadAttachmentInteractor(this._composerRepository);
|
||||
|
||||
Stream<Either<Failure, Success>> execute(FileInfo fileInfo, AccountId accountId, Uri uploadUrl) async* {
|
||||
try {
|
||||
final result = await Future.wait(
|
||||
[_credentialRepository.getUserName(), _credentialRepository.getPassword()],
|
||||
eagerError: true
|
||||
).then((List responses) async {
|
||||
final accountRequest = AccountRequest(responses.first, responses.last);
|
||||
return await _composerRepository.uploadAttachment(UploadRequest(uploadUrl, accountId, fileInfo, accountRequest));
|
||||
});
|
||||
final result = await _composerRepository.uploadAttachment(
|
||||
UploadRequest(uploadUrl, accountId, fileInfo));
|
||||
yield Right<Failure, Success>(UploadAttachmentSuccess(result));
|
||||
} catch (e) {
|
||||
yield Left<Failure, Success>(UploadAttachmentFailure(e));
|
||||
|
||||
@@ -12,7 +12,7 @@ class UploadMultipleAttachmentInteractor {
|
||||
|
||||
Stream<Either<Failure, Success>> execute(List<FileInfo> listFiles, AccountId accountId, Uri uploadUrl) async* {
|
||||
try {
|
||||
yield Right<Failure, Success>(UploadAttachmentLoadingState());
|
||||
yield Right<Failure, Success>(UploadingAttachmentState());
|
||||
final listResult = await Future.wait(
|
||||
listFiles.map((fileInfo) async {
|
||||
final result = await _uploadAttachmentInteractor.execute(fileInfo, accountId, uploadUrl).toList();
|
||||
@@ -24,13 +24,15 @@ class UploadMultipleAttachmentInteractor {
|
||||
} else {
|
||||
final listResultSuccess = listResult.where((either) => either.isRight()).toList();
|
||||
if (listResultSuccess.length == listResult.length) {
|
||||
yield Right<Failure, Success>(UploadAttachmentHasSomeFailure(listResultSuccess));
|
||||
yield Right<Failure, Success>(UploadMultipleAttachmentAllSuccess(listResultSuccess));
|
||||
} else if (listResultSuccess.length == 0) {
|
||||
yield Left<Failure, Success>(UploadMultipleAttachmentAllFailure(listResult));
|
||||
} else {
|
||||
yield Right<Failure, Success>(UploadAttachmentAllSuccess(listResult));
|
||||
yield Right<Failure, Success>(UploadMultipleAttachmentHasSomeFailure(listResultSuccess));
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
yield Left<Failure, Success>(UploadAttachmentAllFailure(e));
|
||||
yield Left<Failure, Success>(UploadMultipleAttachmentFailure(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ import 'package:core/presentation/utils/app_toast.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:html_editor_enhanced/html_editor.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:tmail_ui_user/features/composer/data/datasource/autocomplete_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/composer/data/datasource/composer_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/composer/data/datasource/contact_datasource.dart';
|
||||
@@ -28,9 +27,6 @@ import 'package:tmail_ui_user/features/composer/domain/usecases/get_autocomplete
|
||||
import 'package:tmail_ui_user/features/composer/domain/usecases/send_email_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/usecases/upload_mutiple_attachment_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/composer/presentation/composer_controller.dart';
|
||||
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
|
||||
import 'package:tmail_ui_user/features/login/data/repository/credential_repository_impl.dart';
|
||||
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/usecases/local_file_picker_interactor.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
@@ -46,7 +42,6 @@ class ComposerBindings extends Bindings {
|
||||
DataSourceType.local: Get.find<LocalComposerDataSourceImpl>(),
|
||||
}));
|
||||
Get.lazyPut<ComposerRepository>(() => Get.find<ComposerRepositoryImpl>());
|
||||
Get.lazyPut(() => SendEmailInteractor(Get.find<EmailRepository>()));
|
||||
Get.lazyPut(() => SaveEmailAddressesInteractor(Get.find<ComposerRepository>()));
|
||||
Get.lazyPut(() => AutoCompleteDataSourceImpl());
|
||||
Get.lazyPut<AutoCompleteDataSource>(() => Get.find<AutoCompleteDataSourceImpl>());
|
||||
@@ -69,11 +64,8 @@ class ComposerBindings extends Bindings {
|
||||
Get.find<GetAutoCompleteInteractor>(),
|
||||
Get.find<GetDeviceContactSuggestionsInteractor>()));
|
||||
Get.lazyPut(() => LocalFilePickerInteractor());
|
||||
Get.lazyPut(() => CredentialRepositoryImpl(Get.find<SharedPreferences>()));
|
||||
Get.lazyPut<CredentialRepository>(() => Get.find<CredentialRepositoryImpl>());
|
||||
Get.lazyPut(() => UploadAttachmentInteractor(
|
||||
Get.find<ComposerRepository>(),
|
||||
Get.find<CredentialRepository>()));
|
||||
Get.find<ComposerRepository>()));
|
||||
Get.lazyPut(() => UploadMultipleAttachmentInteractor(Get.find<UploadAttachmentInteractor>()));
|
||||
Get.lazyPut(() => ComposerController(
|
||||
Get.find<SendEmailInteractor>(),
|
||||
@@ -84,8 +76,7 @@ class ComposerBindings extends Bindings {
|
||||
Get.find<Uuid>(),
|
||||
Get.find<HtmlEditorController>(),
|
||||
Get.find<TextEditingController>(),
|
||||
Get.find<HtmlMessagePurifier>()));
|
||||
Get.find<HtmlEditorController>(),
|
||||
Get.find<HtmlMessagePurifier>(),
|
||||
Get.find<LocalFilePickerInteractor>(),
|
||||
Get.find<UploadMultipleAttachmentInteractor>()));
|
||||
}
|
||||
|
||||
@@ -25,8 +25,6 @@ import 'package:tmail_ui_user/features/composer/domain/state/get_autocomplete_st
|
||||
import 'package:tmail_ui_user/features/composer/domain/usecases/get_autocomplete_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/usecases/get_autocomplete_with_device_contact_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/state/upload_attachment_state.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/state/search_email_address_state.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/state/send_email_state.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/usecases/save_email_addresses_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/usecases/send_email_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/usecases/upload_mutiple_attachment_interactor.dart';
|
||||
@@ -35,6 +33,7 @@ import 'package:tmail_ui_user/features/email/presentation/constants/email_consta
|
||||
import 'package:tmail_ui_user/features/email/presentation/extensions/email_content_extension.dart';
|
||||
import 'package:tmail_ui_user/features/email/presentation/model/composer_arguments.dart';
|
||||
import 'package:tmail_ui_user/features/email/presentation/model/message_content.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/mailbox_dashboard_controller.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/state/local_file_picker_state.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/usecases/local_file_picker_interactor.dart';
|
||||
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
|
||||
@@ -43,6 +42,8 @@ import 'package:uuid/uuid.dart';
|
||||
|
||||
class ComposerController extends BaseController {
|
||||
|
||||
final mailboxDashBoardController = Get.find<MailboxDashBoardController>();
|
||||
|
||||
final expandMode = ExpandMode.COLLAPSE.obs;
|
||||
final composerArguments = Rxn<ComposerArguments>();
|
||||
final isEnableEmailSendButton = false.obs;
|
||||
@@ -58,7 +59,6 @@ class ComposerController extends BaseController {
|
||||
final HtmlEditorController composerEditorController;
|
||||
final TextEditingController subjectEmailInputController;
|
||||
final HtmlMessagePurifier _htmlMessagePurifier;
|
||||
final HtmlEditorController htmlEditorController;
|
||||
final LocalFilePickerInteractor _localFilePickerInteractor;
|
||||
final UploadMultipleAttachmentInteractor _uploadMultipleAttachmentInteractor;
|
||||
|
||||
@@ -80,7 +80,6 @@ class ComposerController extends BaseController {
|
||||
this.composerEditorController,
|
||||
this.subjectEmailInputController,
|
||||
this._htmlMessagePurifier,
|
||||
this.htmlEditorController,
|
||||
this._localFilePickerInteractor,
|
||||
this._uploadMultipleAttachmentInteractor,
|
||||
);
|
||||
@@ -97,23 +96,19 @@ class ComposerController extends BaseController {
|
||||
void onDone() {
|
||||
viewState.value.fold(
|
||||
(failure) {
|
||||
if (failure is SendEmailFailure) {
|
||||
_sendEmailFailure(failure);
|
||||
} else if (failure is LocalFilePickerFailure || failure is LocalFilePickerCancel) {
|
||||
if (failure is LocalFilePickerFailure || failure is LocalFilePickerCancel) {
|
||||
_pickFileFailure(failure);
|
||||
} else if (failure is UploadAttachmentFailure
|
||||
|| failure is UploadAttachmentAllFailure) {
|
||||
|| failure is UploadMultipleAttachmentAllFailure) {
|
||||
_uploadAttachmentsFailure(failure);
|
||||
}
|
||||
},
|
||||
(success) {
|
||||
if (success is SendEmailSuccess) {
|
||||
_sendEmailSuccess(success);
|
||||
} else if (success is LocalFilePickerSuccess) {
|
||||
if (success is LocalFilePickerSuccess) {
|
||||
_pickFileSuccess(success);
|
||||
} else if (success is UploadAttachmentSuccess
|
||||
|| success is UploadAttachmentAllSuccess
|
||||
|| success is UploadAttachmentHasSomeFailure) {
|
||||
|| success is UploadMultipleAttachmentAllSuccess
|
||||
|| success is UploadMultipleAttachmentHasSomeFailure) {
|
||||
_uploadAttachmentsSuccess(success);
|
||||
}
|
||||
});
|
||||
@@ -283,40 +278,39 @@ class ComposerController extends BaseController {
|
||||
)},
|
||||
bodyValues: {
|
||||
generatePartId: EmailBodyValue(emailBodyText, false, false)
|
||||
}
|
||||
},
|
||||
attachments: attachments.isNotEmpty ? _generateAttachments() : null
|
||||
);
|
||||
}
|
||||
|
||||
Set<EmailBodyPart> _generateAttachments() {
|
||||
return attachments.map((attachment) =>
|
||||
attachment.toEmailBodyPart(Attachment.dispositionAttachment)).toSet();
|
||||
}
|
||||
|
||||
void sendEmailAction(BuildContext context) async {
|
||||
if (isEnableEmailSendButton.value) {
|
||||
_appToast.showToast(AppLocalizations.of(context).your_email_being_sent);
|
||||
_saveEmailAddress();
|
||||
|
||||
final email = await generateEmail();
|
||||
final accountId = composerArguments.value!.session.accounts.keys.first;
|
||||
final sentMailboxId = composerArguments.value!.mapMailboxId[PresentationMailbox.roleSent];
|
||||
final submissionCreateId = Id(_uuid.v1());
|
||||
|
||||
consumeState(_sendEmailInteractor.execute(accountId, EmailRequest(email, submissionCreateId, mailboxIdSaved: sentMailboxId)));
|
||||
mailboxDashBoardController.consumeState(_sendEmailInteractor.execute(
|
||||
accountId,
|
||||
EmailRequest(email, submissionCreateId, mailboxIdSaved: sentMailboxId)));
|
||||
|
||||
popBack();
|
||||
} else {
|
||||
_appToast.showErrorToast(AppLocalizations.of(context).your_email_should_have_at_least_one_recipient);
|
||||
}
|
||||
}
|
||||
|
||||
void _sendEmailSuccess(Success success) {
|
||||
_saveEmailAddress();
|
||||
_appToast.showSuccessToast(AppLocalizations.of(Get.context!).message_sent);
|
||||
popBack();
|
||||
}
|
||||
|
||||
void _sendEmailFailure(Failure failure) {
|
||||
_appToast.showErrorToast(AppLocalizations.of(Get.context!).error_message_sent);
|
||||
popBack();
|
||||
}
|
||||
|
||||
void _saveEmailAddress() {
|
||||
void _saveEmailAddress() async {
|
||||
final listEmailAddressCanSave = Set<EmailAddress>();
|
||||
listEmailAddressCanSave.addAll(listToEmailAddress + listCcEmailAddress + listBccEmailAddress);
|
||||
_saveEmailAddressInteractor.execute(listEmailAddressCanSave.toList());
|
||||
await _saveEmailAddressInteractor.execute(listEmailAddressCanSave.toList());
|
||||
}
|
||||
|
||||
void _checkContactPermission() async {
|
||||
@@ -391,20 +385,20 @@ class ComposerController extends BaseController {
|
||||
void _uploadAttachmentsSuccess(Success success) {
|
||||
if (success is UploadAttachmentSuccess) {
|
||||
attachments.add(success.attachment);
|
||||
} else if (success is UploadAttachmentAllSuccess) {
|
||||
final listAttachment = success.listResults
|
||||
} else if (success is UploadMultipleAttachmentAllSuccess) {
|
||||
final listAttachment = success.listResults.where((either) => either.isRight())
|
||||
.map((either) => either
|
||||
.map((result) => (result as UploadAttachmentSuccess).attachment)
|
||||
.toIterable().first)
|
||||
.toList();
|
||||
|
||||
attachments.addAll(listAttachment);
|
||||
} else if (success is UploadAttachmentHasSomeFailure) {
|
||||
} else if (success is UploadMultipleAttachmentHasSomeFailure) {
|
||||
final listAttachment = success.listResults
|
||||
.map((either) => either
|
||||
.map((result) => (result as UploadAttachmentSuccess).attachment)
|
||||
.toIterable().first)
|
||||
.toList();
|
||||
.map((either) => either
|
||||
.map((result) => (result as UploadAttachmentSuccess).attachment)
|
||||
.toIterable().first)
|
||||
.toList();
|
||||
|
||||
attachments.addAll(listAttachment);
|
||||
}
|
||||
@@ -418,6 +412,6 @@ class ComposerController extends BaseController {
|
||||
}
|
||||
|
||||
void backToEmailViewAction() {
|
||||
Get.back();
|
||||
popBack();
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import 'package:flutter_svg/flutter_svg.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:html_editor_enhanced/html_editor.dart';
|
||||
import 'package:model/email/attachment.dart';
|
||||
import 'package:model/email/email_action_type.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/state/upload_attachment_state.dart';
|
||||
import 'package:tmail_ui_user/features/composer/presentation/composer_controller.dart';
|
||||
import 'package:tmail_ui_user/features/composer/presentation/widgets/email_address_composer_widget_builder.dart';
|
||||
@@ -62,7 +61,6 @@ class ComposerView extends GetWidget<ComposerController> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmailHeader(BuildContext context) {
|
||||
List<Widget> _pickAttachmentsActionTiles(BuildContext context) {
|
||||
return [
|
||||
_pickPhotoAndVideoAction(context),
|
||||
@@ -89,7 +87,7 @@ class ComposerView extends GetWidget<ComposerController> {
|
||||
.build();
|
||||
}
|
||||
|
||||
Widget _buildEmailHeaderField(BuildContext context) {
|
||||
Widget _buildEmailHeader(BuildContext context) {
|
||||
return Container(
|
||||
margin: EdgeInsets.zero,
|
||||
padding: EdgeInsets.only(top: 20),
|
||||
@@ -152,18 +150,16 @@ class ComposerView extends GetWidget<ComposerController> {
|
||||
_buildEmailHeader(context),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(bottom: 30, top: 16, left: 16, right: 16),
|
||||
child: _buildComposerEditer(context),
|
||||
)
|
||||
child: _buildEmailBodyEditorQuoted(context),
|
||||
child: _buildComposerEditor(context),
|
||||
),
|
||||
_buildLoadingView(),
|
||||
_buildAttachmentsLoadingView(),
|
||||
_buildAttachments(context),
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildComposerEditer(BuildContext context) {
|
||||
Widget _buildComposerEditor(BuildContext context) {
|
||||
return HtmlEditor(
|
||||
key: Key('email_body_editor_quoted'),
|
||||
controller: controller.composerEditorController,
|
||||
@@ -182,10 +178,10 @@ class ComposerView extends GetWidget<ComposerController> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLoadingView() {
|
||||
Widget _buildAttachmentsLoadingView() {
|
||||
return Obx(() => controller.viewState.value.fold(
|
||||
(failure) => SizedBox.shrink(),
|
||||
(success) => success is UploadAttachmentLoadingState
|
||||
(success) => success is UploadingAttachmentState
|
||||
? Center(child: Padding(
|
||||
padding: EdgeInsets.only(top: 8),
|
||||
child: SizedBox(
|
||||
|
||||
Reference in New Issue
Block a user