aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Modules/Tango.PPC.Jobs/ViewModels/MainViewVM.cs
blob: 7026a6ebc1aca6850d91ae3a2bc006bc8c092090 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.Integration.Operation;
using Tango.PPC.Common;
using Tango.PPC.Common.Notifications;
using Tango.PPC.Common.Notifications.NotificationItems;
using Tango.PPC.Jobs.Messages;
using Tango.PPC.Jobs.NavigationObjects;
using Tango.PPC.Jobs.Views;

namespace Tango.PPC.Jobs.ViewModels
{
    /// <summary>
    /// Represents the main view ViewModel.
    /// </summary>
    /// <seealso cref="Tango.PPC.Common.PPCViewModel" />
    public class MainViewVM : PPCViewModel
    {
        /// <summary>
        /// Called when the application has been started.
        /// </summary>
        public override void OnApplicationStarted()
        {
            MachineProvider.MachineOperator.PrintingCompleted += MachineOperator_PrintingCompleted;
            MachineProvider.MachineOperator.PrintingFailed += MachineOperator_PrintingFailed;
        }

        /// <summary>
        /// Handles the PrintingFailed event of the MachineOperator.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Integration.Operation.PrintingFailedEventArgs"/> instance containing the event data.</param>
        private void MachineOperator_PrintingFailed(object sender, PrintingFailedEventArgs e)
        {
            NotificationProvider.PushNotification(new MessageNotificationItem(
                String.Format("'{0}' failed.", e.Job.Name),
                String.Format("The job '{1}' has failed due to unexpected error.{0}{2}{0}{0}Tap to view this job details.", Environment.NewLine, e.Job.Name, e.Exception), MessageNotificationItem.MessageNotificationItemTypes.Error, () =>
                 {
                     NavigationManager.NavigateWithObject<JobsModule, JobView, Job>(e.Job);
                     NavigationManager.ClearHistoryExcept<JobsView>();
                 }));
        }

        /// <summary>
        /// Handles the PrintingCompleted event of the MachineOperator.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Integration.Operation.PrintingEventArgs"/> instance containing the event data.</param>
        private void MachineOperator_PrintingCompleted(object sender, PrintingEventArgs e)
        {
            LogManager.Log($"'{e.Job.Name}' printing complete. Job designation is {e.Job.Designation}.");

            if (e.Job.Designation == BL.Enumerations.JobDesignations.SampleDye)
            {
                NotificationProvider.PushNotification(new MessageNotificationItem(String.Format("'{0}' sample dye completed successfully", e.Job.Name), "Tap to approve or repeat.", MessageNotificationItem.MessageNotificationItemTypes.Success, () =>
                   {
                       NavigationManager.NavigateWithObject<JobsModule, JobView, JobNavigationObject>(new JobNavigationObject() { Job = e.Job, Intent = JobNavigationIntent.SampleDye });
                       NavigationManager.ClearHistoryExcept<JobsView>();
                   }));
            }
            else if (e.Job.Designation == BL.Enumerations.JobDesignations.FineTuning)
            {
                NotificationProvider.PushNotification(new MessageNotificationItem(String.Format("'{0}' fine tuning completed successfully", e.Job.Name), "Tap to approve or repeat.", MessageNotificationItem.MessageNotificationItemTypes.Success, () =>
                {
                    NavigationManager.NavigateWithObject<JobsModule, JobView, JobNavigationObject>(new JobNavigationObject() { Job = e.Job, Intent = JobNavigationIntent.FineTuning });
                    NavigationManager.ClearHistoryExcept<JobsView>();
                }));
            }
            else
            {
                NotificationProvider.PushNotification(new MessageNotificationItem(String.Format("'{0}' completed successfully", e.Job.Name), "Tap to view this job details.", MessageNotificationItem.MessageNotificationItemTypes.Success, () =>
                {
                    NavigationManager.NavigateWithObject<JobsModule, JobView, JobNavigationObject>(new JobNavigationObject() { Job = e.Job });
                    NavigationManager.ClearHistoryExcept<JobsView>();
                }));
            }
        }

        public override void OnNavigatedTo()
        {
            base.OnNavigatedTo();
            RaiseMessage<NavigatedToJobsModuleMessage>();
        }
    }
}
w"> ValidationErrors = new ObservableCollection<string>(); } ValidationErrors.Clear(); OnValidating(); foreach (var prop in this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { foreach (var validation in prop.GetCustomAttributes<ValidationAttribute>()) { if (!validation.IsValid(prop.GetValue(this))) { _currentErrors.Add(new KeyValuePair<string, string>(prop.Name, validation.ErrorMessage)); ValidationErrors.Add(validation.ErrorMessage); } } HasErrors = _currentErrors.Count > 0; RaiseError(prop.Name); } return !HasErrors; } /// <summary> /// Inserts the specified property error (Use inside OnValidating). /// </summary> /// <param name="propName">Name of the property.</param> /// <param name="error">The error.</param> protected void InsertError(String propName, String error) { _currentErrors.Add(new KeyValuePair<string, string>(propName, error)); } /// <summary> /// Called before validating. /// </summary> protected virtual void OnValidating() { } /// <summary> /// Posts the specified message using the messenger service. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="message">The message.</param> /// <returns></returns> protected virtual T PostMessage<T>(T message) { TangoMessenger.Default.Send<T>(message); return message; } /// <summary> /// Registers a messenger message handler. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="handler">The handler.</param> protected virtual void RegisterMessage<T>(Action<T> handler) { TangoMessenger.Default.Register<T>(handler); } } /// <summary> /// Represents an abstract view model which expects an <see cref="IView"/> of the specified type T. /// </summary> /// <typeparam name="T"></typeparam> /// <seealso cref="Tango.Core.ExtendedObject" /> /// <seealso cref="System.ComponentModel.INotifyDataErrorInfo" /> public abstract class ViewModel<T> : ViewModel where T : IView { /// <summary> /// Gets or sets the view. /// </summary> public T View { get; set; } /// <summary> /// Initializes a new instance of the <see cref="ViewModel{T}"/> class. /// </summary> public ViewModel() : base() { TangoIOC.Default.GetInstanceWhenAvailable<T>((view) => { View = view; OnViewAttached(); }); } /// <summary> /// Called when the <see cref="IView"/> is loaded and attached. /// </summary> protected virtual void OnViewAttached() { } } }