Skip to content

Commit a0dd958

Browse files
feat: Converting chat view model to DI
#10
1 parent dbd5537 commit a0dd958

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/TwitchStreamingTools/ViewModels/Pages/ChatViewModel.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System;
22
using System.Collections.ObjectModel;
33
using System.Reactive;
4+
using System.Threading.Tasks;
45

56
using Nullinside.Api.Common.Twitch;
67

78
using ReactiveUI;
89

910
using TwitchLib.Client.Events;
11+
using TwitchLib.Client.Interfaces;
1012

1113
using TwitchStreamingTools.Models;
1214

@@ -20,6 +22,11 @@ public class ChatViewModel : PageViewModelBase, IDisposable {
2022
/// The list of chat names selected in the list.
2123
/// </summary>
2224
private ObservableCollection<string> _selectedTwitchChatNames = [];
25+
26+
/// <summary>
27+
/// The twitch chat client.
28+
/// </summary>
29+
private ITwitchClientProxy _twitchClient;
2330

2431
/// <summary>
2532
/// 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 {
3946
/// <summary>
4047
/// Initializes a new instance of the <see cref="ChatViewModel" /> class.
4148
/// </summary>
42-
public ChatViewModel() {
49+
public ChatViewModel(ITwitchClientProxy twitchClient) {
50+
_twitchClient = twitchClient;
4351
foreach (string channel in Configuration.Instance.TwitchChats ?? []) {
4452
_selectedTwitchChatNames.Add(channel);
53+
_twitchClient.AddMessageCallback(channel, OnChatMessage);
4554
}
4655

4756
OnAddChat = ReactiveCommand.Create(() => {
@@ -52,7 +61,7 @@ public ChatViewModel() {
5261

5362
_selectedTwitchChatNames.Remove(username);
5463
_selectedTwitchChatNames.Add(username);
55-
TwitchClientProxy.Instance.AddMessageCallback(username, OnChatMessage).Wait();
64+
_ = _twitchClient.AddMessageCallback(username, OnChatMessage);
5665

5766
TwitchChatName = null;
5867
Configuration.Instance.TwitchChats = _selectedTwitchChatNames;
@@ -65,9 +74,6 @@ public ChatViewModel() {
6574
Configuration.Instance.TwitchChats = _selectedTwitchChatNames;
6675
Configuration.Instance.WriteConfiguration();
6776
});
68-
69-
TwitchClientProxy.Instance.AddInstanceCallback(OnNewTwitchClient);
70-
OnNewTwitchClient(TwitchClientProxy.Instance);
7177
}
7278

7379
/// <inheritdoc />
@@ -121,12 +127,6 @@ public void Dispose() {
121127
OnRemoveChat.Dispose();
122128
}
123129

124-
private void OnNewTwitchClient(TwitchClientProxy chatClient) {
125-
foreach (string channel in _selectedTwitchChatNames) {
126-
chatClient.AddMessageCallback(channel, OnChatMessage);
127-
}
128-
}
129-
130130
private void OnChatMessage(OnMessageReceivedArgs msg) {
131131
if (_selectedTwitchChatNames.Count > 1) {
132132
TwitchChat = (TwitchChat + $"\n({msg.ChatMessage.Channel}) {msg.ChatMessage.Username}: {msg.ChatMessage.Message}").Trim();

src/TwitchStreamingTools/Views/MainWindow.axaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ protected override void OnInitialized() {
5252
return;
5353
}
5454

55-
Dispatcher.UIThread.Post(async () => {
5655
#if !DEBUG
56+
Dispatcher.UIThread.Post(async () => {
5757
var versionWindow = new NewVersionWindow {
5858
DataContext = new NewVersionWindowViewModel {
5959
LocalVersion = localVersion
6060
}
6161
};
6262

6363
await versionWindow.ShowDialog(this);
64-
#endif
6564
});
65+
#endif
6666
});
6767
}
6868
}

0 commit comments

Comments
 (0)