using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.BL.Entities; using Tango.BL.Enumerations; using Tango.PPC.Common; using Tango.PPC.Common.Notifications; using Tango.PPC.Common.Notifications.NotificationItems; namespace Tango.PPC.Events.ViewModels { /// /// Represents the main view VM and entry point for . /// /// public class MainViewVM : PPCViewModel { private Dictionary _notifications; private ObservableCollection _currentEvents; /// /// Gets or sets the current events. /// public ObservableCollection CurrentEvents { get { return _currentEvents; } set { _currentEvents = value; RaisePropertyChangedAuto(); } } private MachinesEvent _selectedEvent; public MachinesEvent SelectedEvent { get { return _selectedEvent; } set { _selectedEvent = value; RaisePropertyChangedAuto(); } } /// /// Initializes a new instance of the class. /// public MainViewVM() { CurrentEvents = new ObservableCollection(); _notifications = new Dictionary(); } /// /// Called when the application has been started /// public override void OnApplicationStarted() { EventLogger.EventReceived += EventLogger_EventReceived; EventLogger.EventResolved += EventLogger_EventResolved; } private void EventLogger_EventReceived(object sender, MachinesEvent ev) { InvokeUI(() => { if (ev.Group != EventTypeGroups.Transport) { CurrentEvents.Insert(0, ev); if (ev.Group != EventTypeGroups.Application && ev.Group != EventTypeGroups.Transport) { var notificationItem = new MessageNotificationItem(); notificationItem.CanClose = false; notificationItem.Message = ev.EventType.Description; notificationItem.ExpandedMessage = ev.Description; notificationItem.Pressed += async (_, __) => { SelectedEvent = ev; await NavigationManager.NavigateTo(); }; switch (ev.Category) { case EventTypeCategories.Info: notificationItem.MessageType = MessageNotificationItem.MessageNotificationItemTypes.Info; break; case EventTypeCategories.Warning: notificationItem.MessageType = MessageNotificationItem.MessageNotificationItemTypes.Warning; break; case EventTypeCategories.Error: notificationItem.MessageType = MessageNotificationItem.MessageNotificationItemTypes.Error; break; case EventTypeCategories.Critical: notificationItem.MessageType = MessageNotificationItem.MessageNotificationItemTypes.Critical; break; } NotificationProvider.PushNotification(notificationItem); _notifications.Add(ev.EventType.Type, notificationItem); } } }); } private void EventLogger_EventResolved(object sender, MachinesEvent ev) { InvokeUI(() => { if (ev.Group != EventTypeGroups.Transport) { CurrentEvents.Remove(ev); if (ev.Group != EventTypeGroups.Application && ev.Group != EventTypeGroups.Transport) { if (_notifications.ContainsKey(ev.EventType.Type)) { var notification = _notifications[ev.EventType.Type]; _notifications.Remove(ev.EventType.Type); NotificationProvider.PopNotification(notification); } } } }); } public override void OnNavigatedTo() { base.OnNavigatedTo(); NotificationProvider.NotificationsVisible = false; } public override void OnNavigatedFrom() { base.OnNavigatedFrom(); NotificationProvider.NotificationsVisible = true; } } }