aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs
blob: 241297d855b981cfc639955561e078f4a374729d (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.DAL.Observables;
using Tango.MachineStudio.Common.Notifications;
using Tango.SharedUI;

namespace Tango.MachineStudio.MachineDesigner.ViewModels
{
    public class MainViewVM : ViewModel
    {
        private bool _isSaving;
        private INotificationProvider _notification;

        private ObservablesEntitiesAdapter _adapter;
        /// <summary>
        /// Gets or sets the db adapter.
        /// </summary>
        public ObservablesEntitiesAdapter Adapter
        {
            get { return _adapter; }
            set { _adapter = value; RaisePropertyChangedAuto(); }
        }

        private Machine _machine;
        /// <summary>
        /// Gets or sets the machine.
        /// </summary>
        public Machine Machine
        {
            get { return _machine; }
            set { _machine = value; RaisePropertyChangedAuto(); OnMachineChanged(); }
        }

        private Configuration _configuration;
        /// <summary>
        /// Gets or sets the configuration.
        /// </summary>
        public Configuration Configuration
        {
            get { return _configuration; }
            set { _configuration = value; RaisePropertyChangedAuto(); }
        }

        private IdsPack _selectedIds;
        /// <summary>
        /// Gets or sets the selected ids pack.
        /// </summary>
        public IdsPack SelectedIds
        {
            get { return _selectedIds; }
            set { _selectedIds = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        public RelayCommand SaveCommand { get; set; }

        public RelayCommand AddIdsCommand { get; set; }

        public RelayCommand RemoveIdsCommand { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="MainViewVM"/> class.
        /// </summary>
        public MainViewVM(INotificationProvider notification)
        {
            _notification = notification;

            Adapter = ObservablesEntitiesAdapter.Instance;
            Configuration = new Configuration();
            Configuration.Name = "Untitled";

            SaveCommand = new RelayCommand(Save, (x) => !_isSaving);
            AddIdsCommand = new RelayCommand(AddIds, (x) => !_isSaving && Configuration.IdsPacks.Count < 8);
            RemoveIdsCommand = new RelayCommand(RemoveIds, (x) => !_isSaving && SelectedIds != null);
        }

        private void OnMachineChanged()
        {
            if (Machine != null)
            {
                Configuration = Machine.Configuration;
            }
            else
            {
                Configuration = new Configuration() { Name = "Untitled" };
            }
        }

        public void DropTouchPanel(ApplicationDisplayPanelVersion applicationDisplayPanelVersion)
        {
            Configuration.ApplicationDisplayPanelVersions = applicationDisplayPanelVersion;
            Configuration.ApplicationDisplayPanelVersionGuid = applicationDisplayPanelVersion.Guid;
        }

        public void DropApplicationVersion(ApplicationVersion applicationVersion)
        {
            Configuration.ApplicationVersions = applicationVersion;
            Configuration.ApplicationVersionGuid = applicationVersion.Guid;
        }

        public void DropEmbeddedFirmware(EmbeddedFirmwareVersion embeddedFirmwareVersion)
        {
            Configuration.EmbeddedFirmwareVersions = embeddedFirmwareVersion;
            Configuration.EmbeddedFirmwareVersionGuid = embeddedFirmwareVersion.Guid;
        }

        public void DropEmbeddedSoftware(EmbeddedSoftwareVersion embeddedSoftwareVersion)
        {
            Configuration.EmbeddedSoftwareVersions = embeddedSoftwareVersion;
            Configuration.EmbeddedSoftwareVersionGuid = embeddedSoftwareVersion.Guid;
        }

        private void RemoveIds()
        {
            Configuration.IdsPacks.Remove(SelectedIds);
            SelectedIds = null;
        }

        private void AddIds()
        {
            Configuration.IdsPacks.Add(new IdsPack());
            InvalidateRelayCommands();
        }

        public void DropCartridgeType(CartridgeType cartridgeType, IdsPack idsPack)
        {
            idsPack.CartridgeTypes = cartridgeType;
            idsPack.CartridgeTypeGuid = cartridgeType.Guid;
        }

        public void DropDispenser(Dispenser dispenser, IdsPack idsPack)
        {
            idsPack.Dispenser = dispenser;
            idsPack.DispenserGuid = dispenser.Guid;
        }

        public void DropMidTankType(MidTankType midTankType, IdsPack idsPack)
        {
            idsPack.MidTankTypes = midTankType;
            idsPack.MidTankTypeGuid = midTankType.Guid;
        }

        public void DropLiquidType(LiquidType liquidType, IdsPack idsPack)
        {
            idsPack.LiquidTypes = liquidType;
            idsPack.LiquidTypeGuid = liquidType.Guid;
        }

        private async void Save()
        {
            _isSaving = true;
            InvalidateRelayCommands();

            try
            {
                using (_notification.PushTaskItem("Saving Configuration..."))
                {
                    await Configuration.SaveAsync();
                    Configuration = Adapter.Configurations.SingleOrDefault(x => x.Guid == Configuration.Guid);
                }
            }
            catch (Exception ex)
            {
                _notification.ShowError("An error occurred while trying to save the configuration" + Environment.NewLine + ex.Message);
            }
            finally
            {
                _isSaving = false;
                InvalidateRelayCommands();
            }
        }
    }
}