Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TF-2118 Support drag n drop for Image/File #2183

Merged
merged 4 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/lib/domain/extensions/media_type_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ extension MediaTypeExtension on MediaType {

bool isImageFile() => SupportedPreviewFileTypes.imageMimeTypes.contains(mimeType);

bool isImageValid() => type == 'image';
hoangdat marked this conversation as resolved.
Show resolved Hide resolved

bool isDocFile() => SupportedPreviewFileTypes.docMimeTypes.contains(mimeType);

bool isPowerPointFile() => SupportedPreviewFileTypes.pptMimeTypes.contains(mimeType);
Expand Down
94 changes: 94 additions & 0 deletions lib/features/composer/presentation/composer_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:html_editor_enhanced/html_editor.dart' as web_html_editor;
import 'package:http_parser/http_parser.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/id.dart';
Expand Down Expand Up @@ -48,6 +49,7 @@ import 'package:tmail_ui_user/features/composer/domain/usecases/update_email_dra
import 'package:tmail_ui_user/features/composer/presentation/controller/rich_text_web_controller.dart';
import 'package:tmail_ui_user/features/composer/presentation/controller/rich_text_mobile_tablet_controller.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/email_action_type_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/file_upload_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/extensions/list_identities_extension.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/draggable_email_address.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/image_source.dart';
Expand Down Expand Up @@ -1838,6 +1840,98 @@ class ComposerController extends BaseController {
onEditorFocusChange(true);
}

void handleImageUploadSuccess (
BuildContext context,
web_html_editor.FileUpload fileUpload
) async {
log('ComposerController::handleImageUploadSuccess:NAME: ${fileUpload.name} | TYPE: ${fileUpload.type} | SIZE: ${fileUpload.size}');
if (fileUpload.base64 == null) {
_appToast.showToastErrorMessage(
context,
AppLocalizations.of(context).can_not_upload_this_file_as_attachments
);
return;
}

if (fileUpload.type == null) {
final fileInfo = await fileUpload.toFileInfo();
if (fileInfo != null) {
_addAttachmentFromDragAndDrop(fileInfo: fileInfo);
} else if (context.mounted) {
_appToast.showToastErrorMessage(
context,
AppLocalizations.of(context).can_not_upload_this_file_as_attachments
);
}
return;
}

final mediaType = MediaType.parse(fileUpload.type!);
if (mediaType.isImageValid()) {
_addInlineImageFromDragAndDrop(
base64Data: fileUpload.base64!,
name: fileUpload.name,
type: mediaType,
size: fileUpload.size,
);
} else {
final fileInfo = await fileUpload.toFileInfo();
if (fileInfo != null) {
_addAttachmentFromDragAndDrop(fileInfo: fileInfo);
} else if (context.mounted) {
_appToast.showToastErrorMessage(
context,
AppLocalizations.of(context).can_not_upload_this_file_as_attachments
);
}
}
}

void _addInlineImageFromDragAndDrop({
required String base64Data,
String? name,
MediaType? type,
int? size
}) {
log('ComposerController::_addInlineImageFromDragAndDrop:name: $name');
richTextWebController.insertInlineImage(
base64Data: base64Data,
name: name,
type: type,
size: size,
);
}

void handleImageUploadFailure({
required BuildContext context,
required web_html_editor.UploadError uploadError,
web_html_editor.FileUpload? fileUpload,
String? base64Str,
}) {
logError('ComposerController::handleImageUploadFailure:fileUpload: $fileUpload | uploadError: $uploadError');
_appToast.showToastErrorMessage(
context,
'${AppLocalizations.of(context).can_not_upload_this_file_as_attachments}. (${uploadError.name})'
);
}

void _addAttachmentFromDragAndDrop({required FileInfo fileInfo}) {
if (uploadController.hasEnoughMaxAttachmentSize(listFiles: [fileInfo])) {
_uploadAttachmentsAction([fileInfo]);
} else {
if (currentContext != null) {
showConfirmDialogAction(
currentContext!,
AppLocalizations.of(currentContext!).message_dialog_upload_attachments_exceeds_maximum_size(
filesize(mailboxDashBoardController.maxSizeAttachmentsPerEmail?.value ?? 0, 0)),
AppLocalizations.of(currentContext!).got_it,
title: AppLocalizations.of(currentContext!).maximum_files_size,
hasCancelButton: false,
);
}
}
}

FocusNode? getNextFocusOfToEmailAddress() {
if (ccRecipientState.value == PrefixRecipientState.enabled) {
return ccAddressFocusNode;
Expand Down
Loading
Loading