55using System . Net . NetworkInformation ;
66using System . Threading ;
77using System . Threading . Tasks ;
8- using System . Windows . Input ;
98
109using Avalonia . Controls ;
1110
12- using ReactiveUI ;
11+ using CommunityToolkit . Mvvm . ComponentModel ;
12+ using CommunityToolkit . Mvvm . Input ;
1313
1414using 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 }
0 commit comments