Files

2794 lines
110 KiB
Dart

import 'dart:async';
import 'dart:math';
import 'package:core/core.dart';
import 'package:custom_pop_up_menu/custom_pop_up_menu.dart';
import 'package:dartz/dartz.dart';
import 'package:desktop_drop/desktop_drop.dart';
import 'package:dio/dio.dart';
import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:file_picker/file_picker.dart';
import 'package:filesize/filesize.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:html_editor_enhanced/html_editor.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';
import 'package:jmap_dart_client/jmap/core/error/method/error_method_response.dart';
import 'package:jmap_dart_client/jmap/core/error/set_error.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/identities/identity.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/email/email_address.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:model/model.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:pointer_interceptor/pointer_interceptor.dart';
import 'package:rich_text_composer/rich_text_composer.dart';
import 'package:scribe/scribe.dart';
import 'package:super_tag_editor/tag_editor.dart';
import 'package:tmail_ui_user/features/base/base_controller.dart';
import 'package:tmail_ui_user/features/base/before_reconnect_handler.dart';
import 'package:tmail_ui_user/features/base/before_reconnect_manager.dart';
import 'package:tmail_ui_user/features/base/mixin/ai_scribe_mixin.dart';
import 'package:tmail_ui_user/features/base/mixin/auto_complete_result_mixin.dart';
import 'package:tmail_ui_user/features/base/mixin/message_dialog_action_manager.dart';
import 'package:tmail_ui_user/features/base/state/base_ui_state.dart';
import 'package:tmail_ui_user/features/base/state/button_state.dart';
import 'package:tmail_ui_user/features/composer/domain/exceptions/compose_email_exception.dart';
import 'package:tmail_ui_user/features/composer/domain/exceptions/forbidden_to_send_exception.dart';
import 'package:tmail_ui_user/features/composer/domain/exceptions/invalid_recipients_exception.dart';
import 'package:tmail_ui_user/features/composer/domain/exceptions/set_method_exception.dart';
import 'package:tmail_ui_user/features/composer/domain/extensions/set_method_exception_description_extension.dart';
import 'package:tmail_ui_user/features/composer/domain/model/contact_suggestion_source.dart';
import 'package:tmail_ui_user/features/composer/domain/repository/composer_repository.dart';
import 'package:tmail_ui_user/features/composer/domain/state/download_image_as_base64_state.dart';
import 'package:tmail_ui_user/features/composer/domain/state/generate_email_state.dart';
import 'package:tmail_ui_user/features/composer/domain/state/save_email_as_drafts_state.dart';
import 'package:tmail_ui_user/features/composer/domain/state/send_email_state.dart';
import 'package:tmail_ui_user/features/composer/domain/state/update_email_drafts_state.dart';
import 'package:tmail_ui_user/features/composer/domain/state/upload_attachment_state.dart';
import 'package:tmail_ui_user/features/composer/domain/usecases/create_new_and_save_email_to_drafts_interactor.dart';
import 'package:tmail_ui_user/features/composer/domain/usecases/create_new_and_send_email_interactor.dart';
import 'package:tmail_ui_user/features/composer/domain/usecases/download_image_as_base64_interactor.dart';
import 'package:tmail_ui_user/features/composer/domain/usecases/get_all_autocomplete_interactor.dart';
import 'package:tmail_ui_user/features/composer/domain/usecases/get_autocomplete_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/restore_email_inline_images_interactor.dart';
import 'package:tmail_ui_user/features/composer/domain/usecases/save_composer_cache_interactor.dart';
import 'package:tmail_ui_user/features/composer/presentation/controller/rich_text_mobile_tablet_controller.dart';
import 'package:tmail_ui_user/features/composer/presentation/controller/rich_text_web_controller.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/handle_mobile_auto_save_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/refresh_composer_attachments_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/attachment_detection_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/auto_create_tag_for_recipients_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/forbidden_to_send_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/get_draft_mailbox_id_for_composer_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/get_outbox_mailbox_id_for_composer_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/get_sent_mailbox_id_for_composer_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/handle_keyboard_shortcut_actions_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/handle_message_failure_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/handle_recipients_collapsed_extensions.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/invalid_recipients_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/sanitize_signature_in_email_content_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/setup_email_attachments_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/setup_email_content_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/setup_email_important_flag_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/setup_email_other_components_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/setup_email_recipients_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/setup_email_request_read_receipt_flag_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/setup_email_subject_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/setup_email_template_id_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/setup_list_identities_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/setup_selected_identity_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/update_screen_display_mode_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/mixin/drag_drog_file_mixin.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/create_email_request.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/inline_image.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/prefix_recipient_state.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/saved_composing_email.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/screen_display_mode.dart';
import 'package:tmail_ui_user/features/composer/presentation/styles/composer_style.dart';
import 'package:tmail_ui_user/features/composer/presentation/validator/composer_attachment_upload_state_source.dart';
import 'package:tmail_ui_user/features/composer/presentation/view/editor_view_mixin.dart';
import 'package:tmail_ui_user/features/composer/presentation/widgets/mobile/from_composer_bottom_sheet_builder.dart';
import 'package:tmail_ui_user/features/composer/presentation/widgets/saving_message_dialog_view.dart';
import 'package:tmail_ui_user/features/composer/presentation/widgets/saving_template_dialog_view.dart';
import 'package:tmail_ui_user/features/composer/presentation/widgets/sending_message_dialog_view.dart';
import 'package:tmail_ui_user/features/email/domain/exceptions/email_exceptions.dart';
import 'package:tmail_ui_user/features/email/domain/state/get_email_content_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/save_template_email_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/transform_html_email_content_state.dart';
import 'package:tmail_ui_user/features/email/domain/state/update_template_email_state.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/get_email_content_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/print_email_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/save_template_email_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/transform_html_email_content_interactor.dart';
import 'package:tmail_ui_user/features/email/presentation/extensions/presentation_email_extension.dart';
import 'package:tmail_ui_user/features/home/domain/extensions/session_extensions.dart';
import 'package:tmail_ui_user/features/email/presentation/model/composer_arguments.dart';
import 'package:tmail_ui_user/features/mailbox/domain/model/create_new_mailbox_request.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/open_and_close_composer_extension.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/premium_cta_context_extension.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/extensions/update_text_formatting_menu_state_extension.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/model/draggable_app_state.dart';
import 'package:tmail_ui_user/features/manage_account/domain/state/get_all_identities_state.dart';
import 'package:tmail_ui_user/features/manage_account/domain/usecases/get_all_identities_interactor.dart';
import 'package:tmail_ui_user/features/manage_account/presentation/extensions/identity_extension.dart';
import 'package:tmail_ui_user/features/network_connection/presentation/network_connection_controller.dart'
if (dart.library.html) 'package:tmail_ui_user/features/network_connection/presentation/web_network_connection_controller.dart';
import 'package:tmail_ui_user/features/paywall/presentation/extensions/premium_cta_ref_extension.dart';
import 'package:tmail_ui_user/features/server_settings/domain/usecases/get_server_setting_interactor.dart';
import 'package:tmail_ui_user/features/upload/domain/exceptions/pick_file_exception.dart';
import 'package:tmail_ui_user/features/upload/domain/exceptions/upload_exception.dart';
import 'package:tmail_ui_user/features/upload/domain/extensions/file_info_extension.dart';
import 'package:tmail_ui_user/features/upload/domain/extensions/list_file_upload_extension.dart';
import 'package:tmail_ui_user/features/upload/domain/model/upload_task_id.dart';
import 'package:tmail_ui_user/features/upload/domain/state/attachment_upload_state.dart';
import 'package:tmail_ui_user/features/upload/domain/state/local_file_picker_state.dart';
import 'package:tmail_ui_user/features/upload/domain/state/local_image_picker_state.dart';
import 'package:tmail_ui_user/features/upload/domain/usecases/local_file_picker_interactor.dart';
import 'package:tmail_ui_user/features/upload/domain/usecases/local_image_picker_interactor.dart';
import 'package:tmail_ui_user/features/upload/presentation/controller/upload_controller.dart';
import 'package:tmail_ui_user/features/upload/presentation/providers/upload_from_url_providers.dart';
import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_upload_validation_service.dart';
import 'package:tmail_ui_user/main/error/capability_validator.dart';
import 'package:tmail_ui_user/main/exceptions/remote/authentication_exception.dart';
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
import 'package:tmail_ui_user/main/providers/app_provider_container.dart';
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
import 'package:tmail_ui_user/main/universal_import/html_stub.dart' as html;
import 'package:workplace/domain/entity/drive_document.dart';
import 'package:tmail_ui_user/features/composer/presentation/manager/drive_attachment_handler.dart';
import 'package:tmail_ui_user/features/composer/presentation/manager/drive_oversize_attachment_recovery.dart';
import 'package:tmail_ui_user/features/composer/presentation/manager/concurrency_gate.dart';
import 'package:tmail_ui_user/features/composer/presentation/manager/drive_attachment_transfer_runner.dart';
import 'package:tmail_ui_user/features/composer/presentation/utils/composer_web_body_cache.dart';
import 'package:tmail_ui_user/main/utils/app_config.dart';
import 'package:tmail_ui_user/main/utils/toast_manager.dart';
import 'package:workplace/presentation/model/drive_pick_state.dart';
typedef ComposerReloadCacheAction = void Function();
class ComposerController extends BaseController
with
DragDropFileMixin,
AutoCompleteResultMixin,
EditorViewMixin,
AiScribeMixin
implements BeforeReconnectHandler {
final mailboxDashBoardController = Get.find<MailboxDashBoardController>();
/// A restored draft composes, saves and sends in the account it was saved in.
AccountId? get composerAccountId =>
composerArguments.value?.accountId ?? mailboxDashBoardController.activeAccountId;
bool get isComposingOutsideShownAccount =>
composerAccountId != mailboxDashBoardController.activeAccountId;
final networkConnectionController = Get.find<NetworkConnectionController>();
final _beforeReconnectManager = Get.find<BeforeReconnectManager>();
final composerArguments = Rxn<ComposerArguments>();
final isEnableEmailSendButton = false.obs;
final isInitialRecipient = false.obs;
final subjectEmail = Rxn<String>();
final screenDisplayMode = ScreenDisplayMode.normal.obs;
final emailContentsViewState = Rxn<Either<Failure, Success>>();
final hasRequestReadReceipt = false.obs;
final fromRecipientState = PrefixRecipientState.disabled.obs;
final toRecipientState = PrefixRecipientState.enabled.obs;
final ccRecipientState = PrefixRecipientState.disabled.obs;
final bccRecipientState = PrefixRecipientState.disabled.obs;
final replyToRecipientState = PrefixRecipientState.disabled.obs;
final recipientsCollapsedState = PrefixRecipientState.disabled.obs;
final prefixRootState = PrefixEmailAddress.to.obs;
/// Lower-cased addresses the server reported as `invalidRecipients` on the
/// last send attempt, highlighted as invalid until the user fixes them.
final invalidRecipients = Rx<Set<String>>({});
final identitySelected = Rxn<Identity>();
final listFromIdentities = RxList<Identity>();
/// The account [listFromIdentities] was last loaded for.
AccountId? fromIdentitiesAccountId;
final isEmailChanged = Rx<bool>(false);
final isMarkAsImportant = Rx<bool>(false);
final isContentHeightExceeded = Rx<bool>(false);
final editorTextSelection = Rxn<TextSelectionModel>();
final LocalFilePickerInteractor _localFilePickerInteractor;
final LocalImagePickerInteractor _localImagePickerInteractor;
final GetEmailContentInteractor _getEmailContentInteractor;
final GetAllIdentitiesInteractor _getAllIdentitiesInteractor;
final UploadController uploadController;
final SaveComposerCacheInteractor _saveComposerCacheInteractor;
final DownloadImageAsBase64Interactor _downloadImageAsBase64Interactor;
final TransformHtmlEmailContentInteractor _transformHtmlEmailContentInteractor;
final GetServerSettingInteractor _getServerSettingInteractor;
final CreateNewAndSendEmailInteractor _createNewAndSendEmailInteractor;
final CreateNewAndSaveEmailToDraftsInteractor _createNewAndSaveEmailToDraftsInteractor;
final PrintEmailInteractor printEmailInteractor;
final ComposerRepository _composerRepository;
final String? composerId;
final String? autoSaveComposerId;
final ComposerArguments? composerArgs;
final SaveTemplateEmailInteractor _saveTemplateEmailInteractor;
late AttachmentUploadValidationService attachmentUploadValidationService =
AttachmentUploadValidationService(
stateSource: ComposerAttachmentUploadStateSource.fromServerCapability(
uploadController: uploadController,
maxSizeAttachmentsPerEmail: () => mailboxDashBoardController.maxSizeAttachmentsPerEmail?.value,
),
recoveryBuilder: (context) => DriveOversizeAttachmentRecovery(
context: context,
insertHtml: insertHtmlIntoEditor,
),
);
GetAllAutoCompleteInteractor? _getAllAutoCompleteInteractor;
GetAutoCompleteInteractor? _getAutoCompleteInteractor;
GetDeviceContactSuggestionsInteractor? _getDeviceContactSuggestionsInteractor;
RestoreEmailInlineImagesInteractor? restoreEmailInlineImagesInteractor;
List<EmailAddress> listToEmailAddress = <EmailAddress>[];
List<EmailAddress> listCcEmailAddress = <EmailAddress>[];
List<EmailAddress> listBccEmailAddress = <EmailAddress>[];
List<EmailAddress> listReplyToEmailAddress = <EmailAddress>[];
ContactSuggestionSource _contactSuggestionSource = ContactSuggestionSource.tMailContact;
final subjectEmailInputController = TextEditingController();
final toEmailAddressController = TextEditingController();
final ccEmailAddressController = TextEditingController();
final bccEmailAddressController = TextEditingController();
final replyToEmailAddressController = TextEditingController();
final searchIdentitiesInputController = TextEditingController();
final GlobalKey<TagsEditorState> keyToEmailTagEditor = GlobalKey<TagsEditorState>();
final GlobalKey<TagsEditorState> keyCcEmailTagEditor = GlobalKey<TagsEditorState>();
final GlobalKey<TagsEditorState> keyBccEmailTagEditor = GlobalKey<TagsEditorState>();
final GlobalKey<TagsEditorState> keyReplyToEmailTagEditor = GlobalKey<TagsEditorState>();
final GlobalKey headerEditorMobileWidgetKey = GlobalKey();
final GlobalKey<DropdownButton2State> identityDropdownKey = GlobalKey<DropdownButton2State>();
final double defaultPaddingCoordinateYCursorEditor = 8;
FocusNode? subjectEmailInputFocusNode;
FocusNode? toAddressFocusNode;
FocusNode? ccAddressFocusNode;
FocusNode? bccAddressFocusNode;
FocusNode? replyToAddressFocusNode;
FocusNode? searchIdentitiesFocusNode;
FocusNode? toAddressFocusNodeKeyboard;
FocusNode? ccAddressFocusNodeKeyboard;
FocusNode? bccAddressFocusNodeKeyboard;
FocusNode? replyToAddressFocusNodeKeyboard;
FocusNode? keyboardShortcutFocusNode;
StreamSubscription<html.Event>? _subscriptionOnDragEnter;
StreamSubscription<html.Event>? _subscriptionOnDragOver;
StreamSubscription<html.Event>? _subscriptionOnDragLeave;
StreamSubscription<html.Event>? _subscriptionOnDrop;
StreamSubscription<html.Event>? _subscriptionOnBlur;
StreamSubscription<String>? _composerCacheListener;
ComposerReloadCacheAction? _reloadCacheAction;
RichTextMobileTabletController? richTextMobileTabletController;
RichTextWebController? richTextWebController;
CustomPopupMenuController? menuMoreOptionController;
final ScrollController scrollController = ScrollController();
final ScrollController scrollControllerEmailAddress = ScrollController();
final ScrollController scrollControllerAttachment = ScrollController();
final ScrollController scrollControllerIdentities = ScrollController();
List<Attachment> initialAttachments = <Attachment>[];
final ComposerWebBodyCache _webBodyCache = ComposerWebBodyCache();
double? maxWithEditor;
EmailId? emailIdEditing;
bool isAttachmentCollapsed = false;
ButtonState _closeComposerButtonState = ButtonState.enabled;
ButtonState _saveToDraftButtonState = ButtonState.enabled;
ButtonState _sendButtonState = ButtonState.enabled;
ButtonState printDraftButtonState = ButtonState.enabled;
int? _savedEmailDraftHash;
bool restoringSignatureButton = false;
bool synchronizeInitDraftHash = false;
GlobalKey? responsiveContainerKey;
EmailActionType? currentEmailActionType;
EmailActionType? savedActionType;
int minInputLengthAutocomplete = AppConfig.defaultMinInputLengthAutocomplete;
EmailId? currentTemplateEmailId;
AppLifecycleListener? mobileAutoSaveLifecycleListener;
Timer? periodicSnapshotTimer;
Timer? inactiveGuardTimer;
bool isRestoringFromCache = false;
int? get savedEmailDraftHash => _savedEmailDraftHash;
GetEmailContentInteractor get getEmailContentInteractor => _getEmailContentInteractor;
GetServerSettingInteractor get getServerSettingInteractor => _getServerSettingInteractor;
CreateNewAndSaveEmailToDraftsInteractor get createNewAndSaveEmailToDraftsInteractor =>
_createNewAndSaveEmailToDraftsInteractor;
SaveComposerCacheInteractor get saveComposerCacheInteractor =>
_saveComposerCacheInteractor;
Future<CreateEmailRequest?> buildCreateEmailRequestForAutoSave({String? htmlContent}) =>
_generateCreateEmailRequestToSaveAsCache(htmlContent: htmlContent);
GetAllIdentitiesInteractor get getAllIdentitiesInteractor => _getAllIdentitiesInteractor;
TransformHtmlEmailContentInteractor get transformHtmlEmailContentInteractor => _transformHtmlEmailContentInteractor;
String get ownEmailAddress => mailboxDashBoardController.ownerEmailAddress;
Set<String> get ownerEmailAddresses =>
mailboxDashBoardController.ownerEmailAddresses;
late Worker uploadInlineImageWorker;
late bool _isEmailBodyLoaded;
// Recipient FocusScopes keep focus when a chip menu takes it from their text field.
final Set<PrefixEmailAddress> _focusedRecipientScopes = {};
// An unmounted focused recipient FocusScope never reports its blur.
void clearFocusedRecipientScopes() => _focusedRecipientScopes.clear();
void registerReloadCacheAction(ComposerReloadCacheAction action) =>
_reloadCacheAction = action;
/// Writes this composer's reload snapshot now, as beforeunload would.
void saveReloadSnapshot() => _reloadCacheAction?.call();
@visibleForTesting
set isEmailBodyLoaded(bool value) => _isEmailBodyLoaded = value;
ComposerController(
this._localFilePickerInteractor,
this._localImagePickerInteractor,
this._getEmailContentInteractor,
this._getAllIdentitiesInteractor,
this.uploadController,
this._saveComposerCacheInteractor,
this._downloadImageAsBase64Interactor,
this._transformHtmlEmailContentInteractor,
this._getServerSettingInteractor,
this._createNewAndSendEmailInteractor,
this._createNewAndSaveEmailToDraftsInteractor,
this.printEmailInteractor,
this._composerRepository,
this._saveTemplateEmailInteractor,
{
this.composerId,
this.autoSaveComposerId,
this.composerArgs,
}
);
@override
void onInit() {
super.onInit();
restoreEmailInlineImagesInteractor = getBinding<RestoreEmailInlineImagesInteractor>(tag: composerId);
if (PlatformInfo.isWeb) {
responsiveContainerKey = GlobalKey();
richTextWebController = getBinding<RichTextWebController>(tag: composerId);
menuMoreOptionController = CustomPopupMenuController();
} else {
richTextMobileTabletController = getBinding<RichTextMobileTabletController>(tag: composerId);
}
createFocusNodeInput();
scrollControllerEmailAddress.addListener(_scrollControllerEmailAddressListener);
_listenStreamEvent();
_beforeReconnectManager.addListener(onBeforeReconnect);
_injectBinding();
onKeyboardShortcutInit();
if (PlatformInfo.isAndroid) {
initMobileAutoSave();
}
}
@override
void onReady() {
if (PlatformInfo.isWeb) {
_triggerBrowserEventListener();
}
setupComposer();
if (PlatformInfo.isMobile) {
Future.delayed(const Duration(milliseconds: 500), _checkContactPermission);
}
super.onReady();
}
@override
void onClose() {
_reloadCacheAction = null;
_webBodyCache.setTrusted(null);
savedActionType = null;
_savedEmailDraftHash = null;
currentEmailActionType = null;
emailIdEditing = null;
maxWithEditor = null;
initialAttachments.clear();
dispatchState(Right(UIClosedState()));
composerArguments.value = null;
emailContentsViewState.value = Right(UIClosedState());
identitySelected.value = null;
listFromIdentities.clear();
_subscriptionOnDragEnter?.cancel();
_subscriptionOnDragOver?.cancel();
_subscriptionOnDragLeave?.cancel();
_subscriptionOnDrop?.cancel();
_subscriptionOnBlur?.cancel();
subjectEmailInputFocusNode?.removeListener(_subjectEmailInputFocusListener);
_composerCacheListener?.cancel();
_beforeReconnectManager.removeListener(onBeforeReconnect);
restoreEmailInlineImagesInteractor = null;
if (PlatformInfo.isWeb) {
richTextWebController = null;
responsiveContainerKey = null;
menuMoreOptionController?.dispose();
menuMoreOptionController = null;
} else {
richTextMobileTabletController = null;
}
if (PlatformInfo.isAndroid) tearDownMobileAutoSave();
onKeyboardShortcutDispose();
super.onClose();
}
@override
void dispose() {
subjectEmailInputFocusNode?.dispose();
subjectEmailInputFocusNode = null;
toAddressFocusNode?.dispose();
toAddressFocusNode = null;
ccAddressFocusNode?.dispose();
ccAddressFocusNode = null;
bccAddressFocusNode?.dispose();
bccAddressFocusNode = null;
replyToAddressFocusNode?.dispose();
replyToAddressFocusNode = null;
toAddressFocusNodeKeyboard?.dispose();
toAddressFocusNodeKeyboard = null;
ccAddressFocusNodeKeyboard?.dispose();
ccAddressFocusNodeKeyboard = null;
bccAddressFocusNodeKeyboard?.dispose();
bccAddressFocusNodeKeyboard = null;
replyToAddressFocusNodeKeyboard?.dispose();
replyToAddressFocusNodeKeyboard = null;
searchIdentitiesFocusNode?.dispose();
searchIdentitiesFocusNode = null;
subjectEmailInputController.dispose();
toEmailAddressController.dispose();
ccEmailAddressController.dispose();
bccEmailAddressController.dispose();
replyToEmailAddressController.dispose();
uploadInlineImageWorker.dispose();
scrollController.dispose();
scrollControllerEmailAddress.removeListener(_scrollControllerEmailAddressListener);
scrollControllerEmailAddress.dispose();
scrollControllerAttachment.dispose();
scrollControllerIdentities.dispose();
super.dispose();
}
@override
void handleSuccessViewState(Success success) {
if (success is LocalFilePickerSuccess) {
_handlePickFileSuccess(success);
} else if (success is LocalImagePickerSuccess) {
_handlePickImageSuccess(success);
} else if (success is GetAllIdentitiesSuccess) {
_handleGetAllIdentitiesSuccess(success);
} else if (success is DownloadImageAsBase64Success) {
final inlineImage = InlineImage(fileInfo: success.fileInfo, base64Uri: success.base64Uri);
if (PlatformInfo.isWeb) {
richTextWebController?.insertImage(inlineImage);
} else {
richTextMobileTabletController?.insertImage(inlineImage);
}
maxWithEditor = null;
} else {
super.handleSuccessViewState(success);
}
}
@override
void handleFailureViewState(Failure failure) {
if (failure is LocalFilePickerFailure) {
_handlePickFileFailure(failure);
} else if (failure is LocalImagePickerFailure) {
_handlePickImageFailure(failure);
} else {
super.handleFailureViewState(failure);
}
}
@override
Future<void> onBeforeUnloadBrowserListener(html.Event event) {
saveReloadSnapshot();
return Future.value();
}
void _listenStreamEvent() {
uploadInlineImageWorker = ever(uploadController.uploadInlineViewState, (state) {
log('ComposerController::_listenStreamEvent()::uploadInlineImageWorker: $state');
state.fold((failure) => null, (success) {
if (success is SuccessAttachmentUploadState) {
_handleUploadInlineSuccess(success);
}
});
});
}
void _triggerBrowserEventListener() {
_subscriptionOnDragEnter = html.window.onDragEnter.listen((event) {
event.preventDefault();
if (event.dataTransfer.types.validateFilesTransfer) {
mailboxDashBoardController.localFileDraggableAppState.value = DraggableAppState.active;
}
});
_subscriptionOnDragOver = html.window.onDragOver.listen((event) {
event.preventDefault();
if (event.dataTransfer.types.validateFilesTransfer) {
mailboxDashBoardController.localFileDraggableAppState.value = DraggableAppState.active;
}
});
_subscriptionOnDragLeave = html.window.onDragLeave.listen((event) {
event.preventDefault();
if (event.dataTransfer.types.validateFilesTransfer) {
mailboxDashBoardController.localFileDraggableAppState.value = DraggableAppState.inActive;
}
});
_subscriptionOnDrop = html.window.onDrop.listen((event) {
event.preventDefault();
if (event.dataTransfer.types.validateFilesTransfer) {
mailboxDashBoardController.localFileDraggableAppState.value = DraggableAppState.inActive;
}
});
// https://github.com/flutter/flutter/issues/155265#issuecomment-2417101524
_subscriptionOnBlur = html.window.onBlur.listen((event) {
html.document.activeElement?.blur();
});
}
Future<void> _saveComposerSessionCache() async {
final createEmailRequest = await _generateCreateEmailRequestToSaveAsCache();
if (createEmailRequest == null) return;
await _saveComposerCacheInteractor.execute(createEmailRequest: createEmailRequest);
}
Uri? _getUploadUriFromSession(Session session, AccountId accountId) {
try {
return session.getUploadUri(accountId, jmapUrl: dynamicUrlInterceptors.jmapUrl);
} catch (e) {
logWarning('ComposerController::_getUploadUriFromSession:Exception = $e');
return null;
}
}
Future<CreateEmailRequest?> _generateCreateEmailRequestToSaveAsCache({String? htmlContent}) async {
final arguments = composerArguments.value;
final session = mailboxDashBoardController.sessionCurrent;
final accountId = composerAccountId;
if (arguments == null || session == null || accountId == null) {
log('ComposerController::_generateCreateEmailRequest: SESSION or ACCOUNT_ID or ARGUMENTS is NULL');
return null;
}
autoCreateEmailTag();
String emailContent = htmlContent != null
? htmlContent.removeEditorStartTag()
: await getContentInEditor();
if (currentEmailActionType == EmailActionType.compose) {
emailContent = await _composerRepository.removeCollapsedExpandedSignatureEffect(
emailContent: emailContent,
);
}
final uploadUri = _getUploadUriFromSession(session, accountId);
final composerIndex = composerId != null
? mailboxDashBoardController.composerManager.getComposerIndex(composerId!)
: null;
return CreateEmailRequest(
session: session,
accountId: accountId,
emailActionType: arguments.emailActionType,
ownEmailAddress: ownEmailAddress,
subject: subjectEmail.value ?? '',
emailContent: emailContent,
fromSender: arguments.presentationEmail?.from ?? {},
toRecipients: listToEmailAddress.toSet(),
ccRecipients: listCcEmailAddress.toSet(),
bccRecipients: listBccEmailAddress.toSet(),
replyToRecipients: listReplyToEmailAddress.toSet(),
hasRequestReadReceipt: hasRequestReadReceipt.value,
isMarkAsImportant: isMarkAsImportant.value,
identity: identitySelected.value,
attachments: uploadController.attachmentsUploaded,
inlineAttachments: uploadController.mapInlineAttachments,
sentMailboxId: getSentMailboxIdForComposer(),
draftsMailboxId: getDraftMailboxIdForComposer(),
draftsEmailId: getDraftEmailId(),
answerForwardEmailId: arguments.presentationEmail?.id,
unsubscribeEmailId: arguments.previousEmailId,
messageId: arguments.messageId,
references: arguments.references,
emailSendingQueue: arguments.sendingEmail,
displayMode: screenDisplayMode.value,
uploadUri: uploadUri,
composerIndex: composerIndex,
composerId: composerId,
savedDraftHash: _savedEmailDraftHash ?? arguments.savedDraftHash,
savedActionType: savedActionType ?? currentEmailActionType,
savedEmailDraftId: emailIdEditing,
templateEmailId: currentTemplateEmailId,
);
}
void _scrollControllerEmailAddressListener() {
_closeSuggestionBox();
}
void createFocusNodeInput() {
toAddressFocusNode = FocusNode();
ccAddressFocusNode = FocusNode();
bccAddressFocusNode = FocusNode();
replyToAddressFocusNode = FocusNode();
searchIdentitiesFocusNode = FocusNode();
toAddressFocusNodeKeyboard = FocusNode();
ccAddressFocusNodeKeyboard = FocusNode();
bccAddressFocusNodeKeyboard = FocusNode();
replyToAddressFocusNodeKeyboard = FocusNode();
subjectEmailInputFocusNode = FocusNode(
onKeyEvent: PlatformInfo.isWeb ? _subjectEmailInputOnKeyListener : null,
);
subjectEmailInputFocusNode?.addListener(_subjectEmailInputFocusListener);
}
void _subjectEmailInputFocusListener() {
if (subjectEmailInputFocusNode?.hasFocus == true) {
if (PlatformInfo.isMobile
&& currentContext != null
&& !responsiveUtils.isScreenWithShortestSide(currentContext!)) {
richTextMobileTabletController?.richTextController.hideRichTextView();
}
autoCreateEmailTag();
triggerHideRecipientsFieldsWhenUnfocus();
}
}
KeyEventResult _subjectEmailInputOnKeyListener(FocusNode node, KeyEvent event) {
if (event is KeyDownEvent && event.logicalKey == LogicalKeyboardKey.tab) {
subjectEmailInputFocusNode?.unfocus();
richTextWebController?.editorController.setFocus();
return KeyEventResult.handled;
}
return KeyEventResult.ignored;
}
void onCreatedMobileEditorAction(BuildContext context, HtmlEditorApi editorApi, String? content) {
richTextMobileTabletController?.htmlEditorApi = editorApi;
richTextMobileTabletController?.richTextController.onCreateHTMLEditor(
editorApi,
onEnterKeyDown: _onEnterKeyDown,
onFocus: _onEditorFocusOnMobile,
onChangeCursor: (coordinates) {
_onChangeCursorOnMobile(coordinates, context);
},
);
}
Future<void> onLoadCompletedMobileEditorAction(HtmlEditorApi editorApi, WebUri? url) async {
_isEmailBodyLoaded = true;
await setupSelectedIdentity();
_autoFocusFieldWhenLauncher();
if (PlatformInfo.isAndroid) unawaited(restoreIfEditorBlank());
}
void _injectBinding() {
injectAutoCompleteBindings(
mailboxDashBoardController.sessionCurrent,
composerAccountId,
);
}
Future<void> setupComposer() async {
_isEmailBodyLoaded = false;
_focusedRecipientScopes.clear();
final arguments = PlatformInfo.isWeb ? composerArgs : Get.arguments;
if (arguments is! ComposerArguments) return;
composerArguments.value = arguments;
currentEmailActionType = arguments.emailActionType;
savedActionType = arguments.savedActionType;
emailIdEditing = arguments.savedEmailDraftId;
emailContentsViewState.value = Right(GetEmailContentLoading());
setupEmailSubject(arguments);
setupEmailRecipients(arguments);
setupEmailImportantFlag(arguments);
setupEmailAttachments(arguments);
setupEmailOtherComponents(arguments);
setupEmailRequestReadReceiptFlag(arguments);
setupEmailTemplateId(arguments);
await setupListIdentities(arguments);
await setupEmailContent(arguments);
if (screenDisplayMode.value.isNotContentVisible() &&
currentContext != null &&
responsiveUtils.isWebDesktop(currentContext!)) {
await setupSelectedIdentityWithoutApplySignature();
}
if (PlatformInfo.isWeb) {
richTextWebController?.updateFormattingOptions(
mailboxDashBoardController.isTextFormattingMenuOpened.value,
);
}
}
void initAttachmentsAndInlineImages({
List<Attachment>? attachments,
List<Attachment>? inlineImages
}) {
if (attachments?.isNotEmpty == true) {
initialAttachments = attachments!;
uploadController.initializeUploadAttachments(attachments);
}
if (inlineImages?.isNotEmpty == true) {
uploadController.initializeUploadInlineAttachments(inlineImages!);
}
}
void _getAllIdentities() {
log('ComposerController::_getAllIdentities: Fetch again identity !');
final accountId = composerAccountId;
final session = mailboxDashBoardController.sessionCurrent;
if (accountId != null && session != null) {
consumeState(_getAllIdentitiesInteractor.execute(session, accountId));
}
}
void _handleGetAllIdentitiesSuccess(GetAllIdentitiesSuccess success) {
applyFromPickerIdentities(success.identities);
}
void initEmailAddress({
required PresentationEmail presentationEmail,
required EmailActionType actionType,
String? listPost,
}) {
final shownAddresses = mailboxDashBoardController.ownerEmailAddresses;
final isSender = presentationEmail.from
.asList()
.any((element) =>
element.emailAddress.isNotEmpty &&
shownAddresses.contains(element.emailAddress));
final recipients = presentationEmail.generateRecipientsEmailAddressForComposer(
emailActionType: actionType,
isSender: isSender,
shownAddresses: shownAddresses,
listPost: listPost,
);
listToEmailAddress = List.from(recipients.to);
listCcEmailAddress = List.from(recipients.cc);
listBccEmailAddress = List.from(recipients.bcc);
listReplyToEmailAddress = List.from(recipients.replyTo);
if (isRecipientsWithoutReplyToNotEmpty) {
hideAllRecipientsFields();
isInitialRecipient.value = true;
} else {
toRecipientState.value = PrefixRecipientState.enabled;
}
updateStatusEmailSendButton();
}
void updateListEmailAddress(
PrefixEmailAddress prefixEmailAddress,
List<EmailAddress> newListEmailAddress
) {
switch(prefixEmailAddress) {
case PrefixEmailAddress.to:
listToEmailAddress = List.from(newListEmailAddress);
break;
case PrefixEmailAddress.cc:
listCcEmailAddress = List.from(newListEmailAddress);
break;
case PrefixEmailAddress.bcc:
listBccEmailAddress = List.from(newListEmailAddress);
break;
case PrefixEmailAddress.replyTo:
listReplyToEmailAddress = List.from(newListEmailAddress);
break;
default:
break;
}
updateStatusEmailSendButton();
}
void updateStatusEmailSendButton() {
if (listToEmailAddress.isNotEmpty
|| listCcEmailAddress.isNotEmpty
|| listBccEmailAddress.isNotEmpty) {
isEnableEmailSendButton.value = true;
} else {
isEnableEmailSendButton.value = false;
}
}
/// A refused EmailSubmission leaves the created copy in Outbox, so a mailbox without send rights stops here.
bool get _isSubmissionAvailableOnComposerAccount {
final session = mailboxDashBoardController.sessionCurrent;
final accountId = composerAccountId;
return session != null &&
accountId != null &&
CapabilityIdentifier.jmapSubmission.isSupported(session, accountId);
}
void handleClickSendButton(BuildContext context) async {
if (_sendButtonState == ButtonState.disabled) {
log('ComposerController::handleClickSendButton: SENDING EMAIL');
return;
}
_sendButtonState = ButtonState.disabled;
clearFocus();
if (toEmailAddressController.text.isNotEmpty
|| ccEmailAddressController.text.isNotEmpty
|| bccEmailAddressController.text.isNotEmpty
|| replyToEmailAddressController.text.isNotEmpty) {
autoCreateEmailTag();
}
final appLocalizations = AppLocalizations.of(context);
if (!_isSubmissionAvailableOnComposerAccount) {
MessageDialogActionManager().showConfirmDialogAction(
context,
appLocalizations.sendingNotAllowedFromThisMailbox,
appLocalizations.got_it,
title: appLocalizations.sending_failed,
showAsBottomSheet: true,
hasCancelButton: false,
dialogMargin: MediaQuery.paddingOf(context).add(const EdgeInsets.only(bottom: 12)),
).whenComplete(() => _sendButtonState = ButtonState.enabled);
return;
}
if (isEditedEmailContentLoadFailed) {
MessageDialogActionManager().showConfirmDialogAction(
context,
appLocalizations.messageDialogSendEmailContentLoadFailed,
appLocalizations.got_it,
title: appLocalizations.sending_failed,
showAsBottomSheet: true,
hasCancelButton: false,
dialogMargin: MediaQuery.paddingOf(context).add(const EdgeInsets.only(bottom: 12)),
).whenComplete(() => _sendButtonState = ButtonState.enabled);
return;
}
if (isEmailBodyNotReady) {
MessageDialogActionManager().showConfirmDialogAction(
context,
appLocalizations.messageDialogSendEmailContentLoading,
appLocalizations.got_it,
title: appLocalizations.sending_failed,
showAsBottomSheet: true,
hasCancelButton: false,
dialogMargin: MediaQuery.paddingOf(context).add(const EdgeInsets.only(bottom: 12)),
).whenComplete(() => _sendButtonState = ButtonState.enabled);
return;
}
if (!isEnableEmailSendButton.value) {
MessageDialogActionManager().showConfirmDialogAction(context,
appLocalizations.message_dialog_send_email_without_recipient,
appLocalizations.add_recipients,
title: appLocalizations.sending_failed,
hasCancelButton: false,
showAsBottomSheet: true,
dialogMargin: MediaQuery.paddingOf(context).add(const EdgeInsets.only(bottom: 12)),
).whenComplete(() => _sendButtonState = ButtonState.enabled);
return;
}
if (existEmailAddressInvalid) {
MessageDialogActionManager().showConfirmDialogAction(context,
appLocalizations.message_dialog_send_email_with_email_address_invalid,
appLocalizations.fix_email_addresses,
onConfirmAction: showFullRecipients,
showAsBottomSheet: true,
title: appLocalizations.sending_failed,
hasCancelButton: false,
dialogMargin: MediaQuery.paddingOf(context).add(const EdgeInsets.only(bottom: 12)),
).whenComplete(() => _sendButtonState = ButtonState.enabled);
return;
}
if (subjectEmail.value == null || subjectEmail.isEmpty == true) {
MessageDialogActionManager().showConfirmDialogAction(context,
appLocalizations.message_dialog_send_email_without_a_subject,
appLocalizations.send_anyway,
cancelTitle: appLocalizations.cancel,
onConfirmAction: () => _prepareToSendMessages(context),
onCancelAction: popBack,
autoPerformPopBack: false,
title: appLocalizations.empty_subject,
showAsBottomSheet: true,
dialogMargin: MediaQuery.paddingOf(context).add(const EdgeInsets.only(bottom: 12)),
).whenComplete(() => _sendButtonState = ButtonState.enabled);
return;
}
if (!uploadController.allUploadAttachmentsCompleted) {
MessageDialogActionManager().showConfirmDialogAction(
context,
appLocalizations.messageDialogSendEmailUploadingAttachment,
appLocalizations.got_it,
title: appLocalizations.sending_failed,
showAsBottomSheet: true,
hasCancelButton: false,
dialogMargin: MediaQuery.paddingOf(context).add(const EdgeInsets.only(bottom: 12)),
).whenComplete(() => _sendButtonState = ButtonState.enabled);
return;
}
if (attachmentUploadValidationService.isExceededMaxSizeAttachmentsPerEmail()) {
MessageDialogActionManager().showConfirmDialogAction(
context,
appLocalizations.message_dialog_send_email_exceeds_maximum_size(
filesize(mailboxDashBoardController.maxSizeAttachmentsPerEmail?.value ?? 0, 0),
),
appLocalizations.got_it,
title: appLocalizations.sending_failed,
hasCancelButton: false
).whenComplete(() => _sendButtonState = ButtonState.enabled);
return;
}
_prepareToSendMessages(context);
}
bool get isEmailBodyNotReady {
final isContentLoading = emailContentsViewState.value?.fold(
(_) => false,
(success) => success is GetEmailContentLoading,
) ?? false;
final isEditorNotLoaded = PlatformInfo.isWeb
? !_isEmailBodyLoaded && !screenDisplayMode.value.isNotContentVisible()
: !_isEmailBodyLoaded;
return isContentLoading || isEditorNotLoaded;
}
bool get isEditedEmailContentLoadFailed {
final isEditingExistingEmail = const {
EmailActionType.editDraft,
EmailActionType.editAsNewEmail,
EmailActionType.reopenComposerBrowser,
EmailActionType.restoreComposerFromPersistentCache,
}.contains(currentEmailActionType);
final isContentLoadFailed = emailContentsViewState.value?.fold(
(failure) => !_isEmptyEmailContentFailure(failure),
(_) => false,
) ?? false;
return isEditingExistingEmail && isContentLoadFailed;
}
bool _isEmptyEmailContentFailure(Failure failure) =>
failure is GetEmailContentFailure && failure.exception is EmptyEmailContentException;
Future<String> getContentInEditor() async {
try {
final htmlTextEditor = PlatformInfo.isWeb
? textEditorWeb
: await htmlEditorApi?.getText();
return htmlTextEditor?.isNotEmpty == true
? htmlTextEditor!.removeEditorStartTag()
: '';
} catch (e) {
logWarning('ComposerController::getContentInEditor:Exception = $e');
return '';
}
}
Future<void> _prepareToSendMessages(BuildContext context) async {
final arguments = composerArguments.value;
final session = mailboxDashBoardController.sessionCurrent;
final accountId = composerAccountId;
if (arguments == null || session == null || accountId == null) {
log('ComposerController::_handleSendMessages: SESSION or ACCOUNT_ID or ARGUMENTS is NULL');
_sendButtonState = ButtonState.enabled;
_closeComposerAction(closeOverlays: true);
return;
}
if (Get.isDialogOpen == true || Get.isBottomSheetOpen == true) {
popBack();
}
final emailContent = await getContentInEditor();
if (uploadController.attachmentsUploaded.isNotEmpty) {
if (!context.mounted) {
logWarning('ComposerController::_prepareToSendMessages: CONTEXT IS NOT MOUNTED');
_sendButtonState = ButtonState.enabled;
return;
}
_sendMessageToServer(
context: context,
session: session,
accountId: accountId,
arguments: arguments,
emailContent: emailContent,
);
return;
}
final attachmentKeywords = await validateAttachmentReminder(
emailSubject: subjectEmail.value ?? '',
emailContent: emailContent,
);
if (!context.mounted) {
logWarning('ComposerController::_prepareToSendMessages: CONTEXT IS NOT MOUNTED');
_sendButtonState = ButtonState.enabled;
return;
}
if (attachmentKeywords.isNotEmpty) {
showAttachmentReminderModal(
context: context,
keywords: attachmentKeywords,
onConfirmAction: () {
_sendMessageToServer(
context: context,
session: session,
accountId: accountId,
arguments: arguments,
emailContent: emailContent,
);
},
onCancelAction: () {
_sendButtonState = ButtonState.enabled;
}
).whenComplete(() => _sendButtonState = ButtonState.enabled);
return;
}
_sendMessageToServer(
context: context,
session: session,
accountId: accountId,
arguments: arguments,
emailContent: emailContent,
);
}
Future<void> _sendMessageToServer({
required BuildContext context,
required Session session,
required AccountId accountId,
required ComposerArguments arguments,
required String emailContent,
}) async {
final uploadUri = _getUploadUriFromSession(session, accountId);
final resultState = await _showSendingMessageDialog(
session: session,
accountId: accountId,
arguments: arguments,
emailContent: emailContent,
uploadUri: uploadUri,
);
// handleSendMessageResult only uses context after its own mounted check.
// ignore: use_build_context_synchronously
await handleSendMessageResult(context: context, resultState: resultState);
}
@visibleForTesting
Future<void> handleSendMessageResult({
required BuildContext context,
required dynamic resultState,
}) async {
if (resultState is SendEmailSuccess ||
mailboxDashBoardController
.validateSendingEmailFailedWhenNetworkIsLostOnMobile(resultState)) {
_sendButtonState = ButtonState.enabled;
_closeComposerAction(result: resultState);
} else if (resultState is SendEmailFailure ||
resultState is GenerateEmailFailure) {
final exception = resultState.exception;
// Drops marks from a prior invalidRecipients failure so they do not
// linger on addresses this failure says nothing about.
if (exception is! InvalidRecipientsException) {
invalidRecipients.value = {};
}
if (exception is BadCredentialsException) {
_sendButtonState = ButtonState.enabled;
handleBadCredentialsException();
} else if (exception is InvalidRecipientsException) {
_sendButtonState = ButtonState.enabled;
handleInvalidRecipientsFailure(exception);
} else if (exception is ForbiddenToSendException) {
_sendButtonState = ButtonState.enabled;
await handleForbiddenToSendFailure(exception);
} else if (context.mounted) {
await _showConfirmDialogWhenSendMessageFailure(
context: context,
failure: resultState,
);
} else {
_sendButtonState = ButtonState.enabled;
}
} else {
_sendButtonState = ButtonState.enabled;
}
}
Future<bool> insertHtmlIntoEditor(String html) async {
if (PlatformInfo.isWeb) {
final editorController = richTextWebController?.editorController;
if (editorController == null) return false;
editorController.insertHtml(html);
// insertHtml posts to the iframe and returns immediately; getText() shares
// an uncorrelated message channel with other callers (e.g.
// displayScreenTypeComposerAction), so it can't safely confirm the insert
// landed. endOfFrame mirrors the mobile branch below instead.
await SchedulerBinding.instance.endOfFrame;
return true;
}
final editorApi = htmlEditorApi;
if (editorApi == null) return false;
await richTextMobileTabletController?.restoreMobileEditorFocus();
await editorApi.insertHtml(html);
await SchedulerBinding.instance.endOfFrame;
return true;
}
Future<void> handleDrivePickResult(List<DriveDocument> result) async {
try {
await Get.find<DriveAttachmentHandler>().handleDrivePickResult(
result,
insertHtml: insertHtmlIntoEditor,
transferDriveDocuments: (docs) => _transferDriveDocuments(docs),
appLocalizations: currentContext != null ? AppLocalizations.of(currentContext!) : null,
);
} catch (e, s) {
logError(
'ComposerController::handleDrivePickResult failed',
exception: e,
stackTrace: s,
extras: {'docCount': result.length},
);
// A throw here can strand waiting chips, so the failure must be visible.
getBinding<ToastManager>()?.showMessageFailure(DrivePickFailure(
e,
message: currentContext != null
? AppLocalizations.of(currentContext!).driveAttachmentTransferFailed
: null,
));
}
}
static const _maxConcurrentDriveTransfers = 3;
/// One gate per composer: reopening the picker mid-transfer queues the new
/// batch behind the running one instead of multiplying in-flight requests.
late final ConcurrencyGate _driveTransferGate =
ConcurrencyGate(_maxConcurrentDriveTransfers);
Future<DriveTransferOutcome> _transferDriveDocuments(List<DriveDocument> docs) async {
final jmapUrl = dynamicUrlInterceptors.jmapUrl;
if (jmapUrl == null || jmapUrl.isEmpty) {
logError(
'ComposerController::_transferDriveDocuments: jmapUrl is unavailable',
exception: const UploadFromUrlEndpointUnavailableException('jmapUrl is unavailable'),
stackTrace: StackTrace.current,
extras: {'docCount': docs.length},
);
return DriveAttachmentTransferRunner.notStartedOutcome;
}
final session = mailboxDashBoardController.sessionCurrent;
final accountId = composerAccountId;
final uploadUri = session?.getUploadFromUrlUri(
accountId,
jmapUrl: jmapUrl,
);
if (uploadUri == null || accountId == null) {
logError(
'ComposerController::_transferDriveDocuments: upload-from-url endpoint is unavailable',
exception: const UploadFromUrlEndpointUnavailableException('upload-from-url uri could not be resolved'),
stackTrace: StackTrace.current,
// Account id stays out: extras reach Sentry.
extras: {
'docCount': docs.length,
'hasSession': session != null,
},
);
return DriveAttachmentTransferRunner.notStartedOutcome;
}
// Resolved once per batch: each read rebuilds interactor -> repo -> datasource -> dio.
final interactor =
appProviderContainer.read(uploadDriveDocumentFromUrlInteractorProvider);
final runner = DriveAttachmentTransferRunner(
uploadFromUrl: (request) => interactor.execute(request),
gate: _driveTransferGate,
);
return runner.transfer((
docs: docs,
accountId: accountId,
uploadUri: uploadUri,
onPlaceholdersReady: uploadController.addDownloadingPlaceholders,
onSuccess: uploadController.resolveDriveTransferSuccess,
onFailure: uploadController.resolveDriveTransferFailure,
));
}
Future<dynamic> _showSendingMessageDialog({
required Session session,
required AccountId accountId,
required ComposerArguments arguments,
required String emailContent,
required Uri? uploadUri,
}) {
final childWidget = PointerInterceptor(
child: SendingMessageDialogView(
createEmailRequest: CreateEmailRequest(
session: session,
accountId: accountId,
emailActionType: arguments.emailActionType,
ownEmailAddress: ownEmailAddress,
subject: subjectEmail.value ?? '',
emailContent: emailContent,
fromSender: arguments.presentationEmail?.from ?? {},
toRecipients: listToEmailAddress.toSet(),
ccRecipients: listCcEmailAddress.toSet(),
bccRecipients: listBccEmailAddress.toSet(),
replyToRecipients: listReplyToEmailAddress.toSet(),
hasRequestReadReceipt: hasRequestReadReceipt.value,
isMarkAsImportant: isMarkAsImportant.value,
identity: identitySelected.value,
attachments: uploadController.attachmentsUploaded,
inlineAttachments: uploadController.mapInlineAttachments,
outboxMailboxId: getOutboxMailboxIdForComposer(),
sentMailboxId: getSentMailboxIdForComposer(),
draftsEmailId: getDraftEmailId(),
answerForwardEmailId: arguments.presentationEmail?.id,
unsubscribeEmailId: arguments.previousEmailId,
messageId: arguments.messageId,
references: arguments.references,
emailSendingQueue: arguments.sendingEmail,
displayMode: screenDisplayMode.value,
uploadUri: uploadUri,
),
createNewAndSendEmailInteractor: _createNewAndSendEmailInteractor,
),
);
return Get.dialog(
PlatformInfo.isMobile
? PopScope(canPop: false, child: childWidget)
: childWidget,
barrierDismissible: false,
barrierColor: AppColor.colorDefaultCupertinoActionSheet,
);
}
Future<void> _showConfirmDialogWhenSendMessageFailure({
required BuildContext context,
required FeatureFailure failure
}) async {
final messageRecord = getMessageFailure(
appLocalizations: AppLocalizations.of(context),
exception: failure.exception,
);
final providerContainer = ProviderScope.containerOf(context, listen: false);
final needIncreaseMySpace = providerContainer.isPremiumCtaAvailable(
mailboxDashBoardController.currentPremiumCtaContext,
) &&
messageRecord.errorType == SetError.overQuota;
await MessageDialogActionManager().showConfirmDialogAction(
context,
title: '',
messageRecord.message,
needIncreaseMySpace
? AppLocalizations.of(context).increaseYourSpace
: AppLocalizations.of(context).edit,
cancelTitle: needIncreaseMySpace
? AppLocalizations.of(context).edit
: AppLocalizations.of(context).closeAnyway,
alignCenter: true,
outsideDismissible: false,
autoPerformPopBack: false,
onConfirmAction: () {
_sendButtonState = ButtonState.enabled;
popBack();
if (needIncreaseMySpace) {
providerContainer.openPremiumCta(
mailboxDashBoardController.currentPremiumCtaContext,
);
} else {
_autoFocusFieldWhenLauncher();
}
},
onCancelAction: () {
_sendButtonState = ButtonState.enabled;
if (needIncreaseMySpace) {
popBack();
_autoFocusFieldWhenLauncher();
} else {
_closeComposerAction(closeOverlays: true);
}
},
).whenComplete(() => _sendButtonState = ButtonState.enabled);
}
void _checkContactPermission() async {
final permissionStatus = await Permission.contacts.status;
if (permissionStatus.isGranted) {
_contactSuggestionSource = ContactSuggestionSource.all;
} else if (!permissionStatus.isPermanentlyDenied) {
final requestedPermission = await Permission.contacts.request();
_contactSuggestionSource = requestedPermission == PermissionStatus.granted
? ContactSuggestionSource.all
: _contactSuggestionSource;
}
}
Future<List<EmailAddress>> getAutoCompleteSuggestion(String queryString, {int? limit}) async {
log('ComposerController::getAutoCompleteSuggestion():queryString = $queryString | limit = $limit | $_contactSuggestionSource');
final accountId = composerAccountId;
final tmailContactsAvailable = mailboxDashBoardController.sessionCurrent
?.supportsTMailContactAutocomplete(accountId) == true;
_getAllAutoCompleteInteractor = tmailContactsAvailable ? getBinding<GetAllAutoCompleteInteractor>() : null;
_getAutoCompleteInteractor = tmailContactsAvailable ? getBinding<GetAutoCompleteInteractor>() : null;
_getDeviceContactSuggestionsInteractor = getBinding<GetDeviceContactSuggestionsInteractor>();
final autoCompletePattern = AutoCompletePattern(
word: queryString,
limit: limit,
accountId: accountId);
if (_contactSuggestionSource == ContactSuggestionSource.all) {
if (_getAllAutoCompleteInteractor != null) {
return await _getAllAutoCompleteInteractor!
.execute(autoCompletePattern)
.then(
(value) => handleAutoCompleteResultState(
resultState: value,
queryString: queryString,
onFailureCallback: (failure) {
logWarning('ComposerController::getAutoCompleteSuggestion:onFailureCallback: $failure');
consumeState(Stream.value(Left(failure)));
},
),
onError: (error) {
logWarning('ComposerController::getAutoCompleteSuggestion:onError: $error');
consumeState(Stream.value(Left(error)));
},
);
} else if (_getDeviceContactSuggestionsInteractor != null) {
return await _getDeviceContactSuggestionsInteractor!
.execute(autoCompletePattern)
.then((value) => handleAutoCompleteResultState(
resultState: value,
queryString: queryString,
)
);
} else {
return <EmailAddress>[];
}
} else {
return await _getAutoCompleteInteractor
?.execute(autoCompletePattern)
.then(
(value) => handleAutoCompleteResultState(
resultState: value,
queryString: queryString,
onFailureCallback: (failure) {
logWarning('ComposerController::getAutoCompleteSuggestion:onFailureCallback: $failure');
consumeState(Stream.value(Left(failure)));
},
),
onError: (error) {
logWarning('ComposerController::getAutoCompleteSuggestion:onError: $error');
consumeState(Stream.value(Left(error)));
},
) ?? <EmailAddress>[];
}
}
Future<void> openPickAttachmentMenu(BuildContext context, List<Widget> actionTiles) async {
if (PlatformInfo.isMobile) {
try {
await htmlEditorApi?.storeSelectionRange();
} catch (e) {
log('ComposerController::openPickAttachmentMenu(): $e');
}
}
if (!context.mounted) return;
clearFocus();
(ContextMenuBuilder(context)
..addHeader((ContextMenuHeaderBuilder(const Key('attachment_picker_context_menu_header_builder'))
..addLabel(AppLocalizations.of(context).pick_attachments))
.build())
..addTiles(actionTiles)
..addOnCloseContextMenuAction(() => popBack()))
.build();
}
void openFilePickerByType(BuildContext context, FileType fileType) async {
if (!kIsWeb) {
popBack();
}
consumeState(_localFilePickerInteractor.execute(fileType: fileType));
}
void _handlePickFileFailure(LocalFilePickerFailure failure) {
if (currentOverlayContext != null && currentContext != null && failure.exception is! PickFileCanceledException) {
appToast.showToastErrorMessage(
currentOverlayContext!,
AppLocalizations.of(currentContext!).thisFileCannotBePicked);
}
}
void _handlePickImageFailure(LocalImagePickerFailure failure) {
if (currentOverlayContext != null && currentContext != null && failure.exception is! PickFileCanceledException) {
appToast.showToastErrorMessage(
currentOverlayContext!,
AppLocalizations.of(currentContext!).cannotSelectThisImage);
}
}
Future<void> _handlePickFileSuccess(LocalFilePickerSuccess success) {
return attachmentUploadValidationService.validateFiles(
files: success.pickedFiles,
onAllowed: () => uploadAttachmentsAction(pickedFiles: success.pickedFiles));
}
Future<void> _handlePickImageSuccess(LocalImagePickerSuccess success) {
final inlineFile = success.fileInfo.withInline();
return attachmentUploadValidationService.validateFiles(
files: [inlineFile],
onAllowed: () => uploadAttachmentsAction(pickedFiles: [inlineFile]));
}
void uploadAttachmentsAction({required List<FileInfo> pickedFiles}) {
final session = mailboxDashBoardController.sessionCurrent;
final accountId = composerAccountId;
if (session != null && accountId != null) {
try {
final uploadUri = session.getUploadUri(accountId, jmapUrl: dynamicUrlInterceptors.jmapUrl);
uploadController.justUploadAttachmentsAction(
uploadFiles: pickedFiles,
uploadUri: uploadUri,
);
} catch (e) {
logWarning('ComposerController::uploadAttachmentsAction: $e');
uploadController.consumeState(Stream.value(Left(UploadAttachmentFailure(e, pickedFiles[0]))));
}
} else {
logWarning('ComposerController::uploadAttachmentsAction: SESSION OR ACCOUNT_ID is NULL');
}
}
void deleteAttachmentUploaded(UploadTaskId uploadId) {
uploadController.deleteFileUploaded(uploadId);
}
Future<bool> _validateEmailChange() async {
final newDraftHash = await _hashComposingEmail();
return _savedEmailDraftHash != newDraftHash;
}
Future<int> _hashComposingEmail({String? htmlContent}) async {
String emailContent = htmlContent ?? await getContentInEditor();
emailContent = await _composerRepository.removeCollapsedExpandedSignatureEffect(
emailContent: emailContent,
);
if (emailIdEditing != null &&
savedActionType == EmailActionType.compose &&
currentEmailActionType == EmailActionType.reopenComposerBrowser) {
emailContent = await _composerRepository.removeStyleLazyLoadDisplayInlineImages(
emailContent: emailContent,
);
}
final savedEmailDraft = SavedComposingEmail(
subject: subjectEmail.value ?? '',
content: emailContent,
toRecipients: listToEmailAddress.toSet(),
ccRecipients: listCcEmailAddress.toSet(),
bccRecipients: listBccEmailAddress.toSet(),
replyToRecipients: listReplyToEmailAddress.toSet(),
identity: identitySelected.value,
attachments: uploadController.attachmentsUploaded,
hasReadReceipt: hasRequestReadReceipt.value,
isMarkAsImportant: isMarkAsImportant.value,
);
final draftAsString = savedEmailDraft.asString();
final draftAsHasCode = draftAsString.hashCode;
log('ComposerController::_hashDraftEmail:draftAsHasCode = $draftAsHasCode');
return draftAsHasCode;
}
Future<void> _updateSavedEmailDraftHash() async {
_savedEmailDraftHash = await _hashComposingEmail();
}
Future<void> initEmailDraftHash() async {
final currentDraftHash = await _hashComposingEmail();
final oldSavedDraftHash = composerArguments.value?.savedDraftHash;
if (_isNewComposition || currentEmailActionType == EmailActionType.editDraft) {
_savedEmailDraftHash = currentDraftHash;
} else if (currentEmailActionType == EmailActionType.reopenComposerBrowser) {
_savedEmailDraftHash = oldSavedDraftHash;
}
log('ComposerController::initEmailDraftHash:oldSavedDraftHash = $oldSavedDraftHash | currentDraftHash = $currentDraftHash | _savedEmailDraftHash = $_savedEmailDraftHash');
isEmailChanged.value = currentDraftHash != _savedEmailDraftHash;
}
bool get _isNewComposition => const {
EmailActionType.compose,
EmailActionType.reply,
EmailActionType.replyAll,
EmailActionType.replyToList,
EmailActionType.forward,
EmailActionType.editAsNewEmail,
}.contains(currentEmailActionType);
void handleClickSaveAsDraftsButton(BuildContext context) async {
if (_saveToDraftButtonState == ButtonState.disabled) {
log('ComposerController::handleClickSaveAsDraftsButton: Saving to draft');
return;
}
_saveToDraftButtonState = ButtonState.disabled;
final arguments = composerArguments.value;
final session = mailboxDashBoardController.sessionCurrent;
final accountId = composerAccountId;
if (arguments == null ||
session == null ||
accountId == null ||
getDraftMailboxIdForComposer() == null
) {
log('ComposerController::handleClickSaveAsDraftsButton: SESSION or ACCOUNT_ID or ARGUMENTS is NULL');
_saveToDraftButtonState = ButtonState.enabled;
return;
}
final emailContent = await getContentInEditor();
final uploadUri = _getUploadUriFromSession(session, accountId);
final cancelToken = CancelToken();
final resultState = await _showSavingMessageToDraftsDialog(
session: session,
accountId: accountId,
arguments: arguments,
emailContent: emailContent,
uploadUri: uploadUri,
draftEmailId: emailIdEditing,
cancelToken: cancelToken
);
if (resultState is SaveEmailAsDraftsSuccess) {
_saveToDraftButtonState = ButtonState.enabled;
emailIdEditing = resultState.emailId;
mailboxDashBoardController.consumeState(Stream.value(Right<Failure, Success>(resultState)));
_updateSavedEmailDraftHash();
} else if (resultState is UpdateEmailDraftsSuccess) {
_saveToDraftButtonState = ButtonState.enabled;
emailIdEditing = resultState.emailId;
mailboxDashBoardController.consumeState(Stream.value(Right<Failure, Success>(resultState)));
autoRefreshAllAttachments(resultState.attachments, resultState.htmlBodyAttachments);
_updateSavedEmailDraftHash();
} else if ((resultState is SaveEmailAsDraftsFailure && resultState.exception is SavingEmailToDraftsCanceledException) ||
(resultState is UpdateEmailDraftsFailure && resultState.exception is SavingEmailToDraftsCanceledException)) {
_saveToDraftButtonState = ButtonState.enabled;
} else if (resultState is SaveEmailAsDraftsFailure ||
resultState is UpdateEmailDraftsFailure ||
resultState is GenerateEmailFailure
) {
if (resultState.exception is BadCredentialsException) {
_saveToDraftButtonState = ButtonState.enabled;
handleBadCredentialsException();
} else if (context.mounted) {
await _showConfirmDialogWhenSaveMessageToDraftsFailure(
context: context,
failure: resultState,
shouldOfferCloseComposer: false,
onConfirmAction: () {
_saveToDraftButtonState = ButtonState.enabled;
},
onCancelAction: (needIncreaseMySpace) {
_saveToDraftButtonState = ButtonState.enabled;
if (needIncreaseMySpace) {
popBack();
_autoFocusFieldWhenLauncher();
} else {
_closeComposerAction(closeOverlays: true);
}
},
);
} else {
_saveToDraftButtonState = ButtonState.enabled;
}
} else {
_saveToDraftButtonState = ButtonState.enabled;
}
}
Future<void> handleClickSaveAsTemplateButton(BuildContext context) async {
if (composerArguments.value == null ||
mailboxDashBoardController.sessionCurrent == null ||
composerAccountId == null
) {
log('ComposerController::handleClickSaveAsTemplateButton: SESSION or ACCOUNT_ID or ARGUMENTS is NULL');
return;
}
MailboxId? templateMailboxId = mailboxDashBoardController
.getMailboxIdByRole(PresentationMailbox.roleTemplates);
templateMailboxId ??= mailboxDashBoardController.mapMailboxById
.where((_, mailbox) => mailbox.name?.name.toLowerCase() ==
PresentationMailbox.roleTemplates.value.toLowerCase())
.keys
.firstOrNull;
final emailContent = await getContentInEditor();
final cancelToken = CancelToken();
final resultState = await _showSavingMessageToTemplateDialog(
emailContent: emailContent,
templateMailboxId: templateMailboxId,
templateEmailId: currentTemplateEmailId,
createNewMailboxRequest: templateMailboxId != null
? null
: CreateNewMailboxRequest(
MailboxName(PresentationMailbox.roleTemplates.value.toUpperCase()),
),
cancelToken: cancelToken,
);
if (resultState is SaveTemplateEmailSuccess && context.mounted == true) {
currentTemplateEmailId = resultState.emailId;
appToast.showToastSuccessMessage(
context,
AppLocalizations.of(context).saveMessageToTemplateSuccess,
);
} else if (resultState is UpdateTemplateEmailSuccess && context.mounted == true) {
currentTemplateEmailId = resultState.emailId;
autoRefreshAllAttachments(resultState.attachments, resultState.htmlBodyAttachments);
appToast.showToastSuccessMessage(
context,
AppLocalizations.of(context).updateMessageToTemplateSuccess,
);
} else if (resultState is SaveTemplateEmailFailure ||
resultState is UpdateTemplateEmailFailure ||
resultState is GenerateEmailFailure
) {
if (resultState.exception is BadCredentialsException) {
handleBadCredentialsException();
} else if (context.mounted) {
String message = '';
final exception = resultState.exception;
if (exception is SetMethodException) {
final exceptionDescription = exception.getDescriptionFromErrorType(
ErrorMethodResponse.invalidArguments,
);
message = exceptionDescription != null
? AppLocalizations.of(context).invalidArguments(exceptionDescription)
: AppLocalizations.of(context).saveMessageToTemplateFailed;
} else if (cancelToken.isCancelled) {
message = AppLocalizations.of(context).saveMessageToTemplateCancelled;
} else {
message = AppLocalizations.of(context).saveMessageToTemplateFailed;
}
appToast.showToastErrorMessage(context, message);
}
}
}
void clearFocus() {
if (PlatformInfo.isMobile) {
htmlEditorApi?.unfocus();
FocusManager.instance.primaryFocus?.unfocus();
} else {
toAddressFocusNode?.unfocus();
ccAddressFocusNode?.unfocus();
bccAddressFocusNode?.unfocus();
replyToAddressFocusNode?.unfocus();
}
}
void clickOutsideComposer() {
clearFocus();
triggerHideRecipientsFieldsWhenUnfocus();
if (PlatformInfo.isWeb) {
refocusKeyboardShortcutFocus();
}
}
Future<void> _closeComposerAction({dynamic result, bool closeOverlays = false}) async {
if (PlatformInfo.isWeb && richTextWebController != null) {
mailboxDashBoardController.updateTextFormattingMenuState(
richTextWebController!.isFormattingOptionsEnabled,
);
}
if (PlatformInfo.isAndroid) {
await markCleanClose();
}
mailboxDashBoardController.closeComposer(
result: result,
closeOverlays: closeOverlays,
composerId: composerId,
);
}
void displayScreenTypeComposerAction(ScreenDisplayMode displayMode) async {
if (screenDisplayMode.value.isNotContentVisible()) {
_isEmailBodyLoaded = false;
}
if (richTextWebController?.isCodeViewBusy == false && !screenDisplayMode.value.isNotContentVisible()) {
final textCurrent = await richTextWebController!.editorController.getText();
if (richTextWebController?.isCodeViewBusy != true) {
richTextWebController!.editorController.setText(textCurrent);
}
}
setScreenDisplayMode(displayMode);
updateDisplayModeForComposerQueue(displayMode);
await Future.delayed(
const Duration(milliseconds: 300),
_autoFocusFieldWhenLauncher);
}
void addEmailAddressType(PrefixEmailAddress prefixEmailAddress) {
switch(prefixEmailAddress) {
case PrefixEmailAddress.from:
fromRecipientState.value = PrefixRecipientState.enabled;
break;
case PrefixEmailAddress.to:
toRecipientState.value = PrefixRecipientState.enabled;
break;
case PrefixEmailAddress.cc:
ccRecipientState.value = PrefixRecipientState.enabled;
break;
case PrefixEmailAddress.bcc:
bccRecipientState.value = PrefixRecipientState.enabled;
break;
case PrefixEmailAddress.replyTo:
replyToRecipientState.value = PrefixRecipientState.enabled;
break;
}
updatePrefixRootState();
}
void deleteEmailAddressType(PrefixEmailAddress prefixEmailAddress) {
// The removed row's FocusScope is disposed without reporting a blur.
_focusedRecipientScopes.remove(prefixEmailAddress);
updateListEmailAddress(prefixEmailAddress, <EmailAddress>[]);
switch(prefixEmailAddress) {
case PrefixEmailAddress.to:
toRecipientState.value = PrefixRecipientState.disabled;
toAddressFocusNode = FocusNode();
toEmailAddressController.clear();
break;
case PrefixEmailAddress.cc:
ccRecipientState.value = PrefixRecipientState.disabled;
ccAddressFocusNode = FocusNode();
ccEmailAddressController.clear();
break;
case PrefixEmailAddress.bcc:
bccRecipientState.value = PrefixRecipientState.disabled;
bccAddressFocusNode = FocusNode();
bccEmailAddressController.clear();
break;
case PrefixEmailAddress.replyTo:
replyToRecipientState.value = PrefixRecipientState.disabled;
replyToAddressFocusNode = FocusNode();
replyToEmailAddressController.clear();
break;
default:
break;
}
updatePrefixRootState();
}
void clearFocusRecipients() {
toAddressFocusNode?.unfocus();
ccAddressFocusNode?.unfocus();
bccAddressFocusNode?.unfocus();
replyToAddressFocusNode?.unfocus();
}
void clearFocusSubject() {
subjectEmailInputFocusNode?.unfocus();
}
void _closeSuggestionBox() {
if (toEmailAddressController.text.isEmpty) {
keyToEmailTagEditor.currentState?.closeSuggestionBox();
}
if (ccEmailAddressController.text.isEmpty) {
keyCcEmailTagEditor.currentState?.closeSuggestionBox();
}
if (bccEmailAddressController.text.isEmpty) {
keyBccEmailTagEditor.currentState?.closeSuggestionBox();
}
if (replyToEmailAddressController.text.isEmpty) {
keyReplyToEmailTagEditor.currentState?.closeSuggestionBox();
}
}
void onEmailAddressFocusChange(PrefixEmailAddress prefixEmailAddress, bool isFocus) {
if (isFocus) {
_focusedRecipientScopes.add(prefixEmailAddress);
_closeSuggestionBox();
if (PlatformInfo.isMobile
&& currentContext != null
&& !responsiveUtils.isScreenWithShortestSide(currentContext!)) {
richTextMobileTabletController?.richTextController.hideRichTextView();
}
} else {
_focusedRecipientScopes.remove(prefixEmailAddress);
switch(prefixEmailAddress) {
case PrefixEmailAddress.to:
final inputToEmail = toEmailAddressController.text;
if (inputToEmail.trim().isNotEmpty) {
autoCreateEmailTagForType(PrefixEmailAddress.to, inputToEmail);
}
break;
case PrefixEmailAddress.cc:
final inputCcEmail = ccEmailAddressController.text;
if (inputCcEmail.trim().isNotEmpty) {
autoCreateEmailTagForType(PrefixEmailAddress.cc, inputCcEmail);
}
break;
case PrefixEmailAddress.bcc:
final inputBccEmail = bccEmailAddressController.text;
if (inputBccEmail.trim().isNotEmpty) {
autoCreateEmailTagForType(PrefixEmailAddress.bcc, inputBccEmail);
}
break;
case PrefixEmailAddress.replyTo:
final inputReplyToEmail = replyToEmailAddressController.text;
if (inputReplyToEmail.trim().isNotEmpty) {
autoCreateEmailTagForType(PrefixEmailAddress.replyTo, inputReplyToEmail);
}
break;
default:
break;
}
}
}
Future<void> selectIdentity(Identity? newIdentity) async {
final formerIdentity = identitySelected.value;
identitySelected.value = newIdentity;
if (newIdentity == null) return;
await _applyIdentityForAllFieldComposer(formerIdentity, newIdentity);
}
Future<void> _applyIdentityForAllFieldComposer(
Identity? formerIdentity,
Identity newIdentity
) async {
if (formerIdentity != null) {
if (formerIdentity.bcc?.isNotEmpty == true) {
_removeBccEmailAddressFromFormerIdentity(formerIdentity.bcc!);
}
await _removeSignature();
}
if (newIdentity.bcc?.isNotEmpty == true) {
_applyBccEmailAddressFromIdentity(newIdentity.bcc!);
}
if (newIdentity.signatureAsString.isNotEmpty == true) {
await applySignature(newIdentity.signatureAsString.asSignatureHtml());
}
if (PlatformInfo.isMobile) {
await htmlEditorApi?.onDocumentChanged();
}
}
void _applyBccEmailAddressFromIdentity(Set<EmailAddress> listEmailAddress) {
if (bccRecipientState.value == PrefixRecipientState.disabled) {
bccRecipientState.value = PrefixRecipientState.enabled;
}
if (composerArguments.value?.emailActionType == EmailActionType.composeFromMailtoUri) {
listBccEmailAddress = {...listEmailAddress, ...?composerArguments.value?.bcc}.toList();
} else {
listBccEmailAddress = listEmailAddress.toList();
}
updateStatusEmailSendButton();
}
void _removeBccEmailAddressFromFormerIdentity(Set<EmailAddress> listEmailAddress) {
listBccEmailAddress = listBccEmailAddress
.where((address) => !listEmailAddress.contains(address))
.toList();
if (listBccEmailAddress.isEmpty) {
bccRecipientState.value = PrefixRecipientState.disabled;
}
updateStatusEmailSendButton();
}
Future<void> applySignature(String signature) async {
final normalizedSignature = await _normalizeSignature(signature);
if (normalizedSignature == null) {
restoringSignatureButton = false;
return;
}
if (PlatformInfo.isWeb) {
richTextWebController?.editorController.insertSignature(
normalizedSignature,
allowCollapsed: false,
);
} else {
await htmlEditorApi?.insertSignature(
normalizedSignature,
allowCollapsed: false,
);
}
}
/// Identity signatures are stored raw on the server (other clients,
/// provisioning) and the editor inserts them with `innerHTML`, so only the
/// sanitized signature may reach it; a degenerate `line-height` (e.g.
/// `0.1`) is normalized as well. Fail-closed: any failure, unexpected
/// state, exception or blank result (a signature made only of active
/// content) returns null and no signature is inserted.
Future<String?> _normalizeSignature(String signature) async {
try {
final resultState = await _transformHtmlEmailContentInteractor
.execute(signature, TransformConfiguration.forComposerSignature())
.last;
return resultState.fold<String?>(
(failure) => _noSignatureInserted('sanitizing failed: $failure'),
(success) {
if (success is! TransformHtmlEmailContentSuccess) {
return _noSignatureInserted('unexpected state ${success.runtimeType}');
}
// The transform returns a complete document while the editor
// expects a fragment.
final normalizedSignature = success.htmlContent.toHtmlFragment();
return normalizedSignature.trim().isNotEmpty
? normalizedSignature
: _noSignatureInserted('signature is empty after sanitizing');
},
);
} catch (e) {
return _noSignatureInserted('exception while sanitizing', exception: e);
}
}
/// Logs why the composer inserted no signature (fail-closed) and returns
/// null. Forced to the web console so it is visible in profile/release web
/// builds, where a stripped signature would otherwise be silent.
String? _noSignatureInserted(String reason, {Object? exception}) {
logError(
'ComposerController::_normalizeSignature: $reason, no signature inserted',
exception: exception,
webConsoleEnabled: true,
);
return null;
}
Future<void> _removeSignature() async {
try {
if (PlatformInfo.isWeb) {
// The script reports the new body through the editor's own change event.
richTextWebController?.editorController
.evaluateJavascriptWeb(HtmlUtils.removeAllSignatures.name);
} else {
await htmlEditorApi?.webViewController
.evaluateJavascript(source: HtmlUtils.removeAllSignatures.script);
}
} catch (e) {
logWarning('ComposerController::_removeSignature: $e');
}
}
Future<void> insertImage(BuildContext context, double maxWith) async {
if (PlatformInfo.isMobile) {
try {
await htmlEditorApi?.storeSelectionRange();
} catch (e) {
log('ComposerController::insertImage(): $e');
}
}
clearFocus();
if (context.mounted) {
if (responsiveUtils.isMobile(context)) {
maxWithEditor = maxWith - 40;
} else {
maxWithEditor = maxWith - 70;
}
}
consumeState(_localImagePickerInteractor.execute());
}
void _handleUploadInlineSuccess(SuccessAttachmentUploadState uploadState) {
uploadController.clearUploadInlineViewState();
String? baseDownloadUrl;
try {
baseDownloadUrl = mailboxDashBoardController.sessionCurrent?.getDownloadUrl(jmapUrl: dynamicUrlInterceptors.jmapUrl);
} catch (e) {
logWarning('ComposerController::_handleUploadInlineSuccess(): $e');
}
final accountId = composerAccountId;
if (baseDownloadUrl != null && accountId != null) {
final imageUrl = uploadState.attachment.getDownloadUrl(baseDownloadUrl, accountId);
log('ComposerController::_handleUploadInlineSuccess(): imageUrl: $imageUrl');
consumeState(_downloadImageAsBase64Interactor.execute(
imageUrl,
uploadState.attachment.cid!,
uploadState.fileInfo,
options: (maxWidth: maxWithEditor, compress: null),
));
} else {
log('ComposerController::_handleUploadInlineFailure(): baseDownloadUrl: $baseDownloadUrl, accountId: $accountId');
consumeState(Stream.value(Left(DownloadImageAsBase64Failure(e))));
}
}
void handleClickDeleteComposer() {
clearFocus();
_closeComposerAction();
}
Future<void> _onEditorFocusOnMobile() async {
if (PlatformInfo.isAndroid) {
if (FocusManager.instance.primaryFocus?.hasFocus == true) {
FocusManager.instance.primaryFocus?.unfocus();
}
await Future.delayed(
const Duration(milliseconds: 300),
richTextMobileTabletController?.richTextController.showDeviceKeyboard);
}
autoCreateEmailTag();
triggerHideRecipientsFieldsWhenUnfocus();
}
void _onChangeCursorOnMobile(List<int>? coordinates, BuildContext context) {
final headerEditorMobileWidgetRenderObject = headerEditorMobileWidgetKey.currentContext?.findRenderObject();
if (headerEditorMobileWidgetRenderObject is RenderBox?) {
final headerEditorMobileSize = headerEditorMobileWidgetRenderObject?.size;
if (coordinates?[1] != null && coordinates?[1] != 0) {
final coordinateY = max((coordinates?[1] ?? 0) - defaultPaddingCoordinateYCursorEditor, 0);
final realCoordinateY = coordinateY + (headerEditorMobileSize?.height ?? 0);
final outsideHeight = Get.height - MediaQuery.viewInsetsOf(context).bottom - ComposerStyle.keyboardToolBarHeight;
final webViewEditorClientY = max(outsideHeight, 0) + scrollController.position.pixels;
if (scrollController.position.pixels >= realCoordinateY) {
_scrollToCursorEditor(
realCoordinateY.toDouble(),
headerEditorMobileSize?.height ?? 0,
context,
);
} else if ((realCoordinateY) >= webViewEditorClientY) {
_scrollToCursorEditor(
realCoordinateY.toDouble(),
headerEditorMobileSize?.height ?? 0,
context,
);
}
}
}
}
void _scrollToCursorEditor(
double realCoordinateY,
double headerEditorMobileHeight,
BuildContext context,
) {
final scrollTarget = realCoordinateY -
(responsiveUtils.isLandscapeMobile(context)
? 0
: headerEditorMobileHeight / 2);
final maxScrollExtend = scrollController.position.maxScrollExtent;
scrollController.jumpTo(min(scrollTarget, maxScrollExtend));
}
void _onEnterKeyDown() {
if(scrollController.position.pixels < scrollController.position.maxScrollExtent) {
scrollController.animateTo(
scrollController.position.pixels + 20,
duration: const Duration(milliseconds: 1),
curve: Curves.linear,
);
}
}
void toggleRequestReadReceipt(BuildContext context) {
hasRequestReadReceipt.toggle();
appToast.showToastSuccessMessage(
context,
hasRequestReadReceipt.isTrue
? AppLocalizations.of(context).requestReadReceiptHasBeenEnabled
: AppLocalizations.of(context).requestReadReceiptHasBeenDisabled);
}
Future<void> _autoFocusFieldWhenLauncher() async {
if (await hasInputFieldFocused()) {
log('ComposerController::_autoFocusFieldWhenLauncher: INPUT_FIELD_FOCUS = true');
return;
}
if (FocusManager.instance.primaryFocus?.hasFocus == true) {
FocusManager.instance.primaryFocus?.unfocus();
}
if (listToEmailAddress.isEmpty) {
toAddressFocusNode?.requestFocus();
} else if (subjectEmailInputController.text.isEmpty) {
subjectEmailInputFocusNode?.requestFocus();
} else if (PlatformInfo.isWeb) {
richTextWebController?.editorController.setFocus();
} else if (PlatformInfo.isIOS) {
await richTextMobileTabletController?.htmlEditorApi?.requestFocus();
}
}
@visibleForTesting
Future<bool> hasInputFieldFocused() async {
if (PlatformInfo.isWeb) {
return _focusedRecipientScopes.isNotEmpty ||
toAddressFocusNode?.hasFocus == true ||
ccAddressFocusNode?.hasFocus == true ||
bccAddressFocusNode?.hasFocus == true ||
replyToAddressFocusNode?.hasFocus == true ||
subjectEmailInputFocusNode?.hasFocus == true;
} else if (PlatformInfo.isMobile) {
final isEditorFocused = (await richTextMobileTabletController?.isEditorFocused) ?? false;
return _focusedRecipientScopes.isNotEmpty ||
toAddressFocusNode?.hasFocus == true ||
ccAddressFocusNode?.hasFocus == true ||
bccAddressFocusNode?.hasFocus == true ||
replyToAddressFocusNode?.hasFocus == true ||
subjectEmailInputFocusNode?.hasFocus == true ||
isEditorFocused;
}
return false;
}
void handleInitHtmlEditorWeb(String initContent) {
if (_isEmailBodyLoaded) return;
_isEmailBodyLoaded = true;
richTextWebController?.editorController.setFullScreen();
richTextWebController?.editorController.setOnDragDropEvent();
richTextWebController?.setEnableCodeView();
setTextEditorWeb(initContent);
}
Future<void> onInitialContentLoadCompleteWeb(String? initContent) async {
await restoreCollapsibleSignatureButton(initContent);
await setupSelectedIdentity();
_autoFocusFieldWhenLauncher();
}
void handleOnFocusHtmlEditorWeb() {
// This handler only ever runs because the html editor's own native DOM
// element just gained focus (wired exclusively to the editor widget's
// `onFocus` callback), so calling editorController.setFocus() again is
// redundant unless something else in this method actually diverted
// focus away in the meantime. Calling it unconditionally created a
// steady stream of redundant async round-trips (postMessage to the
// iframe) that could resolve later, at an unrelated moment — e.g. right
// after a *different* click's native mousedown-triggered blur — at
// which point Summernote's `hasFocus() || focus()` finds the editable
// blurred and performs a real, un-prevented native `.focus()`, which
// the browser auto-scrolls into view and can shift page content
// mid-click so the click misses its target. Only re-assert focus if we
// actually took focus away from something else in this same call.
final recipientsWereFocused = toAddressFocusNode?.hasFocus == true ||
ccAddressFocusNode?.hasFocus == true ||
bccAddressFocusNode?.hasFocus == true ||
replyToAddressFocusNode?.hasFocus == true;
clearFocusRecipients();
final subjectWasFocused = subjectEmailInputFocusNode?.hasFocus == true;
clearFocusSubject();
// `FocusScopeNode`s (root scope, Navigator scope, modal route scope,
// etc.) are ambient focus-tree scaffolding, not a real focused widget —
// only a genuine leaf FocusNode counts as "something else was focused".
final primaryFocus = FocusManager.instance.primaryFocus;
var primaryWasUnfocused = false;
if (primaryFocus != null && primaryFocus is! FocusScopeNode) {
primaryFocus.unfocus();
primaryWasUnfocused = true;
}
if (richTextWebController?.codeViewEnabled != true &&
(recipientsWereFocused || subjectWasFocused || primaryWasUnfocused)) {
richTextWebController?.editorController.setFocus();
}
richTextWebController?.closeAllMenuPopup();
if (menuMoreOptionController?.menuIsShowing == true) {
menuMoreOptionController?.hideMenu();
}
if (mailboxDashBoardController.isPopupMenuOpened.isTrue) {
popBack();
}
autoCreateEmailTag();
triggerHideRecipientsFieldsWhenUnfocus();
}
FocusNode? getNextFocusOfToEmailAddress() {
if (ccRecipientState.value == PrefixRecipientState.enabled) {
return ccAddressFocusNode;
} else if (bccRecipientState.value == PrefixRecipientState.enabled) {
return bccAddressFocusNode;
} else if (replyToRecipientState.value == PrefixRecipientState.enabled) {
return replyToAddressFocusNode;
} else {
return subjectEmailInputFocusNode;
}
}
FocusNode? getNextFocusOfCcEmailAddress() {
if (bccRecipientState.value == PrefixRecipientState.enabled) {
return bccAddressFocusNode;
} else if (replyToRecipientState.value == PrefixRecipientState.enabled) {
return replyToAddressFocusNode;
} else {
return subjectEmailInputFocusNode;
}
}
FocusNode? getNextFocusOfBccEmailAddress() {
if (replyToRecipientState.value == PrefixRecipientState.enabled) {
return replyToAddressFocusNode;
} else {
return subjectEmailInputFocusNode;
}
}
void handleFocusNextAddressAction() {
autoCreateEmailTag();
}
bool get isNetworkConnectionAvailable => networkConnectionController.isNetworkConnectionAvailable();
String? get textEditorWeb => _webBodyCache.read();
void setTextEditorWeb(String value) => _webBodyCache.setTrusted(value);
HtmlEditorApi? get htmlEditorApi => richTextMobileTabletController?.htmlEditorApi;
void onChangeTextEditorWeb(String? text) {
_webBodyCache.onEditorChange(text, inCodeView: richTextWebController?.codeViewEnabled ?? false);
if (restoringSignatureButton ||
(_isNewComposition && !synchronizeInitDraftHash)) {
synchronizeInitEmailDraftHash(text);
}
}
void setSubjectEmail(String subject) => subjectEmail.value = subject;
Future<void> onAttachmentDropZoneListener(BuildContext context, Attachment attachment) {
log('ComposerController::onAttachmentDropZoneListener: attachment = $attachment');
return attachmentUploadValidationService.validateAttachment(
context: context,
attachment: attachment,
onAllowed: () => uploadController.initializeUploadAttachments([attachment]));
}
Future<void> onChangeIdentity(Identity? newIdentity) async {
await selectIdentity(newIdentity);
}
void _searchIdentities(String searchText) {
if (searchText.isEmpty) {
_getAllIdentities();
} else {
listFromIdentities.value = listFromIdentities
.where((identity) => identity.name?.toLowerCase().contains(searchText.toLowerCase()) == true)
.toList();
}
}
void openSelectIdentityBottomSheet(BuildContext context) {
(
FromComposerBottomSheetBuilder(
context,
imagePaths,
listFromIdentities,
scrollControllerIdentities,
searchIdentitiesInputController
)
..onCloseAction(() => popBack())
..onChangeIdentityAction((identity) {
onChangeIdentity(identity);
popBack();
})
..onTextSearchChangedAction((searchText) => _searchIdentities(searchText))
).build();
}
void handleClickCloseComposer(BuildContext context) async {
log('ComposerController::handleClickCloseComposer:');
if (_closeComposerButtonState == ButtonState.disabled) {
log('ComposerController::handleClickCloseComposer: _closeComposerButtonState = disabled');
return;
}
_closeComposerButtonState = ButtonState.disabled;
autoCreateEmailTag();
if (_validateCloseComposerWithoutSave()) {
log('ComposerController::handleClickCloseComposer: ARGUMENTS is NULL or EMAIL NOT LOADED');
_closeComposerButtonState = ButtonState.enabled;
clearFocus();
_closeComposerAction();
return;
}
final isChanged = await _validateEmailChange();
if (isChanged && context.mounted) {
clearFocus();
await _showConfirmDialogSaveMessage(context);
return;
}
_closeComposerButtonState = ButtonState.enabled;
clearFocus();
_closeComposerAction();
}
bool _validateCloseComposerWithoutSave() {
if (composerArguments.value == null) return true;
if (PlatformInfo.isWeb &&
!_isEmailBodyLoaded &&
!screenDisplayMode.value.isNotContentVisible()) {
return true;
}
if (PlatformInfo.isMobile && !_isEmailBodyLoaded) return true;
return false;
}
Future<void> _showConfirmDialogSaveMessage(BuildContext context) async {
await MessageDialogActionManager().showConfirmDialogAction(
context,
title: AppLocalizations.of(context).saveMessage.capitalizeFirstEach,
AppLocalizations.of(context).warningMessageWhenClickCloseComposer,
AppLocalizations.of(context).save,
cancelTitle: AppLocalizations.of(context).discardChanges,
alignCenter: true,
autoPerformPopBack: false,
isArrangeActionButtonsVertical: true,
isScrollContentEnabled: responsiveUtils.isLandscapeMobile(context),
usePopScope: true,
onConfirmAction: () => _handleSaveMessageToDraft(context),
onCancelAction: () {
_closeComposerButtonState = ButtonState.enabled;
_closeComposerAction(closeOverlays: true);
},
onCloseButtonAction: () {
_closeComposerButtonState = ButtonState.enabled;
popBack();
_autoFocusFieldWhenLauncher();
},
onPopInvoked: (didPop, _) {
log('ComposerController::_showConfirmDialogSaveMessage: didPop = $didPop');
if (!didPop) {
_closeComposerButtonState = ButtonState.enabled;
popBack();
_autoFocusFieldWhenLauncher();
}
},
);
_closeComposerButtonState = ButtonState.enabled;
}
void handleOnDragEnterHtmlEditorWeb(List<dynamic>? types) {
if (types.validateFilesTransfer) {
mailboxDashBoardController.localFileDraggableAppState.value = DraggableAppState.active;
}
}
void handleOnDragOverHtmlEditorWeb(List<dynamic>? types) {
if (types.validateFilesTransfer) {
mailboxDashBoardController.localFileDraggableAppState.value = DraggableAppState.active;
}
}
void onLocalFileDropZoneListener({
required BuildContext context,
required DropDoneDetails details,
required double maxWidth
}) async {
mailboxDashBoardController.localFileDraggableAppState.value = DraggableAppState.inActive;
_setUpMaxWidthInlineImage(context: context, maxWidth: maxWidth);
final droppedFiles = await onDragDone(context: context, details: details);
if (!context.mounted) return;
final listFileInfo = droppedFiles.files;
final folderCount = droppedFiles.folderCount;
if (listFileInfo.isEmpty) {
final onlyFolders = folderCount > 0 && folderCount == details.files.length;
appToast.showToastErrorMessage(
context,
onlyFolders
? AppLocalizations.of(context).cannotUploadFoldersAsAttachments(folderCount)
: AppLocalizations.of(context).can_not_upload_this_file_as_attachments
);
return;
}
if (folderCount > 0) {
appToast.showToastErrorMessage(
context,
AppLocalizations.of(context).cannotUploadFoldersAsAttachments(folderCount));
}
await attachmentUploadValidationService.validateFiles(
context: context,
files: listFileInfo,
onAllowed: () => uploadAttachmentsAction(pickedFiles: listFileInfo));
}
void _handleSaveMessageToDraft(BuildContext context) async {
final arguments = composerArguments.value;
final session = mailboxDashBoardController.sessionCurrent;
final accountId = composerAccountId;
if (arguments == null ||
session == null ||
accountId == null ||
getDraftMailboxIdForComposer() == null
) {
log('ComposerController::_handleSaveMessageToDraft: SESSION or ACCOUNT_ID or ARGUMENTS is NULL');
_closeComposerButtonState = ButtonState.enabled;
_closeComposerAction(closeOverlays: true);
return;
}
popBack();
final emailContent = await getContentInEditor();
final uploadUri = _getUploadUriFromSession(session, accountId);
final draftEmailId = getDraftEmailId();
log('ComposerController::_handleSaveMessageToDraft: draftEmailId = $draftEmailId');
final cancelToken = CancelToken();
final resultState = await _showSavingMessageToDraftsDialog(
session: session,
accountId: accountId,
arguments: arguments,
emailContent: emailContent,
uploadUri: uploadUri,
draftEmailId: draftEmailId,
cancelToken: cancelToken,
isUpdateDraftToClose: true,
);
if (resultState is SaveEmailAsDraftsSuccess || resultState is UpdateEmailDraftsSuccess) {
_closeComposerButtonState = ButtonState.enabled;
_closeComposerAction(result: resultState);
} else if ((resultState is SaveEmailAsDraftsFailure && resultState.exception is SavingEmailToDraftsCanceledException) ||
(resultState is UpdateEmailDraftsFailure && resultState.exception is SavingEmailToDraftsCanceledException)) {
_closeComposerButtonState = ButtonState.enabled;
} else if (resultState is SaveEmailAsDraftsFailure ||
resultState is UpdateEmailDraftsFailure ||
resultState is GenerateEmailFailure
) {
if (resultState.exception is BadCredentialsException) {
_closeComposerButtonState = ButtonState.enabled;
handleBadCredentialsException();
} else if (context.mounted) {
await _showConfirmDialogWhenSaveMessageToDraftsFailure(
context: context,
failure: resultState,
);
} else {
_closeComposerButtonState = ButtonState.enabled;
}
} else {
_closeComposerButtonState = ButtonState.enabled;
}
}
EmailId? getDraftEmailId() {
if (emailIdEditing != null &&
emailIdEditing != composerArguments.value!.presentationEmail?.id) {
return emailIdEditing;
} else if (currentEmailActionType == EmailActionType.editDraft) {
return composerArguments.value!.presentationEmail?.id;
} else {
return null;
}
}
Future<dynamic> _showSavingMessageToDraftsDialog({
required Session session,
required AccountId accountId,
required ComposerArguments arguments,
required String emailContent,
required Uri? uploadUri,
EmailId? draftEmailId,
CancelToken? cancelToken,
bool isUpdateDraftToClose = false,
}) {
final childWidget = PointerInterceptor(
child: SavingMessageDialogView(
createEmailRequest: CreateEmailRequest(
session: session,
accountId: accountId,
emailActionType: arguments.emailActionType,
ownEmailAddress: ownEmailAddress,
subject: subjectEmail.value ?? '',
emailContent: emailContent,
fromSender: arguments.presentationEmail?.from ?? {},
toRecipients: listToEmailAddress.toSet(),
ccRecipients: listCcEmailAddress.toSet(),
bccRecipients: listBccEmailAddress.toSet(),
replyToRecipients: listReplyToEmailAddress.toSet(),
hasRequestReadReceipt: hasRequestReadReceipt.value,
isMarkAsImportant: isMarkAsImportant.value,
identity: identitySelected.value,
attachments: uploadController.attachmentsUploaded,
inlineAttachments: uploadController.mapInlineAttachments,
sentMailboxId: getSentMailboxIdForComposer(),
draftsMailboxId: getDraftMailboxIdForComposer(),
draftsEmailId: draftEmailId,
answerForwardEmailId: arguments.presentationEmail?.id,
unsubscribeEmailId: arguments.previousEmailId,
messageId: arguments.messageId,
references: arguments.references,
emailSendingQueue: arguments.sendingEmail,
displayMode: screenDisplayMode.value,
uploadUri: uploadUri,
isUpdateDraftToClose: isUpdateDraftToClose,
),
createNewAndSaveEmailToDraftsInteractor: _createNewAndSaveEmailToDraftsInteractor,
onCancelSavingEmailToDraftsAction: _handleCancelSavingMessageToDrafts,
cancelToken: cancelToken,
),
);
return Get.dialog(
PlatformInfo.isMobile
? PopScope(canPop: false, child: childWidget)
: childWidget,
barrierDismissible: false,
barrierColor: AppColor.colorDefaultCupertinoActionSheet,
);
}
Future<dynamic> _showSavingMessageToTemplateDialog({
required String emailContent,
required MailboxId? templateMailboxId,
required EmailId? templateEmailId,
required CreateNewMailboxRequest? createNewMailboxRequest,
CancelToken? cancelToken,
}) {
final childWidget = PointerInterceptor(
child: SavingTemplateDialogView(
createEmailRequest: CreateEmailRequest(
session: mailboxDashBoardController.sessionCurrent!,
accountId: composerAccountId!,
emailActionType: composerArguments.value!.emailActionType,
ownEmailAddress: ownEmailAddress,
subject: subjectEmail.value ?? '',
emailContent: emailContent,
fromSender: composerArguments.value!.presentationEmail?.from ?? {},
toRecipients: listToEmailAddress.toSet(),
ccRecipients: listCcEmailAddress.toSet(),
bccRecipients: listBccEmailAddress.toSet(),
replyToRecipients: listReplyToEmailAddress.toSet(),
hasRequestReadReceipt: hasRequestReadReceipt.value,
isMarkAsImportant: isMarkAsImportant.value,
identity: identitySelected.value,
attachments: uploadController.attachmentsUploaded,
inlineAttachments: uploadController.mapInlineAttachments,
sentMailboxId: getSentMailboxIdForComposer(),
templateMailboxId: templateMailboxId,
templateEmailId: templateEmailId,
answerForwardEmailId: composerArguments.value!.presentationEmail?.id,
unsubscribeEmailId: composerArguments.value!.previousEmailId,
messageId: composerArguments.value!.messageId,
references: composerArguments.value!.references,
emailSendingQueue: composerArguments.value!.sendingEmail,
displayMode: screenDisplayMode.value
),
saveTemplateEmailInteractor: _saveTemplateEmailInteractor,
createNewMailboxRequest: createNewMailboxRequest,
onCancel: (cancelToken) => cancelToken?.cancel(),
cancelToken: cancelToken,
),
);
return Get.dialog(
PlatformInfo.isMobile
? PopScope(canPop: false, child: childWidget)
: childWidget,
barrierDismissible: false,
barrierColor: AppColor.colorDefaultCupertinoActionSheet,
);
}
void _handleCancelSavingMessageToDrafts({CancelToken? cancelToken}) {
cancelToken?.cancel([SavingEmailToDraftsCanceledException()]);
}
Future<void> _showConfirmDialogWhenSaveMessageToDraftsFailure({
required BuildContext context,
required FeatureFailure failure,
bool shouldOfferCloseComposer = true,
VoidCallback? onConfirmAction,
Function(bool)? onCancelAction,
}) async {
final messageRecord = getMessageFailure(
appLocalizations: AppLocalizations.of(context),
exception: failure.exception,
isDraft: true,
);
final providerContainer = ProviderScope.containerOf(context, listen: false);
final needIncreaseMySpace = providerContainer.isPremiumCtaAvailable(
mailboxDashBoardController.currentPremiumCtaContext,
) &&
messageRecord.errorType == SetError.overQuota;
await MessageDialogActionManager().showConfirmDialogAction(
context,
title: '',
messageRecord.message,
needIncreaseMySpace
? AppLocalizations.of(context).increaseYourSpace
: AppLocalizations.of(context).edit,
cancelTitle: needIncreaseMySpace
? AppLocalizations.of(context).edit
: AppLocalizations.of(context).closeAnyway,
hasCancelButton: shouldOfferCloseComposer || needIncreaseMySpace,
alignCenter: true,
outsideDismissible: false,
autoPerformPopBack: false,
onConfirmAction: () {
if (onConfirmAction == null) {
_closeComposerButtonState = ButtonState.enabled;
} else {
onConfirmAction();
}
popBack();
if (needIncreaseMySpace) {
providerContainer.openPremiumCta(
mailboxDashBoardController.currentPremiumCtaContext,
);
} else {
_autoFocusFieldWhenLauncher();
}
},
onCancelAction: () {
if (onCancelAction != null) {
onCancelAction(needIncreaseMySpace);
} else {
_closeComposerButtonState = ButtonState.enabled;
if (needIncreaseMySpace || !shouldOfferCloseComposer) {
popBack();
_autoFocusFieldWhenLauncher();
} else {
_closeComposerAction(closeOverlays: true);
}
}
},
);
}
@override
Future<void> onBeforeReconnect() async {
if (!PlatformInfo.isWeb) return;
if (composerAccountId != null &&
mailboxDashBoardController.sessionCurrent?.username != null
) {
await _saveComposerSessionCache();
}
}
void _setUpMaxWidthInlineImage({
required BuildContext context,
required double maxWidth
}) {
if (context.mounted && responsiveUtils.isMobile(context)) {
maxWithEditor = maxWidth - 40;
} else {
maxWithEditor = maxWidth - 70;
}
}
void handleOnPasteImageSuccessAction({
required BuildContext context,
required double maxWidth,
required List<FileUpload> listFileUpload
}) async {
log('ComposerController::handleOnPasteImageSuccessAction: listFileUpload = ${listFileUpload.length}');
_setUpMaxWidthInlineImage(context: context, maxWidth: maxWidth);
final listFileInfo = listFileUpload.toListFileInfo();
await attachmentUploadValidationService.validateFiles(
context: context,
files: listFileInfo,
onAllowed: () => uploadAttachmentsAction(pickedFiles: listFileInfo));
}
void handleOnPasteImageFailureAction({
required BuildContext context,
List<FileUpload>? listFileUpload,
String? base64,
required UploadError uploadError
}) {
logWarning('ComposerController::handleOnPasteImageFailureAction: $uploadError');
if (!context.mounted) return;
appToast.showToastErrorMessage(
context,
AppLocalizations.of(context).thisImageCannotBePastedIntoTheEditor);
}
void onCompleteSetupComposer() {
initEmailDraftHash();
}
void onPopupMenuChanged(bool isShowing) {
if (isShowing) {
clearFocusRecipients();
clearFocusSubject();
}
}
}