BililiveRecorder/BililiveRecorder.WPF/Pages/AnnouncementPage.xaml.cs

158 lines
5.7 KiB
C#
Raw Permalink Normal View History

2020-12-11 19:22:04 +08:00
using System;
2021-04-20 22:37:29 +08:00
using System.Diagnostics;
2021-01-06 20:42:50 +08:00
using System.Globalization;
2020-12-11 19:22:04 +08:00
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using System.Xaml;
using Microsoft.WindowsAPICodePack.Dialogs;
2021-02-23 18:03:37 +08:00
#nullable enable
2020-12-11 19:22:04 +08:00
namespace BililiveRecorder.WPF.Pages
{
/// <summary>
/// Interaction logic for AnnouncementPage.xaml
/// </summary>
public partial class AnnouncementPage
{
private static readonly HttpClient client;
2021-02-23 18:03:37 +08:00
private static MemoryStream? AnnouncementCache = null;
2020-12-12 21:11:42 +08:00
private static DateTimeOffset AnnouncementCacheTime = DateTimeOffset.MinValue;
2021-01-06 20:42:50 +08:00
internal static CultureInfo CultureInfo = CultureInfo.CurrentUICulture;
2020-12-11 19:22:04 +08:00
static AnnouncementPage()
{
client = new HttpClient();
2021-02-26 21:57:10 +08:00
client.DefaultRequestHeaders.Add("User-Agent", $"BililiveRecorder/{GitVersionInformation.FullSemVer}");
2020-12-11 19:22:04 +08:00
}
public AnnouncementPage()
{
2021-01-04 14:01:11 +08:00
this.InitializeComponent();
2021-02-23 18:03:37 +08:00
_ = this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Func<Task>)(async () => await this.LoadAnnouncementAsync(ignore_cache: false, show_error: false)));
2020-12-11 19:22:04 +08:00
}
2021-05-01 17:57:43 +08:00
#pragma warning disable VSTHRD100 // Avoid async void methods
private async void Button_Click(object sender, RoutedEventArgs e)
#pragma warning restore VSTHRD100 // Avoid async void methods
{
try
{
await this.LoadAnnouncementAsync(ignore_cache: true, show_error: Keyboard.Modifiers.HasFlag(ModifierKeys.Control));
}
catch (Exception) { }
}
2020-12-11 19:22:04 +08:00
private async Task LoadAnnouncementAsync(bool ignore_cache, bool show_error)
{
2021-02-23 18:03:37 +08:00
MemoryStream? data;
2020-12-11 19:22:04 +08:00
bool success;
2021-01-04 14:01:11 +08:00
this.Container.Child = null;
this.Error.Visibility = Visibility.Collapsed;
this.Loading.Visibility = Visibility.Visible;
2020-12-11 19:22:04 +08:00
if (AnnouncementCache is not null && !ignore_cache)
{
data = AnnouncementCache;
success = true;
}
else
{
try
{
2021-06-07 22:05:36 +08:00
var uri = Program.DebugMode
2021-04-20 22:37:29 +08:00
? $"http://rec.127-0-0-1.nip.io/wpf/announcement.php?c={CultureInfo.Name}"
: $"https://rec.danmuji.org/wpf/announcement.xml?c={CultureInfo.Name}";
2021-01-06 20:42:50 +08:00
var resp = await client.GetAsync(uri);
2020-12-11 19:22:04 +08:00
var stream = await resp.EnsureSuccessStatusCode().Content.ReadAsStreamAsync();
var mstream = new MemoryStream();
await stream.CopyToAsync(mstream);
2020-12-12 21:11:42 +08:00
AnnouncementCacheTime = DateTimeOffset.Now;
2020-12-11 19:22:04 +08:00
data = mstream;
success = true;
}
catch (Exception ex)
{
data = null;
success = false;
2021-01-04 14:01:11 +08:00
if (show_error) MessageBox.Show(ex.ToString(), "Loading Error");
2020-12-11 19:22:04 +08:00
}
}
2021-02-23 18:03:37 +08:00
if (success && data is not null)
2020-12-11 19:22:04 +08:00
{
try
{
using var stream = new MemoryStream();
data.Position = 0;
await data.CopyToAsync(stream);
stream.Position = 0;
using var reader = new XamlXmlReader(stream, System.Windows.Markup.XamlReader.GetWpfSchemaContext());
var obj = System.Windows.Markup.XamlReader.Load(reader);
if (obj is UIElement elem)
2021-01-04 14:01:11 +08:00
this.Container.Child = elem;
2020-12-11 19:22:04 +08:00
}
catch (Exception ex)
{
data = null;
success = false;
2021-01-04 14:01:11 +08:00
if (show_error) MessageBox.Show(ex.ToString(), "Loading Error");
2020-12-11 19:22:04 +08:00
}
}
2021-01-04 14:01:11 +08:00
this.Loading.Visibility = Visibility.Collapsed;
2020-12-11 19:22:04 +08:00
if (success)
2020-12-12 21:11:42 +08:00
{
2021-01-04 14:01:11 +08:00
this.RefreshButton.ToolTip = "Load Time: " + AnnouncementCacheTime.ToString("F");
2020-12-11 19:22:04 +08:00
AnnouncementCache = data;
2020-12-12 21:11:42 +08:00
}
2020-12-11 19:22:04 +08:00
else
2020-12-12 21:11:42 +08:00
{
2021-01-04 14:01:11 +08:00
this.RefreshButton.ToolTip = null;
this.Error.Visibility = Visibility.Visible;
2020-12-12 21:11:42 +08:00
}
2020-12-11 19:22:04 +08:00
}
2021-05-01 17:57:43 +08:00
#pragma warning disable VSTHRD100 // Avoid async void methods
2020-12-11 19:22:04 +08:00
private async void Button_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
2021-05-01 17:57:43 +08:00
#pragma warning restore VSTHRD100 // Avoid async void methods
2020-12-11 19:22:04 +08:00
{
if (!Keyboard.Modifiers.HasFlag(ModifierKeys.Control) || !Keyboard.Modifiers.HasFlag(ModifierKeys.Alt)) return;
var fileDialog = new CommonOpenFileDialog()
{
IsFolderPicker = false,
Multiselect = false,
Title = "Load local file",
AddToMostRecentlyUsedList = false,
EnsurePathExists = true,
NavigateToShortcut = true,
};
if (fileDialog.ShowDialog() == CommonFileDialogResult.Ok)
{
try
{
var ms = new MemoryStream();
using (var fs = File.OpenRead(fileDialog.FileName))
{
await fs.CopyToAsync(ms);
}
AnnouncementCache = ms;
2020-12-12 21:11:42 +08:00
AnnouncementCacheTime = DateTimeOffset.Now;
2020-12-11 19:22:04 +08:00
}
catch (Exception ex)
{
2021-01-04 14:01:11 +08:00
MessageBox.Show(ex.ToString(), "Loading Error");
2020-12-11 19:22:04 +08:00
}
2021-01-04 14:01:11 +08:00
await this.LoadAnnouncementAsync(ignore_cache: false, show_error: true);
2020-12-11 19:22:04 +08:00
}
}
}
}