blob: 3cbb44bbe133a066a7ddaa72e1853c708c316e1f (
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
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.Core.DI;
using Tango.DragAndDrop;
using Tango.PPC.Common;
using Tango.PPC.Jobs.Messages;
using Tango.PPC.Jobs.Views;
namespace Tango.PPC.Jobs.ViewModels
{
/// <summary>
/// Represents the jobs view model.
/// </summary>
/// <seealso cref="Tango.PPC.Common.PPCViewModel" />
public class JobsViewVM : PPCViewModel
{
private ObservablesContext _jobsContext; //Holds the db context for the job list.
#region Properties
private ObservableCollection<Job> _jobs;
/// <summary>
/// Gets or sets the collection of jobs.
/// </summary>
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();
}
}
private bool _isLoadingJobs;
/// <summary>
/// Gets or sets a value indicating whether this instance is loading jobs.
/// </summary>
public bool IsLoadingJobs
{
get { return _isLoadingJobs; }
set { _isLoadingJobs = value; RaisePropertyChangedAuto(); }
}
#endregion
#region Commands
/// <summary>
/// Gets or sets the job selected command.
/// </summary>
public RelayCommand<Object> JobSelectedCommand { get; set; }
/// <summary>
/// Gets or sets the job drag and drop command.
/// </summary>
public RelayCommand<DropEventArgs> JobDragedDroppedCommand { get; set; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="JobsViewVM"/> class.
/// </summary>
public JobsViewVM()
{
Jobs = new ObservableCollection<Job>();
JobSelectedCommand = new RelayCommand<Object>((x) => JobSelected(x as Job));
JobDragedDroppedCommand = new RelayCommand<DropEventArgs>((e) =>
{
Job draggedJob = e.Draggable.DataContext as Job;
Job droppedJob = e.Droppable.DataContext as Job;
OnDragAndDropJob(draggedJob, droppedJob);
});
}
#endregion
#region Drag & Drop
/// <summary>
/// Called when a job has been dragged and dropped into another job.
/// </summary>
/// <param name="draggedJob">The dragged job.</param>
/// <param name="droppedJob">The dropped job.</param>
private void OnDragAndDropJob(Job draggedJob, Job droppedJob)
{
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();
}
#endregion
#region Job Selection & Loading
/// <summary>
/// Handles the job selected command.
/// </summary>
/// <param name="job">The job.</param>
private void JobSelected(Job job)
{
NavigationManager.NavigateTo<JobsModule>(nameof(JobView));
RaiseMessage(new JobSelectedMessage() { Job = job });
}
/// <summary>
/// Loads the jobs the database.
/// </summary>
private void LoadJobs()
{
Task.Factory.StartNew(() =>
{
IsLoadingJobs = true;
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));
IsLoadingJobs = false;
});
}
#endregion
#region Override Methods
/// <summary>
/// Called when the application has been started.
/// </summary>
public override void OnApplicationStarted()
{
LoadJobs();
}
/// <summary>
/// Called when the navigation system has navigated to this VM view.
/// </summary>
public override void OnNavigatedTo()
{
base.OnNavigatedTo();
}
public override void OnNavigatedFrom()
{
base.OnNavigatedFrom();
}
#endregion
}
}
|