1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Collections . ObjectModel ;
3
4
using System . Linq ;
4
5
using System . Reactive ;
6
+ using System . Text ;
5
7
using System . Threading . Tasks ;
6
8
7
9
using Avalonia ;
15
17
using TwitchLib . Client . Events ;
16
18
17
19
using TwitchStreamingTools . Models ;
20
+ using TwitchStreamingTools . Utilities ;
18
21
19
22
namespace TwitchStreamingTools . ViewModels . Pages ;
20
23
@@ -27,6 +30,11 @@ public class ChatViewModel : PageViewModelBase, IDisposable {
27
30
/// </summary>
28
31
private readonly IConfiguration _configuration ;
29
32
33
+ /// <summary>
34
+ /// The twitch chat log.
35
+ /// </summary>
36
+ private readonly ITwitchChatLog _twitchChatLog ;
37
+
30
38
/// <summary>
31
39
/// The twitch chat client.
32
40
/// </summary>
@@ -57,7 +65,9 @@ public class ChatViewModel : PageViewModelBase, IDisposable {
57
65
/// </summary>
58
66
/// <param name="twitchClient">The twitch chat client.</param>
59
67
/// <param name="configuration">The application configuration.</param>
60
- public ChatViewModel ( ITwitchClientProxy twitchClient , IConfiguration configuration ) {
68
+ /// <param name="twitchChatLog">The twitch chat log.</param>
69
+ public ChatViewModel ( ITwitchClientProxy twitchClient , IConfiguration configuration , ITwitchChatLog twitchChatLog ) {
70
+ _twitchChatLog = twitchChatLog ;
61
71
_twitchClient = twitchClient ;
62
72
_configuration = configuration ;
63
73
@@ -196,22 +206,61 @@ from user in _selectedTwitchChatNames
196
206
/// <param name="msg">The message received.</param>
197
207
private void OnChatMessage ( OnMessageReceivedArgs msg ) {
198
208
if ( _selectedTwitchChatNames . Count > 1 ) {
199
- TwitchChat = ( TwitchChat + $ "( { msg . ChatMessage . Channel } ) { msg . ChatMessage . Username } : { msg . ChatMessage . Message } " ) . Trim ( ) ;
209
+ TwitchChat = ( TwitchChat + FormatChatMessage ( msg . ChatMessage . Username , msg . ChatMessage . Message , msg . GetTimestamp ( ) ?? DateTime . UtcNow , msg . ChatMessage . Channel ) ) . Trim ( ) ;
200
210
}
201
211
else {
202
- TwitchChat = ( TwitchChat + $ " { msg . ChatMessage . Username } : { msg . ChatMessage . Message } " ) . Trim ( ) ;
212
+ TwitchChat = ( TwitchChat + FormatChatMessage ( msg . ChatMessage . Username , msg . ChatMessage . Message , msg . GetTimestamp ( ) ?? DateTime . UtcNow ) ) . Trim ( ) ;
203
213
}
204
214
205
215
TwitchChat += "\n " ;
206
216
TextBoxCursorPosition = int . MaxValue ;
207
217
}
208
218
219
+ /// <summary>
220
+ /// Formats a chat message for display.
221
+ /// </summary>
222
+ /// <param name="username">The username of the user that sent the message.</param>
223
+ /// <param name="message">The chat message sent by the user.</param>
224
+ /// <param name="timestamp">The timestamp of the message, in UTC.</param>
225
+ /// <param name="channel">The channel the message was sent in, if you want it included.</param>
226
+ /// <returns>The formatted message.</returns>
227
+ private string FormatChatMessage ( string username , string message , DateTime timestamp , string ? channel = null ) {
228
+ return null == channel ? $ "[{ timestamp : MM/dd/yy H:mm:ss} ] { username } : { message } " : $ "[{ timestamp : MM/dd/yy H:mm:ss} ] ({ channel } ) { username } : { message } ";
229
+ }
230
+
209
231
/// <summary>
210
232
/// Handles registering for twitch chat messages while the UI is open.
211
233
/// </summary>
212
234
public override void OnLoaded ( ) {
213
235
base . OnLoaded ( ) ;
214
236
237
+ // Connect to the twitch chats from the configuration.
238
+ InitializeTwitchChatConnections ( ) ;
239
+
240
+ // Loads the
241
+ PopulateChatHistory ( ) ;
242
+ }
243
+
244
+ /// <summary>
245
+ /// Populates the UI with the historic list of chat messages.
246
+ /// </summary>
247
+ private void PopulateChatHistory ( ) {
248
+ // Get the history of messages
249
+ IEnumerable < TwitchChatMessage > messages = _twitchChatLog . GetMessages ( ) ;
250
+
251
+ // Convert them into a string
252
+ var sb = new StringBuilder ( ) ;
253
+ sb . AppendJoin ( '\n ' , messages . Select ( l => FormatChatMessage ( l . Username , l . Message , l . Timestamp , l . Channel ) ) ) ;
254
+ sb . Append ( '\n ' ) ;
255
+
256
+ // Update the UI
257
+ TwitchChat = sb . ToString ( ) ;
258
+ }
259
+
260
+ /// <summary>
261
+ /// Connects to the twitch chats in the configuration file.
262
+ /// </summary>
263
+ private void InitializeTwitchChatConnections ( ) {
215
264
foreach ( TwitchChatConfiguration channel in _configuration . TwitchChats ?? [ ] ) {
216
265
if ( string . IsNullOrWhiteSpace ( channel . TwitchChannel ) ) {
217
266
continue ;
0 commit comments