aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/MainViewVM.cs
blob: ff42d870cd837cd147a153009887e89845894ca8 (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
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.Builders;
using Tango.BL.Entities;
using Tango.MachineStudio.Common;
using Tango.MachineStudio.Common.Notifications;

namespace Tango.MachineStudio.RML.ViewModels
{
    public class MainViewVM : StudioViewModel
    {
        private INotificationProvider _notification;

        private ObservablesContext _rmls_context;
        private ObservablesContext _active_context;

        private ObservableCollection<Rml> _rmls;
        public ObservableCollection<Rml> Rmls
        {
            get { return _rmls; }
            set { _rmls = value; RaisePropertyChangedAuto(); }
        }

        private Rml _selectedRML;
        public Rml SelectedRML
        {
            get { return _selectedRML; }
            set { _selectedRML = value; RaisePropertyChangedAuto(); OnSelectedRmlChanged(); }
        }

        private Rml _activeRML;
        public Rml ActiveRML
        {
            get { return _activeRML; }
            set { _activeRML = value; RaisePropertyChangedAuto(); }
        }

        public MainViewVM(INotificationProvider notificationProvider)
        {
            _notification = notificationProvider;
        }

        public override void OnApplicationReady()
        {
            LoadRmls();
        }

        private void LoadRmls()
        {
            using (_rmls_context = ObservablesContext.CreateDefault())
            {
                Rmls = _rmls.ToList().ToObservableCollection();
            }
        }

        private void OnSelectedRmlChanged()
        {
            if (SelectedRML != null)
            {
                LoadActiveRML(SelectedRML.Guid);
            }
            else
            {
                UnloadActiveRML();
            }
        }

        private void UnloadActiveRML()
        {
            ActiveRML = null;
        }

        private async void LoadActiveRML(String guid)
        {
            using (_notification.PushTaskItem("Loading RML..."))
            {
                if (_active_context != null)
                {
                    _active_context.Dispose();
                }

                _active_context = ObservablesContext.CreateDefault();

                ActiveRML = await new RmlBuilder(_active_context)
                    .Set(guid)
                    .WithAllParametersGroup()
                    .WithCCT()
                    .WithLiquidFactors()
                    .BuildAsync();
            }
        }
    }
}