using System.ComponentModel; using System.ComponentModel.Composition; using System.Threading.Tasks; using System.Windows.Media; using DTS.Common.Enums.Database; using DTS.Common.Events; using DTS.Common.Events.Database; using Prism.Events; using Prism.Regions; using Unity; using DTS.Common.Interactivity; using Prism.Commands; using DTS.Common.Interface.Database; using DTS.Common.Storage; // ReSharper disable CheckNamespace // ReSharper disable MemberCanBePrivate.Global // ReSharper disable InconsistentNaming namespace DatabaseServices { /// /// this class handles DatabaseCopy functionality /// [PartCreationPolicy(CreationPolicy.Shared)] public class DatabaseStatusBarViewModel : IDatabaseStatusBarViewModel { /// /// The DatabaseCopy view /// public IDatabaseStatusBarView View { get; set; } private IEventAggregator _eventAggregator { get; } private IRegionManager _regionManager; private IUnityContainer UnityContainer { get; } public InteractionRequest NotificationRequest { get; } public InteractionRequest ConfirmationRequest { get; } /// /// /// Occurs when a property value changes. /// public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #region constructors and initializers /// /// Creates a new instance of the DatabaseCopyViewModel /// /// /// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed. /// The EventAggregator which allows different components to publish/subscribe to events without being coupled to each other. /// The unityContainer. public DatabaseStatusBarViewModel(IDatabaseStatusBarView view, IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer) { View = view; View.DataContext = this; NotificationRequest = new InteractionRequest(); ConfirmationRequest = new InteractionRequest(); _eventAggregator = eventAggregator; UnityContainer = unityContainer; _regionManager = regionManager; _eventAggregator.GetEvent().Subscribe(OnRaiseNotification); _eventAggregator.GetEvent() .Subscribe(OnBusyIndicatorNotification, ThreadOption.PublisherThread, true); _eventAggregator.GetEvent().Subscribe(OnDbEvent); } public DatabaseStatusBarViewModel() { } #endregion #region Methods private void OnDbEvent(DbStatusArg args) { if (args.Status == DbStatusArg.EventTypes.LegacyStatus) { if (RemoteConnected) { ServerName = DbOperations.Connection.Server; } else { ServerName = "Local"; } OnPropertyChanged("ActiveDbName"); OnPropertyChanged("BackgroundBrush"); } } public void Unset() { } public DbType DatabaseType { get; private set; } public void InitializeValues(DbType dbType, string serverName, bool remoteConnected) { DatabaseType = dbType; ServerName = serverName; OnPropertyChanged("ActiveDbName"); OnPropertyChanged("BackgroundBrush"); } public bool RemoteConnected => DbOperations._usingCentralizedDB; public string ServerName { get; private set; } public string ActiveDbName { get { switch (DatabaseType) { case DbType.LocalOnly: return Resources.StringResources.Local; case DbType.RemoteOnly: return ServerName; case DbType.RemoteLocalHybrid: { return RemoteConnected ? ServerName : Resources.StringResources.Local; } } return ""; } } public Brush BackgroundBrush { get { switch (DatabaseType) { case DbType.RemoteLocalHybrid: return RemoteConnected ? Brushes.Transparent : Brushes.Red; } return Brushes.Transparent; } } public void Cleanup() { } public Task CleanupAsync() { return Task.CompletedTask; } public void Initialize() { } public void Initialize(object parameter) { } public void Initialize(object parameter, object model) { } public Task InitializeAsync() { return Task.CompletedTask; } public Task InitializeAsync(object parameter) { return Task.CompletedTask; } public void Activated() { } /// /// Private Event handler for RaiseNotification event. /// private void OnBusyIndicatorNotification(bool eventArg) { IsBusy = eventArg; } /// /// Private Event handler for RaiseNotification event. /// private void OnRaiseNotification(NotificationContentEventArgs eventArgsWithTitle) { // The NotificationRequest.Raise triggers the Invoke() method of the PopupWindowAction object to show the NotificationWindow window // Notification object expects a NotificationContentEventArgsWithoutTitle object and a Title string. var eventArgsWithoutTitle = new NotificationContentEventArgs(eventArgsWithTitle.Message, "", eventArgsWithTitle.Image, string.Empty); NotificationRequest.Raise(new Notification { Content = eventArgsWithoutTitle, Title = eventArgsWithTitle.Title }); } #endregion #region Properties public bool IsDirty { get; private set; } private bool _isBusy = false; public bool IsBusy { get => _isBusy; set { _isBusy = value; OnPropertyChanged("IsBusy"); } } private bool _isMenuIncluded; public bool IsMenuIncluded { get => _isMenuIncluded; set { _isMenuIncluded = value; OnPropertyChanged("IsMenuIncluded"); } } private bool _isNavigationIncluded; public bool IsNavigationIncluded { get => _isNavigationIncluded; set { _isNavigationIncluded = value; OnPropertyChanged("IsNavigationIncluded"); } } #endregion Properties #region Commands #endregion } }