aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.Common/FSEViewModel.cs
blob: 6b53971d64e3f6115b24e5c490683f29f1a6eedc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using Tango.BL;
using Tango.Core.DI;
using Tango.FSE.BL;
using Tango.FSE.Common.Authentication;
using Tango.FSE.Common.Connection;
using Tango.FSE.Common.Diagnostics;
using Tango.FSE.Common.FileSystem;
using Tango.FSE.Common.FSEApplication;
using Tango.FSE.Common.Logging;
using Tango.FSE.Common.Navigation;
using Tango.FSE.Common.Notifications;
using Tango.FSE.Common.Performance;
using Tango.FSE.Common.MachineUpdates;
using Tango.FSE.Common.RemoteDesktop;
using Tango.FSE.Common.Resolution;
using Tango.FSE.Common.Storage;
using Tango.FSE.Common.SystemInfo;
using Tango.Settings;
using Tango.SharedUI;
using static Tango.SharedUI.Controls.NavigationControl;
using Tango.FSE.BL.Connectivity;
using Tango.FSE.Common.BugReporting;
using Tango.FSE.Common.RemoteUpgrade;
using Tango.FSE.Common.AutoComplete;
using Tango.BL.Entities;
using Tango.FSE.Common.Firmware;
using Tango.FSE.Common.Threading;
using Tango.FSE.Common.Events;
using Tango.FSE.Common.Tiles;
using Tango.FSE.Common.RemoteJob;
using Tango.FSE.Common.WindowsManager;
using Tango.FSE.Common.DemoMode;
using Tango.FSE.Common.FileAssociation;
using Tango.FSE.Common.Insights;
using Tango.FSE.Common.Build;
using Tango.FSE.Common.Statistics;

namespace Tango.FSE.Common
{
    /// <summary>
    /// Represents a FSE view model base class.
    /// </summary>
    /// <seealso cref="Tango.SharedUI.ViewModel" />
    public abstract class FSEViewModel : ViewModel, INavigationBlocker
    {
        /// <summary>
        /// Gets the static observable entities adapter.
        /// </summary>
        public ObservablesStaticCollections Adapter
        {
            get { return ObservablesStaticCollections.Instance; }
        }

        /// <summary>
        /// Gets or sets the application manager.
        /// </summary>
        [TangoInject]
        public IFSEApplicationManager ApplicationManager { get; set; }

        private IAuthenticationProvider _authenticationProvider;
        /// <summary>
        /// Gets or sets the authentication provider.
        /// </summary>
        [TangoInject]
        public IAuthenticationProvider AuthenticationProvider
        {
            get { return _authenticationProvider; }
            set
            {
                _authenticationProvider = value;
                RaisePropertyChangedAuto();
                if (_authenticationProvider != null)
                {
                    _authenticationProvider.CurrentUserChanged += OnCurrentUserChanged;
                }
            }
        }


        /// <summary>
        /// Gets or sets the navigation manager.
        /// </summary>
        [TangoInject]
        public INavigationManager NavigationManager { get; set; }

