using FoxTube.Models.Collections; using FoxTube.Utils; using Newtonsoft.Json; using System; using System.Net; using System.Net.Http; using System.Threading.Tasks; using Windows.UI.Notifications; namespace FoxTube.Services { public static class InboxService { public const string lastChangelogVersionKey = "Inbox.lastChangelogVersion"; public const string lastCheckKey = "Inbox.lastChangelogVersion"; private static readonly HttpClient client = new HttpClient(); public static InboxCollection GetInboxCollection() => new InboxCollection(); public static async Task PushNew() { try { // TODO: Add backend HttpResponseMessage response = await client.GetAsync($"https://xfox111.net/FoxTube/Inbox?" + $"toast=true&" + $"publishedAfter={Storage.Registry.Values[lastCheckKey]}&" + $"lang={Storage.GetValue(Storage.Settings.UILanguage)}&" + $"appVersion={Metrics.CurrentVersion}"); Storage.Registry.Values[lastCheckKey] = DateTime.UtcNow.Ticks; if (response.StatusCode == HttpStatusCode.NoContent) return; string[] toasts = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); foreach (string toast in toasts) ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toast.ToXml())); } catch (Exception e) { Metrics.SendReport(new Exception("Unable to retrieve toast notifications", e)); } } /// /// Fires toast notification with the last changelog content /// public static async void PushChangelog() { try { // TODO: Add backend if ((string)Storage.Registry.Values[lastChangelogVersionKey] == Metrics.CurrentVersion) return; Storage.Registry.Values[lastChangelogVersionKey] = Metrics.CurrentVersion; HttpResponseMessage response = await client.GetAsync($"https://xfox111.net/API/FoxTube/Changelog?" + $"lang={Storage.GetValue(Storage.Settings.UILanguage)}&" + $"version={Metrics.CurrentVersion}"); if (response.StatusCode == HttpStatusCode.NoContent) return; ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification((await response.Content.ReadAsStringAsync()).ToXml())); } catch (Exception e) { Metrics.SendReport(new Exception("Unable to retrieve changelog", e)); } } } }