Skip to content

Commit

Permalink
TW-1615: Support auto jump to lastest message when user send an attac…
Browse files Browse the repository at this point in the history
…hments (#1810)

* TW-1615: Support auto jump to lastest message when user send an attachment

* TW-1615: Support auto jump to lastest message when use copy/paste
  • Loading branch information
nqhhdev authored and Te-Z committed Jun 18, 2024
1 parent 5b38b91 commit 4dc05e1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
25 changes: 21 additions & 4 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,12 @@ class ChatController extends State<Chat>

void handleDragDone(DropDoneDetails details) async {
final matrixFiles = await onDragDone(details);
sendFileOnWebAction(context, room: room, matrixFilesList: matrixFiles);
sendFileOnWebAction(
context,
room: room,
matrixFilesList: matrixFiles,
onSendFileCallback: scrollDown,
);
}

void _handleReceivedShareFiles() {
Expand Down Expand Up @@ -1292,7 +1297,12 @@ class ChatController extends State<Chat>
_showMediaPicker(context);
} else {
final matrixFiles = await pickFilesFromSystem();
sendFileOnWebAction(context, room: room, matrixFilesList: matrixFiles);
sendFileOnWebAction(
context,
room: room,
matrixFilesList: matrixFiles,
onSendFileCallback: scrollDown,
);
}
}

Expand All @@ -1309,16 +1319,21 @@ class ChatController extends State<Chat>
type: action,
room: room,
context: context,
onSendFileCallback: scrollDown,
),
onSendTap: () {
sendMedia(
imagePickerController,
room: room,
caption: _captionsController.text,
);
scrollDown();
_captionsController.clear();
},
onCameraPicked: (_) => sendMedia(imagePickerController, room: room),
onCameraPicked: (_) {
sendMedia(imagePickerController, room: room);
scrollDown();
},
captionController: _captionsController,
focusSuggestionController: _focusSuggestionController,
typeAheadKey: _chatMediaPickerTypeAheadKey,
Expand Down Expand Up @@ -1876,7 +1891,9 @@ class ChatController extends State<Chat>
@override
void initState() {
_initializePinnedEvents();
registerPasteShortcutListeners();
registerPasteShortcutListeners(
onSendFileCallback: scrollDown,
);
keyboardVisibilitySubscription =
keyboardVisibilityController.onChange.listen(_keyboardListener);
scrollController.addListener(_updateScrollController);
Expand Down
23 changes: 19 additions & 4 deletions lib/presentation/mixins/handle_clipboard_action_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,38 @@ mixin HandleClipboardActionMixin on PasteImageMixin {

TextEditingController get sendController;

void registerPasteShortcutListeners() {
ClipboardEvents.instance?.registerPasteEventListener(_onPasteEvent);
void registerPasteShortcutListeners({
VoidCallback? onSendFileCallback,
}) {
ClipboardEvents.instance?.registerPasteEventListener(
(event) => _onPasteEvent(
event,
onSendFileCallback: onSendFileCallback,
),
);
}

void unregisterPasteShortcutListeners() {
ClipboardEvents.instance?.unregisterPasteEventListener(_onPasteEvent);
}

void _onPasteEvent(ClipboardReadEvent event) async {
void _onPasteEvent(
ClipboardReadEvent event, {
VoidCallback? onSendFileCallback,
}) async {
if (chatFocusNode.hasFocus != true) {
return;
}
final clipboardReader = await event.getClipboardReader();
if (await TwakeClipboard.instance
.isReadableImageFormat(clipboardReader: clipboardReader) &&
room != null) {
await pasteImage(context, room!, clipboardReader: clipboardReader);
await pasteImage(
context,
room!,
clipboardReader: clipboardReader,
onSendFileCallback: onSendFileCallback,
);
} else {
sendController.pasteText(clipboardReader: clipboardReader);
}
Expand Down
14 changes: 13 additions & 1 deletion lib/presentation/mixins/paste_image_mixin.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:fluffychat/pages/chat/send_file_dialog/send_file_dialog.dart';
import 'package:fluffychat/presentation/enum/chat/send_media_with_caption_status_enum.dart';
import 'package:fluffychat/utils/clipboard.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/twake_snackbar.dart';
Expand All @@ -12,6 +13,7 @@ mixin PasteImageMixin {
BuildContext context,
Room room, {
ClipboardReader? clipboardReader,
VoidCallback? onSendFileCallback,
}) async {
if (!(await TwakeClipboard.instance
.isReadableImageFormat(clipboardReader: clipboardReader))) {
Expand Down Expand Up @@ -41,7 +43,7 @@ mixin PasteImageMixin {
)
.cast<MatrixImageFile>()
.toList();
await showDialog(
final result = await showDialog(
context: context,
useRootNavigator: PlatformInfos.isWeb,
builder: (context) {
Expand All @@ -51,5 +53,15 @@ mixin PasteImageMixin {
);
},
);
if (result is SendMediaWithCaptionStatus) {
switch (result) {
case SendMediaWithCaptionStatus.done:
case SendMediaWithCaptionStatus.error:
onSendFileCallback?.call();
break;
case SendMediaWithCaptionStatus.cancel:
break;
}
}
}
}
10 changes: 8 additions & 2 deletions lib/presentation/mixins/send_files_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mixin SendFilesMixin {
BuildContext context, {
Room? room,
List<FileInfo>? fileInfos,
VoidCallback? onSendFileCallback,
}) async {
if (room == null) {}
final sendFileInteractor = getIt.get<SendFileInteractor>();
Expand All @@ -54,7 +55,7 @@ mixin SendFilesMixin {
.toList();

if (fileInfos == null || fileInfos.isEmpty == true) return;

onSendFileCallback?.call();
sendFileInteractor.execute(room: room!, fileInfos: fileInfos);
}

Expand All @@ -72,12 +73,17 @@ mixin SendFilesMixin {
required BuildContext context,
Room? room,
required PickerType type,
VoidCallback? onSendFileCallback,
}) async {
switch (type) {
case PickerType.gallery:
break;
case PickerType.documents:
sendFileAction(context, room: room);
sendFileAction(
context,
room: room,
onSendFileCallback: onSendFileCallback,
);
break;
case PickerType.location:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mixin SendFilesWithCaptionWebMixin {
BuildContext context, {
Room? room,
required List<MatrixFile> matrixFilesList,
VoidCallback? onSendFileCallback,
}) async {
if (matrixFilesList.length <= AppConfig.maxFilesSendPerDialog &&
matrixFilesList.isNotEmpty) {
Expand All @@ -25,6 +26,7 @@ mixin SendFilesWithCaptionWebMixin {
);
},
);
onSendFileCallback?.call();
if (result is SendMediaWithCaptionStatus) {
switch (result) {
case SendMediaWithCaptionStatus.done:
Expand Down

0 comments on commit 4dc05e1

Please sign in to comment.