Skip to content

Commit 57387fd

Browse files
committed
[SmartScripts] Asynchronous save to database
1 parent 54021c4 commit 57387fd

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

WDE.SmartScriptEditor/Editor/ViewModels/SmartScriptEditorViewModel.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
using WDE.Common.Providers;
2020
using WDE.Common.Solution;
2121
using System.Diagnostics;
22+
using System.Threading.Tasks;
2223
using System.Windows;
2324
using System.Windows.Data;
2425
using System.Windows.Input;
26+
using AsyncAwaitBestPractices.MVVM;
2527
using WDE.Common.Managers;
28+
using WDE.Common.Utils;
2629
using WDE.SmartScriptEditor.Editor.UserControls;
2730

2831
namespace WDE.SmartScriptEditor.Editor.ViewModels
@@ -35,6 +38,7 @@ public class SmartScriptEditorViewModel : BindableBase, IDocument, System.IDispo
3538
private readonly IItemFromListProvider itemFromListProvider;
3639
private readonly ISmartFactory smartFactory;
3740
private readonly ISmartTypeListProvider smartTypeListProvider;
41+
private readonly IStatusBar statusbar;
3842
private readonly ISolutionItemNameRegistry itemNameRegistry;
3943

4044
private SmartScriptSolutionItem _item;
@@ -75,7 +79,7 @@ public class SmartScriptEditorViewModel : BindableBase, IDocument, System.IDispo
7579
public DelegateCommand CopyCommand { get; set; }
7680
public DelegateCommand CutCommand { get; set; }
7781
public DelegateCommand PasteCommand { get; set; }
78-
public DelegateCommand SaveCommand { get; set; }
82+
public AsyncAutoCommand SaveCommand { get; set; }
7983
public DelegateCommand DeleteSelected { get; set; }
8084
public DelegateCommand EditSelected { get; set; }
8185

@@ -102,6 +106,7 @@ public SmartScriptEditorViewModel(IHistoryManager history,
102106
ISmartFactory smartFactory,
103107
IItemFromListProvider itemFromListProvider,
104108
ISmartTypeListProvider smartTypeListProvider,
109+
IStatusBar statusbar,
105110
ISolutionItemNameRegistry itemNameRegistry)
106111
{
107112
this.history = history;
@@ -110,6 +115,7 @@ public SmartScriptEditorViewModel(IHistoryManager history,
110115
this.smartFactory = smartFactory;
111116
this.itemFromListProvider = itemFromListProvider;
112117
this.smartTypeListProvider = smartTypeListProvider;
118+
this.statusbar = statusbar;
113119
this.itemNameRegistry = itemNameRegistry;
114120

115121
EditEvent = new DelegateCommand(EditEventCommand);
@@ -192,7 +198,10 @@ public SmartScriptEditorViewModel(IHistoryManager history,
192198
AddEvent = new DelegateCommand(AddEventCommand);
193199
AddAction = new DelegateCommand<NewActionViewModel>(AddActionCommand);
194200

195-
SaveCommand = new DelegateCommand(SaveAllToDb);
201+
SaveCommand = new AsyncAutoCommand(SaveAllToDb, null, e =>
202+
{
203+
statusbar.PublishNotification(new PlainNotification(NotificationType.Error, "Error while saving script to the database: " + e.Message));
204+
});
196205

197206
DeleteAction = new DelegateCommand<SmartAction>(DeleteActionCommand);
198207
DeleteSelected = new DelegateCommand(() =>
@@ -515,10 +524,10 @@ internal void SetSolutionItem(SmartScriptSolutionItem item)
515524

516525
history.AddHandler(new SaiHistoryHandler(script));
517526
}
518-
519-
private void SaveAllToDb()
527+
528+
private async Task SaveAllToDb()
520529
{
521-
530+
statusbar.PublishNotification(new PlainNotification(NotificationType.Info, "Saving to database"));
522531
List<AbstractSmartScriptLine> lines = new List<AbstractSmartScriptLine>();
523532

524533
int eventId = 1;
@@ -541,14 +550,9 @@ private void SaveAllToDb()
541550
}
542551
}
543552

544-
try
545-
{
546-
database.InstallScriptFor(_item.Entry, _item.SmartType, lines);
547-
}
548-
catch (Exception e)
549-
{
550-
MessageBox.Show("Error while saving script to the database: " + e.Message);
551-
}
553+
await database.InstallScriptFor(_item.Entry, _item.SmartType, lines);
554+
555+
statusbar.PublishNotification(new PlainNotification(NotificationType.Success, "Saved to database"));
552556
}
553557

554558
private AbstractSmartScriptLine GenerateSingleSai(int eventId, SmartEvent ev, SmartAction action, int link = 0, string comment = null)

WDE.SmartScriptEditor/WDE.SmartScriptEditor.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
</Content>
7171
</ItemGroup>
7272
<ItemGroup>
73+
<PackageReference Include="AsyncAwaitBestPractices.MVVM">
74+
<Version>5.0.2</Version>
75+
</PackageReference>
7376
<PackageReference Include="CommonServiceLocator">
7477
<Version>2.0.5</Version>
7578
</PackageReference>

0 commit comments

Comments
 (0)