aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Notifications/MessageBoxWindow.xaml
blob: 988e3c21ff052c02a49dad1fe28cc29b32fdfe04 (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
<Window x:Class="Tango.MachineStudio.UI.Notifications.MessageBoxWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Tango.MachineStudio.UI.Notifications"
        xmlns:mahapps="http://metro.mahapps.com/winfx/xaml/controls"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:converters="clr-namespace:Tango.SharedUI.Converters;assembly=Tango.SharedUI"
        mc:Ignorable="d"
        Title="Machine Studio" MinHeight="220" MaxHeight="600" SizeToContent="Height" Width="570" Opacity="0" AllowsTransparency="True" WindowStyle="None" WindowStartupLocation="CenterOwner" Background="Transparent" ShowInTaskbar="False">

    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"></BooleanToVisibilityConverter>
    </Window.Resources>

    <Grid>
        <Border Background="{StaticResource Dialog.Background}" CornerRadius="10" Padding="10" Margin="20">
            <Border.Effect>
                <DropShadowEffect ShadowDepth="0" BlurRadius="10"></DropShadowEffect>
            </Border.Effect>
            <DockPanel LastChildFill="True">
                <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Right" DockPanel.Dock="Bottom">
                    <Button Style="{StaticResource MaterialDesignFlatButton}" IsDefault="True" Margin="0 8 8 0" Click="OnOKClicked">
                        ACCEPT
                    </Button>
                    <Button Visibility="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=HasCancel,Converter={StaticResource BooleanToVisibilityConverter}}" Style="{StaticResource MaterialDesignFlatButton}" IsCancel="False" Margin="0 8 8 0" Click="OnCancelClicked">
                        CANCEL
                    </Button>
                </StackPanel>
                <Grid>
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0 30 0 0">
                        <materialDesign:PackIcon Kind="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=IconKind}" VerticalAlignment="Top" Width="50" Height="50" Foreground="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=IconColor}" />
                        <TextBlock Padding="0 10 0 0" TextWrapping="Wrap" Margin="10 0 0 0" VerticalAlignment="Top" FontSize="14" Text="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=Message}" Width="400" Foreground="{StaticResource Dialog.Foreground}"></TextBlock>
                    </StackPanel>
                </Grid>
            </DockPanel>
        </Border>
    </Grid>
</Window>
r */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.Core.Commands;
using Tango.MachineStudio.Common.EventLogging;
using Tango.MachineStudio.Common.Messages;
using Tango.MachineStudio.Common.Notifications;
using Tango.MachineStudio.Common.StudioApplication;
using Tango.MachineStudio.Logging.Navigation;
using Tango.MachineStudio.Logging.Views;
using Tango.SharedUI;
using System.Data.Entity;

namespace Tango.MachineStudio.Logging.ViewModels
{
    public class EventsViewVM : ViewModel
    {
        private INotificationProvider _notification;
        private IStudioApplicationManager _application;
        private IEventLogger _eventLogger;
        private ObservableCollection<MachinesEvent> _realTimeEvents;
        private LoggingNavigationManager _navigation;
        private bool _dialog_shown;
        private ObservablesContext _db;
        private List<MachinesEvent> _history_events;

        private Machine _selectedMachine;
        public Machine SelectedMachine
        {
            get { return _selectedMachine; }
            set
            {
                if (_selectedMachine != value)
                {
                    _selectedMachine = value; RaisePropertyChangedAuto(); OnSelectedMachineChanged();
                }
            }
        }

        private ObservableCollection<MachinesEvent> _events;
        public ObservableCollection<MachinesEvent> Events
        {
            get { return _events; }
            set { _events = value; RaisePropertyChangedAuto(); }
        }

        private MachinesEvent _selectedEvent;
        public MachinesEvent SelectedEvent
        {
            get { return _selectedEvent; }
            set { _selectedEvent = value; RaisePropertyChangedAuto(); OnSelectedEventChanged(); }
        }

        private ObservableCollection<DateTime> _dates;
        public ObservableCollection<DateTime> Dates
        {
            get { return _dates; }
            set { _dates = value; RaisePropertyChangedAuto(); }
        }

        private DateTime _selectedDate;
        public DateTime SelectedDate
        {
            get { return _selectedDate; }
            set { _selectedDate = value; RaisePropertyChangedAuto(); OnSelectedDateChanged(); }
        }

        private DateTime _minDate;
        public DateTime MinDate
        {
            get { return _minDate; }
            set { _minDate = value; RaisePropertyChangedAuto(); }
        }

        private DateTime _maxDate;
        public DateTime MaxDate
        {
            get { return _maxDate; }
            set { _maxDate = value; RaisePropertyChangedAuto(); }
        }

        private bool _isRealTime;
        public bool IsRealTime
        {
            get { return _isRealTime; }
            set { _isRealTime = value; RaisePropertyChangedAuto(); OnSelectedDateChanged(); }
        }

        private TimelineViewVM _timelineViewVM;

        public TimelineViewVM TimelineViewVM
        {
            get { return _timelineViewVM; }
            set { _timelineViewVM = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand<MachinesEvent> DisplayTimelineCommand { get; set; }

        public RelayCommand NavigateToEventsCommand { get; set; }

        public RelayCommand NavigateToHomeCommand { get; set; }

        public EventsViewVM(INotificationProvider notification, IEventLogger eventLogger, IStudioApplicationManager application, LoggingNavigationManager navigation)
        {
            TimelineViewVM = new TimelineViewVM(notification);

            _navigation = navigation;
            _application = application;
            _notification = notification;
            _eventLogger = eventLogger;
            _realTimeEvents = new ObservableCollection<MachinesEvent>();
            _eventLogger.NewLog += _eventLogger_NewLog;

            DisplayTimelineCommand = new RelayCommand<MachinesEvent>(DisplayTimeline);
            NavigateToEventsCommand = new RelayCommand(() => _navigation.NavigateTo(LoggingNavigationView.EventsView));
            NavigateToHomeCommand = new RelayCommand(() => _navigation.NavigateTo(LoggingNavigationView.HomeView));
        }

        private void _eventLogger_NewLog(object sender, MachinesEvent machineEvent)
        {
            InvokeUI(() =>
            {
                _realTimeEvents.Insert(0, machineEvent);
            });
        }

        private async void OnSelectedMachineChanged()
        {
            if (SelectedMachine != null)
            {
                using (_notification.PushTaskItem("Loading machine events..."))
                {
                    await Task.Factory.StartNew(() =>
                    {
                        _db = ObservablesContext.CreateDefault();

                        _db.EventTypes.Load();

                        DateTime now = DateTime.UtcNow.AddMonths(-1);

                        _history_events = _db.MachinesEvents.Where(x => x.MachineGuid == SelectedMachine.Guid && x.DateTime > now).Include(x => x.User).Include(x => x.User.Contact).Include(x => x.Machine).ToList();

                        Dates = new ObservableCollection<DateTime>();

                        foreach (var day in _history_events.GroupBy(x => x.DateTime.DayOfYear).Select(x => x.First().DateTime).OrderByDescending(x => x))
                        {
                            Dates.Add(day);
                        }

                        if (Dates.Count > 0)
                        {
                            MinDate = Dates.Min();
                            MaxDate = Dates.Max();
                        }

                        SelectedDate = Dates.FirstOrDefault();
                    });
                }
            }
        }

        private void OnSelectedDateChanged()
        {
            if (SelectedDate != null && SelectedMachine != null)
            {
                if (IsRealTime)
                {
                    Events = _realTimeEvents;
                }
                else if (_history_events != null)
                {
                    Events = _history_events.Where(x => x.DateTime.DayOfYear == SelectedDate.Date.DayOfYear).OrderByDescending(x => x.DateTime).ToObservableCollection();
                }
            }
        }

        private void OnSelectedEventChanged()
        {
            if (SelectedEvent != null && SelectedEvent.Type != BL.Enumerations.EventTypes.APPLICATION_STARTED && !_dialog_shown)
            {
                _dialog_shown = true;
                _notification.ShowModalDialog<EventDetailsViewVM, EventDetailsView>(new EventDetailsViewVM(SelectedEvent), (x) =>
                {

                }, () =>
                {
                    _dialog_shown = false;
                });
            }
        }

        private void DisplayTimeline(MachinesEvent ev)
        {
            var events = Events.OrderBy(x => x.DateTime).SkipWhile(x => x != ev).Skip(1).TakeWhile(x => x.DateTime > ev.DateTime && x.Type != BL.Enumerations.EventTypes.APPLICATION_STARTED).ToObservableCollection();
            events.Insert(0, ev);

            TimelineViewVM.Initialize(events.ToList());

            _navigation.NavigateTo(LoggingNavigationView.TimelineWrapperView);
        }
    }
}