TF-56 Implement upload single attachment
This commit is contained in:
@@ -3,6 +3,7 @@ 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';
|
||||
@@ -12,6 +13,7 @@ import 'package:tmail_ui_user/features/composer/data/datasource_impl/contact_dat
|
||||
import 'package:tmail_ui_user/features/composer/data/datasource_impl/local_autocomplete_datasource_impl.dart';
|
||||
import 'package:tmail_ui_user/features/composer/data/datasource_impl/local_composer_datasource_impl.dart';
|
||||
import 'package:tmail_ui_user/features/composer/data/local/email_address_database_manager.dart';
|
||||
import 'package:tmail_ui_user/features/composer/data/network/composer_api.dart';
|
||||
import 'package:tmail_ui_user/features/composer/data/repository/auto_complete_repository_impl.dart';
|
||||
import 'package:tmail_ui_user/features/composer/data/repository/composer_repository_impl.dart';
|
||||
import 'package:tmail_ui_user/features/composer/data/repository/contact_repository_impl.dart';
|
||||
@@ -20,18 +22,22 @@ import 'package:tmail_ui_user/features/composer/domain/repository/composer_repos
|
||||
import 'package:tmail_ui_user/features/composer/domain/repository/contact_repository.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/usecases/get_device_contact_suggestions_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/usecases/upload_attachment_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/usecases/save_email_addresses_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/usecases/get_autocomplete_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/composer/domain/usecases/send_email_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';
|
||||
|
||||
class ComposerBindings extends Bindings {
|
||||
@override
|
||||
void dependencies() {
|
||||
Get.lazyPut(() => EmailAddressDatabaseManager(Get.find<DatabaseClient>()));
|
||||
Get.lazyPut(() => ComposerDataSourceImpl());
|
||||
Get.lazyPut(() => ComposerDataSourceImpl(Get.find<ComposerAPI>()));
|
||||
Get.lazyPut(() => LocalComposerDataSourceImpl(Get.find<EmailAddressDatabaseManager>()));
|
||||
Get.lazyPut<ComposerDataSource>(() => Get.find<ComposerDataSourceImpl>());
|
||||
Get.lazyPut(() => ComposerRepositoryImpl({
|
||||
@@ -61,6 +67,12 @@ class ComposerBindings extends Bindings {
|
||||
Get.lazyPut(() => GetAutoCompleteWithDeviceContactInteractor(
|
||||
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.lazyPut(() => ComposerController(
|
||||
Get.find<SendEmailInteractor>(),
|
||||
Get.find<SaveEmailAddressesInteractor>(),
|
||||
@@ -71,5 +83,8 @@ class ComposerBindings extends Bindings {
|
||||
Get.find<HtmlEditorController>(),
|
||||
Get.find<TextEditingController>(),
|
||||
Get.find<HtmlMessagePurifier>()));
|
||||
Get.find<HtmlEditorController>(),
|
||||
Get.find<LocalFilePickerInteractor>(),
|
||||
Get.find<UploadAttachmentInteractor>()));
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import 'dart:async';
|
||||
import 'package:core/core.dart';
|
||||
import 'package:core/presentation/utils/app_toast.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:html_editor_enhanced/html_editor.dart';
|
||||
@@ -23,6 +24,10 @@ import 'package:tmail_ui_user/features/composer/domain/model/email_request.dart'
|
||||
import 'package:tmail_ui_user/features/composer/domain/state/get_autocomplete_state.dart';
|
||||
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/upload_attachment_interactor.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/presentation/extensions/email_action_type_extension.dart';
|
||||
@@ -30,7 +35,10 @@ 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/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';
|
||||
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class ComposerController extends BaseController {
|
||||
@@ -39,6 +47,7 @@ class ComposerController extends BaseController {
|
||||
final composerArguments = Rxn<ComposerArguments>();
|
||||
final isEnableEmailSendButton = false.obs;
|
||||
final listReplyToEmailAddress = <EmailAddress>[].obs;
|
||||
final attachments = <Attachment>[].obs;
|
||||
|
||||
final SendEmailInteractor _sendEmailInteractor;
|
||||
final SaveEmailAddressesInteractor _saveEmailAddressInteractor;
|
||||
@@ -49,6 +58,9 @@ class ComposerController extends BaseController {
|
||||
final HtmlEditorController composerEditorController;
|
||||
final TextEditingController subjectEmailInputController;
|
||||
final HtmlMessagePurifier _htmlMessagePurifier;
|
||||
final HtmlEditorController htmlEditorController;
|
||||
final LocalFilePickerInteractor _localFilePickerInteractor;
|
||||
final UploadAttachmentInteractor _uploadAttachmentInteractor;
|
||||
|
||||
List<EmailAddress> listToEmailAddress = [];
|
||||
List<EmailAddress> listCcEmailAddress = [];
|
||||
@@ -68,6 +80,9 @@ class ComposerController extends BaseController {
|
||||
this.composerEditorController,
|
||||
this.subjectEmailInputController,
|
||||
this._htmlMessagePurifier,
|
||||
this.htmlEditorController,
|
||||
this._localFilePickerInteractor,
|
||||
this._uploadAttachmentInteractor,
|
||||
);
|
||||
|
||||
@override
|
||||
@@ -82,20 +97,29 @@ class ComposerController extends BaseController {
|
||||
void onDone() {
|
||||
viewState.value.fold(
|
||||
(failure) {
|
||||
_appToast.showErrorToast(AppLocalizations.of(Get.context!).error_message_sent);
|
||||
Get.back();
|
||||
if (failure is SendEmailFailure) {
|
||||
_sendEmailFailure(failure);
|
||||
} else if (failure is LocalFilePickerFailure || failure is LocalFilePickerCancel) {
|
||||
_pickFileFailure(failure);
|
||||
} else if (failure is UploadAttachmentFailure) {
|
||||
_uploadAttachmentsFailure(failure);
|
||||
}
|
||||
},
|
||||
(success) {
|
||||
_saveEmailAddress();
|
||||
_appToast.showSuccessToast(AppLocalizations.of(Get.context!).message_sent);
|
||||
Get.back();
|
||||
if (success is SendEmailSuccess) {
|
||||
_sendEmailSuccess(success);
|
||||
} else if (success is LocalFilePickerSuccess) {
|
||||
_pickFileSuccess(success);
|
||||
} else if (success is UploadAttachmentSuccess) {
|
||||
_uploadAttachmentsSuccess(success);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void onError(error) {
|
||||
_appToast.showErrorToast(AppLocalizations.of(Get.context!).error_message_sent);
|
||||
Get.back();
|
||||
popBack();
|
||||
}
|
||||
|
||||
void _getSelectedEmail() {
|
||||
@@ -275,6 +299,17 @@ class ComposerController extends BaseController {
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
final listEmailAddressCanSave = Set<EmailAddress>();
|
||||
listEmailAddressCanSave.addAll(listToEmailAddress + listCcEmailAddress + listBccEmailAddress);
|
||||
@@ -306,6 +341,63 @@ class ComposerController extends BaseController {
|
||||
(success) => success is GetAutoCompleteSuccess ? success.listEmailAddress : <EmailAddress>[]));
|
||||
}
|
||||
|
||||
void openPickAttachmentMenu(BuildContext context, List<Widget> actionTiles) {
|
||||
(ContextMenuBuilder(context)
|
||||
..addHeader(
|
||||
(ContextMenuHeaderBuilder(Key('attachment_picker_context_menu_header_builder'))
|
||||
..addLabel(AppLocalizations.of(context).pick_attachments))
|
||||
.build())
|
||||
..addTiles(actionTiles))
|
||||
.build();
|
||||
}
|
||||
|
||||
void openFilePickerByType(BuildContext context, FileType fileType) async {
|
||||
popBack();
|
||||
consumeState(_localFilePickerInteractor.execute(fileType: fileType));
|
||||
}
|
||||
|
||||
void _pickFileFailure(Failure failure) {
|
||||
if (failure is LocalFilePickerFailure) {
|
||||
if (Get.context != null) {
|
||||
_appToast.showErrorToast(AppLocalizations.of(Get.context!).can_not_upload_this_file_as_attachments);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _pickFileSuccess(Success success) {
|
||||
if (success is LocalFilePickerSuccess) {
|
||||
_uploadAttachmentsAction(success.fileInfo);
|
||||
}
|
||||
}
|
||||
|
||||
void _uploadAttachmentsAction(FileInfo fileInfo) async {
|
||||
if (composerArguments.value != null) {
|
||||
final accountId = composerArguments.value!.session.accounts.keys.first;
|
||||
final uploadUrl = composerArguments.value!.session.getUploadUrl(accountId);
|
||||
|
||||
consumeState(_uploadAttachmentInteractor.execute(fileInfo, accountId, uploadUrl));
|
||||
}
|
||||
}
|
||||
|
||||
void _uploadAttachmentsFailure(Failure failure) {
|
||||
if (Get.context != null) {
|
||||
_appToast.showErrorToast(AppLocalizations.of(Get.context!).can_not_upload_this_file_as_attachments);
|
||||
}
|
||||
}
|
||||
|
||||
void _uploadAttachmentsSuccess(Success success) {
|
||||
if (success is UploadAttachmentSuccess) {
|
||||
attachments.add(success.attachment);
|
||||
if (Get.context != null) {
|
||||
_appToast.showSuccessToast(AppLocalizations.of(Get.context!).attachments_uploaded_successfully);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void removeAttachmentAction(Attachment attachmentRemoved) {
|
||||
attachments.removeWhere((attachment) => attachment == attachmentRemoved);
|
||||
}
|
||||
|
||||
void backToEmailViewAction() {
|
||||
Get.back();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
import 'package:core/core.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
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';
|
||||
import 'package:tmail_ui_user/features/composer/presentation/widgets/top_bar_composer_widget_builder.dart';
|
||||
import 'package:tmail_ui_user/features/email/presentation/widgets/attachment_file_tile_builder.dart';
|
||||
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
|
||||
|
||||
class ComposerView extends GetWidget<ComposerController> {
|
||||
@@ -50,12 +56,40 @@ class ComposerView extends GetWidget<ComposerController> {
|
||||
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
child: Obx(() => (TopBarComposerWidgetBuilder(imagePaths, controller.isEnableEmailSendButton.value)
|
||||
..addSendEmailActionClick(() => controller.sendEmailAction(context))
|
||||
..addAttachFileActionClick(() => controller.openPickAttachmentMenu(context, _pickAttachmentsActionTiles(context)))
|
||||
..addBackActionClick(() => controller.backToEmailViewAction()))
|
||||
.build()),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmailHeader(BuildContext context) {
|
||||
List<Widget> _pickAttachmentsActionTiles(BuildContext context) {
|
||||
return [
|
||||
_pickPhotoAndVideoAction(context),
|
||||
_browseFileAction(context),
|
||||
SizedBox(height: 30),
|
||||
];
|
||||
}
|
||||
|
||||
Widget _pickPhotoAndVideoAction(BuildContext context) {
|
||||
return (SimpleContextMenuActionBuilder(
|
||||
Key('pick_photo_and_video_context_menu_action'),
|
||||
SvgPicture.asset(imagePaths.icPhotoLibrary, width: 24, height: 24, fit: BoxFit.fill),
|
||||
AppLocalizations.of(context).photos_and_videos)
|
||||
..onActionClick((_) => controller.openFilePickerByType(context, FileType.media)))
|
||||
.build();
|
||||
}
|
||||
|
||||
Widget _browseFileAction(BuildContext context) {
|
||||
return (SimpleContextMenuActionBuilder(
|
||||
Key('browse_file_context_menu_action'),
|
||||
SvgPicture.asset(imagePaths.icMore, width: 24, height: 24, fit: BoxFit.fill),
|
||||
AppLocalizations.of(context).browse)
|
||||
..onActionClick((_) => controller.openFilePickerByType(context, FileType.any)))
|
||||
.build();
|
||||
}
|
||||
|
||||
Widget _buildEmailHeaderField(BuildContext context) {
|
||||
return Container(
|
||||
margin: EdgeInsets.zero,
|
||||
padding: EdgeInsets.only(top: 20),
|
||||
@@ -120,6 +154,10 @@ class ComposerView extends GetWidget<ComposerController> {
|
||||
padding: EdgeInsets.only(bottom: 30, top: 16, left: 16, right: 16),
|
||||
child: _buildComposerEditer(context),
|
||||
)
|
||||
child: _buildEmailBodyEditorQuoted(context),
|
||||
),
|
||||
_buildLoadingView(),
|
||||
_buildAttachments(context),
|
||||
]
|
||||
)
|
||||
);
|
||||
@@ -143,4 +181,43 @@ class ComposerView extends GetWidget<ComposerController> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLoadingView() {
|
||||
return Obx(() => controller.viewState.value.fold(
|
||||
(failure) => SizedBox.shrink(),
|
||||
(success) => success is UploadAttachmentLoadingState
|
||||
? Center(child: Padding(
|
||||
padding: EdgeInsets.only(top: 8),
|
||||
child: SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(color: AppColor.primaryColor))))
|
||||
: SizedBox.shrink()));
|
||||
}
|
||||
|
||||
Widget _buildAttachments(BuildContext context) {
|
||||
return Obx(() => controller.attachments.isNotEmpty
|
||||
? ListView.builder(
|
||||
shrinkWrap: true,
|
||||
primary: false,
|
||||
key: Key('attachment_list'),
|
||||
padding: EdgeInsets.only(top: 16, bottom: 24, right: 50, left: 24),
|
||||
itemCount: controller.attachments.length,
|
||||
itemBuilder: (context, index) => (AttachmentFileTileBuilder(
|
||||
imagePaths,
|
||||
controller.attachments[index],
|
||||
0,
|
||||
0)
|
||||
..height(60)
|
||||
..addButtonAction(_buildRemoveButtonAttachment(controller.attachments[index])))
|
||||
.build())
|
||||
: SizedBox.shrink());
|
||||
}
|
||||
|
||||
Widget _buildRemoveButtonAttachment(Attachment attachment) {
|
||||
return (IconBuilder(imagePaths.icComposerClose)
|
||||
..size(35)
|
||||
..addOnTapActionClick(() => controller.removeAttachmentAction(attachment)))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,12 @@ import 'package:flutter/widgets.dart';
|
||||
|
||||
typedef OnBackActionClick = void Function();
|
||||
typedef OnSendEmailActionClick = void Function();
|
||||
typedef OnAttachFileActionClick = void Function();
|
||||
|
||||
class TopBarComposerWidgetBuilder {
|
||||
OnBackActionClick? _onBackActionClick;
|
||||
OnSendEmailActionClick? _onSendEmailActionClick;
|
||||
OnAttachFileActionClick? _onAttachFileActionClick;
|
||||
|
||||
final ImagePaths _imagePaths;
|
||||
final bool _isEnableEmailSendButton;
|
||||
@@ -23,6 +25,10 @@ class TopBarComposerWidgetBuilder {
|
||||
_onSendEmailActionClick = onSendEmailActionClick;
|
||||
}
|
||||
|
||||
void addAttachFileActionClick(OnAttachFileActionClick onAttachFileActionClick) {
|
||||
_onAttachFileActionClick = onAttachFileActionClick;
|
||||
}
|
||||
|
||||
Widget build() {
|
||||
return Container(
|
||||
key: Key('top_bar_composer_widget'),
|
||||
@@ -64,7 +70,13 @@ class TopBarComposerWidgetBuilder {
|
||||
children: [
|
||||
ButtonBuilder(_imagePaths.icComposerFileShare).key(Key('button_file_share')).build(),
|
||||
SizedBox(width: 2),
|
||||
ButtonBuilder(_imagePaths.icShare).key(Key('button_attachment')).build(),
|
||||
ButtonBuilder(_imagePaths.icShare)
|
||||
.key(Key('button_attachment'))
|
||||
.onPressActionClick(() {
|
||||
if (_onAttachFileActionClick != null) {
|
||||
_onAttachFileActionClick!();
|
||||
}})
|
||||
.build(),
|
||||
SizedBox(width: 2),
|
||||
ButtonBuilder(_imagePaths.icComposerSend)
|
||||
.key(Key('button_send_email'))
|
||||
|
||||
Reference in New Issue
Block a user