        private INotificationProvider _notificationProvider;
        /// <summary>
        /// Gets or sets the notification provider.
        /// </summary>
        [TangoInject]
        public INotificationProvider NotificationProvider
        {
            get { return _notificationProvider; }
            set { _notificationProvider = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Gets or sets the machine provider.
        /// </summary>
        [TangoInject]
        public IMachineProvider MachineProvider { get; set; }

        /// <summary>
        /// Gets or sets the diagnostics provider.
        /// </summary>
        [TangoInject]
        public IDiagnosticsProvider DiagnosticsProvider { get; set; }

        /// <summary>
        /// Gets or sets the remote desktop provider.
        /// </summary>
        [TangoInject]
        public IRemoteDesktopProvider RemoteDesktopProvider { get; set; }

        /// <summary>
        /// Gets or sets the performance provider.
        /// </summary>
        [TangoInject]
        public IPerformanceProvider PerformanceProvider { get; set; }

        /// <summary>
        /// Gets or sets the system information provider.
        /// </summary>
        [TangoInject]
        public ISystemInfoProvider SystemInfoProvider { get; set; }

        /// <summary>
        /// Gets or sets the logging provider.
        /// </summary>
        [TangoInject]
        public ILoggingProvider LoggingProvider { get; set; }

        private IResolutionService _resolutionService;
        /// <summary>
        /// Gets or sets the resolution service.
        /// </summary>
        [TangoInject]
        public IResolutionService ResolutionService
        {
            get { return _resolutionService; }
            set { _resolutionService = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Gets or sets the file system provider.
        /// </summary>
        [TangoInject]
        public IFileSystemProvider FileSystemProvider { get; set; }

        /// <summary>
        /// Gets or sets the storage provider.
        /// </summary>
        [TangoInject]
        public IStorageProvider StorageProvider { get; set; }

        /// <summary>
        /// Gets or sets the machine updates provider.
        /// </summary>
        [TangoInject]
        public IMachineUpdatesProvider MachineUpdatesProvider { get; set; }

        /// <summary>
        /// Gets or sets the connectivity provider.
        /// </summary>
        [TangoInject]
        public IConnectivityProvider ConnectivityProvider { get; set; }

        /// <summary>
        /// Gets or sets the bug reporter.
        /// </summary>
        [TangoInject]
        public IBugReporter BugReporter { get; set; }

        /// <summary>
        /// Gets or sets the remote upgrade manager.
        /// </summary>
        [TangoInject]
        public IRemoteUpgradeManager RemoteUpgradeManager { get; set; }

        /// <summary>
        /// Gets or sets the firmware storage provider.
        /// </summary>
        [TangoInject]
        public IFirmwareStorageProvider FirmwareStorageProvider { get; set; }

        /// <summary>
        /// Gets or sets the UI dispatcher provider.
        /// </summary>
        [TangoInject]
        public IDispatcherProvider DispatcherProvider { get; set; }

        /// <summary>
        /// Gets or sets the events provider.
        /// </summary>
        [TangoInject]
        public IEventsProvider EventsProvider { get; set; }

        /// <summary>
        /// Gets or sets the remote job provider.
        /// </summary>
        [TangoInject]
        public IRemoteJobProvider RemoteJobProvider { get; set; }

        /// <summary>
        /// Gets or sets the dashboard manager.
        /// </summary>
        [TangoInject]
        public IDashboardManager DashboardManager { get; set; }

        /// <summary>
        /// Gets or sets the windows manager.
        /// </summary>
        [TangoInject]
        public IWindowsManager WindowsManager { get; set; }

        /// <summary>
        /// Gets or sets the demo mode manager.
        /// </summary>
        [TangoInject]
        public IDemoModeManager DemoModeManager { get; set; }

        /// <summary>
        /// Gets or sets the file association provider.
        /// </summary>
        [TangoInject]
        public IFileAssociationProvider FileAssociationProvider { get; set; }

        /// <summary>
        /// Gets or sets the insights provider.
        /// </summary>
        [TangoInject]
        public IInsightsProvider InsightsProvider { get; set; }

        /// <summary>
        /// Gets or sets the cryptography provider.
        /// </summary>
        [TangoInject]
        public ICryptographyProvider CryptographyProvider { get; set; }

        /// <summary>
        /// Gets or sets the current build provider.
        /// </summary>
        [TangoInject]
        public IBuildProvider BuildProvider { get; set; }

        /// <summary>
        /// Gets or sets the statistics data provider.
        /// </summary>
        [TangoInject]
        public IStatisticsProvider StatisticsProvider { get; set; }

        /// <summary>
        /// Gets or sets the FSE service.
        /// </summary>
        [TangoInject]
        public FSEServicesContainer Services { get; set; }

        /// <summary>
        /// Gets or sets the machines automatic complete provider.
        /// </summary>
        public AutoCompleteSource<Machine> MachinesAutoCompleteProvider { get; set; }

        private FSESettings _settings;
        /// <summary>
        /// Gets the main FSE settings.
        /// </summary>
        public FSESettings Settings
        {
            get
            {
                if (_settings == null)
                {
                    _settings = SettingsManager.Default.GetOrCreate<FSESettings>();
                }

                return _settings;
            }
            private set { _settings = value; }
        }

        /// <summary>
        /// Gets the current user.
        /// </summary>
        public User CurrentUser
        {
            get { return AuthenticationProvider.CurrentUser; }
        }


        private bool _isVisible;
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="FSEViewModel"/> view is visible.
        /// </summary>
        public bool IsVisible
        {
            get { return _isVisible; }
            private set { _isVisible = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FSEViewModel"/> class.
        /// </summary>
        public FSEViewModel()
        {
            MachinesAutoCompleteProvider = new AutoCompleteSource<Machine>(AutoCompleteMachines);
        }

        private List<Machine> AutoCompleteMachines(string key)
        {
            key = key ?? String.Empty;

            try
            {
                return Services.MachinesService.GetAllMachines().Result.Where(x => x.SerialNumber.ToLower().StartsWith(key.ToLower())).Take(8).ToList();
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error on auto complete machine filter.");
                return new List<Machine>();
            }
        }

        /// <summary>
        /// Called when the application has been started.
        /// </summary>
        public virtual void OnApplicationStarted()
        {

        }

        /// <summary>
        /// Called when the application is shutting down.
        /// </summary>
        public virtual void OnApplicationShuttingDown()
        {

        }

        /// <summary>
        /// Called when the navigation system has navigated to this VM view.
        /// </summary>
        public virtual void OnNavigatedTo()
        {
            IsVisible = true;
        }

        /// <summary>
        /// Called when the navigation system has navigated to this VM view.
        /// </summary>
        /// <param name="fromVM">The view model instance of the previous view model</param>
        public virtual void OnNavigatedTo(FSEViewModel fromVM)
        {

        }

        /// <summary>
        /// Called when the navigation system has navigated from this VM view.
        /// </summary>
        public virtual void OnNavigatedFrom()
        {
            IsVisible = false;
        }

        /// <summary>
        /// Called before the navigation system has navigated to this VM view.
        /// </summary>
        public virtual void OnBeforeNavigatedTo()
        {

        }

        /// <summary>
        /// Called before the navigation system has navigated from this VM view.
        /// </summary>
        public virtual void OnBeforeNavigatedFrom()
        {
            IsVisible = false;
        }

        /// <summary>
        /// Raises the specified message using the default <see cref="TangoMessenger"/>.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="message">The message.</param>
        protected void RaiseMessage<T>(T message) where T : class
        {
            TangoMessenger.Default.Send<T>(message);
        }

        /// <summary>
        /// Raises the specified message using the default <see cref="TangoMessenger"/>.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        protected void RaiseMessage<T>() where T : class
        {
            TangoMessenger.Default.Send<T>(Activator.CreateInstance<T>());
        }

        /// <summary>
        /// Registers a message handle for the specified message type T.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="handler">The handler.</param>
        protected void RegisterForMessage<T>(Action<T> handler)
        {
            TangoMessenger.Default.Register<T>(handler);
        }

        /// <summary>
        /// Called before the navigation system navigates from this object.
        /// Return false to abort the navigation.
        /// </summary>
        /// <returns></returns>
        public virtual Task<bool> OnNavigateOutRequest()
        {
            return Task.FromResult(true);
        }

        /// <summary>
        /// Called before the navigation system navigates back from this object.
        /// Return false to abort the navigation.
        /// </summary>
        /// <returns></returns>
        public virtual Task<bool> OnNavigateBackRequest()
        {
            return Task.FromResult(true);
        }

        /// <summary>
        /// Called when the application is ready and all modules views are loaded.
        /// </summary>
        public virtual void OnApplicationReady()
        {

        }

        /// <summary>
        /// Called when the current user has logged out of the application.
        /// </summary>
        public virtual Task<bool> OnApplicationLogout()
        {
            return Task.FromResult(true);
        }

        /// <summary>
        /// Raises the property changed event.
        /// </summary>
        /// <param name="caller"></param>
        protected override void RaisePropertyChangedAuto([CallerMemberName] string caller = null)
        {
            base.RaisePropertyChangedAuto(caller);
        }

        /// <summary>
        /// Raises the property changed event.
        /// </summary>
        /// <param name="propName">Name of the property.</param>
        protected override void RaisePropertyChanged(string propName)
        {
            base.RaisePropertyChanged(propName);

            if (propName == nameof(IsFree))
            {
                if (IsFree)
                {
                    Mouse.OverrideCursor = null;
                }
                else
                {
                    Mouse.OverrideCursor = Cursors.AppStarting;
                }
            }
        }

        /// <summary>
        /// Called on <see cref="IAuthenticationProvider.CurrentUserChanged"/>.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="user">The user.</param>
        protected virtual void OnCurrentUserChanged(object sender, User user)
        {
            InvalidateRelayCommands();
        }
    }

    /// <summary>
    /// Represents an FSE view model with a auto ModuleSettings property.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <seealso cref="Tango.FSE.Common.FSEViewModel" />
    public abstract class FSEViewModelWithModuleSettings<T> : FSEViewModel where T : SettingsBase
    {
        /// <summary>
        /// Gets or sets the module settings.
        /// </summary>
        public T ModuleSettings { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="FSEViewModelWithModuleSettings{T}"/> class.
        /// </summary>
        public FSEViewModelWithModuleSettings()
        {
            ModuleSettings = SettingsManager.Default.GetOrCreate<T>();
        }
    }

    /// <summary>
    /// Represents a FSE view model base class a View property as an instance of a <see cref="IFSEView"/> contract.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <seealso cref="Tango.SharedUI.ViewModel" />
    public abstract class FSEViewModel<T> : FSEViewModel where T : IFSEView
    {
        private T _view;
        /// <summary>
        /// Gets the IFSEView instance.
        /// </summary>
        [TangoInject(TangoInjectMode.WhenAvailable)]
        public T View
        {
            get { return _view; }
            set
            {
                _view = value;
                ViewAttached = true;
                OnViewAttached();
            }
        }


        /// <summary>
        /// Gets a value indicating whether the instance of IFSEView is available.
        /// </summary>
        public bool ViewAttached { get; private set; }

        /// <summary>
        /// Called when the application has been started.
        /// </summary>
        public override void OnApplicationStarted()
        {

        }

        /// <summary>
        /// Called when the instance of IFSEView is available.
        /// </summary>
        public virtual void OnViewAttached()
        {

        }
    }

    public abstract class FSEViewModelWithModuleSettings<TView,TSettings> : FSEViewModel<TView> where TView : IFSEView where TSettings : SettingsBase
    {
        /// <summary>
        /// Gets or sets the module settings.
        /// </summary>
        public TSettings ModuleSettings { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="FSEViewModelWithModuleSettings{T}"/> class.
        /// </summary>
        public FSEViewModelWithModuleSettings()
        {
            ModuleSettings = SettingsManager.Default.GetOrCreate<TSettings>();
        }
    }
}