1
1
using System ;
2
2
using System . Collections . ObjectModel ;
3
3
using System . Reactive ;
4
+ using System . Threading . Tasks ;
4
5
5
6
using Nullinside . Api . Common . Twitch ;
6
7
7
8
using ReactiveUI ;
8
9
9
10
using TwitchLib . Client . Events ;
11
+ using TwitchLib . Client . Interfaces ;
10
12
11
13
using TwitchStreamingTools . Models ;
12
14
@@ -20,6 +22,11 @@ public class ChatViewModel : PageViewModelBase, IDisposable {
20
22
/// The list of chat names selected in the list.
21
23
/// </summary>
22
24
private ObservableCollection < string > _selectedTwitchChatNames = [ ] ;
25
+
26
+ /// <summary>
27
+ /// The twitch chat client.
28
+ /// </summary>
29
+ private ITwitchClientProxy _twitchClient ;
23
30
24
31
/// <summary>
25
32
/// The current position of the cursor for the text box showing our chat logs, increment to move down.
@@ -39,9 +46,11 @@ public class ChatViewModel : PageViewModelBase, IDisposable {
39
46
/// <summary>
40
47
/// Initializes a new instance of the <see cref="ChatViewModel" /> class.
41
48
/// </summary>
42
- public ChatViewModel ( ) {
49
+ public ChatViewModel ( ITwitchClientProxy twitchClient ) {
50
+ _twitchClient = twitchClient ;
43
51
foreach ( string channel in Configuration . Instance . TwitchChats ?? [ ] ) {
44
52
_selectedTwitchChatNames . Add ( channel ) ;
53
+ _twitchClient . AddMessageCallback ( channel , OnChatMessage ) ;
45
54
}
46
55
47
56
OnAddChat = ReactiveCommand . Create ( ( ) => {
@@ -52,22 +61,19 @@ public ChatViewModel() {
52
61
53
62
_selectedTwitchChatNames . Remove ( username ) ;
54
63
_selectedTwitchChatNames . Add ( username ) ;
55
- TwitchClientProxy . Instance . AddMessageCallback ( username , OnChatMessage ) . Wait ( ) ;
64
+ _ = _twitchClient . AddMessageCallback ( username , OnChatMessage ) ;
56
65
57
66
TwitchChatName = null ;
58
67
Configuration . Instance . TwitchChats = _selectedTwitchChatNames ;
59
68
Configuration . Instance . WriteConfiguration ( ) ;
60
69
} ) ;
61
70
62
- OnRemoveChat = ReactiveCommand . Create < string > ( s => {
63
- _selectedTwitchChatNames . Remove ( s ) ;
64
- // todo: disconnect from chat
71
+ OnRemoveChat = ReactiveCommand . Create < string > ( channel => {
72
+ _selectedTwitchChatNames . Remove ( channel ) ;
73
+ _twitchClient . RemoveMessageCallback ( channel , OnChatMessage ) ;
65
74
Configuration . Instance . TwitchChats = _selectedTwitchChatNames ;
66
75
Configuration . Instance . WriteConfiguration ( ) ;
67
76
} ) ;
68
-
69
- TwitchClientProxy . Instance . AddInstanceCallback ( OnNewTwitchClient ) ;
70
- OnNewTwitchClient ( TwitchClientProxy . Instance ) ;
71
77
}
72
78
73
79
/// <inheritdoc />
@@ -121,12 +127,6 @@ public void Dispose() {
121
127
OnRemoveChat . Dispose ( ) ;
122
128
}
123
129
124
- private void OnNewTwitchClient ( TwitchClientProxy chatClient ) {
125
- foreach ( string channel in _selectedTwitchChatNames ) {
126
- chatClient . AddMessageCallback ( channel , OnChatMessage ) ;
127
- }
128
- }
129
-
130
130
private void OnChatMessage ( OnMessageReceivedArgs msg ) {
131
131
if ( _selectedTwitchChatNames . Count > 1 ) {
132
132
TwitchChat = ( TwitchChat + $ "\n ({ msg . ChatMessage . Channel } ) { msg . ChatMessage . Username } : { msg . ChatMessage . Message } ") . Trim ( ) ;
0 commit comments