1
1
using Caliburn . Micro ;
2
2
using Dawn ;
3
+ using Notifications . Wpf . Core ;
3
4
using Pixel . Automation . AppExplorer . ViewModels . Contracts ;
4
5
using Pixel . Automation . Core . Interfaces ;
5
6
using Pixel . Automation . Core . Models ;
6
7
using Pixel . Automation . Editor . Core ;
8
+ using Pixel . Automation . Editor . Core . Helpers ;
7
9
using Pixel . Persistence . Services . Client ;
8
10
using Serilog ;
9
11
using System . ComponentModel ;
@@ -26,6 +28,7 @@ public class ApplicationExplorerViewModel : AnchorableHost
26
28
private readonly ITypeProvider typeProvider ;
27
29
private readonly IApplicationDataManager applicationDataManager ;
28
30
private readonly IWindowManager windowManager ;
31
+ private readonly INotificationManager notificationManager ;
29
32
30
33
/// <summary>
31
34
/// Child views that are dependent on the selected application such as control explorer and prefab explorer
@@ -90,13 +93,14 @@ public ApplicationDescriptionViewModel SelectedApplication
90
93
/// <param name="typeProvider"></param>
91
94
/// <param name="childView"></param>
92
95
public ApplicationExplorerViewModel ( IEventAggregator eventAggregator , IApplicationDataManager applicationDataManager ,
93
- ITypeProvider typeProvider , IEnumerable < IApplicationAware > childView , IWindowManager windowManager )
96
+ ITypeProvider typeProvider , IEnumerable < IApplicationAware > childView , IWindowManager windowManager , INotificationManager notificationManager )
94
97
{
95
98
this . DisplayName = "Application Repository" ;
96
- this . eventAggregator = Guard . Argument ( eventAggregator ) . NotNull ( ) . Value ; ;
97
- this . typeProvider = Guard . Argument ( typeProvider ) . NotNull ( ) . Value ; ;
98
- this . applicationDataManager = Guard . Argument ( applicationDataManager ) . NotNull ( ) . Value ;
99
- this . windowManager = Guard . Argument ( windowManager ) . NotNull ( ) . Value ;
99
+ this . eventAggregator = Guard . Argument ( eventAggregator , nameof ( eventAggregator ) ) . NotNull ( ) . Value ; ;
100
+ this . typeProvider = Guard . Argument ( typeProvider , nameof ( typeProvider ) ) . NotNull ( ) . Value ; ;
101
+ this . applicationDataManager = Guard . Argument ( applicationDataManager , nameof ( applicationDataManager ) ) . NotNull ( ) . Value ;
102
+ this . windowManager = Guard . Argument ( windowManager , nameof ( windowManager ) ) . NotNull ( ) . Value ;
103
+ this . notificationManager = Guard . Argument ( notificationManager , nameof ( notificationManager ) ) . NotNull ( ) . Value ;
100
104
this . ChildViews . AddRange ( childView ) ;
101
105
this . SelectedView = this . ChildViews [ 0 ] ;
102
106
@@ -155,7 +159,7 @@ private void CreateCollectionView()
155
159
156
160
public void OpenApplication ( ApplicationDescriptionViewModel applicationDescriptionViewModel )
157
161
{
158
- Guard . Argument ( applicationDescriptionViewModel ) . NotNull ( ) ;
162
+ Guard . Argument ( applicationDescriptionViewModel , nameof ( applicationDescriptionViewModel ) ) . NotNull ( ) ;
159
163
160
164
IsApplicationOpen = true ;
161
165
foreach ( var childView in ChildViews )
@@ -186,33 +190,49 @@ public void GoBack()
186
190
187
191
public async Task AddApplication ( KnownApplication knownApplication )
188
192
{
189
- Guard . Argument ( knownApplication ) . NotNull ( ) ;
193
+ try
194
+ {
195
+ Guard . Argument ( knownApplication ) . NotNull ( ) ;
190
196
191
- IApplication application = ( IApplication ) Activator . CreateInstance ( knownApplication . UnderlyingApplicationType ) ;
197
+ IApplication application = ( IApplication ) Activator . CreateInstance ( knownApplication . UnderlyingApplicationType ) ;
192
198
193
- ApplicationDescription newApplication = new ApplicationDescription ( application ) ;
194
- var applicationDescriptionViewModel = new ApplicationDescriptionViewModel ( newApplication ) ;
195
- applicationDescriptionViewModel . AddScreen ( "Home" ) ;
196
- applicationDescriptionViewModel . ScreenCollection . SetActiveScreen ( "Home" ) ;
197
- if ( string . IsNullOrEmpty ( applicationDescriptionViewModel . ApplicationName ) )
199
+ ApplicationDescription newApplication = new ApplicationDescription ( application ) ;
200
+ var applicationDescriptionViewModel = new ApplicationDescriptionViewModel ( newApplication ) ;
201
+ applicationDescriptionViewModel . AddScreen ( "Home" ) ;
202
+ applicationDescriptionViewModel . ScreenCollection . SetActiveScreen ( "Home" ) ;
203
+ if ( string . IsNullOrEmpty ( applicationDescriptionViewModel . ApplicationName ) )
204
+ {
205
+ applicationDescriptionViewModel . ApplicationName = $ "{ this . Applications . Count ( ) + 1 } ";
206
+ applicationDescriptionViewModel . ApplicationType = knownApplication . UnderlyingApplicationType . Name ;
207
+ }
208
+
209
+ this . Applications . Add ( applicationDescriptionViewModel ) ;
210
+ this . SelectedApplication = applicationDescriptionViewModel ;
211
+ await SaveApplicationAsync ( applicationDescriptionViewModel ) ;
212
+ await EditApplicationAsync ( applicationDescriptionViewModel ) ;
213
+ NotifyOfPropertyChange ( ( ) => Applications ) ;
214
+ logger . Information ( "New application of type {0} has been added to the application repository" , application . ToString ( ) ) ;
215
+ }
216
+ catch ( Exception ex )
198
217
{
199
- applicationDescriptionViewModel . ApplicationName = $ " { this . Applications . Count ( ) + 1 } " ;
200
- applicationDescriptionViewModel . ApplicationType = knownApplication . UnderlyingApplicationType . Name ;
218
+ logger . Error ( ex , "There was an error while trying to add a '{0}' application" , knownApplication . ToString ( ) ) ;
219
+ await notificationManager . ShowErrorNotificationAsync ( ex ) ;
201
220
}
202
-
203
- this . Applications . Add ( applicationDescriptionViewModel ) ;
204
- this . SelectedApplication = applicationDescriptionViewModel ;
205
- await SaveApplicationAsync ( applicationDescriptionViewModel ) ;
206
- await EditApplicationAsync ( applicationDescriptionViewModel ) ;
207
- NotifyOfPropertyChange ( ( ) => Applications ) ;
208
- logger . Information ( "New application of type {0} has been added to the application repository" , application . ToString ( ) ) ;
209
221
}
210
222
211
223
public async Task EditApplicationAsync ( ApplicationDescriptionViewModel applicationDescriptionViewModel )
212
224
{
213
225
await this . eventAggregator . PublishOnUIThreadAsync ( new PropertyGridObjectEventArgs ( applicationDescriptionViewModel . ApplicationDetails ,
214
226
async ( ) => {
215
- await SaveApplicationAsync ( applicationDescriptionViewModel ) ;
227
+ try
228
+ {
229
+ await SaveApplicationAsync ( applicationDescriptionViewModel ) ;
230
+ }
231
+ catch ( Exception ex )
232
+ {
233
+ logger . Error ( ex , "There was an error while trying to save application : '{0}' after edit" , applicationDescriptionViewModel ? . ApplicationName ) ;
234
+ await notificationManager . ShowErrorNotificationAsync ( ex ) ;
235
+ }
216
236
} ,
217
237
( ) => {
218
238
return true ;
@@ -221,7 +241,7 @@ await this.eventAggregator.PublishOnUIThreadAsync(new PropertyGridObjectEventArg
221
241
222
242
public async Task SaveApplicationAsync ( ApplicationDescriptionViewModel applicationDescriptionViewModel )
223
243
{
224
- Guard . Argument ( applicationDescriptionViewModel ) . NotNull ( ) ;
244
+ Guard . Argument ( applicationDescriptionViewModel , nameof ( applicationDescriptionViewModel ) ) . NotNull ( ) ;
225
245
await this . applicationDataManager . AddOrUpdateApplicationAsync ( applicationDescriptionViewModel . Model ) ;
226
246
logger . Information ( $ "Saved application data for : { applicationDescriptionViewModel . ApplicationName } ") ;
227
247
await this . eventAggregator . PublishOnUIThreadAsync ( new ApplicationUpdatedEventArgs ( applicationDescriptionViewModel . ApplicationId ) ) ;
@@ -236,9 +256,9 @@ public async Task SaveApplicationAsync(ApplicationDescriptionViewModel applicati
236
256
/// <returns></returns>
237
257
public async Task DeleteApplicationAsync ( ApplicationDescriptionViewModel applicationDescriptionViewModel )
238
258
{
239
- Guard . Argument ( applicationDescriptionViewModel , nameof ( applicationDescriptionViewModel ) ) . NotNull ( ) ;
240
259
try
241
260
{
261
+ Guard . Argument ( applicationDescriptionViewModel , nameof ( applicationDescriptionViewModel ) ) . NotNull ( ) ;
242
262
MessageBoxResult result = MessageBox . Show ( "Are you sure you want to delete this application?" , "Confirm Delete" , MessageBoxButton . OKCancel ) ;
243
263
if ( result == MessageBoxResult . OK )
244
264
{
@@ -248,7 +268,8 @@ public async Task DeleteApplicationAsync(ApplicationDescriptionViewModel applica
248
268
}
249
269
catch ( Exception ex )
250
270
{
251
- logger . Error ( ex , ex . Message ) ;
271
+ logger . Error ( ex , "There was an error while trying to delete application : '{0}'" , applicationDescriptionViewModel ? . ApplicationName ) ;
272
+ await notificationManager . ShowErrorNotificationAsync ( ex ) ;
252
273
}
253
274
}
254
275
@@ -272,6 +293,7 @@ public async Task CreateScreen(ApplicationDescriptionViewModel applicationDescri
272
293
catch ( Exception ex )
273
294
{
274
295
logger . Error ( ex , "There was an error while creating new screen" ) ;
296
+ await notificationManager . ShowErrorNotificationAsync ( ex ) ;
275
297
}
276
298
}
277
299
@@ -297,6 +319,7 @@ public async Task RenameScreen(ApplicationDescriptionViewModel applicationDescri
297
319
catch ( Exception ex )
298
320
{
299
321
logger . Error ( ex , "There was an error while renaming the screen" ) ;
322
+ await notificationManager . ShowErrorNotificationAsync ( ex ) ;
300
323
}
301
324
}
302
325
@@ -351,6 +374,7 @@ public async Task RenameApplication(ActionExecutionContext context, ApplicationD
351
374
if ( this . Applications . Any ( a => a . ApplicationName . Equals ( newName ) ) )
352
375
{
353
376
logger . Warning ( $ "An application already exists with name { newName } .") ;
377
+ await notificationManager . ShowErrorNotificationAsync ( $ "An application already exists with name { newName } ") ;
354
378
return ;
355
379
}
356
380
if ( newName != applicationDescriptionViewModel . ApplicationName )
@@ -366,8 +390,9 @@ public async Task RenameApplication(ActionExecutionContext context, ApplicationD
366
390
}
367
391
catch ( Exception ex )
368
392
{
369
- logger . Error ( ex , ex . Message ) ;
393
+ logger . Error ( ex , "There was an error while trying to rename application : '{0}'" , applicationDescriptionViewModel . ApplicationName ) ;
370
394
CanEdit = false ;
395
+ await notificationManager . ShowErrorNotificationAsync ( ex ) ;
371
396
}
372
397
}
373
398
0 commit comments