From 1a99a38469c9762dce061184bcf0dae2d383a586 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Wed, 25 Aug 2021 15:40:26 +0530 Subject: [PATCH 1/2] refactor(llc, ui): make cooldown non-nullable. Signed-off-by: xsahil03x --- .../stream_chat/lib/src/client/channel.dart | 12 ++--- .../lib/src/message_input.dart | 50 ++++++++++--------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/packages/stream_chat/lib/src/client/channel.dart b/packages/stream_chat/lib/src/client/channel.dart index 22738794..999dfb0b 100644 --- a/packages/stream_chat/lib/src/client/channel.dart +++ b/packages/stream_chat/lib/src/client/channel.dart @@ -202,15 +202,15 @@ class Channel { } /// Cooldown count - int? get cooldown { + int get cooldown { _checkInitialized(); - return state?._channelState.channel?.cooldown; + return state!._channelState.channel?.cooldown ?? 0; } /// Cooldown count as a stream - Stream? get cooldownStream { + Stream get cooldownStream { _checkInitialized(); - return state?.channelStateStream.map((cs) => cs.channel?.cooldown); + return state!.channelStateStream.map((cs) => cs.channel?.cooldown ?? 0); } /// Stores time at which cooldown was started @@ -540,9 +540,7 @@ class Channel { skipPush: skipPush, ); state!.addMessage(response.message); - if (cooldown! > 0) { - cooldownStartedAt = DateTime.now(); - } + if (cooldown > 0) cooldownStartedAt = DateTime.now(); return response; } catch (e) { if (e is StreamChatNetworkError && e.isRetriable) { diff --git a/packages/stream_chat_flutter/lib/src/message_input.dart b/packages/stream_chat_flutter/lib/src/message_input.dart index 4227c90d..0c787e0e 100644 --- a/packages/stream_chat_flutter/lib/src/message_input.dart +++ b/packages/stream_chat_flutter/lib/src/message_input.dart @@ -338,15 +338,10 @@ class MessageInputState extends State { bool get _hasQuotedMessage => widget.quotedMessage != null; bool get _messageIsPresent => textEditingController.text.trim().isNotEmpty; - late DateTime? _cooldownStartedAt; - int? _timeOut; - - Timer? _slowModeTimer; @override void initState() { super.initState(); - _startSlowMode(); _focusNode = widget.focusNode ?? FocusNode(); _emojiNames = Emoji.all().where((it) => it.name != null).map((e) => e.name!); @@ -377,25 +372,33 @@ class MessageInputState extends State { }); } + int _timeOut = 0; + Timer? _slowModeTimer; + void _startSlowMode() { final channel = StreamChannel.of(context).channel; - if (channel.cooldownStartedAt != null) { - _cooldownStartedAt = channel.cooldownStartedAt; - if (DateTime.now().difference(_cooldownStartedAt!).inSeconds < - channel.cooldown!) { - _timeOut = channel.cooldown! - - DateTime.now().difference(_cooldownStartedAt!).inSeconds; - _slowModeTimer = Timer.periodic(const Duration(seconds: 1), (timer) { - if (_timeOut == 0) { - timer.cancel(); - } else { - setState(() => _timeOut = _timeOut! - 1); - } - }); + final cooldownStartedAt = channel.cooldownStartedAt; + if (cooldownStartedAt != null) { + final diff = DateTime.now().difference(cooldownStartedAt).inSeconds; + if (diff < channel.cooldown) { + _timeOut = channel.cooldown - diff; + if (_timeOut > 0) { + _slowModeTimer = Timer.periodic(const Duration(seconds: 1), (timer) { + if (_timeOut == 0) { + timer.cancel(); + } else { + if (mounted) { + setState(() => _timeOut -= 1); + } + } + }); + } } } } + void _stopSlowMode() => _slowModeTimer?.cancel(); + @override Widget build(BuildContext context) { Widget child = DecoratedBox( @@ -545,10 +548,8 @@ class MessageInputState extends State { Widget _animateSendButton(BuildContext context) { late Widget sendButton; - if (_timeOut != null && _timeOut! > 0) { - sendButton = _CountdownButton( - count: _timeOut!, - ); + if (_timeOut > 0) { + sendButton = _CountdownButton(count: _timeOut); } else if (!_messageIsPresent && _attachments.isEmpty) { sendButton = widget.idleSendButton ?? _buildIdleSendButton(context); } else { @@ -2249,7 +2250,8 @@ class MessageInputState extends State { _emojiOverlay?.remove(); _mentionsOverlay?.remove(); _keyboardListener?.cancel(); - _slowModeTimer?.cancel(); + textEditingController.dispose(); + _stopSlowMode(); super.dispose(); } @@ -2259,6 +2261,8 @@ class MessageInputState extends State { void didChangeDependencies() { _streamChatTheme = StreamChatTheme.of(context); _messageInputTheme = MessageInputTheme.of(context); + if (widget.editMessage == null) _startSlowMode(); + if ((widget.editMessage != null || widget.initialMessage != null) && !_initialized) { FocusScope.of(context).requestFocus(_focusNode); From 2d820e3f2f572c8be6540ae7de30ef0198bf040c Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Wed, 25 Aug 2021 15:46:20 +0530 Subject: [PATCH 2/2] chore(ui): flutter format, fix analyzer Signed-off-by: xsahil03x --- packages/stream_chat_flutter/lib/src/message_input.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/stream_chat_flutter/lib/src/message_input.dart b/packages/stream_chat_flutter/lib/src/message_input.dart index 0c787e0e..651a204c 100644 --- a/packages/stream_chat_flutter/lib/src/message_input.dart +++ b/packages/stream_chat_flutter/lib/src/message_input.dart @@ -834,7 +834,7 @@ class MessageInputState extends State { if (_attachments.isNotEmpty) { return context.translations.addACommentOrSendLabel; } - if (_timeOut != 0 && _timeOut != null) { + if (_timeOut != 0) { return context.translations.slowModeOnLabel; }