Skip to content

Commit

Permalink
[SmartScripts] Asynchronous save to database
Browse files Browse the repository at this point in the history
  • Loading branch information
BAndysc committed Jan 11, 2021
1 parent 54021c4 commit 57387fd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
using WDE.Common.Providers;
using WDE.Common.Solution;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using AsyncAwaitBestPractices.MVVM;
using WDE.Common.Managers;
using WDE.Common.Utils;
using WDE.SmartScriptEditor.Editor.UserControls;

namespace WDE.SmartScriptEditor.Editor.ViewModels
Expand All @@ -35,6 +38,7 @@ public class SmartScriptEditorViewModel : BindableBase, IDocument, System.IDispo
private readonly IItemFromListProvider itemFromListProvider;
private readonly ISmartFactory smartFactory;
private readonly ISmartTypeListProvider smartTypeListProvider;
private readonly IStatusBar statusbar;
private readonly ISolutionItemNameRegistry itemNameRegistry;

private SmartScriptSolutionItem _item;
Expand Down Expand Up @@ -75,7 +79,7 @@ public class SmartScriptEditorViewModel : BindableBase, IDocument, System.IDispo
public DelegateCommand CopyCommand { get; set; }
public DelegateCommand CutCommand { get; set; }
public DelegateCommand PasteCommand { get; set; }
public DelegateCommand SaveCommand { get; set; }
public AsyncAutoCommand SaveCommand { get; set; }
public DelegateCommand DeleteSelected { get; set; }
public DelegateCommand EditSelected { get; set; }

Expand All @@ -102,6 +106,7 @@ public SmartScriptEditorViewModel(IHistoryManager history,
ISmartFactory smartFactory,
IItemFromListProvider itemFromListProvider,
ISmartTypeListProvider smartTypeListProvider,
IStatusBar statusbar,
ISolutionItemNameRegistry itemNameRegistry)
{
this.history = history;
Expand All @@ -110,6 +115,7 @@ public SmartScriptEditorViewModel(IHistoryManager history,
this.smartFactory = smartFactory;
this.itemFromListProvider = itemFromListProvider;
this.smartTypeListProvider = smartTypeListProvider;
this.statusbar = statusbar;
this.itemNameRegistry = itemNameRegistry;

EditEvent = new DelegateCommand(EditEventCommand);
Expand Down Expand Up @@ -192,7 +198,10 @@ public SmartScriptEditorViewModel(IHistoryManager history,
AddEvent = new DelegateCommand(AddEventCommand);
AddAction = new DelegateCommand<NewActionViewModel>(AddActionCommand);

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

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

history.AddHandler(new SaiHistoryHandler(script));
}

private void SaveAllToDb()
private async Task SaveAllToDb()
{

statusbar.PublishNotification(new PlainNotification(NotificationType.Info, "Saving to database"));
List<AbstractSmartScriptLine> lines = new List<AbstractSmartScriptLine>();

int eventId = 1;
Expand All @@ -541,14 +550,9 @@ private void SaveAllToDb()
}
}

try
{
database.InstallScriptFor(_item.Entry, _item.SmartType, lines);
}
catch (Exception e)
{
MessageBox.Show("Error while saving script to the database: " + e.Message);
}
await database.InstallScriptFor(_item.Entry, _item.SmartType, lines);

statusbar.PublishNotification(new PlainNotification(NotificationType.Success, "Saved to database"));
}

private AbstractSmartScriptLine GenerateSingleSai(int eventId, SmartEvent ev, SmartAction action, int link = 0, string comment = null)
Expand Down
3 changes: 3 additions & 0 deletions WDE.SmartScriptEditor/WDE.SmartScriptEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="AsyncAwaitBestPractices.MVVM">
<Version>5.0.2</Version>
</PackageReference>
<PackageReference Include="CommonServiceLocator">
<Version>2.0.5</Version>
</PackageReference>
Expand Down

0 comments on commit 57387fd

Please sign in to comment.