aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/JobsViewVM.cs
blob: b5e3e18fc33c66568e84a5090368e40a3dcca9c9 (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
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;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.Authentication;
using Tango.PPC.Common.Navigation;
using Tango.PPC.Common.Notifications;
using Tango.Settings;
using Tango.SharedUI;

namespace Tango.PPC.UI.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 JobSelectedCommand { get; set; }

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

        public JobsViewVM(IPPCApplicationManager application, IAuthenticationProvider authentication, INavigationManager navigation, INotificationProvider notification) : base(application, authentication, navigation, notification)
        {
            Jobs = new ObservableCollection<Job>();

            JobSelectedCommand = new RelayCommand((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);
            });
        }

        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 void JobSelected(Job job)
        {
            Debug.WriteLine(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));
            });
        }
    }
}