aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Modules/Tango.PPC.Jobs/ViewModels/JobsViewVM.cs
blob: 413027d5dd15c0de35259565c1bfc8ef84cc2c16 (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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Data;
using Tango.BL;
using Tango.BL.Entities;
using Tango.Core.Commands;
using Tango.DragAndDrop;
using Tango.PPC.Common;

namespace Tango.PPC.Jobs.ViewModels
{
    public class JobsViewVM : PPCViewModel
    {
        private ObservablesContext _jobsContext;

        private ObservableCollection<Job> _jobs;
        public ObservableCollection<Job> Jobs
        {
            get { return _jobs; }
            set { _jobs = value; RaisePropertyChangedAuto(); }
        }

        private ICollectionView _jobsCollectionView;
        /// <summary>
        /// Gets or sets the jobs collection view.
        /// </summary>
        public ICollectionView JobsCollectionView
        {
            get { return _jobsCollectionView; }
            set
            {
                _jobsCollectionView = value;
                RaisePropertyChangedAuto();
            }
        }

        public RelayCommand<Object> JobSelectedCommand { get; set; }

        public RelayCommand<DropEventArgs> OnDragAndDropCommand { get; set; }

        public RelayCommand<Job> RemoveJobCommand { get; set; }

        private void OnDragAndDropJobs(Job draggedJob, Job droppedJob)
        {
            Debug.WriteLine(draggedJob.Name + " Dragged on to " + droppedJob.Name);

            if (draggedJob.JobIndex > droppedJob.JobIndex)
            {
                draggedJob.JobIndex = droppedJob.JobIndex - 1;
            }
            else
            {
                draggedJob.JobIndex = droppedJob.JobIndex + 1;
            }

            int index = 1;

            foreach (var job in Jobs.OrderBy(x => x.JobIndex))
            {
                job.JobIndex = index++;
            }

            JobsCollectionView.Refresh();
        }

        private async void JobSelected(Job job)
        {
            Debug.WriteLine(job.Name);
            await NotificationProvider.ShowInfo("Job details not yet implemented...");
        }

        public JobsViewVM()
        {
            Jobs = new ObservableCollection<Job>();

            JobSelectedCommand = new RelayCommand<Object>((x) => JobSelected(x as Job));
            OnDragAndDropCommand = new RelayCommand<DropEventArgs>((e) =>
            {
                Job draggedJob = e.Draggable.DataContext as Job;
                Job droppedJob = e.Droppable.DataContext as Job;

                OnDragAndDropJobs(draggedJob, droppedJob);
            });

            RemoveJobCommand = new RelayCommand<Job>(RemoveJob);
        }

        private void RemoveJob(Job job)
        {
            NotificationProvider.ShowQuestion("Are you sure you want to remove " + job.Name);
        }

        public override void OnApplicationStarted()
        {
            LoadJobs();
        }

        private void LoadJobs()
        {
            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(500);

                if (_jobsContext != null)
                {
                    _jobsContext.Dispose();
                }

                _jobsContext = ObservablesContext.CreateDefault();

                Jobs = _jobsContext.Jobs.Where(x => x.Machine.SerialNumber == Settings.MachineSerialNumber).ToObservableCollection();
                JobsCollectionView = CollectionViewSource.GetDefaultView(Jobs);
                JobsCollectionView.SortDescriptions.Add(new SortDescription(nameof(Job.LastUpdated), ListSortDirection.Descending));
            });
        }
    }
}