-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolBarViewModel.cs
More file actions
226 lines (184 loc) · 6.83 KB
/
ToolBarViewModel.cs
File metadata and controls
226 lines (184 loc) · 6.83 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using CommunityToolkit.Maui;
using CommunityToolkit.Maui.Storage;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using NodeSharp.NodeEngine;
using NodeSharp.Nodes.Common.Extension;
using NodeSharp.Nodes.Common.ViewModels;
namespace NodeSharp.Client.ViewModel;
[SuppressMessage("Usage", "CsWinRT1030:Project does not enable unsafe blocks")]
[SuppressMessage("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "MVVMTK0045:Using [ObservableProperty] on fields is not AOT compatible for WinRT")]
public partial class ToolBarViewModel : ObservableObject
{
[ObservableProperty] private bool isSaveEnabled = false;
[ObservableProperty] private bool isSaveAsEnabled = false;
[ObservableProperty] private bool isLoadEnabled = true;
[ObservableProperty] private bool isNewEnabled = true;
[ObservableProperty] private bool isQuitEnabled = true;
private readonly DiagramViewModel diagramViewModel;
private readonly LineConnectionManager lineConnectionManager;
private readonly NodeIo nodeIo;
private readonly IPopupService popupService;
private readonly ErrorPopupViewModel errorPopupViewModel;
private readonly DebugViewModel debugViewModel;
/// <inheritdoc/>
public ToolBarViewModel(DiagramViewModel diagramViewModel, LineConnectionManager lineConnectionManager,
NodeIo nodeIo, IPopupService popupService, ErrorPopupViewModel errorPopupViewModel, DebugViewModel debugViewModel)
{
this.diagramViewModel = diagramViewModel;
this.lineConnectionManager = lineConnectionManager;
this.nodeIo = nodeIo;
this.popupService = popupService;
this.errorPopupViewModel = errorPopupViewModel;
this.debugViewModel = debugViewModel;
// Subscribe to collection changes to refresh command states
nodeIo.Nodes.CollectionChanged += (s, e) => { UpdateToolbarCommandStates(); };
}
private void UpdateToolbarCommandStates()
{
LoadCommand.NotifyCanExecuteChanged();
SaveCommand.NotifyCanExecuteChanged();
SaveAsCommand.NotifyCanExecuteChanged();
}
[RelayCommand(CanExecute = nameof(CanDoSave))]
private async Task Save()
{
Console.WriteLine("Save executed!");
try
{
var nodes = diagramViewModel.BoxNodes;
foreach (var nodeBox in nodes)
{
nodeBox.Node.X = (int)nodeBox.X;
nodeBox.Node.Y = (int)nodeBox.Y;
}
if (!string.IsNullOrEmpty(nodeIo.FileNameSaved))
{
await nodeIo.SaveToFileAsync();
}
}
catch (Exception exception)
{
throw new Exception("Save: error during save process.", exception);
}
}
[RelayCommand(CanExecute = nameof(CanDoSaveAs))]
private async Task SaveAs()
{
Console.WriteLine("SaveAs executed!");
var ms = new MemoryStream();
await nodeIo.SaveToFileAsync(ms);
var fileSaverResult = await FileSaver.Default.SaveAsync(
"Nodes.json",
ms,
CancellationToken.None);
if (fileSaverResult.IsSuccessful)
{
// User picked a location, file saved successfully
nodeIo.FileNameSaved = fileSaverResult.FilePath;
Console.WriteLine($"File saved at: {nodeIo.FileNameSaved}");
UpdateToolbarCommandStates();
}
else
{
// Handle error or cancellation
Console.WriteLine($"Error: {fileSaverResult.Exception?.Message}");
}
}
[RelayCommand(CanExecute = nameof(CanDoLoad))]
private async Task Load()
{
Console.WriteLine("Load executed!");
try
{
diagramViewModel.Clear();
WeakReferenceMessenger.Default.Send(new NodeActionEvent { ActionEventType = NodeActionEventType.Reset});
WeakReferenceMessenger.Default.Send(new ConnectionPointStatus { IsCanvasInvalid = false });
await PickFileAsync();
WeakReferenceMessenger.Default.Send(new RebuildAnchorPointStatus { IsAnchorAdded = true });
WeakReferenceMessenger.Default.Send(new ConnectionPointStatus { IsCanvasInvalid = true });
debugViewModel.UpdateToolbarCommandStates();
}
// catch (NodeParseException nodeParseException)
// {
// Debug.WriteLine(nodeParseException.Message);
// }
catch (Exception e)
{
await e.ShowPopupAsync("Error!!!");
Debug.WriteLine(e.Message);
}
}
[RelayCommand(CanExecute = nameof(CanDoNew))]
private Task New()
{
Console.WriteLine("New executed!");
nodeIo.Clear();
WeakReferenceMessenger.Default.Send(new NodeActionEvent { ActionEventType = NodeActionEventType.Reset});
WeakReferenceMessenger.Default.Send(new ConnectionPointStatus { IsCanvasInvalid = true });
UpdateToolbarCommandStates();
debugViewModel.UpdateToolbarCommandStates();
return Task.CompletedTask;
}
[RelayCommand(CanExecute = nameof(CanDoQuit))]
private Task Quit()
{
Console.WriteLine("Quit executed!");
Application.Current?.Quit();
return Task.CompletedTask;
}
private bool CanDoSave()
{
IsSaveEnabled = diagramViewModel.BoxNodes.Count != 0 && nodeIo.FileNameSaved != null;
return IsSaveEnabled;
}
private bool CanDoSaveAs()
{
IsSaveAsEnabled = diagramViewModel.BoxNodes.Count != 0;
return IsSaveAsEnabled;
}
private bool CanDoLoad()
{
IsLoadEnabled = diagramViewModel.BoxNodes.Count == 0;
return IsLoadEnabled;
}
private bool CanDoNew()
{
return IsNewEnabled;
}
private bool CanDoQuit()
{
return IsQuitEnabled;
}
private async Task PickFileAsync()
{
var result = await FilePicker.Default.PickAsync();
if (result != null)
{
// Full path (Windows/macOS only; on mobile you get a stream)
var filePath = result.FullPath;
Console.WriteLine($"Picked file: {filePath}");
// Open as stream
using var stream = await result.OpenReadAsync();
using var reader = new StreamReader(stream);
// var content = await reader.ReadToEndAsync();
await diagramViewModel.Init(reader, filePath);
Console.WriteLine($"File content: {filePath}");
}
else
{
Console.WriteLine("User canceled file picking.");
}
}
}
public class ConnectionPointStatus
{
public bool IsCanvasInvalid { get; set; }
}
public class RebuildAnchorPointStatus
{
public bool IsAnchorAdded { get; set; }
}