aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.UI/Notifications/DefaultNotificationProvider.cs
blob: 02e41e0871fb509800eec678768f7dbc59f16647 (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
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
{
    public class DefaultNotificationProvider : ExtendedObject, INotificationProvider
    {
        private class PendingMessageBox
        {
            public MessageBoxVM VM { get; set; }
            public TaskCompletionSource<bool> CompletionSource { get; set; }
        }

        private ConcurrentQueue<PendingMessageBox> _pendingMessageBoxes;

        public DefaultNotificationProvider()
        {
            _pendingMessageBoxes = new ConcurrentQueue<PendingMessageBox>();
        }

        private MessageBoxVM _currentMessageBox;
        public MessageBoxVM CurrentMessageBox
        {
            get { return _currentMessageBox; }
            private set
            {
                _currentMessageBox = value;
                RaisePropertyChangedAuto();
                RaisePropertyChanged(nameof(HasMessageBox));
            }
        }

        public bool HasMessageBox
        {
            get
            {
                return CurrentMessageBox != null;
            }
        }

        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,
            });
        }

        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,
            });
        }

        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,
            });
        }

        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,
            });
        }

        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;
        }

        private void OnMessageBoxClosed()
        {
            CurrentMessageBox = null;

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