Optimization, refactoring, debugging
Related Work Items: #251, #252, #261
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
using Google.Apis.Services;
|
||||
using Google.Apis.YouTube.v3;
|
||||
using Google.Apis.YouTube.v3.Data;
|
||||
using Microsoft.AppCenter.Analytics;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
using Windows.ApplicationModel.Background;
|
||||
@@ -17,8 +17,9 @@ namespace FoxTube.Background
|
||||
public sealed class BackgroundProcessor : IBackgroundTask
|
||||
{
|
||||
private DateTime lastCheck = DateTime.Now;
|
||||
private readonly ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
|
||||
private YouTubeService Service => new YouTubeService(new BaseClientService.Initializer()
|
||||
private readonly ApplicationDataContainer settings = ApplicationData.Current.RoamingSettings;
|
||||
dynamic prefs;
|
||||
private readonly YouTubeService Service = new YouTubeService(new BaseClientService.Initializer()
|
||||
{
|
||||
ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0",
|
||||
ApplicationName = "FoxTube"
|
||||
@@ -30,21 +31,20 @@ namespace FoxTube.Background
|
||||
try
|
||||
{
|
||||
def = taskInstance.GetDeferral();
|
||||
taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);
|
||||
|
||||
if (settings.Values["lastCheck"] == null)
|
||||
{
|
||||
settings.Values.Add("lastCheck", DateTime.Now.ToString());
|
||||
settings.Values["lastCheck"] = DateTime.Now.ToString();
|
||||
def.Complete();
|
||||
return;
|
||||
}
|
||||
else
|
||||
lastCheck = DateTime.Parse(settings.Values["lastCheck"] as string);
|
||||
|
||||
bool[] notificationsSettings = JsonConvert.DeserializeObject<bool[]>(await FileIO.ReadTextAsync(await ApplicationData.Current.RoamingFolder.GetFileAsync("notifications.json")));
|
||||
if (notificationsSettings[0])
|
||||
prefs = JsonConvert.DeserializeObject<dynamic>(settings.Values["settings"] as string);
|
||||
if ((bool)prefs.devNotifications)
|
||||
CheckAnnouncements();
|
||||
if (notificationsSettings[1])
|
||||
if ((bool)prefs.videoNotifications)
|
||||
await CheckAccount();
|
||||
}
|
||||
finally
|
||||
@@ -54,20 +54,11 @@ namespace FoxTube.Background
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
|
||||
{
|
||||
Analytics.TrackEvent("Background task caneled", new Dictionary<string, string>()
|
||||
{
|
||||
{ "Reason", reason.ToString() }
|
||||
});
|
||||
settings.Values["lastCheck"] = DateTime.Now.ToString();
|
||||
}
|
||||
|
||||
async Task CheckAccount()
|
||||
{
|
||||
try
|
||||
{
|
||||
Dictionary<string, string> subscriptions = JsonConvert.DeserializeObject<Dictionary<string, string>>(await FileIO.ReadTextAsync(await ApplicationData.Current.RoamingFolder.GetFileAsync("background.json")));
|
||||
Dictionary<string, string> subscriptions = JsonConvert.DeserializeObject<Dictionary<string, string>>(settings.Values["subscriptions"] as string);
|
||||
|
||||
List<SearchResult> results = new List<SearchResult>();
|
||||
|
||||
@@ -94,25 +85,28 @@ namespace FoxTube.Background
|
||||
TileUpdater updater = TileUpdateManager.CreateTileUpdaterForApplication();
|
||||
updater.EnableNotificationQueue(true);
|
||||
updater.Clear();
|
||||
for (int i = 0; i < 5; i++)
|
||||
updater.Update(Tiles.GetTileLayout(results[i].Snippet.Title, results[i].Snippet.ChannelTitle, results[i].Snippet.Thumbnails.Medium.Url, subscriptions[results[i].Snippet.ChannelId]));
|
||||
for (int i = 0; i < 5 && i < results.Count; i++)
|
||||
updater.Update(Tiles.GetTileLayout(System.Security.SecurityElement.Escape(results[i].Snippet.Title), System.Security.SecurityElement.Escape(results[i].Snippet.ChannelTitle), results[i].Snippet.Thumbnails.Medium.Url.Replace("&", "%26"), subscriptions[results[i].Snippet.ChannelId]));
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
void CheckAnnouncements()
|
||||
async void CheckAnnouncements()
|
||||
{
|
||||
try
|
||||
{
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.Load(XmlReader.Create("http://foxgame-studio.000webhostapp.com/foxtube-messages.xml"));
|
||||
if ((DateTime.Parse((doc["posts"].FirstChild as XmlElement).GetAttribute("time")) - lastCheck).TotalSeconds > 0)
|
||||
doc.LoadXml(await new HttpClient().GetStringAsync("http://foxgame-studio.000webhostapp.com/foxtube-messages.xml"));
|
||||
XmlElement item = doc["posts"].FirstChild as XmlElement;
|
||||
|
||||
DateTime date = DateTime.Parse(item.GetAttribute("time"));
|
||||
if (date > lastCheck && date < DateTime.Now)
|
||||
ToastNotificationManager.CreateToastNotifier().Show(
|
||||
Notification.GetInternalToast(doc["posts"].FirstChild["id"].InnerText,
|
||||
doc["posts"].FirstChild["header"].InnerText,
|
||||
doc["posts"].FirstChild["content"].InnerText,
|
||||
doc["posts"].FirstChild["thumbnail"].InnerText,
|
||||
doc["posts"].FirstChild["avatar"].InnerText));
|
||||
Notification.GetInternalToast(item["id"].InnerText,
|
||||
item["header"][(string)prefs.language].InnerText,
|
||||
item["content"][(string)prefs.language].InnerText,
|
||||
item["thumbnail"].InnerText,
|
||||
item["avatar"].InnerText));
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
@@ -8,12 +8,12 @@ namespace FoxTube.Background
|
||||
{
|
||||
public static class Notification
|
||||
{
|
||||
private static Dictionary<string, string> languagePack = LoadPack();
|
||||
private static readonly Dictionary<string, string> languagePack = LoadPack();
|
||||
|
||||
private static Dictionary<string, string> LoadPack()
|
||||
{
|
||||
object[] saved = JsonConvert.DeserializeObject<object[]>(ApplicationData.Current.RoamingSettings.Values["settings"] as string);
|
||||
if (saved[7] as string == "ru-RU")
|
||||
dynamic saved = JsonConvert.DeserializeObject<dynamic>(ApplicationData.Current.RoamingSettings.Values["settings"] as string);
|
||||
if (saved.language as string == "ru-RU")
|
||||
return new Dictionary<string, string>()
|
||||
{
|
||||
{ "addLater", "Посмотреть позже" },
|
||||
@@ -60,8 +60,8 @@ namespace FoxTube.Background
|
||||
<binding template='ToastGeneric'>
|
||||
<image placement='hero' src='{thumbnail.Replace("&", "%26")}'/>
|
||||
<image placement='appLogoOverride' hint-crop='circle' src='{avatar.Replace("&", "%26") ?? "http://foxgame-studio.000webhostapp.com/FoxTubeAssets/LogoAvatar.png"}'/>
|
||||
<text>{title}</text>
|
||||
<text>{channel} {languagePack["videoContent"]}</text>
|
||||
<text>{System.Security.SecurityElement.Escape(title)}</text>
|
||||
<text>{System.Security.SecurityElement.Escape(channel)} {languagePack["videoContent"]}</text>
|
||||
</binding>
|
||||
</visual>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user