-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
NotificationManager.cs
144 lines (135 loc) · 5.27 KB
/
NotificationManager.cs
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
using BreadPlayer.Core.Common;
using BreadPlayer.Core.Models;
using BreadPlayer.Dispatcher;
using BreadPlayer.Interfaces;
using BreadPlayer.Services;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.Data.Xml.Dom;
using Windows.Foundation.Metadata;
using Windows.UI.Core;
using Windows.UI.Notifications;
using Windows.UI.Popups;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
namespace BreadPlayer.NotificationManager
{
public class BreadNotificationManager : ObservableObject, INotificationManager
{
private Queue<string> NotificationQueue => GSingleton<Queue<string>>.Instance.Singleton;
private ICommand _closeCommand;
private DispatcherTimer _hideTimer;
private string _status = string.Empty;
private StatusBar statusBar;
public ICommand CloseCommand
{
get => _closeCommand ?? (_closeCommand = new DelegateCommand(HideStaticMessage));
}
public string Status
{
get => _status;
set => Set(ref _status, value);
}
private bool _show;
public bool Show
{
get => _show;
set => Set(ref _show, value);
}
public void HideStaticMessage()
{
HideTimer_Tick(_hideTimer, null);
}
public void ShowStaticMessage(string message)
{
Status = message;
Show = true;
}
public async Task ShowMessageAsync(string status, int duration = 10)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
if (!Show)
{
Status = status;
Show = true;
if (duration > 0)
{
_hideTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(duration) };
_hideTimer.Start();
_hideTimer.Tick += HideTimer_Tick;
}
}
else
{
NotificationQueue.Enqueue(status);
}
});
}
public async Task ShowMessageBoxAsync(string message, string title)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
var dialog = new MessageDialog(message, title);
await dialog.ShowAsync();
});
}
public void SendUpcomingSongNotification(IMediafile mediaFile)
{
if (mediaFile != null)
{
ToastNotificationManager.History.Clear();
var notificationXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
IXmlNode toastNode = notificationXml.SelectSingleNode("/toast");
((XmlElement)toastNode).SetAttribute("launch", "NowPlaying.xaml");
XmlElement audio = notificationXml.CreateElement("audio");
audio.SetAttribute("silent", "True"); //Here
toastNode.AppendChild(audio);
var toeastElement = notificationXml.GetElementsByTagName("text");
toeastElement[0].AppendChild(notificationXml.CreateTextNode("Upcoming Song"));
toeastElement[1].AppendChild(notificationXml.CreateTextNode(mediaFile.Title));
toeastElement[2].AppendChild(notificationXml.CreateTextNode(mediaFile.LeadArtist));
var imageElement = notificationXml.GetElementsByTagName("image");
imageElement[0].Attributes[1].NodeValue = mediaFile.AttachedPicture ?? "ms-appx:///Assets/albumart.png";
var toastNotification = new ToastNotification(notificationXml)
{
Group = "upcoming-song"
};
ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
}
}
private async void HideTimer_Tick(object sender, object e)
{
await BreadDispatcher.InvokeAsync(() =>
{
Status = string.Empty;
Show = false;
_hideTimer?.Stop();
});
if (NotificationQueue.Count > 0)
{
await ShowMessageAsync(NotificationQueue.Dequeue()).ConfigureAwait(false);
}
}
public async Task ShowStatusBarMessageAsync(string message)
{
if (ApiInformation.IsApiContractPresent("Windows.Phone.PhoneContract", 1))
{
await BreadDispatcher.InvokeAsync(async () =>
{
if(statusBar == null)
statusBar = Windows.UI.ViewManagement.StatusBar.GetForCurrentView();
await statusBar.ShowAsync();
statusBar.ProgressIndicator.Text = message;
await statusBar.ProgressIndicator.ShowAsync();
await Task.Delay(3000);
await statusBar.ProgressIndicator.HideAsync();
await statusBar.HideAsync();
});
}
}
}
}