aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.Common/FSEViewModel.cs
blob: 831c82bcbeca0a1578ecc66e129ed8744b130d2a (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tango.BL.Entities;
using Tango.DragAndDrop;
using Tango.MachineStudio.UsersAndRoles.ViewModels;
using static Tango.SharedUI.Controls.MultiTransitionControl;
using static Tango.SharedUI.Controls.NavigationControl;

namespace Tango.MachineStudio.UsersAndRoles.Views
{
    /// <summary>
    /// Interaction logic for UserManagementView.xaml
    /// </summary>
    public partial class UserManagementView : UserControl, INavigationView
    {
        private MainViewVM _vm;
        
        public DraggingSurface DraggingSurface
        {
            get { return (DraggingSurface)GetValue(DraggingSurfaceProperty); }
            set { SetValue(DraggingSurfaceProperty, value); }
        }
        public static readonly DependencyProperty DraggingSurfaceProperty =
            DependencyProperty.Register("DraggingSurface", typeof(DraggingSurface), typeof(UserManagementView), new PropertyMetadata(null));

        public UserManagementView()
        {
            InitializeComponent();
            DraggingSurface = dragSurface;
            Loaded += (_, __) => _vm = DataContext as MainViewVM;
        }

        private void OnDropRole(object sender, DropEventArgs e)
        {
            if (e.Draggable.DataContext is Role)
            {
                _vm.OnDropRole(e.Draggable.DataContext as Role);
            }
        }

        public void OnNavigatedTo()
        {
            address_auto.Text = "";
        }

        public void OnNavigatedFrom()
        {
            
        }
    }
}
'>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
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;

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 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>();
        }
    }
}