aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.UI/Notifications/DefaultNotificationProvider.cs
blob: fc1ba894f42371813b1f3791be86e520ba0988de (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Tango.PPC.Common.Notifications;
using Tango.Core;
using System.Collections.Concurrent;
using System.Windows.Media.Imaging;
using Tango.SharedUI.Helpers;
using System.Timers;
using Tango.Core.Commands;
using Tango.Touch.Controls;

namespace Tango.PPC.UI.Notifications
{
    /// <summary>
    /// Represents the default PPC notification provider.
    /// </summary>
    /// <seealso cref="Tango.Core.ExtendedObject" />
    /// <seealso cref="Tango.PPC.Common.Notifications.INotificationProvider" />
    public class DefaultNotificationProvider : ExtendedObject, INotificationProvider
    {
        /// <summary>
        /// Gets the collection of notification items.
        /// </summary>
        public ObservableCollection<NotificationItem> NotificationItems { get; private set; }

        /// <summary>
        /// Represents a pending message box.
        /// </summary>
        private class PendingMessageBox
        {
            /// <summary>
            /// Gets or sets the message view model.
            /// </summary>
            public MessageBoxVM VM { get; set; }

            /// <summary>
            /// Gets or sets the message task completion source.
            /// </summary>
            public TaskCompletionSource<bool> CompletionSource { get; set; }
        }

        private ConcurrentQueue<PendingMessageBox> _pendingMessageBoxes;

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultNotificationProvider"/> class.
        /// </summary>
        public DefaultNotificationProvider()
        {
            NotificationItems = new ObservableCollection<NotificationItem>();
            _pendingMessageBoxes = new ConcurrentQueue<PendingMessageBox>();

            PopNotificationCommand = new RelayCommand<NotificationItem>((x) => PopNotification(x));
        }

        private MessageBoxVM _currentMessageBox;
        /// <summary>
        /// Gets the current message box if any.
        /// </summary>
        public MessageBoxVM CurrentMessageBox
        {
            get { return _currentMessageBox; }
            private set
            {
                _currentMessageBox = value;
                RaisePropertyChangedAuto();
                RaisePropertyChanged(nameof(HasMessageBox));
            }
        }

        /// <summary>
        /// Gets a value indicating whether a message box is available.
        /// </summary>
        public bool HasMessageBox
        {
            get
            {
                return CurrentMessageBox != null;
            }
        }

        /// <summary>
        /// Shows an error message box.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public Task ShowError(string message)
        {
            return ShowMessageBox(new MessageBoxVM()
            {
                Message = message,
                Icon = ResourceHelper.GetImageFromResources("Images/MessageBox Icons/information.png"),
                Title = "Error",
                Brush = Application.Current.Resources["TangoMessageBoxErrorBrush"] as Brush,
            });
        }

        /// <summary>
        /// Shows an information message box.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public Task ShowInfo(string message)
        {
            return ShowMessageBox(new MessageBoxVM()
            {
                Message = message,
                Icon = ResourceHelper.GetImageFromResources("Images/MessageBox Icons/information.png"),
                Title = "Information",
                Brush = Application.Current.Resources["TangoMessageBoxInfoBrush"] as Brush,
            });
        }

        /// <summary>
        /// Shows warning message box.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public Task ShowWarning(string message)
        {
            return ShowMessageBox(new MessageBoxVM()
            {
                Message = message,
                Icon = ResourceHelper.GetImageFromResources("Images/MessageBox Icons/information.png"),
                Title = "Warning",
                Brush = Application.Current.Resources["TangoMessageBoxWarningBrush"] as Brush,
            });
        }

        /// <summary>
        /// Shows a question message box.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public Task<bool> ShowQuestion(string message)
        {
            return ShowMessageBox(new MessageBoxVM()
            {
                Message = message,
                Icon = ResourceHelper.GetImageFromResources("Images/MessageBox Icons/information.png"),
                Title = "Confirm",
                HasCancel = true,
                Brush = Application.Current.Resources["TangoMessageBoxQuestionBrush"] as Brush,
            });
        }

        /// <summary>
        /// Shows the message box.
        /// </summary>
        /// <param name="vm">The view model.</param>
        /// <returns></returns>
        private Task<bool> ShowMessageBox(MessageBoxVM vm)
        {
            TaskCompletionSource<bool> source = new TaskCompletionSource<bool>();

            vm.Accepted += () => { OnMessageBoxClosed(); source.SetResult(true); };
            vm.Canceled += () => { OnMessageBoxClosed(); source.SetResult(false); };

            if (CurrentMessageBox == null)
            {
                CurrentMessageBox = vm;
            }
            else
            {
                _pendingMessageBoxes.Enqueue(new PendingMessageBox()
                {
                    VM = vm,
                    CompletionSource = source,
                });
            }

            return source.Task;
        }

        /// <summary>
        /// Called when the message box has been closed.
        /// </summary>
        private void OnMessageBoxClosed()
        {
            CurrentMessageBox = null;

            if (_pendingMessageBoxes.Count > 0)
            {
                PendingMessageBox p = null;
                if (_pendingMessageBoxes.TryDequeue(out p))
                {
                    CurrentMessageBox = p.VM;
                }
            }
        }

        /// <summary>
        /// Inserts the notification item to the bottom of the notifications collection.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public NotificationItem PushNotification(NotificationItem item)
        {
            return PushNotification(item, null, false, null);
        }

        /// <summary>
        /// Inserts the notification item to the bottom of the notifications collection.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="condition">A condition which determines if this item is still relevant.</param>
        /// <param name="autoCheck">Determines whether to perform automatic checking of the condition.</param>
        /// <param name="checkInterval">Determines how frequently the condition function will be invoked. (Default 1 second)</param>
        /// <returns></returns>
        public NotificationItem PushNotification(NotificationItem item, Func<bool> condition, bool autoCheck = true, TimeSpan? checkInterval = default(TimeSpan?))
        {
            item.Condition = condition;
            item.AutoCheck = autoCheck;
            item.AutoCheckInterval = checkInterval != null ? checkInterval.Value : TimeSpan.FromSeconds(1);
            item.RemoveAction = () => { PopNotification(item); };
            NotificationItems.Insert(0, item);

            if (autoCheck && condition != null)
            {
                Timer timer = new Timer();
                item.Timer = timer;
                timer.Interval = item.AutoCheckInterval.TotalMilliseconds;
                timer.Elapsed += (x, y) =>
                {
                    if (!item.Condition())
                    {
                        PopNotification(item);
                    }
                };
                timer.Start();
            }

            RaisePropertyChanged(nameof(HasNotificationItems));

            return item;
        }

        /// <summary>
        /// Removed the specified notification item.
        /// </summary>
        /// <param name="item">The item.</param>
        public void PopNotification(NotificationItem item)
        {
            if (item.Timer != null)
            {
                item.Timer.Stop();
            }

            NotificationItems.Remove(item);

            RaisePropertyChanged(nameof(HasNotificationItems));
        }

        /// <summary>
        /// Invokes the notification items conditions.
        /// </summary>
        public void InvokeNotificationItemsConditions()
        {
            var list = NotificationItems.ToList();

            foreach (var item in list.Where(x => x.Condition != null))
            {
                if (!item.Condition())
                {
                    PopNotification(item);
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance has notification items.
        /// </summary>
        public bool HasNotificationItems
        {
            get
            {
                return NotificationItems.Count > 0;
            }
        }

        public RelayCommand<NotificationItem> PopNotificationCommand { get; private set; }
    }
}