|
7 | 7 | using NapCatScript.Core.JsonFormat; |
8 | 8 | using static NapCatScript.Core.MsgHandle.Utils; |
9 | 9 |
|
10 | | -namespace NapCatScript.Desktop.ViewModels |
| 10 | +namespace NapCatScript.Desktop.ViewModels; |
| 11 | + |
| 12 | +public class MainWindowViewModel : ViewModelBase |
11 | 13 | { |
12 | | - public class MainWindowViewModel : ViewModelBase |
| 14 | + private bool _webUiInit = false; |
| 15 | + private ViewModelBase currView; |
| 16 | + private string _selectedItem; |
| 17 | + |
| 18 | + public IReactiveCommand ListBoxPropertyChangedCommand { get; private set; } |
| 19 | + private ViewModelBase? _logViewModel; |
| 20 | + private ViewModelBase? _netWorkModel; |
| 21 | + private ViewModelBase? _webUIConnectionViewModel; |
| 22 | + public ViewModelBase LogViewModel => _logViewModel ??= new LogViewModel(); |
| 23 | + public ViewModelBase NetWorkModel => _netWorkModel ??= new NetWorkViewModel(); |
| 24 | + public ViewModelBase WebUIConnectionViewModel => _webUIConnectionViewModel ??= new WebUIConnectionViewModel(); |
| 25 | + |
| 26 | + public ObservableCollection<string> Items { get; } = []; |
| 27 | + |
| 28 | + public string SelectedItem |
13 | 29 | { |
14 | | - private bool _webUiInit = false; |
15 | | - private ViewModelBase currView; |
16 | | - private string _selectedItem; |
17 | | - |
18 | | - public IReactiveCommand ListBoxPropertyChangedCommand {get; private set; } |
19 | | - private ViewModelBase? _logViewModel; |
20 | | - public ViewModelBase LogViewModel |
21 | | - { |
22 | | - get { |
23 | | - if (_logViewModel is null) { |
24 | | - _logViewModel = new LogViewModel(); |
25 | | - } |
26 | | - return _logViewModel; |
27 | | - } |
28 | | - } |
29 | | - |
30 | | - private ViewModelBase? _netWorkModel; |
31 | | - public ViewModelBase NetWorkModel |
32 | | - { |
33 | | - get { |
34 | | - if (_netWorkModel is null) { |
35 | | - _netWorkModel = new NetWorkViewModel(); |
36 | | - } |
37 | | - return _netWorkModel; |
38 | | - } |
39 | | - } |
| 30 | + get => _selectedItem; |
| 31 | + set => this.RaiseAndSetIfChanged(ref _selectedItem, value); |
| 32 | + } |
40 | 33 |
|
41 | | - private ViewModelBase? _webUIConnectionViewModel; |
42 | | - public ViewModelBase WebUIConnectionViewModel |
43 | | - { |
44 | | - get { |
45 | | - if (_webUIConnectionViewModel is null) { |
46 | | - _webUIConnectionViewModel = new WebUIConnectionViewModel(); |
47 | | - } |
48 | | - return _webUIConnectionViewModel; |
49 | | - } |
50 | | - } |
51 | | - public ObservableCollection<string> Items { get; } = []; |
52 | | - public string SelectedItem { get => _selectedItem; set => this.RaiseAndSetIfChanged(ref _selectedItem, value); } |
53 | | - public ViewModelBase CurrView { get => currView; set => this.RaiseAndSetIfChanged(ref currView, value); } |
54 | | - public MainWindowViewModel() |
55 | | - { |
56 | | - this.WhenAnyValue(f => f.SelectedItem) |
57 | | - .WhereNotNull() |
58 | | - .Subscribe(ListBoxPropertyChanged); |
| 34 | + public ViewModelBase CurrView |
| 35 | + { |
| 36 | + get => currView; |
| 37 | + set => this.RaiseAndSetIfChanged(ref currView, value); |
| 38 | + } |
59 | 39 |
|
60 | | - Interactions.WebUIConnectionEvent.RegisterHandler(WebUILoginEvent); |
61 | | - Items.Add("连接"); |
62 | | - Items.Add("聊天"); |
63 | | - Items.Add("日志"); |
64 | | - Items.Add("网络"); |
65 | | - CurrView = WebUIConnectionViewModel; |
66 | | - } |
| 40 | + public MainWindowViewModel() |
| 41 | + { |
| 42 | + this.WhenAnyValue(f => f.SelectedItem) |
| 43 | + .WhereNotNull() |
| 44 | + .Subscribe(ListBoxPropertyChanged); |
67 | 45 |
|
68 | | - private void ListBoxPropertyChanged(string str) |
69 | | - {/* |
70 | | - if (CurrView is LogViewModel log){ |
71 | | - log.Dispose(); |
72 | | - } |
73 | | - CurrView = null; |
74 | | - GC.Collect();*/ |
75 | | - |
76 | | - if(_webUiInit == false) |
77 | | - return; |
78 | | - if(str == "网络") CurrView = NetWorkModel; |
79 | | - if(str == "日志") CurrView = LogViewModel; |
80 | | - if(str == "连接") CurrView = WebUIConnectionViewModel; |
81 | | - } |
| 46 | + Interactions.WebUIConnectionInteraction.RegisterHandler(WebUILoginEvent); |
| 47 | + Items.Add("连接"); |
| 48 | + Items.Add("聊天"); |
| 49 | + Items.Add("日志"); |
| 50 | + Items.Add("网络"); |
| 51 | + CurrView = WebUIConnectionViewModel; |
| 52 | + } |
82 | 53 |
|
83 | | - private async Task WebUILoginEvent(IInteractionContext<(string,string),Unit> interaction) |
84 | | - { |
85 | | - (string webURL, string token) connectionValue = interaction.Input; |
86 | | - interaction.SetOutput(Unit.Default); |
87 | | - if(connectionValue.token == ConfigValue.WebToken && connectionValue.webURL == ConfigValue.WebUri) |
88 | | - return; |
89 | | - |
90 | | - var rms = await WebUILogin(connectionValue.webURL, connectionValue.token); |
91 | | - var requJson = await rms.Content.ReadAsStringAsync(); |
92 | | - if(!requJson.GetJsonElement(out var json)) |
93 | | - return; |
94 | | - if(!json.TryGetPropertyValue("message", out var Jvalue)) |
95 | | - return; |
| 54 | + private void ListBoxPropertyChanged(string str) |
| 55 | + { |
| 56 | + if (_webUiInit == false) |
| 57 | + return; |
| 58 | + if (str == "网络") CurrView = NetWorkModel; |
| 59 | + if (str == "日志") CurrView = LogViewModel; |
| 60 | + if (str == "连接") CurrView = WebUIConnectionViewModel; |
| 61 | + } |
96 | 62 |
|
97 | | - if(Jvalue.GetString() != "success") |
98 | | - return; |
99 | | - |
100 | | - var aut = await GetAuthentication(connectionValue.webURL, connectionValue.token); |
101 | | - ConfigValue.WebToken = connectionValue.token; |
102 | | - ConfigValue.AuthToken = aut; |
103 | | - ConfigValue.WebUri = connectionValue.webURL; |
104 | | - CurrView = LogViewModel; |
105 | | - _webUiInit = true; |
| 63 | + private async Task WebUILoginEvent(IInteractionContext<(string, string), bool> interaction) |
| 64 | + { |
| 65 | + (string webURL, string token) connectionValue = interaction.Input; |
| 66 | + if (connectionValue.token == ConfigValue.WebToken && connectionValue.webURL == ConfigValue.WebUri) { |
| 67 | + interaction.SetOutput(false); |
| 68 | + return; |
106 | 69 | } |
| 70 | + |
| 71 | + var rms = await WebUILogin(connectionValue.webURL, connectionValue.token); |
| 72 | + var requJson = await rms.Content.ReadAsStringAsync(); |
| 73 | + if (!requJson.GetJsonElement(out var json) || |
| 74 | + !json.TryGetPropertyValue("message", out var Jvalue) || |
| 75 | + Jvalue.GetString() != "success") { |
| 76 | + interaction.SetOutput(false); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + var aut = await GetAuthentication(connectionValue.webURL, connectionValue.token); |
| 81 | + ConfigValue.WebToken = connectionValue.token; |
| 82 | + ConfigValue.AuthToken = aut; |
| 83 | + ConfigValue.WebUri = connectionValue.webURL; |
| 84 | + interaction.SetOutput(true); |
| 85 | + CurrView = LogViewModel; |
| 86 | + _webUiInit = true; |
107 | 87 | } |
108 | 88 | } |
0 commit comments