33using Contracts . Key ;
44using Contracts . Scripts ;
55using Contracts . Scripts . Base ;
6- using Data ;
6+ using Data . Service . Interfaces ;
77using JobManager . Service ;
88using MaterialSkin . Controls ;
99using Serilog ;
@@ -20,32 +20,58 @@ public partial class RunTabControl : UserControl
2020 private readonly ScriptListAdapter _scriptListAdapter ;
2121 private readonly ListenKeysService _listenKeysService ;
2222 private IRunScriptService ? _runScriptService ;
23+ private IScriptsService ? _scriptsService ;
24+ private List < ScriptAbs > _scripts ;
2325 public RunTabControl ( )
2426 {
2527 InitializeComponent ( ) ;
2628
29+ _scripts = new ( ) ;
30+
2731 _scriptListAdapter = new ScriptListAdapter ( pnlScripts ) ;
2832 _scriptListAdapter . ShowMenu += ShowContextMenuScript ;
2933 _scriptListAdapter . StatusClicked += ChangeScriptStatusClicked ;
3034
3135 _listenKeysService = ListenKeysService . GetInstance ( ) ;
3236 _listenKeysService . KeyUpClicked += ListenKeysService_KeyUpClicked ;
3337 }
38+
39+ #region Public methods
3440 public void SetRunScriptService ( IRunScriptService runScriptService )
3541 {
3642 _runScriptService = runScriptService ;
3743 _runScriptService . OneOffScriptExecuted += OneOffJobWasExecuted ;
3844 }
45+ public List < ScriptAbs > SetScriptsService ( IScriptsService scriptsService )
46+ {
47+ _scriptsService = scriptsService ;
48+ _scripts = _scriptsService . GetScripts ( ) ;
49+ return _scripts ;
50+ }
51+ public async void RefreshScriptStatusAsync ( string scriptId )
52+ {
53+ var script = _scripts . FirstOrDefault ( s => s . Id == scriptId ) ? . Clone ( ) ;
54+ await ChangeScriptStatusAsync ( script ) ;
55+ }
56+ public async void RunScriptsOnStartupAsync ( )
57+ {
58+ var scripts = GetRunningScriptsByType < ScriptOnStartup > ( ) ;
59+ foreach ( var script in scripts )
60+ {
61+ await _runScriptService ! . RunScriptAsync ( script ) ;
62+ }
63+ }
64+ #endregion
3965 #region Events
4066 private async void OnLoad ( object sender , EventArgs e )
4167 {
4268 if ( this . DesignMode ) return ;
43- _scriptListAdapter . CreateWithList ( SettingsManager . Scripts ) ;
44- await CheckIfScriptsNeedToRunOnLoad ( ) ;
69+ _scriptListAdapter . CreateWithList ( _scripts ) ;
70+ await CheckIfScriptsNeedToRunOnLoadAsync ( ) ;
4571 }
4672 private void OneOffJobWasExecuted ( string scriptId )
4773 {
48- var script = SettingsManager . FindScriptById ( scriptId ) ;
74+ var script = _scripts . FirstOrDefault ( s => s . Id == scriptId ) ? . Clone ( ) ;
4975 if ( script != null )
5076 ChangeScriptStatusThreadSafe ( script ) ;
5177 }
@@ -74,7 +100,7 @@ private async void ContextMenuItemClicked(object sender, ToolStripItemClickedEve
74100 }
75101 else if ( e . ClickedItem . Text == "Remove" )
76102 {
77- await ShowRemoveScriptDialogAsync ( script ) ;
103+ ShowRemoveScriptDialog ( script ) ;
78104 }
79105 }
80106 private void ListenKeysService_KeyUpClicked ( KeyPressed keyPressed )
@@ -93,18 +119,18 @@ private void ListenKeysService_KeyUpClicked(KeyPressed keyPressed)
93119 #endregion
94120
95121 #region Private Methods
96- private async Task CheckIfScriptsNeedToRunOnLoad ( )
122+ private async Task CheckIfScriptsNeedToRunOnLoadAsync ( )
97123 {
98124 foreach ( var script in GetRunningScriptsByType < ScriptScheduled > ( ) )
99125 await _runScriptService ! . RunScriptAsync ( script ) ;
100126
101127 if ( IsListenKeyServiceNecessary ( ) )
102128 _listenKeysService . Run ( ) ;
103129 }
104- private static bool IsListenKeyServiceNecessary ( string ? executingScriptId = null )
130+ private bool IsListenKeyServiceNecessary ( string ? executingScriptId = null )
105131 => GetRunningScriptsByType < ScriptListenKey > ( ) ? . Any ( s => s . Id != executingScriptId ) ?? false ;
106- private static IEnumerable < T > GetRunningScriptsByType < T > ( )
107- where T : ScriptAbs => SettingsManager . Scripts . OfType < T > ( ) . Where ( s => s . ScriptStatus == ScriptStatus . Running ) ;
132+ private IEnumerable < T > GetRunningScriptsByType < T > ( )
133+ where T : ScriptAbs => _scripts . OfType < T > ( ) . Where ( s => s . ScriptStatus == ScriptStatus . Running ) ;
108134 private void ChangeScriptStatusThreadSafe ( ScriptAbs script )
109135 => this . Invoke ( ( MethodInvoker ) async delegate ( ) { await ChangeScriptStatusAsync ( script ) ; } ) ;
110136 private async Task ShowEditScriptFormAsync ( ScriptAbs script )
@@ -114,7 +140,7 @@ await ShowScriptForm(script, async (ScriptAbs? editedScript) => {
114140 return ;
115141 Log . Debug ( "Editing Script {@ScriptName} ({@ScriptType})" , editedScript . ScriptName , editedScript . ScriptType ) ;
116142 bool rescheduleJob = ScriptAbs . HasScriptTypeChanged ( script . ScriptType , editedScript . ScriptType ) || ScriptAbs . HasScheduledTimeChanged ( script , editedScript ) ;
117- await SettingsManager . EditScriptAsync ( editedScript ) ;
143+ UpdateScript ( editedScript ) ;
118144 if ( editedScript . ScriptStatus == ScriptStatus . Running && rescheduleJob )
119145 {
120146 await _runScriptService ! . StopScriptAsync ( editedScript ) ;
@@ -129,23 +155,24 @@ await ShowScriptForm(script, async (ScriptAbs? editedScript) => {
129155 }
130156 private async Task ShowAddScriptForm ( )
131157 {
132- await ShowScriptForm ( null , async ( ScriptAbs ? addedScript ) => {
158+ await ShowScriptForm ( null , ( ScriptAbs ? addedScript ) => {
133159 if ( addedScript == null )
134- return ;
160+ return Task . CompletedTask ;
135161 Log . Debug ( "Adding Script {@ScriptName} ({@ScriptType})" , addedScript . ScriptName , addedScript . ScriptType ) ;
136162 addedScript . Id = Guid . NewGuid ( ) . ToString ( ) ;
137- await SettingsManager . AddScriptAsync ( addedScript ) ;
163+ AddScript ( addedScript ) ;
138164 _scriptListAdapter . AddItem ( addedScript ) ;
139165 ScriptAdded ? . Invoke ( addedScript ) ;
166+ return Task . CompletedTask ;
140167 } ) ;
141168 }
142- private async Task ShowRemoveScriptDialogAsync ( ScriptAbs script )
169+ private void ShowRemoveScriptDialog ( ScriptAbs script )
143170 {
144171 var dialog = new MaterialDialog ( this . ParentForm , "Remove" , "Do you want to remove the script?" , "Yes" , true , "No" ) ;
145172 if ( dialog . ShowDialog ( this ) == DialogResult . OK )
146173 {
147174 Log . Debug ( "Removing ScriptId {@ScriptId}" , script . Id ) ;
148- await SettingsManager . RemoveScriptAsync ( script . Id ) ;
175+ DeleteScript ( script . Id ) ;
149176 _scriptListAdapter . RemoveItem ( script . Id ) ;
150177 ScriptRemoved ? . Invoke ( script ) ;
151178 }
@@ -182,7 +209,7 @@ private async Task ChangeScriptStatusAsync(ScriptAbs? script)
182209 await _runScriptService ! . StopScriptAsync ( script ) ;
183210 }
184211
185- await SettingsManager . EditScriptAsync ( script ) ;
212+ UpdateScript ( script ) ;
186213
187214 _scriptListAdapter . RefreshScriptStatus ( script . Id , script . ScriptStatus ) ;
188215 ScriptEdited ? . Invoke ( script ) ;
@@ -204,13 +231,26 @@ private static ScriptStatus GetNewScriptStatus(ScriptStatus oldStatus)
204231 }
205232 return newStatus ;
206233 }
207- #endregion
208-
209- #region Public methods
210- public async void RefreshScriptStatusAsync ( string scriptId )
234+ private void AddScript ( ScriptAbs addedScript )
211235 {
212- var script = SettingsManager . FindScriptById ( scriptId ) ;
213- await ChangeScriptStatusAsync ( script ) ;
236+ _scriptsService ! . AddScript ( addedScript ) ;
237+ _scripts . Add ( addedScript ) ;
238+ }
239+ private void UpdateScript ( ScriptAbs editedScript )
240+ {
241+ _scriptsService ! . UpdateScript ( editedScript ) ;
242+ int index = _scripts . FindIndex ( s => s . Id == editedScript . Id ) ;
243+ if ( index == - 1 )
244+ return ;
245+ _scripts [ index ] = editedScript ;
246+ }
247+ private void DeleteScript ( string scriptId )
248+ {
249+ _scriptsService ! . DeleteScript ( scriptId ) ;
250+ var scriptToDelete = _scripts . FirstOrDefault ( s => s . Id == scriptId ) ;
251+ if ( scriptToDelete == null )
252+ return ;
253+ _scripts . Remove ( scriptToDelete ) ;
214254 }
215255 #endregion
216256
0 commit comments