Skip to content

Commit dc74ea6

Browse files
Merge pull request #45 from nullinside-development-group/feat/mvvm
feat: CommunityToolkit.Mvvm
2 parents da8678e + f17ba43 commit dc74ea6

File tree

7 files changed

+60
-136
lines changed

7 files changed

+60
-136
lines changed

src/SiteMonitor/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal sealed class Program {
1414
/// The logger.
1515
/// </summary>
1616
private static readonly ILog LOG = LogManager.GetLogger(typeof(Program));
17-
17+
1818
// Initialization code. Don't use any Avalonia, third-party APIs or any
1919
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
2020
// yet and stuff might break.
@@ -25,13 +25,13 @@ public static void Main(string[] args) {
2525
#else
2626
XmlConfigurator.Configure(new FileInfo("log4net.config"));
2727
#endif
28-
28+
2929
LOG.Info("Started application");
3030

3131
AppDomain.CurrentDomain.UnhandledException += (_, exceptArgs) => {
3232
LOG.Fatal("Unhandled exception", exceptArgs.ExceptionObject as Exception);
3333
};
34-
34+
3535
BuildAvaloniaApp()
3636
.StartWithClassicDesktopLifetime(args);
3737
}

src/SiteMonitor/SiteMonitor.csproj

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,28 @@
2626
</ItemGroup>
2727

2828
<ItemGroup>
29-
<PackageReference Include="Avalonia" Version="11.3.6" />
30-
<PackageReference Include="Avalonia.Desktop" Version="11.3.6" />
31-
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.6" />
32-
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.6" />
29+
<PackageReference Include="Avalonia" Version="11.3.6"/>
30+
<PackageReference Include="Avalonia.Desktop" Version="11.3.6"/>
31+
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.6"/>
32+
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.6"/>
3333
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
3434
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.3.1"/>
35-
<PackageReference Include="Avalonia.ReactiveUI" Version="11.3.6" />
35+
<PackageReference Include="Avalonia.ReactiveUI" Version="11.3.6"/>
36+
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0"/>
3637
</ItemGroup>
3738

3839
<ItemGroup>
3940
<ProjectReference Include="..\nullinside-api\src\Nullinside.Api.Common\Nullinside.Api.Common.csproj"/>
4041
</ItemGroup>
4142

4243
<ItemGroup>
43-
<None Remove="log4net.config" />
44-
<Content Include="log4net.config">
45-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
46-
</Content>
47-
<None Remove="log4net.debug.config" />
48-
<Content Include="log4net.debug.config">
49-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
50-
</Content>
44+
<None Remove="log4net.config"/>
45+
<Content Include="log4net.config">
46+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
47+
</Content>
48+
<None Remove="log4net.debug.config"/>
49+
<Content Include="log4net.debug.config">
50+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
51+
</Content>
5152
</ItemGroup>
5253
</Project>

src/SiteMonitor/ViewModels/MainWindowViewModel.cs

Lines changed: 26 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
using System.Net.NetworkInformation;
66
using System.Threading;
77
using System.Threading.Tasks;
8-
using System.Windows.Input;
98

109
using Avalonia.Controls;
1110

12-
using ReactiveUI;
11+
using CommunityToolkit.Mvvm.ComponentModel;
12+
using CommunityToolkit.Mvvm.Input;
1313

1414
using Renci.SshNet;
1515

@@ -20,26 +20,32 @@ namespace SiteMonitor.ViewModels;
2020
/// <summary>
2121
/// The view model for the main UI.
2222
/// </summary>
23-
public class MainWindowViewModel : ViewModelBase {
24-
private bool _apiUp = true;
25-
private string? _chatTimestamp;
26-
private bool _isDisplayingAdvancedCommands;
27-
private bool _isMinimized;
28-
private bool _nullUp = true;
23+
public partial class MainWindowViewModel : ViewModelBase {
24+
[ObservableProperty] private bool _apiUp = true;
25+
26+
[ObservableProperty] private string? _chatTimestamp;
27+
28+
[ObservableProperty] private bool _isDisplayingAdvancedCommands;
29+
30+
[ObservableProperty] private bool _isMinimized;
31+
32+
[ObservableProperty] private bool _nullUp = true;
33+
2934
private string? _serverAddress;
30-
private bool _serverUp = true;
35+
36+
[ObservableProperty] private bool _serverUp = true;
37+
3138
private string? _sshPassword;
3239
private string? _sshUsername;
33-
private bool _websiteUp = true;
40+
41+
[ObservableProperty] private bool _websiteUp = true;
42+
3443
private WindowState _windowState;
3544

3645
/// <summary>
3746
/// Initializes a new instance of the <see cref="MainWindowViewModel" /> class.
3847
/// </summary>
3948
public MainWindowViewModel() {
40-
OnShowCommandsCommand = ReactiveCommand.Create(OnShowCommands);
41-
OnRestartCommand = ReactiveCommand.Create(OnRestart);
42-
OnRestartImagesCommand = ReactiveCommand.Create(OnRestartImages);
4349
Task.Factory.StartNew(PingServer);
4450
Task.Factory.StartNew(PingSite);
4551
ServerAddress = Configuration.Instance.ServerAddress;
@@ -53,7 +59,7 @@ public MainWindowViewModel() {
5359
public string? ServerAddress {
5460
get => _serverAddress;
5561
set {
56-
this.RaiseAndSetIfChanged(ref _serverAddress, value);
62+
SetProperty(ref _serverAddress, value);
5763

5864
try {
5965
Configuration.Instance.ServerAddress = value;
@@ -63,86 +69,25 @@ public string? ServerAddress {
6369
}
6470
}
6571

66-
/// <summary>
67-
/// The timestamp of the last chat message that was received.
68-
/// </summary>
69-
public string? ChatTimestamp {
70-
get => _chatTimestamp;
71-
set => this.RaiseAndSetIfChanged(ref _chatTimestamp, value);
72-
}
73-
74-
/// <summary>
75-
/// True if the server is online.
76-
/// </summary>
77-
public bool ServerUp {
78-
get => _serverUp;
79-
set => this.RaiseAndSetIfChanged(ref _serverUp, value);
80-
}
81-
82-
/// <summary>
83-
/// True if the website is returning a 200.
84-
/// </summary>
85-
public bool WebsiteUp {
86-
get => _websiteUp;
87-
set => this.RaiseAndSetIfChanged(ref _websiteUp, value);
88-
}
89-
90-
/// <summary>
91-
/// True if the API is online.
92-
/// </summary>
93-
public bool ApiUp {
94-
get => _apiUp;
95-
set => this.RaiseAndSetIfChanged(ref _apiUp, value);
96-
}
97-
98-
/// <summary>
99-
/// True if the null API is online.
100-
/// </summary>
101-
public bool NullUp {
102-
get => _nullUp;
103-
set => this.RaiseAndSetIfChanged(ref _nullUp, value);
104-
}
105-
10672
/// <summary>
10773
/// Sets the window to normal, minimized, maximized, etc.
10874
/// </summary>
10975
public WindowState WindowState {
11076
get => _windowState;
11177
set {
112-
this.RaiseAndSetIfChanged(ref _windowState, value);
78+
SetProperty(ref _windowState, value);
11379
IsMinimized = _windowState == WindowState.Minimized;
11480
}
11581
}
11682

117-
/// <summary>
118-
/// True if the application is minimized, false otherwise.
119-
/// </summary>
120-
public bool IsMinimized {
121-
get => _isMinimized;
122-
set => this.RaiseAndSetIfChanged(ref _isMinimized, value);
123-
}
124-
125-
/// <summary>
126-
/// Shows the commands in the UI.
127-
/// </summary>
128-
public ICommand OnShowCommandsCommand { get; set; }
129-
130-
/// <summary>
131-
/// True if displaying advanced commands, false otherwise.
132-
/// </summary>
133-
public bool IsDisplayingAdvancedCommands {
134-
get => _isDisplayingAdvancedCommands;
135-
set => this.RaiseAndSetIfChanged(ref _isDisplayingAdvancedCommands, value);
136-
}
137-
13883
/// <summary>
13984
/// The username to use for the SSH session for commands.
14085
/// </summary>
14186
public string? SshUsername {
14287
get => _sshUsername;
14388
set {
14489
Configuration.Instance.ServerUsername = value;
145-
this.RaiseAndSetIfChanged(ref _sshUsername, value);
90+
SetProperty(ref _sshUsername, value);
14691
try {
14792
Configuration.WriteConfiguration();
14893
}
@@ -157,7 +102,7 @@ public string? SshPassword {
157102
get => _sshPassword;
158103
set {
159104
Configuration.Instance.ServerPassword = value;
160-
this.RaiseAndSetIfChanged(ref _sshPassword, value);
105+
SetProperty(ref _sshPassword, value);
161106
try {
162107
Configuration.WriteConfiguration();
163108
}
@@ -168,16 +113,7 @@ public string? SshPassword {
168113
/// <summary>
169114
/// Restarts the remote machine.
170115
/// </summary>
171-
public ICommand OnRestartCommand { get; set; }
172-
173-
/// <summary>
174-
/// Restarts the remote docker images.
175-
/// </summary>
176-
public ICommand OnRestartImagesCommand { get; set; }
177-
178-
/// <summary>
179-
/// Restarts the remote machine.
180-
/// </summary>
116+
[RelayCommand]
181117
private async Task OnRestart() {
182118
using SshClient client = new(_serverAddress!, _sshUsername!, _sshPassword!);
183119
await client.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
@@ -188,6 +124,7 @@ private async Task OnRestart() {
188124
/// <summary>
189125
/// Restarts the docker images.
190126
/// </summary>
127+
[RelayCommand]
191128
private async Task OnRestartImages() {
192129
await Task.Run(async () => {
193130
using SshClient client = new(_serverAddress!, _sshUsername!, _sshPassword!);
@@ -208,6 +145,7 @@ await Task.Run(async () => {
208145
/// <summary>
209146
/// Handles showing the server commands.
210147
/// </summary>
148+
[RelayCommand]
211149
private void OnShowCommands() {
212150
IsDisplayingAdvancedCommands = true;
213151
}

src/SiteMonitor/ViewModels/NewVersionWindowViewModel.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Avalonia.Controls;
66
using Avalonia.Threading;
77

8+
using CommunityToolkit.Mvvm.ComponentModel;
9+
810
using Nullinside.Api.Common.Desktop;
911

1012
using ReactiveUI;
@@ -16,11 +18,11 @@ namespace SiteMonitor.ViewModels;
1618
/// <summary>
1719
/// The view model for the <seealso cref="NewVersionWindow" /> class.
1820
/// </summary>
19-
public class NewVersionWindowViewModel : ViewModelBase {
21+
public partial class NewVersionWindowViewModel : ViewModelBase {
2022
/// <summary>
2123
/// True if updating the application currently, false otherwise.
2224
/// </summary>
23-
private bool _isUpdating;
25+
[ObservableProperty] private bool _isUpdating;
2426

2527
/// <summary>
2628
/// The local version of the software.
@@ -35,7 +37,7 @@ public class NewVersionWindowViewModel : ViewModelBase {
3537
/// <summary>
3638
/// The version of the application on the GitHub server.
3739
/// </summary>
38-
private string? _serverVersion;
40+
[ObservableProperty] private string? _serverVersion;
3941

4042
/// <summary>
4143
/// Initializes a new instance of the <see cref="NewVersionWindowViewModel" /> class.
@@ -66,22 +68,6 @@ public string? LocalVersion {
6668
set => _localVersion = value;
6769
}
6870

69-
/// <summary>
70-
/// The version of the software on the GitHub server.
71-
/// </summary>
72-
public string? ServerVersion {
73-
get => _serverVersion;
74-
set => this.RaiseAndSetIfChanged(ref _serverVersion, value);
75-
}
76-
77-
/// <summary>
78-
/// True if updating the application currently, false otherwise.
79-
/// </summary>
80-
public bool IsUpdating {
81-
get => _isUpdating;
82-
set => this.RaiseAndSetIfChanged(ref _isUpdating, value);
83-
}
84-
8571
/// <summary>
8672
/// A command to update the software.
8773
/// </summary>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using ReactiveUI;
1+
using CommunityToolkit.Mvvm.ComponentModel;
22

33
namespace SiteMonitor.ViewModels;
44

55
/// <summary>
66
/// A base class for all view models.
77
/// </summary>
8-
public class ViewModelBase : ReactiveObject {
8+
public class ViewModelBase : ObservableObject {
99
}

src/SiteMonitor/Views/MainWindow.axaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<StackPanel Grid.Row="2">
4545
<Button HorizontalAlignment="Right"
4646
IsVisible="{Binding !IsDisplayingAdvancedCommands}"
47-
Command="{Binding OnShowCommandsCommand}">
47+
Command="{Binding ShowCommandsCommand}">
4848
Show Advanced Commands
4949
</Button>
5050
<StackPanel IsVisible="{Binding IsDisplayingAdvancedCommands}">
@@ -81,11 +81,11 @@
8181
</Grid>
8282
<StackPanel Orientation="Horizontal">
8383
<Button HorizontalAlignment="Right"
84-
Command="{Binding OnRestartCommand}">
84+
Command="{Binding RestartCommand}">
8585
Restart Computer
8686
</Button>
8787
<Button HorizontalAlignment="Right"
88-
Command="{Binding OnRestartImagesCommand}">
88+
Command="{Binding RestartImagesCommand}">
8989
Restart Services
9090
</Button>
9191
</StackPanel>

src/SiteMonitor/Views/MainWindow.axaml.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
#if !DEBUG
2+
using Avalonia.Threading;
3+
4+
using SiteMonitor.ViewModels;
5+
#else
6+
using Avalonia;
7+
#endif
18
using System;
29
using System.Linq;
310
using System.Threading.Tasks;
@@ -8,14 +15,6 @@
815
using Microsoft.Extensions.DependencyInjection;
916

1017
using Nullinside.Api.Common.Desktop;
11-
#if !DEBUG
12-
using Avalonia.Threading;
13-
14-
using SiteMonitor.ViewModels;
15-
16-
#else
17-
using Avalonia;
18-
#endif
1918

2019

2120
namespace SiteMonitor.Views;

0 commit comments

Comments
 (0)