aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/PPCViewModel.cs
blob: b9114dba46f5fcae65a2c226b299c746a227d46b (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.Core.DI;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.Authentication;
using Tango.PPC.Common.Connection;
using Tango.PPC.Common.ExternalBridge;
using Tango.PPC.Common.Navigation;
using Tango.PPC.Common.Notifications;
using Tango.Settings;
using Tango.SharedUI;
using static Tango.SharedUI.Controls.NavigationControl;

namespace Tango.PPC.Common
{
    /// <summary>
    /// Represents a PPC view model base class.
    /// </summary>
    /// <seealso cref="Tango.SharedUI.ViewModel" />
    public abstract class PPCViewModel : ViewModel, INavigationViewModel, INavigationBlocker
    {
        /// <summary>
        /// Gets the static observable entities adapter.
        /// </summary>
        public ObservablesEntitiesAdapter Adapter
        {
            get { return ObservablesEntitiesAdapter.Instance; }
        }

        /// <summary>
        /// Gets or sets the application manager.
        /// </summary>
        [TangoInject]
        public IPPCApplicationManager ApplicationManager { get; set; }

        /// <summary>
        /// Gets or sets the authentication provider.
        /// </summary>
        [TangoInject]
        public IAuthenticationProvider AuthenticationProvider { get; set; }

        /// <summary>
        /// Gets or sets the navigation manager.
        /// </summary>
        [TangoInject]
        public INavigationManager NavigationManager { get; set; }

        /// <summary>
        /// Gets or sets the notification provider.
        /// </summary>
        [TangoInject]
        public INotificationProvider NotificationProvider { get; set; }

        /// <summary>
        /// Gets or sets the machine provider.
        /// </summary>
        [TangoInject]
        public IMachineProvider MachineProvider { get; set; }

        /// <summary>
        /// Gets or sets the external bridge service.
        /// </summary>
        [TangoInject]
        public IPPCExternalBridgeService ExternalBridgeService { get; set; }


        private PPCSettings _settings;
        /// <summary>
        /// Gets the main PPC settings.
        /// </summary>
        public PPCSettings Settings
        {
            get
            {
                if (_settings == null)
                {
                    _settings = SettingsManager.Default.GetOrCreate<PPCSettings>();
                }

                return _settings;
            }
            private set { _settings = value; }
        }

        private bool _isVisible;
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="PPCViewModel"/> view is visible.
        /// </summary>
        public bool IsVisible
        {
            get { return _isVisible; }
            private set { _isVisible = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Called when the application has been started.
        /// </summary>
        public abstract void OnApplicationStarted();

        /// <summary>
        /// Called when the application is shutting down.
        /// </summary>
        public virtual void OnApplicationShuttingDown()
        {

        }

        /// <summary>
        /// Called when the navigation system has navigated to this VM view.
        /// </summary>
        public virtual void OnNavigatedTo()
        {
            IsVisible = true;
        }

        /// <summary>
        /// Called when the navigation system has navigated from this VM view.
        /// </summary>
        public virtual void OnNavigatedFrom()
        {
            IsVisible = false;
        }

        /// <summary>
        /// Raises the specified message using the default <see cref="TangoMessenger"/>.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="message">The message.</param>
        protected void RaiseMessage<T>(T message) where T : class
        {
            TangoMessenger.Default.Send<T>(message);
        }

        /// <summary>
        /// Registers a message handle for the specified message type T.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="handler">The handler.</param>
        protected void RegisterForMessage<T>(Action<T> handler)
        {
            TangoMessenger.Default.Register<T>(handler);
        }

        /// <summary>
        /// Called before the navigation system navigates from this object.
        /// Return false to abort the navigation.
        /// </summary>
        /// <returns></returns>
        public virtual Task<bool> OnNavigateOutRequest()
        {
            return Task.FromResult(true);
        }

        /// <summary>
        /// Called before the navigation system navigates back from this object.
        /// Return false to abort the navigation.
        /// </summary>
        /// <returns></returns>
        public virtual Task<bool> OnNavigateBackRequest()
        {
            return Task.FromResult(true);
        }
    }

    /// <summary>
    /// Represents a PPC view model base class a View property as an instance of a <see cref="IPPCView"/> contract.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <seealso cref="Tango.SharedUI.ViewModel" />
    public abstract class PPCViewModel<T> : PPCViewModel where T : IPPCView
    {
        /// <summary>
        /// Gets the IPPCView instance.
        /// </summary>
        public T View { get; private set; }

        /// <summary>
        /// Gets a value indicating whether the instance of IPPCView is available.
        /// </summary>
        public bool ViewAttached { get; private set; }

        /// <summary>
        /// Called when the application has been started.
        /// </summary>
        public override void OnApplicationStarted()
        {
            TangoIOC.Default.GetInstanceWhenAvailable<T>((view) =>
            {
                ViewAttached = true;
                View = view;
                OnViewAttached();
            });
        }

        /// <summary>
        /// Called when the instance of IPPCView is available.
        /// </summary>
        public virtual void OnViewAttached()
        {

        }
    }
}