Skip to content

Commit

Permalink
Merge pull request #148 from GetStream/hotfix/debounce
Browse files Browse the repository at this point in the history
Hotfix/debounce
  • Loading branch information
imtoori authored Nov 24, 2020
2 parents 84fea9f + 9525db4 commit 3781408
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
83 changes: 51 additions & 32 deletions lib/src/message_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,19 @@ class MessageInputState extends State<MessageInput> {
Future<List<Member>> 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) {
Expand Down Expand Up @@ -421,7 +429,7 @@ class MessageInputState extends State<MessageInput> {
return ListView(
padding: const EdgeInsets.all(0),
shrinkWrap: true,
children: snapshot.data
children: (snapshot.data ?? members)
.map((m) => ListTile(
leading: UserAvatar(
user: m.user,
Expand Down Expand Up @@ -930,7 +938,8 @@ class MessageInputState extends State<MessageInput> {
super.initState();

if (!kIsWeb) {
_keyboardListener = KeyboardVisibility.onChange.listen((visible) {
_keyboardListener =
KeyboardVisibilityController().onChange.listen((visible) {
if (visible) {
_onChange();
} else {
Expand All @@ -952,37 +961,47 @@ class MessageInputState extends State<MessageInput> {
}
}

Timer _debounce;
void _onChange() {
final s = textEditingController.text;
StreamChannel.of(context).channel.keyStroke(
widget.parentMessage?.id,
);
if (_debounce?.isActive == true) _debounce.cancel();
_debounce = Timer(
const Duration(milliseconds: 350),
() {
if (!mounted) {
return;
}
final s = textEditingController.text;
StreamChannel.of(context).channel.keyStroke(
widget.parentMessage?.id,
);

setState(() {
_messageIsPresent = s.trim().isNotEmpty;
});
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('@')) {
_mentionsOverlay = _buildMentionsOverlayEntry();
Overlay.of(context).insert(_mentionsOverlay);
}
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) {
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down

0 comments on commit 3781408

Please sign in to comment.