using System.ComponentModel; using System.ComponentModel.Composition; using System.Threading.Tasks; using DTS.Common.Events; using DTS.Common.Interface.Menu.HamburgerMenu; using Prism.Events; using Prism.Regions; using Unity; using DTS.Common.Interactivity; using System.Windows.Controls; using HamburgerMenu.Model; using System.Windows.Automation; // ReSharper disable CheckNamespace // ReSharper disable MemberCanBePrivate.Global // ReSharper disable InconsistentNaming namespace HamburgerMenu { /// /// this class handles HardwareList functionality /// [PartCreationPolicy(CreationPolicy.Shared)] public class HamburgerMenuViewModel : IHamburgerMenuViewModel { public IHamburgerMenuView 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 HamburgerMenuViewModel /// /// /// 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 HamburgerMenuViewModel(IHamburgerMenuView 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(OnTestEvent); } #endregion #region Methods public void OnTestEvent(TestEventArg arg) { switch (arg.Status) { case TestEventStatus.TestEnded: IsEnabled = true; break; case TestEventStatus.TestStarted: IsEnabled = false; break; } } public ContextMenu GetContextMenu() { lock (MyLock) { if (null == _contextMenu) { BuildContextMenu(); } return _contextMenu; } } public void SetMenuItems(string[] items) { _menuItems = items; BuildContextMenu(); } public void ClearMenuItems() { _menuItems = new string[0]; } private void BuildContextMenu() { lock (MyLock) { _contextMenu = new ContextMenu(); _contextMenu.SetValue(AutomationProperties.AutomationIdProperty, "HamburgerMenu"); foreach (var item in _menuItems) { if (string.IsNullOrWhiteSpace(item)) { _contextMenu.Items.Add(new Separator()); } else { var mia = new MenuItem() { Header = item }; mia.SetValue(AutomationProperties.AutomationIdProperty, $"MenuItem_{item.Replace(" ", "")}"); mia.Command = new MenuCommand(item, ItemPressed); _contextMenu.Items.Add(mia); } } } } private void ItemPressed(string id) { MenuItemPressed?.Invoke(id); } public void Unset() { } public void OnSetActive() { } 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 }); } public void EnableMenu(bool enable) { IsEnabled = enable; } #endregion #region Properties private bool isEnabled = true; public bool IsEnabled { get => isEnabled; private set { isEnabled = value; OnPropertyChanged("IsEnabled"); } } public MenuItemPressedDelegate MenuItemPressed { get; set; } private static object MyLock = new object(); private string[] _menuItems = new string[0]; private ContextMenu _contextMenu; public bool IsDirty { get; private set; } private bool _isBusy; 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 } }