aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/ViewModels/LayoutViewVM.cs
blob: 98710e23a855d5136e22f3b7c59c0112e94446d8 (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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.FSE.Common;
using Tango.FSE.Common.Navigation;
using Tango.FSE.Common.Notifications;
using Tango.FSE.UI.Panes;
using Tango.Integration.ExternalBridge;
using Tango.SharedUI.Helpers;

namespace Tango.FSE.UI.ViewModels
{
    public class LayoutViewVM : FSEViewModel
    {
        private bool _isConnectionPaneOpened;
        public bool IsConnectionPaneOpened
        {
            get { return _isConnectionPaneOpened; }
            set { _isConnectionPaneOpened = value; RaisePropertyChangedAuto(); OnConnectionPaneToggleChanged(); }
        }

        private MachineConnectionPaneVM _connectionPaneVM;
        public MachineConnectionPaneVM ConnectionPaneVM
        {
            get { return _connectionPaneVM; }
            set { _connectionPaneVM = value; RaisePropertyChangedAuto(); }
        }

        private ConnectedMachinePaneVM _connectedMachinePaneVM;
        public ConnectedMachinePaneVM ConnectedMachinePaneVM
        {
            get { return _connectedMachinePaneVM; }
            set { _connectedMachinePaneVM = value; RaisePropertyChangedAuto(); }
        }

        private bool _isMenuOpened;
        public bool IsMenuOpened
        {
            get { return _isMenuOpened; }
            set { _isMenuOpened = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand ToggleConnectionPaneCommand { get; set; }

        public LayoutViewVM()
        {
            ToggleConnectionPaneCommand = new RelayCommand(() => IsConnectionPaneOpened = !IsConnectionPaneOpened);
            ConnectionPaneVM = new MachineConnectionPaneVM();
            ConnectionPaneVM.MachineSelected += ConnectionPaneVM_MachineSelected;

            ConnectedMachinePaneVM = new ConnectedMachinePaneVM();
            ConnectedMachinePaneVM.DisconnectedFromMachine += ConnectedMachinePaneVM_DisconnectedFromMachine;
        }

        private async void ConnectedMachinePaneVM_DisconnectedFromMachine(object sender, EventArgs e)
        {
            IsConnectionPaneOpened = false;
            await MachineProvider.DisconnectMachine();
        }

        private async void ConnectionPaneVM_MachineSelected(object sender, IExternalBridgeClient machine)
        {
            IsConnectionPaneOpened = false;

            try
            {
                if (await MachineProvider.ConnectToMachine(machine))
                {
                    NotificationProvider.PushSnackbarItem(MessageType.Success, "Machine successfully connected", false, $"{MachineProvider.Machine.SerialNumber}, {MachineProvider.Machine.Name}", TimeSpan.FromSeconds(5));
                }
            }
            catch (Exception ex)
            {
                await NotificationProvider.ShowError($"An error occurred while trying to connect to the specified machine.\n{ex.FlattenMessage()}");
                LogManager.Log(ex, "Error connecting to machine.");
            }
        }

        private void OnConnectionPaneToggleChanged()
        {
            if (IsConnectionPaneOpened)
            {
                ConnectionPaneVM.Start();
            }
            else
            {
                ConnectionPaneVM.Stop();
            }
        }

        public override void OnApplicationStarted()
        {
            DiagnosticsProvider.FrameReceived += DiagnosticsProvider_FrameReceived;

            NavigationManager.MenuItems.Add(new NavigationMenuItem(() =>
            {
                Debug.WriteLine("Pressed");
            })
            {
                Name = "Account",
                Index = -1,
                Description = "Examine the machine",
                Image = ResourceHelper.GetImageFromResources("Images/Menu/account.png"),
            });

            NavigationManager.MenuItems.Add(new NavigationMenuItem(() =>
            {
                Debug.WriteLine("Pressed");
            })
            {
                Name = "Settings",
                Index = 2,
                Description = "Examine the machine",
                Image = ResourceHelper.GetImageFromResources("Images/Menu/settings.png"),
            });
        }

        private void DiagnosticsProvider_FrameReceived(object sender, Common.Diagnostics.DiagnosticsFrameReceivedEventArgs e)
        {
            Debug.WriteLine("Diagnostics Received...");
        }
    }
}