aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs
blob: dbba1962e9eea3d4ada375af640d93e60cc8766e (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
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(); }
        }

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

        public RelayCommand SaveCommand { 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 = "Config 2";


            for (int i = 0; i < 8; i++)
            {
                Configuration.IdsPacks.Add(new IdsPack() { Name = "IDS PACK " + i });
            }

            SaveCommand = new RelayCommand(Save, (x) => !_isSaving);
        }

        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();
            }
        }
    }
}