aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Modules/Tango.PPC.Technician/ViewModels/UpdatesViewVM.cs
blob: 3f42322520d00f7783ad5b437c430855d020377d (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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.Core.Commands;
using Tango.PPC.Common;
using System.Data.Entity;
using Tango.PPC.Technician.Dialogs;
using Tango.PPC.Common.Synchronization;

namespace Tango.PPC.Technician.ViewModels
{
    public class UpdatesViewVM : PPCViewModel
    {
        public RelayCommand SynchronizeCommand { get; set; }

        private List<TangoUpdate> _updates;
        public List<TangoUpdate> Updates
        {
            get { return _updates; }
            set { _updates = value; RaisePropertyChangedAuto(); }
        }

        private TangoUpdate _selectedUpdate;
        public TangoUpdate SelectedUpdate
        {
            get { return _selectedUpdate; }
            set { _selectedUpdate = value; OnSelectedUpdateChanged(); }
        }

        private SynchronizationStatus _selectedSynchronization;
        public SynchronizationStatus SelectedSynchronization
        {
            get { return _selectedSynchronization; }
            set { _selectedSynchronization = value; OnSelectedSynchronizationChanged();  }
        }

        public UpdatesViewVM()
        {
            Updates = new List<TangoUpdate>();
            SynchronizeCommand = new RelayCommand(Synchronize, () => !MachineDataSynchronizer.IsSynchronizing);
        }

        public override void OnApplicationStarted()
        {

        }

        public override void OnApplicationReady()
        {
            base.OnApplicationReady();
            MachineDataSynchronizer.SynchronizationStarted += (_, __) => InvalidateRelayCommands();
            MachineDataSynchronizer.SynchronizationEnded += (_, __) => InvalidateRelayCommands();
        }

        private async void Synchronize()
        {
            try
            {
                await MachineDataSynchronizer.Synchronize();
            }
            catch { }
        }

        public async override void OnNavigatedTo()
        {
            base.OnNavigatedTo();

            try
            {
                using (ObservablesContext db = ObservablesContext.CreateDefault())
                {
                    Updates = await db.TangoUpdates.Where(x =>
                    x.Status != (int)TangoUpdateStatuses.SynchronizationCompleted &&
                    x.Status != (int)TangoUpdateStatuses.SynchronizationFailed &&
                    x.Status != (int)TangoUpdateStatuses.SynchronizationStarted
                    ).OrderByDescending(x => x.StartDate).ToListAsync();
                }
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error loading update history.");
            }
        }

        private async void OnSelectedUpdateChanged()
        {
            if (SelectedUpdate != null)
            {
                await NotificationProvider.ShowDialog<UpdateDetailsViewVM>(new UpdateDetailsViewVM() { Update = SelectedUpdate });
            }
        }

        private async void OnSelectedSynchronizationChanged()
        {
            if (SelectedSynchronization != null)
            {
                await NotificationProvider.ShowDialog<SynchronizationDetailsViewVM>(new SynchronizationDetailsViewVM() { Status = SelectedSynchronization });
            }
        }
    }
}