Skip to content

Commit

Permalink
feat: render /clear events (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmo314 authored Jul 5, 2022
1 parent 083796e commit 9a37d80
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
28 changes: 28 additions & 0 deletions lib/components/chat_history/chat_cleared_event.dart
Original file line number Diff line number Diff line change
@@ -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,
)),
));
}
}
5 changes: 4 additions & 1 deletion lib/components/chat_history/message.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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");
}
}
}
6 changes: 6 additions & 0 deletions lib/models/adapters/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class UpdateDeltaEvent extends DeltaEvent {
const UpdateDeltaEvent(this.messageId, this.update);
}

class ClearDeltaEvent extends DeltaEvent {
const ClearDeltaEvent();
}

DeltaEvent? _toDeltaEvent(
List<Emote> emotes, DocumentChange<Map<String, dynamic>> change) {
final data = change.doc.data();
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions lib/models/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class MessagesModel extends ChangeNotifier {
}
}
}
} else if (event is ClearDeltaEvent) {
_messages = [ChatClearedEventModel()];
_tts?.stop();
}
notifyListeners();
});
Expand Down
4 changes: 4 additions & 0 deletions lib/models/messages/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
4 changes: 4 additions & 0 deletions lib/models/tts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ class TtsModel extends ChangeNotifier {
_pending.remove(messageId);
}

void stop() {
_pending.clear();
}

TtsModel.fromJson(Map<String, dynamic> json) {
if (json['isBotMuted'] != null) {
_isBotMuted = json['isBotMuted'];
Expand Down

0 comments on commit 9a37d80

Please sign in to comment.