using FoxTube.Models; using FoxTube.Utils; using Newtonsoft.Json; using System; using System.Net; using System.Net.Http; using System.Threading.Tasks; using Windows.Storage; using Windows.UI.Notifications; namespace FoxTube.Services { public static class Inbox { private static readonly HttpClient client = new HttpClient(); private static readonly ApplicationDataContainer storage = ApplicationData.Current.RoamingSettings; public static async void PushNew() { try { // TODO: Add backend HttpResponseMessage response = await client.GetAsync($"https://xfox111.net/FoxTube/Inbox?toast=true&publishedAfter={storage.Values["Inbox.lastCheck"]}&lang={Settings.Language}&appVersion={Metrics.CurrentVersion}"); storage.Values["Inbox.lastCheck"] = 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)); } } public static async Task GetMessages() { try { // TODO: Add backend HttpResponseMessage response = await client.GetAsync($"https://xfox111.net/API/FoxTube/Inbox?lang={Settings.Language}¤tVersion={Metrics.CurrentVersion}"); if (response.StatusCode == HttpStatusCode.NoContent) return new InboxItem[0]; return JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); } catch (Exception e) { Metrics.SendReport(new Exception("Unable to retrieve inbox messages", e)); return new InboxItem[0]; } } /// /// Fires toast notification with the last changelog content /// public static async void PushChangelog() { try { // TODO: Add backend Settings.LastReviewedVersion = Metrics.CurrentVersion; HttpResponseMessage response = await client.GetAsync($"https://xfox111.net/API/FoxTube/Changelog?lang={Settings.Language}&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)); } } } }