ref(ui): minor improvements

Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
Sahil Kumar
2021-09-27 14:05:56 +05:30
committed by xsahil03x
parent 9a1cb38e2a
commit f345fa1d36
@@ -171,7 +171,7 @@ class MessageInput extends StatefulWidget {
this.disableAttachments = false, this.disableAttachments = false,
this.initialMessage, this.initialMessage,
this.textEditingController, this.textEditingController,
this.actions, this.actions = const [],
this.actionsLocation = ActionsLocation.left, this.actionsLocation = ActionsLocation.left,
this.attachmentThumbnailBuilders, this.attachmentThumbnailBuilders,
this.focusNode, this.focusNode,
@@ -248,7 +248,7 @@ class MessageInput extends StatefulWidget {
final TextEditingController? textEditingController; final TextEditingController? textEditingController;
/// List of action widgets /// List of action widgets
final List<Widget>? actions; final List<Widget> actions;
/// The location of the custom actions /// The location of the custom actions
final ActionsLocation actionsLocation; final ActionsLocation actionsLocation;
@@ -324,7 +324,7 @@ class MessageInputState extends State<MessageInput> {
final List<User> _mentionedUsers = []; final List<User> _mentionedUsers = [];
final _imagePicker = ImagePicker(); final _imagePicker = ImagePicker();
late final FocusNode _focusNode; late final _focusNode = widget.focusNode ?? FocusNode();
bool _inputEnabled = true; bool _inputEnabled = true;
bool _commandEnabled = false; bool _commandEnabled = false;
bool _showCommandsOverlay = false; bool _showCommandsOverlay = false;
@@ -337,7 +337,8 @@ class MessageInputState extends State<MessageInput> {
int _filePickerIndex = 0; int _filePickerIndex = 0;
/// The editing controller passed to the input TextField /// The editing controller passed to the input TextField
late final TextEditingController textEditingController; late final TextEditingController textEditingController =
widget.textEditingController ?? TextEditingController();
late StreamChatThemeData _streamChatTheme; late StreamChatThemeData _streamChatTheme;
late MessageInputThemeData _messageInputTheme; late MessageInputThemeData _messageInputTheme;
@@ -349,18 +350,10 @@ class MessageInputState extends State<MessageInput> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_focusNode = widget.focusNode ?? FocusNode();
textEditingController =
widget.textEditingController ?? TextEditingController();
if (widget.editMessage != null || widget.initialMessage != null) { if (widget.editMessage != null || widget.initialMessage != null) {
_parseExistingMessage(widget.editMessage ?? widget.initialMessage!); _parseExistingMessage(widget.editMessage ?? widget.initialMessage!);
} }
textEditingController.addListener(_onChangedDebounced);
textEditingController.addListener(() {
_onChanged(context, textEditingController.text);
});
_focusNode.addListener(() { _focusNode.addListener(() {
if (_focusNode.hasFocus) { if (_focusNode.hasFocus) {
_openFilePickerSection = false; _openFilePickerSection = false;
@@ -625,7 +618,7 @@ class MessageInputState extends State<MessageInput> {
), ),
secondChild: widget.disableAttachments && secondChild: widget.disableAttachments &&
!widget.showCommandsButton && !widget.showCommandsButton &&
widget.actions?.isNotEmpty != true widget.actions.isNotEmpty != true
? const Offstage() ? const Offstage()
: Wrap( : Wrap(
children: <Widget>[ children: <Widget>[
@@ -636,7 +629,7 @@ class MessageInputState extends State<MessageInput> {
channel.state != null && channel.state != null &&
channel.config?.commands.isNotEmpty == true) channel.config?.commands.isNotEmpty == true)
_buildCommandButton(context), _buildCommandButton(context),
...widget.actions ?? [], ...widget.actions,
].insertBetween(const SizedBox(width: 8)), ].insertBetween(const SizedBox(width: 8)),
), ),
duration: const Duration(milliseconds: 300), duration: const Duration(milliseconds: 300),
@@ -804,50 +797,32 @@ class MessageInputState extends State<MessageInput> {
).merge(passedDecoration); ).merge(passedDecoration);
} }
Timer? _debounce; late final _onChangedDebounced = debounce(
() {
var value = textEditingController.text;
if (!mounted) return;
value = value.trim();
String? _previousValue; final channel = StreamChannel.of(context).channel;
if (value.isNotEmpty) {
channel.keyStroke(widget.parentMessage?.id).catchError((e) {});
}
void _onChanged( var actionsLength = widget.actions.length;
BuildContext context, if (widget.showCommandsButton) actionsLength += 1;
String s, if (!widget.disableAttachments) actionsLength += 1;
) {
if (s == _previousValue) {
return;
}
_previousValue = s;
if (_debounce?.isActive == true) _debounce!.cancel(); setState(() {
_debounce = Timer( _actionsShrunk = value.isNotEmpty && actionsLength > 1;
const Duration(milliseconds: 350), });
() {
if (!mounted) {
return;
}
if (s.isNotEmpty) { _checkCommands(value, context);
StreamChannel.of(context) _checkMentions(value, context);
.channel _checkEmoji(value, context);
.keyStroke(widget.parentMessage?.id) },
.catchError((e) {}); const Duration(milliseconds: 350),
} leading: true,
);
_checkCommands(s.trim(), context);
_checkMentions(s, context);
_checkEmoji(s, context);
setState(() {
_actionsShrunk = s.trim().isNotEmpty &&
((widget.actions?.length ?? 0) +
(widget.showCommandsButton ? 1 : 0) +
(widget.disableAttachments ? 0 : 1) >
1);
});
},
);
}
String _getHint(BuildContext context) { String _getHint(BuildContext context) {
if (_commandEnabled && _chosenCommand!.name == 'giphy') { if (_commandEnabled && _chosenCommand!.name == 'giphy') {
@@ -867,10 +842,7 @@ class MessageInputState extends State<MessageInput> {
if (s.isNotEmpty && if (s.isNotEmpty &&
textEditingController.selection.baseOffset > 0 && textEditingController.selection.baseOffset > 0 &&
textEditingController.text textEditingController.text
.substring( .substring(0, textEditingController.selection.baseOffset)
0,
textEditingController.selection.baseOffset,
)
.contains(':')) { .contains(':')) {
final textToSelection = textEditingController.text final textToSelection = textEditingController.text
.substring(0, textEditingController.value.selection.start); .substring(0, textEditingController.value.selection.start);
@@ -1207,7 +1179,7 @@ class MessageInputState extends State<MessageInput> {
offset: rejoin.length, offset: rejoin.length,
), ),
); );
_debounce!.cancel(); _onChangedDebounced.cancel();
setState(() { setState(() {
_showMentionsOverlay = false; _showMentionsOverlay = false;
}); });