1919using WDE . Common . Providers ;
2020using WDE . Common . Solution ;
2121using System . Diagnostics ;
22+ using System . Threading . Tasks ;
2223using System . Windows ;
2324using System . Windows . Data ;
2425using System . Windows . Input ;
26+ using AsyncAwaitBestPractices . MVVM ;
2527using WDE . Common . Managers ;
28+ using WDE . Common . Utils ;
2629using WDE . SmartScriptEditor . Editor . UserControls ;
2730
2831namespace 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 )
0 commit comments