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
|
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;
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>
/// 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()
{
_pendingMessageBoxes = new ConcurrentQueue<PendingMessageBox>();
}
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;
}
}
}
}
}
|