From 5a45454ac89b30c4d64ea436945a948d5ebf6fc7 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 24 Nov 2020 16:02:02 +0100 Subject: [PATCH 1/5] add debounce to mentions overlay --- lib/src/message_input.dart | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/src/message_input.dart b/lib/src/message_input.dart index a4bfcd189..5d36fd991 100644 --- a/lib/src/message_input.dart +++ b/lib/src/message_input.dart @@ -380,11 +380,19 @@ class MessageInputState extends State { Future> queryMembers; if (query.isNotEmpty) { - queryMembers = StreamChannel.of(context).channel.queryMembers(filter: { - 'name': { - '\$autocomplete': query, + queryMembers = StreamChannel.of(context).channel.queryMembers( + filter: { + 'name': { + '\$autocomplete': query, + }, }, - }).then((res) => res.members); + sort: [ + SortOption( + 'name', + direction: SortOption.ASC, + ), + ], + ).then((res) => res.members); } final members = StreamChannel.of(context).channel.state.members?.where((m) { @@ -952,6 +960,7 @@ class MessageInputState extends State { } } + Timer _debounce; void _onChange() { final s = textEditingController.text; StreamChannel.of(context).channel.keyStroke( @@ -980,8 +989,14 @@ class MessageInputState extends State { .split(' ') .last .contains('@')) { - _mentionsOverlay = _buildMentionsOverlayEntry(); - Overlay.of(context).insert(_mentionsOverlay); + if (_debounce?.isActive == true) _debounce.cancel(); + _debounce = Timer( + const Duration(milliseconds: 350), + () { + _mentionsOverlay = _buildMentionsOverlayEntry(); + Overlay.of(context).insert(_mentionsOverlay); + }, + ); } } From 897ef850506b06991b45fb94c30d9fbf5de87cd9 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 24 Nov 2020 16:05:21 +0100 Subject: [PATCH 2/5] use offline list if error --- lib/src/message_input.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/message_input.dart b/lib/src/message_input.dart index 5d36fd991..a3956481e 100644 --- a/lib/src/message_input.dart +++ b/lib/src/message_input.dart @@ -429,7 +429,7 @@ class MessageInputState extends State { return ListView( padding: const EdgeInsets.all(0), shrinkWrap: true, - children: snapshot.data + children: (snapshot.data ?? members) .map((m) => ListTile( leading: UserAvatar( user: m.user, @@ -991,7 +991,7 @@ class MessageInputState extends State { .contains('@')) { if (_debounce?.isActive == true) _debounce.cancel(); _debounce = Timer( - const Duration(milliseconds: 350), + const Duration(milliseconds: 500), () { _mentionsOverlay = _buildMentionsOverlayEntry(); Overlay.of(context).insert(_mentionsOverlay); From 0c875ab68e9fa08c2e437365e22f1f906b0f6b59 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 24 Nov 2020 16:17:53 +0100 Subject: [PATCH 3/5] update dependencies --- lib/src/message_input.dart | 65 +++++++++++++++++++------------------- pubspec.yaml | 4 +-- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/lib/src/message_input.dart b/lib/src/message_input.dart index a3956481e..0df0c670c 100644 --- a/lib/src/message_input.dart +++ b/lib/src/message_input.dart @@ -938,7 +938,8 @@ class MessageInputState extends State { super.initState(); if (!kIsWeb) { - _keyboardListener = KeyboardVisibility.onChange.listen((visible) { + _keyboardListener = + KeyboardVisibilityController().onChange.listen((visible) { if (visible) { _onChange(); } else { @@ -962,42 +963,42 @@ class MessageInputState extends State { Timer _debounce; void _onChange() { - final s = textEditingController.text; - StreamChannel.of(context).channel.keyStroke( - widget.parentMessage?.id, - ); - - setState(() { - _messageIsPresent = s.trim().isNotEmpty; - }); + if (_debounce?.isActive == true) _debounce.cancel(); + _debounce = Timer( + const Duration(milliseconds: 350), + () { + final s = textEditingController.text; + StreamChannel.of(context).channel.keyStroke( + widget.parentMessage?.id, + ); + + setState(() { + _messageIsPresent = s.trim().isNotEmpty; + }); - _commandsOverlay?.remove(); - _commandsOverlay = null; - _mentionsOverlay?.remove(); - _mentionsOverlay = null; + _commandsOverlay?.remove(); + _commandsOverlay = null; + _mentionsOverlay?.remove(); + _mentionsOverlay = null; - if (s.trim().startsWith('/')) { - _commandsOverlay = _buildCommandsOverlayEntry(); - Overlay.of(context).insert(_commandsOverlay); - } + if (s.trim().startsWith('/')) { + _commandsOverlay = _buildCommandsOverlayEntry(); + Overlay.of(context).insert(_commandsOverlay); + } - if (_messageIsPresent && - textEditingController.selection.isCollapsed && - textEditingController.selection.baseOffset > 0 && - textEditingController.text - .substring(0, textEditingController.selection.baseOffset) - .split(' ') - .last - .contains('@')) { - if (_debounce?.isActive == true) _debounce.cancel(); - _debounce = Timer( - const Duration(milliseconds: 500), - () { + if (_messageIsPresent && + textEditingController.selection.isCollapsed && + textEditingController.selection.baseOffset > 0 && + textEditingController.text + .substring(0, textEditingController.selection.baseOffset) + .split(' ') + .last + .contains('@')) { _mentionsOverlay = _buildMentionsOverlayEntry(); Overlay.of(context).insert(_mentionsOverlay); - }, - ); - } + } + }, + ); } void _parseExistingMessage(Message message) { diff --git a/pubspec.yaml b/pubspec.yaml index f667acfb6..ac5f99e6d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -19,10 +19,10 @@ dependencies: flutter_markdown: ^0.5.0 url_launcher: ^5.4.11 video_player: ^1.0.0 - chewie: ^0.10.4 + chewie: ^0.12.0 file_picker: ^2.0.12 image_picker: ^0.6.7+2 - flutter_keyboard_visibility: ^3.3.0 + flutter_keyboard_visibility: ^4.0.1 stream_chat: ^0.2.13 mime: ^0.9.6+3 visibility_detector: ^0.1.5 From 7ce3e00bb78e27d984ff0bb6f9778959c319a865 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 24 Nov 2020 16:19:56 +0100 Subject: [PATCH 4/5] bump version --- CHANGELOG.md | 4 ++++ pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d5b93f3a..15164d4fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.13+2 + +- Add debounce to on change messageinput listener + ## 0.2.13+1 - Use TextEditingController.addListener instead of TextField.onChanged diff --git a/pubspec.yaml b/pubspec.yaml index ac5f99e6d..c7ebaba9e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: stream_chat_flutter homepage: https://github.com/GetStream/stream-chat-flutter description: Stream Chat official Flutter SDK. Build your own chat experience using Dart and Flutter. -version: 0.2.13+1 +version: 0.2.13+2 repository: https://github.com/GetStream/stream-chat-flutter issue_tracker: https://github.com/GetStream/stream-chat-flutter/issues From 9525db4dc3d03fd940810e9bc1b6cf54afc97497 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 24 Nov 2020 16:41:32 +0100 Subject: [PATCH 5/5] add if(mounted) check --- lib/src/message_input.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/src/message_input.dart b/lib/src/message_input.dart index 0df0c670c..db39345f4 100644 --- a/lib/src/message_input.dart +++ b/lib/src/message_input.dart @@ -967,6 +967,9 @@ class MessageInputState extends State { _debounce = Timer( const Duration(milliseconds: 350), () { + if (!mounted) { + return; + } final s = textEditingController.text; StreamChannel.of(context).channel.keyStroke( widget.parentMessage?.id,