aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.MachineConfiguration/ViewModels/MachineViewVM.cs
blob: 822f2cb80da7b2562b85c6cd5cd2842f548bb5c8 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.FSE.Common;
using Tango.FSE.Common.Navigation;
using Tango.FSE.MachineConfiguration.Messages;
using Tango.FSE.MachineConfiguration.Navigation;
using static Tango.FSE.BL.Services.MachineConfigurationService;
using static Tango.FSE.MachineConfiguration.ViewModels.MachineViewVM;

namespace Tango.FSE.MachineConfiguration.ViewModels
{
    public class MachineViewVM : ConfigurationViewModel, INavigationObjectReceiver<NavigationObject>
    {
        public class NavigationObject
        {
            public String MachineGuid { get; set; }
        }

        public enum NavigationView
        {
            SettingsView,
            DataStoreView,
        }

        private NavigationView _selectedView;
        public NavigationView SelectedView
        {
            get { return _selectedView; }
            set
            {
                if (value == NavigationView.DataStoreView)
                {
                    if (!CurrentUser.HasPermission(Tango.BL.Enumerations.Permissions.DataStoreRead))
                    {
                        Task.Delay(500).ContinueWith((x) =>
                        {
                            SelectedView = _selectedView;
                            NotificationProvider.ShowError("The current user profile does not allow accessing the data store.\nPlease contact your administrator.");
                        });
                        return;
                    }
                }

                _selectedView = value;
                RaisePropertyChangedAuto();
            }
        }

        private MachineEditingComposition _editingComposition;
        public MachineEditingComposition EditingComposition
        {
            get { return _editingComposition; }
            set { _editingComposition = value; RaisePropertyChangedAuto(); }
        }

        public void OnNavigatedToWithObject(NavigationObject obj)
        {
            SelectedView = NavigationView.SettingsView;
            LoadMachine(obj.MachineGuid);
        }

        private async void LoadMachine(String machineGuid)
        {
            using (NotificationProvider.PushTaskItem("Loading machine configuration..."))
            {
                try
                {
                    EditingComposition = await Services.MachineConfigurationService.GetMachineEditingComposition(machineGuid);
                    RaiseMessage(new EditingCompositionLoadedMessage() { EditingComposition = EditingComposition });
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error loading machine.");
                    await Task.Delay(1000);
                    await ModularNavigationManager.NavigateBack();
                    await NotificationProvider.ShowError($"Error loading the selected machine.\n{ex.FlattenMessage()}");
                }
            }
        }
    }
}