AdminViewModel.cs
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Text.Json.Nodes;
namespace IndustrialControl.ViewModels;
public partial class AdminViewModel : ObservableObject
{
private readonly IConfigLoader _cfg;
[ObservableProperty] private int schemaVersion;
[ObservableProperty] private string ipAddress = "";
[ObservableProperty] private int port;
[ObservableProperty] private string baseUrl = "";
public AdminViewModel(IConfigLoader cfg)
{
_cfg = cfg;
LoadFromConfig();
}
private void LoadFromConfig()
{
JsonNode node = _cfg.Load();
SchemaVersion = node["schemaVersion"]?.GetValue<int?>() ?? 0;
var server = node["server"] as JsonObject ?? new JsonObject();
IpAddress = server["ipAddress"]?.GetValue<string>() ?? "";
Port = server["port"]?.GetValue<int?>() ?? 80;
BaseUrl = $"http://{IpAddress}:{Port}";
}
[RelayCommand]
public Task SaveAsync()
{
var node = _cfg.Load();
var server = node["server"] as JsonObject ?? new JsonObject();
server["ipAddress"] = IpAddress.Trim();
server["port"] = Port;
node["server"] = server;
_cfg.Save(node);
BaseUrl = $"http://{IpAddress}:{Port}";
return Shell.Current.DisplayAlert("已保存", "配置已保存,可立即生效。", "确定");
}
[RelayCommand]
public async Task ResetToPackageAsync()
{
await _cfg.EnsureLatestAsync(); // 包内 schemaVersion 高则触发合并覆盖
LoadFromConfig();
await Shell.Current.DisplayAlert("已重载", "已从包内默认配置重载/合并。", "确定");
}
}