refactor(llc, ui): make cooldown non-nullable.

Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
Sahil Kumar
2021-08-25 15:40:26 +05:30
committed by xsahil03x
parent 3ad35602bc
commit 1a99a38469
2 changed files with 32 additions and 30 deletions
@@ -202,15 +202,15 @@ class Channel {
} }
/// Cooldown count /// Cooldown count
int? get cooldown { int get cooldown {
_checkInitialized(); _checkInitialized();
return state?._channelState.channel?.cooldown; return state!._channelState.channel?.cooldown ?? 0;
} }
/// Cooldown count as a stream /// Cooldown count as a stream
Stream<int?>? get cooldownStream { Stream<int> get cooldownStream {
_checkInitialized(); _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 /// Stores time at which cooldown was started
@@ -540,9 +540,7 @@ class Channel {
skipPush: skipPush, skipPush: skipPush,
); );
state!.addMessage(response.message); state!.addMessage(response.message);
if (cooldown! > 0) { if (cooldown > 0) cooldownStartedAt = DateTime.now();
cooldownStartedAt = DateTime.now();
}
return response; return response;
} catch (e) { } catch (e) {
if (e is StreamChatNetworkError && e.isRetriable) { if (e is StreamChatNetworkError && e.isRetriable) {
@@ -338,15 +338,10 @@ class MessageInputState extends State<MessageInput> {
bool get _hasQuotedMessage => widget.quotedMessage != null; bool get _hasQuotedMessage => widget.quotedMessage != null;
bool get _messageIsPresent => textEditingController.text.trim().isNotEmpty; bool get _messageIsPresent => textEditingController.text.trim().isNotEmpty;
late DateTime? _cooldownStartedAt;
int? _timeOut;
Timer? _slowModeTimer;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_startSlowMode();
_focusNode = widget.focusNode ?? FocusNode(); _focusNode = widget.focusNode ?? FocusNode();
_emojiNames = _emojiNames =
Emoji.all().where((it) => it.name != null).map((e) => e.name!); Emoji.all().where((it) => it.name != null).map((e) => e.name!);
@@ -377,25 +372,33 @@ class MessageInputState extends State<MessageInput> {
}); });
} }
int _timeOut = 0;
Timer? _slowModeTimer;
void _startSlowMode() { void _startSlowMode() {
final channel = StreamChannel.of(context).channel; final channel = StreamChannel.of(context).channel;
if (channel.cooldownStartedAt != null) { final cooldownStartedAt = channel.cooldownStartedAt;
_cooldownStartedAt = channel.cooldownStartedAt; if (cooldownStartedAt != null) {
if (DateTime.now().difference(_cooldownStartedAt!).inSeconds < final diff = DateTime.now().difference(cooldownStartedAt).inSeconds;
channel.cooldown!) { if (diff < channel.cooldown) {
_timeOut = channel.cooldown! - _timeOut = channel.cooldown - diff;
DateTime.now().difference(_cooldownStartedAt!).inSeconds; if (_timeOut > 0) {
_slowModeTimer = Timer.periodic(const Duration(seconds: 1), (timer) { _slowModeTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
if (_timeOut == 0) { if (_timeOut == 0) {
timer.cancel(); timer.cancel();
} else { } else {
setState(() => _timeOut = _timeOut! - 1); if (mounted) {
} setState(() => _timeOut -= 1);
}); }
}
});
}
} }
} }
} }
void _stopSlowMode() => _slowModeTimer?.cancel();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
Widget child = DecoratedBox( Widget child = DecoratedBox(
@@ -545,10 +548,8 @@ class MessageInputState extends State<MessageInput> {
Widget _animateSendButton(BuildContext context) { Widget _animateSendButton(BuildContext context) {
late Widget sendButton; late Widget sendButton;
if (_timeOut != null && _timeOut! > 0) { if (_timeOut > 0) {
sendButton = _CountdownButton( sendButton = _CountdownButton(count: _timeOut);
count: _timeOut!,
);
} else if (!_messageIsPresent && _attachments.isEmpty) { } else if (!_messageIsPresent && _attachments.isEmpty) {
sendButton = widget.idleSendButton ?? _buildIdleSendButton(context); sendButton = widget.idleSendButton ?? _buildIdleSendButton(context);
} else { } else {
@@ -2249,7 +2250,8 @@ class MessageInputState extends State<MessageInput> {
_emojiOverlay?.remove(); _emojiOverlay?.remove();
_mentionsOverlay?.remove(); _mentionsOverlay?.remove();
_keyboardListener?.cancel(); _keyboardListener?.cancel();
_slowModeTimer?.cancel(); textEditingController.dispose();
_stopSlowMode();
super.dispose(); super.dispose();
} }
@@ -2259,6 +2261,8 @@ class MessageInputState extends State<MessageInput> {
void didChangeDependencies() { void didChangeDependencies() {
_streamChatTheme = StreamChatTheme.of(context); _streamChatTheme = StreamChatTheme.of(context);
_messageInputTheme = MessageInputTheme.of(context); _messageInputTheme = MessageInputTheme.of(context);
if (widget.editMessage == null) _startSlowMode();
if ((widget.editMessage != null || widget.initialMessage != null) && if ((widget.editMessage != null || widget.initialMessage != null) &&
!_initialized) { !_initialized) {
FocusScope.of(context).requestFocus(_focusNode); FocusScope.of(context).requestFocus(_focusNode);