5
5
using System . Net . NetworkInformation ;
6
6
using System . Threading ;
7
7
using System . Threading . Tasks ;
8
- using System . Windows . Input ;
9
8
10
9
using Avalonia . Controls ;
11
10
12
- using ReactiveUI ;
11
+ using CommunityToolkit . Mvvm . ComponentModel ;
12
+ using CommunityToolkit . Mvvm . Input ;
13
13
14
14
using Renci . SshNet ;
15
15
@@ -20,26 +20,32 @@ namespace SiteMonitor.ViewModels;
20
20
/// <summary>
21
21
/// The view model for the main UI.
22
22
/// </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
+
29
34
private string ? _serverAddress ;
30
- private bool _serverUp = true ;
35
+
36
+ [ ObservableProperty ] private bool _serverUp = true ;
37
+
31
38
private string ? _sshPassword ;
32
39
private string ? _sshUsername ;
33
- private bool _websiteUp = true ;
40
+
41
+ [ ObservableProperty ] private bool _websiteUp = true ;
42
+
34
43
private WindowState _windowState ;
35
44
36
45
/// <summary>
37
46
/// Initializes a new instance of the <see cref="MainWindowViewModel" /> class.
38
47
/// </summary>
39
48
public MainWindowViewModel ( ) {
40
- OnShowCommandsCommand = ReactiveCommand . Create ( OnShowCommands ) ;
41
- OnRestartCommand = ReactiveCommand . Create ( OnRestart ) ;
42
- OnRestartImagesCommand = ReactiveCommand . Create ( OnRestartImages ) ;
43
49
Task . Factory . StartNew ( PingServer ) ;
44
50
Task . Factory . StartNew ( PingSite ) ;
45
51
ServerAddress = Configuration . Instance . ServerAddress ;
@@ -53,7 +59,7 @@ public MainWindowViewModel() {
53
59
public string ? ServerAddress {
54
60
get => _serverAddress ;
55
61
set {
56
- this . RaiseAndSetIfChanged ( ref _serverAddress , value ) ;
62
+ SetProperty ( ref _serverAddress , value ) ;
57
63
58
64
try {
59
65
Configuration . Instance . ServerAddress = value ;
@@ -63,86 +69,25 @@ public string? ServerAddress {
63
69
}
64
70
}
65
71
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
-
106
72
/// <summary>
107
73
/// Sets the window to normal, minimized, maximized, etc.
108
74
/// </summary>
109
75
public WindowState WindowState {
110
76
get => _windowState ;
111
77
set {
112
- this . RaiseAndSetIfChanged ( ref _windowState , value ) ;
78
+ SetProperty ( ref _windowState , value ) ;
113
79
IsMinimized = _windowState == WindowState . Minimized ;
114
80
}
115
81
}
116
82
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
-
138
83
/// <summary>
139
84
/// The username to use for the SSH session for commands.
140
85
/// </summary>
141
86
public string ? SshUsername {
142
87
get => _sshUsername ;
143
88
set {
144
89
Configuration . Instance . ServerUsername = value ;
145
- this . RaiseAndSetIfChanged ( ref _sshUsername , value ) ;
90
+ SetProperty ( ref _sshUsername , value ) ;
146
91
try {
147
92
Configuration . WriteConfiguration ( ) ;
148
93
}
@@ -157,7 +102,7 @@ public string? SshPassword {
157
102
get => _sshPassword ;
158
103
set {
159
104
Configuration . Instance . ServerPassword = value ;
160
- this . RaiseAndSetIfChanged ( ref _sshPassword , value ) ;
105
+ SetProperty ( ref _sshPassword , value ) ;
161
106
try {
162
107
Configuration . WriteConfiguration ( ) ;
163
108
}
@@ -168,16 +113,7 @@ public string? SshPassword {
168
113
/// <summary>
169
114
/// Restarts the remote machine.
170
115
/// </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 ]
181
117
private async Task OnRestart ( ) {
182
118
using SshClient client = new ( _serverAddress ! , _sshUsername ! , _sshPassword ! ) ;
183
119
await client . ConnectAsync ( CancellationToken . None ) . ConfigureAwait ( false ) ;
@@ -188,6 +124,7 @@ private async Task OnRestart() {
188
124
/// <summary>
189
125
/// Restarts the docker images.
190
126
/// </summary>
127
+ [ RelayCommand ]
191
128
private async Task OnRestartImages ( ) {
192
129
await Task . Run ( async ( ) => {
193
130
using SshClient client = new ( _serverAddress ! , _sshUsername ! , _sshPassword ! ) ;
@@ -208,6 +145,7 @@ await Task.Run(async () => {
208
145
/// <summary>
209
146
/// Handles showing the server commands.
210
147
/// </summary>
148
+ [ RelayCommand ]
211
149
private void OnShowCommands ( ) {
212
150
IsDisplayingAdvancedCommands = true ;
213
151
}
0 commit comments