From 9a37d80bf9a7a227885a54167c85e45d42720d90 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Tue, 5 Jul 2022 15:40:59 -0400 Subject: [PATCH] feat: render /clear events (#543) --- .../chat_history/chat_cleared_event.dart | 28 +++++++++++++++++++ lib/components/chat_history/message.dart | 5 +++- lib/models/adapters/messages.dart | 6 ++++ lib/models/messages.dart | 3 ++ lib/models/messages/message.dart | 4 +++ lib/models/tts.dart | 4 +++ 6 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 lib/components/chat_history/chat_cleared_event.dart diff --git a/lib/components/chat_history/chat_cleared_event.dart b/lib/components/chat_history/chat_cleared_event.dart new file mode 100644 index 000000000..abb4351a9 --- /dev/null +++ b/lib/components/chat_history/chat_cleared_event.dart @@ -0,0 +1,28 @@ +import 'package:flutter/material.dart'; +import 'package:intl/intl.dart'; +import 'package:rtchat/models/messages/message.dart'; + +class ChatClearedEventWidget extends StatelessWidget { + final ChatClearedEventModel model; + + const ChatClearedEventWidget(this.model, {Key? key}) : super(key: key); + + @override + Widget build(BuildContext context) { + final date = DateFormat.yMMMMEEEEd().format(model.timestamp); + final time = DateFormat.jms().format(model.timestamp); + return Padding( + padding: const EdgeInsets.symmetric(vertical: 8), + child: Container( + color: Theme.of(context).dividerColor, + width: double.infinity, + child: Padding( + padding: const EdgeInsets.all(8), + child: Text( + "Chat cleared at $date, $time", + textAlign: TextAlign.center, + style: Theme.of(context).textTheme.caption, + )), + )); + } +} diff --git a/lib/components/chat_history/message.dart b/lib/components/chat_history/message.dart index 5fcaf7308..f2b455936 100644 --- a/lib/components/chat_history/message.dart +++ b/lib/components/chat_history/message.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:provider/provider.dart'; +import 'package:rtchat/components/chat_history/chat_cleared_event.dart'; import 'package:rtchat/components/chat_history/stream_state_event.dart'; import 'package:rtchat/components/chat_history/timeout_dialog.dart'; import 'package:rtchat/components/chat_history/twitch/channel_point_event.dart'; @@ -257,8 +258,10 @@ class ChatHistoryMessage extends StatelessWidget { builder: (_, config, __) => config.showEvent ? TwitchRaidingEventWidget(m) : Container(), ); + } else if (m is ChatClearedEventModel) { + return ChatClearedEventWidget(m); } else { - throw AssertionError("invalid message type"); + throw AssertionError("invalid message type $m"); } } } diff --git a/lib/models/adapters/messages.dart b/lib/models/adapters/messages.dart index 7e878b7dd..e8cfd5f01 100644 --- a/lib/models/adapters/messages.dart +++ b/lib/models/adapters/messages.dart @@ -35,6 +35,10 @@ class UpdateDeltaEvent extends DeltaEvent { const UpdateDeltaEvent(this.messageId, this.update); } +class ClearDeltaEvent extends DeltaEvent { + const ClearDeltaEvent(); +} + DeltaEvent? _toDeltaEvent( List emotes, DocumentChange> change) { final data = change.doc.data(); @@ -102,6 +106,8 @@ DeltaEvent? _toDeltaEvent( viewers: data['event']['viewers'], timestamp: data['timestamp'].toDate()); return AppendDeltaEvent(model); + case "clear": + return const ClearDeltaEvent(); case "host": if (data['hosterChannelId'] == null) { // Since we might have some events saved without this field. diff --git a/lib/models/messages.dart b/lib/models/messages.dart index a21e06ca7..77df9c3d0 100644 --- a/lib/models/messages.dart +++ b/lib/models/messages.dart @@ -44,6 +44,9 @@ class MessagesModel extends ChangeNotifier { } } } + } else if (event is ClearDeltaEvent) { + _messages = [ChatClearedEventModel()]; + _tts?.stop(); } notifyListeners(); }); diff --git a/lib/models/messages/message.dart b/lib/models/messages/message.dart index ff146d6ac..0648712d7 100644 --- a/lib/models/messages/message.dart +++ b/lib/models/messages/message.dart @@ -21,3 +21,7 @@ class SystemMessageModel extends MessageModel { SystemMessageModel({required this.text}) : super(messageId: "", timestamp: DateTime.now()); } + +class ChatClearedEventModel extends MessageModel { + ChatClearedEventModel() : super(messageId: "", timestamp: DateTime.now()); +} diff --git a/lib/models/tts.dart b/lib/models/tts.dart index 8fe24ea19..5c54180e8 100644 --- a/lib/models/tts.dart +++ b/lib/models/tts.dart @@ -185,6 +185,10 @@ class TtsModel extends ChangeNotifier { _pending.remove(messageId); } + void stop() { + _pending.clear(); + } + TtsModel.fromJson(Map json) { if (json['isBotMuted'] != null) { _isBotMuted = json['isBotMuted'];