Archived
1
0

DownloadAgent

This commit is contained in:
Michael Gordeev
2018-07-11 18:58:05 +03:00
parent 839be10ed2
commit 71cc12106c
39 changed files with 1359 additions and 55 deletions
+67
View File
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Windows.ApplicationModel.Background;
using Windows.Storage;
using Windows.UI.Notifications;
namespace FoxTube.Background
{
public sealed class BackgroundProcessor : IBackgroundTask
{
public List<Notification> Notifications = new List<Notification>();
private DateTime lastCheck;
private ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
BackgroundTaskDeferral def;
public async void Run(IBackgroundTaskInstance taskInstance)
{
XmlDocument doc = new XmlDocument();
def = taskInstance.GetDeferral();
if (settings.Values["notificationsHistory"] != null)
doc.LoadXml(settings.Values["notificationsHistory"] as string);
else
{
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
doc.AppendChild(doc.CreateElement("notifications"));
settings.Values.Add("notificationsHistory", doc.InnerXml);
}
CheckAccount();
CheckAnnouncements();
def.Complete();
}
void CheckAccount()
{
}
void CheckAnnouncements()
{
XmlDocument doc = new XmlDocument();
doc.Load(XmlReader.Create("http://foxgame.hol.es/ftp.xml"));
if ((XmlConvert.ToDateTime((doc["posts"].FirstChild as XmlElement).GetAttribute("time"), XmlDateTimeSerializationMode.Utc) - DateTime.UtcNow).TotalSeconds > 0)
Notifications.Add(new Notification(NotificationType.Internal,
doc["posts"].FirstChild["notificationHeader"].InnerText,
doc["posts"].FirstChild["content"].InnerText,
XmlConvert.ToDateTime((doc["posts"].FirstChild as XmlElement).GetAttribute("time"),
XmlDateTimeSerializationMode.Local), (doc["posts"].FirstChild as XmlElement).GetAttribute("image")));
if ((bool)settings.Values["newmessagesNotification"] == true)
SendNotification(Notifications.Last());
}
void SendNotification(Notification notification)
{
ToastNotificationManager.CreateToastNotifier().Show(notification.GetToast(0));
}
}
}
@@ -0,0 +1,136 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{FC9128D7-E3AA-48ED-8641-629794B88B28}</ProjectGuid>
<OutputType>winmdobj</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>FoxTube.Background</RootNamespace>
<AssemblyName>FoxTube.Background</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.17134.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.15063.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<AllowCrossPlatformRetargeting>false</AllowCrossPlatformRetargeting>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<PlatformTarget>x86</PlatformTarget>
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM'">
<PlatformTarget>ARM</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\ARM\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM'">
<PlatformTarget>ARM</PlatformTarget>
<OutputPath>bin\ARM\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<PlatformTarget>x64</PlatformTarget>
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
<ItemGroup>
<Compile Include="BackgroundProcessor.cs" />
<Compile Include="Notification.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ToastTemplates.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.1.5</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Reference Include="System.Net.WebClient">
<HintPath>..\..\..\..\..\..\Program Files (x86)\Microsoft SDKs\UWPNuGetPackages\microsoft.netcore.universalwindowsplatform\6.1.5\ref\uap10.0.15138\System.Net.WebClient.dll</HintPath>
</Reference>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
+175
View File
@@ -0,0 +1,175 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Xml.Dom;
using Windows.UI;
using Windows.UI.Notifications;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
namespace FoxTube.Background
{
public enum NotificationType
{
Video, Comment, Post, Internal
}
public class Notification
{
public string Channel { get; set; }
public string Content { get; set; }
public DateTime TimeStamp { get; set; }
public NotificationType Type { get; set; }
public string Avatar { get; set; }
public string Thumbnail { get; set; }
public Notification(NotificationType type,
string channelName, string content, DateTime date,
string thumbnailUrl, string avatarUrl = "ms-appx:///Assets/Icons/Profile.png")
{
Channel = channelName;
Content = content;
TimeStamp = date;
Type = type;
Avatar = avatarUrl;
Thumbnail = thumbnailUrl;
}
public UIElement GetNotification()
{
StackPanel stackPanel = new StackPanel() { Margin = new Thickness(10, 0, 0, 0) };
//Channel
switch (Type)
{
case NotificationType.Comment:
stackPanel.Children.Add(new TextBlock()
{
FontSize = 14,
FontStyle = Windows.UI.Text.FontStyle.Italic,
Foreground = new SolidColorBrush(Colors.Gray),
Text = string.Format("{0} replied to your comment", Channel)
});
break;
case NotificationType.Internal:
stackPanel.Children.Add(new TextBlock()
{
FontSize = 14,
FontStyle = Windows.UI.Text.FontStyle.Italic,
Foreground = new SolidColorBrush(Colors.Gray),
Text = Channel
});
break;
case NotificationType.Post:
stackPanel.Children.Add(new TextBlock()
{
FontSize = 14,
FontStyle = Windows.UI.Text.FontStyle.Italic,
Foreground = new SolidColorBrush(Colors.Gray),
Text = string.Format("{0} published new post", Channel)
});
break;
case NotificationType.Video:
stackPanel.Children.Add(new TextBlock()
{
FontSize = 14,
FontStyle = Windows.UI.Text.FontStyle.Italic,
Foreground = new SolidColorBrush(Colors.Gray),
Text = string.Format("{0} uploaded new video", Channel)
});
break;
}
//Content
stackPanel.Children.Add(new TextBlock()
{
TextWrapping = TextWrapping.WrapWholeWords,
Text = Content,
});
//Time
stackPanel.Children.Add(new TextBlock()
{
FontSize = 13,
Foreground = new SolidColorBrush(Colors.Gray),
Text = TimeStamp.ToString()
});
PersonPicture avatar = new PersonPicture()
{
Height = 50,
VerticalAlignment = VerticalAlignment.Top,
ProfilePicture = string.IsNullOrWhiteSpace(Avatar) ? null : new BitmapImage(new Uri(Avatar))
};
Grid grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(50) });
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.Children.Add(avatar);
Grid.SetColumn(stackPanel, 1);
grid.Children.Add(stackPanel);
Button item = new Button()
{
HorizontalAlignment = HorizontalAlignment.Stretch,
HorizontalContentAlignment = HorizontalAlignment.Left,
Background = new SolidColorBrush(Colors.Transparent),
Content = grid
};
return item;
}
public ToastNotification GetToast(int assignedId)
{
XmlDocument template = new XmlDocument();
switch (Type)
{
case NotificationType.Comment:
template.LoadXml($@"<toast launch='type=comment&amp;action=open&amp;id={assignedId}'>
<visual>
<binding template='ToastGeneric'>
<image placement='appLogoOverride' hint-crop='circle' src='{Avatar}'/>
<text>{Channel} posted a new comment</text>
<text>{Content}</text>
</binding>
</visual>
<actions>
<input id='textBox' type='text' placeHolderContent='Send a reply'/>
<action content='Send' imageUri='Assets/Icons/Send.png'
hint-inputId='textBox' activationType='background'
arguments='type=comment&amp;action=sendReply&amp;id={assignedId}'/>
<action content='Like'
arguments='type=comment&amp;action=like&amp;id={assignedId}'/>
</actions>
</toast>");
break;
case NotificationType.Video:
template.LoadXml($@"<toast launch='action=viewPhoto&amp;photoId=92187'>
<visual>
<binding template='ToastGeneric'>
<image placement='appLogoOverride' hint-crop='circle' src='{Avatar}'/>
<text>{Channel} uploaded a new video</text>
<text>{Content}</text>
<image src='{Thumbnail}'/>
</binding>
</visual>
<actions>
<action content='Watch later'
activationType='background'
arguments='likePhoto&amp;photoId=92187'/>
<action content='Go to channel'
arguments='action=commentPhoto&amp;photoId=92187'/>
</actions>
</toast>");
@@ -0,0 +1,29 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("FoxTube.Background")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FoxTube.Background")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: ComVisible(false)]
+45
View File
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FoxTube.Background
{
public static class ToastTemplates
{
public static string Comment =
@"<toast launch='action=openThread&amp;threadId=92187'>
<visual>
<binding template='ToastGeneric'>
<image placement='appLogoOverride' hint-crop='circle' src='Assets/Icons/Profile.png'/>
<text>[ChannelName] posted a new comment</text>
<text>[VideoName]</text>
</binding>
</visual>
<actions>
<input id='textBox' type='text' placeHolderContent='Send a reply'/>
<action content='Send' imageUri='Assets/Icons/Send.png'
hint-inputId='textBox' activationType='background'
arguments='action=reply&amp;threadId=92187'/>
<action content='Like'
arguments='action=commentPhoto&amp;photoId=92187'/>
<action content='Go to comment'
arguments='action=commentPhoto&amp;photoId=92187'/>
</actions>
</toast>";
public static string Video =
@"<toast launch='action=viewPhoto&amp;photoId=92187'>
<visual>
<binding template='ToastGeneric'>
<image placement='appLogoOverride' hint-crop='circle' src='Assets/Icons/Profile.png'/>
<text>[ChannelName] uploaded a new video</text>
<text>[VideoName]</text>
<image src='Assets/videoThumbSample.png'/>
</binding>
</visual>
<actions>