Skip to content

Commit 04bf92c

Browse files
chore: adding [RelayCommand]
Converting all the boilerplate code over to use the attribute tags
1 parent fe4435b commit 04bf92c

File tree

9 files changed

+28
-87
lines changed

9 files changed

+28
-87
lines changed

src/Nullinside.TwitchStreamingTools/Controls/Keybind.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<StackPanel>
1111
<Button IsVisible="{Binding !Listening}"
1212
Content="{Binding Keybind, TargetNullValue='[None]'}"
13-
Command="{Binding ListenForKeystroke}"
13+
Command="{Binding StartListenKeystrokeCommand}"
1414
ToolTip.Tip="ESC to unset" />
1515
<StackPanel IsVisible="{Binding Listening}"
1616
Orientation="Horizontal">

src/Nullinside.TwitchStreamingTools/Controls/ViewModels/KeybindViewModel.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Reactive;
22

33
using CommunityToolkit.Mvvm.ComponentModel;
4+
using CommunityToolkit.Mvvm.Input;
45

56
using Nullinside.TwitchStreamingTools.Models;
67
using Nullinside.TwitchStreamingTools.Services;
@@ -34,17 +35,12 @@ public partial class KeybindViewModel : ObservableObject {
3435
/// <param name="service">The listener for keystrokes on the keyboard.</param>
3536
public KeybindViewModel(IGlobalKeyPressService service) {
3637
_service = service;
37-
ListenForKeystroke = ReactiveCommand.Create(StartListenKeystroke);
3838
}
3939

40-
/// <summary>
41-
/// Listens for keystrokes.
42-
/// </summary>
43-
public ReactiveCommand<Unit, Unit> ListenForKeystroke { get; }
44-
4540
/// <summary>
4641
/// Starts listening for keystrokes.
4742
/// </summary>
43+
[RelayCommand]
4844
private void StartListenKeystroke() {
4945
Listening = true;
5046
_service.OnKeystroke -= OnKeystroke;

src/Nullinside.TwitchStreamingTools/ViewModels/NewVersionWindowViewModel.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
using System.Diagnostics;
22
using System.Threading.Tasks;
3-
using System.Windows.Input;
43

54
using Avalonia.Controls;
65
using Avalonia.Threading;
76

87
using CommunityToolkit.Mvvm.ComponentModel;
8+
using CommunityToolkit.Mvvm.Input;
99

1010
using Nullinside.Api.Common.Desktop;
1111
using Nullinside.TwitchStreamingTools.Views;
1212

13-
using ReactiveUI;
14-
1513
namespace Nullinside.TwitchStreamingTools.ViewModels;
1614

1715
/// <summary>
@@ -42,9 +40,6 @@ public partial class NewVersionWindowViewModel : ViewModelBase {
4240
/// Initializes a new instance of the <see cref="NewVersionWindowViewModel" /> class.
4341
/// </summary>
4442
public NewVersionWindowViewModel() {
45-
UpdateSoftware = ReactiveCommand.Create(StartUpdateSoftware);
46-
CloseWindow = ReactiveCommand.Create<Window>(CloseWindowCommand);
47-
4843
// asynchronously determine the current version number.
4944
Task.Factory.StartNew(async () => {
5045
GithubLatestReleaseJson? version =
@@ -67,27 +62,19 @@ public string? LocalVersion {
6762
set => _localVersion = value;
6863
}
6964

70-
/// <summary>
71-
/// A command to update the software.
72-
/// </summary>
73-
public ICommand UpdateSoftware { get; }
74-
75-
/// <summary>
76-
/// A command to close the current window.
77-
/// </summary>
78-
public ICommand CloseWindow { get; }
79-
8065
/// <summary>
8166
/// A command to close the current window.
8267
/// </summary>
8368
/// <param name="self">The reference to our own window.</param>
84-
private void CloseWindowCommand(Window self) {
69+
[RelayCommand]
70+
private void CloseWindow(Window self) {
8571
self.Close();
8672
}
8773

8874
/// <summary>
8975
/// Launches the web browser at the new release page.
9076
/// </summary>
77+
[RelayCommand]
9178
private void StartUpdateSoftware() {
9279
IsUpdating = true;
9380
GitHubUpdateManager.PrepareUpdate()

src/Nullinside.TwitchStreamingTools/ViewModels/Pages/AccountViewModel.cs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
using System.IO;
44
using System.Net.Http;
55
using System.Net.WebSockets;
6-
using System.Reactive;
76
using System.Threading;
87
using System.Threading.Tasks;
98

109
using Avalonia.Media.Imaging;
1110

1211
using CommunityToolkit.Mvvm.ComponentModel;
12+
using CommunityToolkit.Mvvm.Input;
1313

1414
using log4net;
1515

@@ -21,16 +21,14 @@
2121
using Nullinside.TwitchStreamingTools.Services;
2222
using Nullinside.TwitchStreamingTools.Utilities;
2323

24-
using ReactiveUI;
25-
2624
using TwitchLib.Api.Helix.Models.Users.GetUsers;
2725

2826
namespace Nullinside.TwitchStreamingTools.ViewModels.Pages;
2927

3028
/// <summary>
3129
/// Handles binding your account to the application.
3230
/// </summary>
33-
public partial class AccountViewModel : PageViewModelBase, IDisposable {
31+
public partial class AccountViewModel : PageViewModelBase {
3432
/// <summary>
3533
/// The path to the folder containing cached profile images.
3634
/// </summary>
@@ -92,8 +90,6 @@ public AccountViewModel(ITwitchAccountService twitchAccountService, IConfigurati
9290
_twitchAccountService.OnCredentialsStatusChanged += OnCredentialsStatusChanged;
9391
_twitchAccountService.OnCredentialsChanged += OnCredentialsChanged;
9492
_configuration = configuration;
95-
OnPerformLogin = ReactiveCommand.Create(PerformLogin);
96-
OnLogout = ReactiveCommand.Create(ClearCredentials);
9793

9894
// Set the initial state of the ui
9995
HasValidOAuthToken = _twitchAccountService.CredentialsAreValid;
@@ -103,27 +99,11 @@ public AccountViewModel(ITwitchAccountService twitchAccountService, IConfigurati
10399
/// <inheritdoc />
104100
public override string IconResourceKey { get; } = "InprivateAccountRegular";
105101

106-
/// <summary>
107-
/// Called when the user clicks the login button.
108-
/// </summary>
109-
public ReactiveCommand<Unit, Unit> OnPerformLogin { get; }
110-
111-
/// <summary>
112-
/// Called when logging out the current user.
113-
/// </summary>
114-
public ReactiveCommand<Unit, Unit> OnLogout { get; }
115-
116102
/// <summary>
117103
/// The application version number.
118104
/// </summary>
119105
public string? Version => Constants.APP_VERSION;
120106

121-
/// <inheritdoc />
122-
public void Dispose() {
123-
OnPerformLogin.Dispose();
124-
OnLogout.Dispose();
125-
}
126-
127107
/// <summary>
128108
/// Loads the profile image when the UI loads.
129109
/// </summary>
@@ -202,7 +182,8 @@ private void OnCredentialsStatusChanged(bool valid) {
202182
/// <summary>
203183
/// Launches the computer's default browser to generate an OAuth token.
204184
/// </summary>
205-
private async void PerformLogin() {
185+
[RelayCommand]
186+
private async Task PerformLogin() {
206187
LoggingIn = true;
207188
try {
208189
CancellationToken token = CancellationToken.None;
@@ -251,6 +232,7 @@ private async void PerformLogin() {
251232
/// <summary>
252233
/// Clears the credentials out (logging out).
253234
/// </summary>
235+
[RelayCommand]
254236
private void ClearCredentials() {
255237
_twitchAccountService.DeleteCredentials();
256238
}

src/Nullinside.TwitchStreamingTools/ViewModels/Pages/ChatViewModel.cs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
44
using System.Linq;
5-
using System.Reactive;
65
using System.Text;
76
using System.Threading.Tasks;
87

@@ -11,13 +10,12 @@
1110
using Avalonia.Media;
1211

1312
using CommunityToolkit.Mvvm.ComponentModel;
13+
using CommunityToolkit.Mvvm.Input;
1414

1515
using Nullinside.Api.Common.Twitch;
1616
using Nullinside.TwitchStreamingTools.Models;
1717
using Nullinside.TwitchStreamingTools.Utilities;
1818

19-
using ReactiveUI;
20-
2119
using TwitchLib.Client.Events;
2220

2321
namespace Nullinside.TwitchStreamingTools.ViewModels.Pages;
@@ -72,9 +70,6 @@ public ChatViewModel(ITwitchClientProxy twitchClient, IConfiguration configurati
7270
_twitchClient = twitchClient;
7371
_configuration = configuration;
7472

75-
OnAddChat = ReactiveCommand.Create(OnAddChatCommand);
76-
OnRemoveChat = ReactiveCommand.Create<string>(OnRemoveChatCommand);
77-
7873
Application.Current!.TryFindResource("DeleteRegular", out object? icon);
7974
DeleteIcon = (StreamGeometry)icon!;
8075
}
@@ -87,16 +82,6 @@ public ChatViewModel(ITwitchClientProxy twitchClient, IConfiguration configurati
8782
/// </summary>
8883
public StreamGeometry DeleteIcon { get; set; }
8984

90-
/// <summary>
91-
/// Called when adding a chat.
92-
/// </summary>
93-
public ReactiveCommand<Unit, Unit> OnAddChat { get; }
94-
95-
/// <summary>
96-
/// Called when removing a chat.
97-
/// </summary>
98-
public ReactiveCommand<string, Unit> OnRemoveChat { get; }
99-
10085
/// <inheritdoc />
10186
public void Dispose() {
10287
foreach (TwitchChatConfiguration channel in _configuration.TwitchChats ?? []) {
@@ -106,15 +91,13 @@ public void Dispose() {
10691

10792
_twitchClient.RemoveMessageCallback(channel.TwitchChannel, OnChatMessage);
10893
}
109-
110-
OnAddChat.Dispose();
111-
OnRemoveChat.Dispose();
11294
}
11395

11496
/// <summary>
11597
/// Handles adding a new chat to monitor.
11698
/// </summary>
117-
private void OnAddChatCommand() {
99+
[RelayCommand]
100+
private void AddChat() {
118101
// Ensure we have a username
119102
string? username = TwitchChatName?.Trim();
120103
if (string.IsNullOrWhiteSpace(username) || SelectedTwitchChatNames.Contains(username)) {
@@ -147,7 +130,8 @@ from user in SelectedTwitchChatNames
147130
/// Handles removing a chat we were monitoring.
148131
/// </summary>
149132
/// <param name="channel">The chat to remove.</param>
150-
private void OnRemoveChatCommand(string channel) {
133+
[RelayCommand]
134+
private void RemoveChat(string channel) {
151135
SelectedTwitchChatNames.Remove(channel);
152136

153137
// Remove the callback
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,38 @@
11
using System.Reactive;
22

3+
using CommunityToolkit.Mvvm.Input;
4+
35
using ReactiveUI;
46

57
namespace Nullinside.TwitchStreamingTools.ViewModels.Pages;
68

79
/// <summary>
810
/// A base class for all pages of the application that are navigable through the left nav of the application.
911
/// </summary>
10-
public abstract class PageViewModelBase : ViewModelBase {
12+
public abstract partial class PageViewModelBase : ViewModelBase {
1113
/// <summary>
1214
/// Initializes a new instance of the <see cref="PageViewModelBase" /> class.
1315
/// </summary>
1416
protected PageViewModelBase() {
15-
OnLoadedCommand = ReactiveCommand.Create(OnLoaded);
16-
OnUnloadedCommand = ReactiveCommand.Create(OnUnloaded);
1717
}
1818

1919
/// <summary>
2020
/// The style resource key name of the icon.
2121
/// </summary>
2222
public abstract string IconResourceKey { get; }
2323

24-
/// <summary>
25-
/// Called when the UI is loaded.
26-
/// </summary>
27-
public ReactiveCommand<Unit, Unit> OnLoadedCommand { protected set; get; }
28-
29-
/// <summary>
30-
/// Called when the UI is unloaded.
31-
/// </summary>
32-
public ReactiveCommand<Unit, Unit> OnUnloadedCommand { protected set; get; }
33-
3424
/// <summary>
3525
/// Called then Ui is loaded.
3626
/// </summary>
27+
[RelayCommand]
3728
public virtual void OnLoaded() {
3829
// Just exist to be overridden.
3930
}
4031

4132
/// <summary>
4233
/// Called when the Ui is unloaded.
4334
/// </summary>
35+
[RelayCommand]
4436
public virtual void OnUnloaded() {
4537
}
4638
}

src/Nullinside.TwitchStreamingTools/Views/NewVersionWindow.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<TextBlock Text="{Binding ServerVersion}" HorizontalAlignment="Center" VerticalAlignment="Center" />
3434
</StackPanel>
3535
<StackPanel Orientation="Horizontal">
36-
<Button Command="{Binding UpdateSoftware}" Margin="0 10 0 0">Install New Version</Button>
37-
<Button Command="{Binding CloseWindow}" CommandParameter="{Binding ElementName=Window}"
36+
<Button Command="{Binding StartUpdateSoftwareCommand}" Margin="0 10 0 0">Install New Version</Button>
37+
<Button Command="{Binding CloseWindowCommand}" CommandParameter="{Binding ElementName=Window}"
3838
Margin="10 10 0 0">
3939
Close
4040
</Button>

src/Nullinside.TwitchStreamingTools/Views/Pages/AccountView.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<controls1:Loading IsVisible="{Binding LoggingIn}" Width="75" Height="75" />
2929
<Button IsVisible="{Binding !HasValidOAuthToken}"
3030
IsEnabled="{Binding !LoggingIn}"
31-
Command="{Binding OnPerformLogin}"
31+
Command="{Binding PerformLoginCommand}"
3232
HorizontalAlignment="Center"
3333
VerticalContentAlignment="Top">
3434
Login
@@ -47,7 +47,7 @@
4747
<TextBlock Grid.Column="0" VerticalAlignment="Center" Margin="0 0 10 0">User:</TextBlock>
4848
<TextBox Grid.Column="1" Text="{Binding TwitchUsername}" IsReadOnly="True" />
4949
</Grid>
50-
<Button Command="{Binding OnLogout}"
50+
<Button Command="{Binding ClearCredentialsCommand}"
5151
HorizontalAlignment="Center"
5252
VerticalContentAlignment="Top">
5353
Logout

src/Nullinside.TwitchStreamingTools/Views/Pages/ChatView.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
</Grid.ColumnDefinitions>
3030
<TextBlock Grid.Column="0" VerticalAlignment="Center" Margin="0 0 5 0">Twitch Channel:</TextBlock>
3131
<TextBox Grid.Column="1" Text="{Binding TwitchChatName}" Margin="0 0 5 0" />
32-
<Button Grid.Column="2" IsDefault="True" Command="{Binding OnAddChat}" Content="Add" />
32+
<Button Grid.Column="2" IsDefault="True" Command="{Binding AddChatCommand}" Content="Add" />
3333
</Grid>
3434

3535
<Label Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center">Twitch Channels</Label>
@@ -45,7 +45,7 @@
4545
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Text="{Binding }" Padding="0" />
4646
<Button Grid.Column="1"
4747
FontWeight="Bold"
48-
Command="{Binding $parent[UserControl].((chatView:ChatViewModel)DataContext).OnRemoveChat, FallbackValue=null}"
48+
Command="{Binding $parent[UserControl].((chatView:ChatViewModel)DataContext).RemoveChatCommand, FallbackValue=null}"
4949
CommandParameter="{Binding }">
5050
<Button.Styles>
5151
<Style Selector="Button">

0 commit comments

Comments
 (0)