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
@@ -338,15 +338,10 @@ class MessageInputState extends State<MessageInput> {
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<MessageInput> {
});
}
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<MessageInput> {
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<MessageInput> {
_emojiOverlay?.remove();
_mentionsOverlay?.remove();
_keyboardListener?.cancel();
_slowModeTimer?.cancel();
textEditingController.dispose();
_stopSlowMode();
super.dispose();
}
@@ -2259,6 +2261,8 @@ class MessageInputState extends State<MessageInput> {
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);