aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Modules/Tango.PPC.Jobs/ViewModels/JobSummeryViewVM.cs
blob: 847edb52fbdead63e7d278b914146114c4e607be (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Builders;
using Tango.BL.Entities;
using Tango.ColorConversion;
using Tango.Core.Commands;
using Tango.PPC.Common;
using Tango.PPC.Common.Messages;
using Tango.PPC.Common.Navigation;
using Tango.PPC.Jobs.Messages;
using Tango.PPC.Jobs.NavigationObjects;
using Tango.PPC.Jobs.Views;

namespace Tango.PPC.Jobs.ViewModels
{
    /// <summary>
    /// Represents the job summary view model.
    /// </summary>
    /// <seealso cref="Tango.PPC.Common.PPCViewModel" />
    public class JobSummeryViewVM : PPCViewModel, INavigationObjectReceiver<JobSummeryNavigationObject>
    {
        private ObservablesContext _context;

        private Job _job;
        /// <summary>
        /// Gets or sets the job.
        /// </summary>
        public Job Job
        {
            get { return _job; }
            set { _job = value; RaisePropertyChangedAuto(); }
        }

        private TimeSpan _estimatedDuration;
        /// <summary>
        /// Gets or sets the duration of the estimated.
        /// </summary>

        public TimeSpan EstimatedDuration
        {
            get { return _estimatedDuration; }
            set { _estimatedDuration = value; RaisePropertyChangedAuto(); }
        }


        /// <summary>
        /// Gets or sets the dye command.
        /// </summary>
        public RelayCommand DyeCommand { get; set; }

        /// <summary>
        /// Gets or sets the edit command.
        /// </summary>
        public RelayCommand EditCommand { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="JobSummeryViewVM"/> class.
        /// </summary>
        public JobSummeryViewVM()
        {
            DyeCommand = new RelayCommand(StartJob);
            EditCommand = new RelayCommand(EditJob);
        }

        /// <summary>
        /// Edits the job.
        /// </summary>
        private void EditJob()
        {
            LogManager.Log("Edit command pressed.");
            NavigationManager.NavigateTo<JobsModule>(false, nameof(JobView));
        }

        /// <summary>
        /// Starts the job.
        /// </summary>
        private async void StartJob()
        {
            try
            {
                LogManager.Log("Start job command pressed. Starting job and navigating to job progress view...");

                await PrintingManager.Print(Job, _context);
                await NavigationManager.NavigateTo<JobsModule>(false, nameof(JobProgressView));
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Could not start the current job.");
                await NotificationProvider.ShowError($"{ex.Message}");
            }
        }

        /// <summary>
        /// Called when the application has been started.
        /// </summary>
        public override void OnApplicationStarted()
        {

        }

        public async void OnNavigatedToWithObject(JobSummeryNavigationObject obj)
        {
            _context = obj.Context;
            Job = obj.Job;

            Job = await new JobBuilder(_context).Set(Job.Guid)
                    .WithConfiguration()
                    .WithRML()
                    .WithUser()
                    .WithSegments()
                    .WithBrushStops()
                    .BuildAsync();

            await Task.Factory.StartNew(() =>
            {
                try
                {
                    IColorConverter _converter = new DefaultColorConverter();
                    EstimatedDuration = Job.GetEstimatedDuration(_converter.GetRecommendedProcessParameters(Job));
                }
                catch { }
            });
        }
    }
}