Skip to content

Commit

Permalink
TF-3308 Implement super_drag_and_drop
Browse files Browse the repository at this point in the history
  • Loading branch information
tddang-linagora committed Dec 6, 2024
1 parent 1bb66f3 commit c9f0f82
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 166 deletions.
94 changes: 51 additions & 43 deletions core/lib/presentation/views/text/text_field_builder.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:core/presentation/extensions/color_extension.dart';
import 'package:core/presentation/views/text/text_drop_zone_web.dart';
import 'package:core/utils/direction_utils.dart';
import 'package:flutter/material.dart';
import 'package:languagetool_textfield/languagetool_textfield.dart';
Expand Down Expand Up @@ -27,6 +28,7 @@ class TextFieldBuilder extends StatefulWidget {
final bool readOnly;
final MouseCursor? mouseCursor;
final LanguageToolController? languageToolController;
final bool dropTextEnabled;

const TextFieldBuilder({
super.key,
Expand All @@ -52,6 +54,7 @@ class TextFieldBuilder extends StatefulWidget {
this.onTapOutside,
this.onTextChange,
this.onTextSubmitted,
this.dropTextEnabled = false,
});

@override
Expand Down Expand Up @@ -85,8 +88,10 @@ class _TextFieldBuilderState extends State<TextFieldBuilder> {

@override
Widget build(BuildContext context) {
Widget child;

if (_languageToolController != null) {
return LanguageToolTextField(
child = LanguageToolTextField(
key: widget.key,
controller: _languageToolController!,
cursorColor: widget.cursorColor,
Expand All @@ -104,55 +109,46 @@ class _TextFieldBuilderState extends State<TextFieldBuilder> {
textDirection: _textDirection,
readOnly: widget.readOnly,
mouseCursor: widget.mouseCursor,
onTextChange: (value) {
widget.onTextChange?.call(value);
if (value.isNotEmpty) {
final directionByText = DirectionUtils.getDirectionByEndsText(value);
if (directionByText != _textDirection) {
setState(() {
_textDirection = directionByText;
});
}
}
},
onTextChange: _onChanged,
onTextSubmitted: widget.onTextSubmitted,
onTap: widget.onTap,
onTapOutside: widget.onTapOutside,
);
} else {
child = TextField(
key: widget.key,
controller: _controller,
cursorColor: widget.cursorColor,
autocorrect: widget.autocorrect,
textInputAction: widget.textInputAction,
decoration: widget.decoration,
maxLines: widget.maxLines,
minLines: widget.minLines,
keyboardAppearance: widget.keyboardAppearance,
style: widget.textStyle,
obscureText: widget.obscureText,
keyboardType: widget.keyboardType,
autofocus: widget.autoFocus,
focusNode: widget.focusNode,
textDirection: _textDirection,
readOnly: widget.readOnly,
mouseCursor: widget.mouseCursor,
onChanged: _onChanged,
onSubmitted: widget.onTextSubmitted,
onTap: widget.onTap,
onTapOutside: widget.onTapOutside,
);
}

return TextField(
key: widget.key,
controller: _controller,
cursorColor: widget.cursorColor,
autocorrect: widget.autocorrect,
textInputAction: widget.textInputAction,
decoration: widget.decoration,
maxLines: widget.maxLines,
minLines: widget.minLines,
keyboardAppearance: widget.keyboardAppearance,
style: widget.textStyle,
obscureText: widget.obscureText,
keyboardType: widget.keyboardType,
autofocus: widget.autoFocus,
focusNode: widget.focusNode,
textDirection: _textDirection,
readOnly: widget.readOnly,
mouseCursor: widget.mouseCursor,
onChanged: (value) {
widget.onTextChange?.call(value);
if (value.isNotEmpty) {
final directionByText = DirectionUtils.getDirectionByEndsText(value);
if (directionByText != _textDirection) {
setState(() {
_textDirection = directionByText;
});
}
}
if (!widget.dropTextEnabled) return child;

return TextDropZoneWeb(
onDrop: (value) {
(_languageToolController ?? _controller)?.text += value;
widget.focusNode?.requestFocus();
_onChanged(value);
},
onSubmitted: widget.onTextSubmitted,
onTap: widget.onTap,
onTapOutside: widget.onTapOutside,
child: child,
);
}

Expand All @@ -166,4 +162,16 @@ class _TextFieldBuilderState extends State<TextFieldBuilder> {
}
super.dispose();
}

void _onChanged(String value) {
widget.onTextChange?.call(value);
if (value.isNotEmpty) {
final directionByText = DirectionUtils.getDirectionByEndsText(value);
if (directionByText != _textDirection) {
setState(() {
_textDirection = directionByText;
});
}
}
}
}
7 changes: 2 additions & 5 deletions lib/features/composer/presentation/composer_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'dart:math';
import 'package:collection/collection.dart';
import 'package:core/core.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';
Expand Down Expand Up @@ -2295,15 +2294,13 @@ class ComposerController extends BaseController
}
}

void onLocalFileDropZoneListener({
Future<void> onSuperDropListener({
required BuildContext context,
required DropDoneDetails details,
required List<FileInfo> listFileInfo,
required double maxWidth
}) async {
_setUpMaxWidthInlineImage(context: context, maxWidth: maxWidth);

final listFileInfo = await onDragDone(context: context, details: details);

if (listFileInfo.isEmpty && context.mounted) {
appToast.showToastErrorMessage(
context,
Expand Down
21 changes: 12 additions & 9 deletions lib/features/composer/presentation/composer_view_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class ComposerView extends GetWidget<ComposerController> {
onTextChange: controller.setSubjectEmail,
padding: ComposerStyle.mobileSubjectPadding,
margin: ComposerStyle.mobileSubjectMargin,
dropTextEnabled: true,
),
Expanded(
child: LayoutBuilder(
Expand Down Expand Up @@ -301,10 +302,10 @@ class ComposerView extends GetWidget<ComposerController> {
imagePaths: controller.imagePaths,
width: constraintsEditor.maxWidth,
height: constraintsEditor.maxHeight,
onLocalFileDropZoneListener: (details) =>
controller.onLocalFileDropZoneListener(
onSuperDrop: (listFileInfo) =>
controller.onSuperDropListener(
context: context,
details: details,
listFileInfo: listFileInfo,
maxWidth: constraintsEditor.maxWidth,
),
)
Expand Down Expand Up @@ -472,6 +473,7 @@ class ComposerView extends GetWidget<ComposerController> {
onTextChange: controller.setSubjectEmail,
padding: ComposerStyle.desktopSubjectPadding,
margin: ComposerStyle.desktopSubjectMargin,
dropTextEnabled: true,
),
Expanded(
child: LayoutBuilder(
Expand Down Expand Up @@ -607,10 +609,10 @@ class ComposerView extends GetWidget<ComposerController> {
imagePaths: controller.imagePaths,
width: constraintsEditor.maxWidth,
height: constraintsEditor.maxHeight,
onLocalFileDropZoneListener: (details) =>
controller.onLocalFileDropZoneListener(
onSuperDrop: (listFileInfo) =>
controller.onSuperDropListener(
context: context,
details: details,
listFileInfo: listFileInfo,
maxWidth: constraintsEditor.maxWidth,
),
)
Expand Down Expand Up @@ -779,6 +781,7 @@ class ComposerView extends GetWidget<ComposerController> {
onTextChange: controller.setSubjectEmail,
padding: ComposerStyle.tabletSubjectPadding,
margin: ComposerStyle.tabletSubjectMargin,
dropTextEnabled: true,
),
Expanded(
child: LayoutBuilder(
Expand Down Expand Up @@ -912,10 +915,10 @@ class ComposerView extends GetWidget<ComposerController> {
imagePaths: controller.imagePaths,
width: constraintsBody.maxWidth,
height: constraintsBody.maxHeight,
onLocalFileDropZoneListener: (details) =>
controller.onLocalFileDropZoneListener(
onSuperDrop: (listFileInfo) =>
controller.onSuperDropListener(
context: context,
details: details,
listFileInfo: listFileInfo,
maxWidth: constraintsBody.maxWidth,
),
)
Expand Down
33 changes: 0 additions & 33 deletions lib/features/composer/presentation/mixin/drag_drog_file_mixin.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import 'dart:async' as async;
import 'package:async/async.dart';
import 'package:core/data/constants/constant.dart';
import 'package:desktop_drop/desktop_drop.dart';
import 'package:flutter/material.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:model/upload/file_info.dart';
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';

mixin DragDropFileMixin {
Expand All @@ -19,34 +16,4 @@ mixin DragDropFileMixin {
future: future,
);
}

async.Future<List<FileInfo>> onDragDone({
required BuildContext context,
required DropDoneDetails details
}) async {
final bytesList = await showFutureLoadingDialogFullScreen(
context: context,
future: () => async.Future.wait(
details.files.map(
(xFile) => xFile.readAsBytes(),
),
),
);

if (bytesList.error != null) return [];

final listFileInfo = <FileInfo>[];
for (var i = 0; i < bytesList.result!.length; i++) {
listFileInfo.add(
FileInfo(
bytes: bytesList.result![i],
fileName: details.files[i].name,
type: details.files[i].mimeType,
fileSize: bytesList.result![i].length,
isInline: details.files[i].mimeType?.startsWith(Constant.imageType) == true
),
);
}
return listFileInfo;
}
}
Loading

0 comments on commit c9f0f82

Please sign in to comment.