Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the ManageProcessViewModel class.
        /// </summary>
        public ManageProcessViewModel(IProcessManager processManager, DialogHandler dialogHandler)
        {
            _processManager = processManager;
            _dialogHandler  = dialogHandler;

            Messenger.Default.Register <UpdateMessage>(this, message =>
            {
                switch (message.UpdateType)
                {
                case UpdateType.NewProcessCreated:
                    var processes        = Processes.ToList();
                    var currentProcess   = (Process)message.Parameter;
                    currentProcess.Start = currentProcess.Start.ToLocalTime();
                    currentProcess.End   = currentProcess.End.ToLocalTime();
                    processes.Add(currentProcess);
                    DispatcherHelper.CheckBeginInvokeOnUI(() => Processes = new ObservableCollection <Process>(processes.OrderByDescending(p => p.Start)));
                    break;
                }
            });

            LoadedCommand = new RelayCommand(async() =>
            {
                try
                {
                    Mouse.SetCursor(Cursors.Arrow);
                    SetGridSettings();
                    if (_loadedFirst && _processManager != null)
                    {
                        _loadedFirst = false;
                        await _dialogHandler.ShowProgress(null, async() =>
                        {
                            DispatcherHelper.CheckBeginInvokeOnUI(() => Processes.Clear());
                            Log.Info(LogMessages.ManageProcessLoadProcesses);
                            var response = await _processManager.GetProcessesAsync(true);
                            if (ResponseValidator.Validate(response, false))
                            {
                                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                {
                                    if (response.ResponseObject.Any())
                                    {
                                        var ordered = response.ResponseObject.OrderByDescending(p => p.Start);
                                        Processes   = new ObservableCollection <Process>(ordered.Select(p =>
                                        {
                                            p.Start = p.Start.ToLocalTime();
                                            p.End   = p.End.ToLocalTime();
                                            return(p);
                                        }));
                                        RaisePropertyChanged("Processes");
                                    }
                                });
                            }
                        });
                    }
                }
                catch (Exception exception)
                {
                    Messenger.Default.Send(exception);
                }
            });

            RefreshProcessCommand = new RelayCommand <string>(async id =>
            {
                try
                {
                    var processResponse = await _processManager.GetProcessAsync(id);
                    if (ResponseValidator.Validate(processResponse, false))
                    {
                        var selectedItem = Processes.FirstOrDefault(p => p.Id == id);
                        if (selectedItem != null)
                        {
                            Processes[Processes.IndexOf(selectedItem)] = processResponse.ResponseObject;
                            Processes = new ObservableCollection <Process>(Processes);
                        }
                    }
                }
                catch (Exception exception)
                {
                    Messenger.Default.Send(exception);
                }
            });

            CancelProcessCommand = new RelayCommand <Process>(async process =>
            {
                try
                {
                    var processResponse = await _processManager.CancelProcessAsync(process.Id);
                    if (ResponseValidator.Validate(processResponse, false))
                    {
                        var selectedItem = Processes.FirstOrDefault(p => p.Id == process.Id);
                        if (selectedItem != null)
                        {
                            selectedItem.Status = ProcessStatusEnum.Cancelled;
                            Processes           = new ObservableCollection <Process>(Processes);
                        }
                    }
                }
                catch (Exception exception)
                {
                    Messenger.Default.Send(exception);
                }
            });

            ShowProcessDetailsCommand = new RelayCommand <Process>(async process =>
            {
                if (process == null)
                {
                    return;
                }
                var context = new CommonDialogViewModel
                {
                    Header  = "Process details",
                    Buttons = ButtonsEnum.Ok,
                    Content = new JContent(process)
                };
                var view = new CommonDialog {
                    DataContext = context
                };
                await _dialogHandler.Show(view);
            });
        }