public async Task <MessageOfTheDay> GetNewMessage() { try { // Make the request. var webResult = await NetworkManager.MakeGetRequest(CMotdUrl); // Get the input stream and json reader. // NOTE!! We are really careful not to use a string here so we don't have to allocate a huge string. var inputStream = await webResult.ReadAsInputStreamAsync(); using (var reader = new StreamReader(inputStream.AsStreamForRead())) using (JsonReader jsonReader = new JsonTextReader(reader)) { // Parse the Json as an object var serializer = new JsonSerializer(); return(await Task.Run(() => serializer.Deserialize <MessageOfTheDay>(jsonReader))); } } catch (Exception e) { _mBaconMan.MessageMan.DebugDia("failed to get motd", e); TelemetryManager.ReportUnexpectedEvent(this, "FailedToGetMotd", e); } return(null); }
/// <summary> /// Returns the current submission post draft /// </summary> /// <returns></returns> public async Task <PostSubmissionDraftData> GetPostSubmissionDraft() { try { // Get the folder and file. var folder = ApplicationData.Current.LocalFolder; var file = await folder.CreateFileAsync(PostDraftFile, CreationCollisionOption.OpenIfExists); // Check that is exists. if (file == null) { return(null); } // Get the data var fileData = await FileIO.ReadTextAsync(file); // Deseralize the data return(JsonConvert.DeserializeObject <PostSubmissionDraftData>(fileData)); } catch (Exception e) { _baconMan.MessageMan.DebugDia("failed to read draft", e); TelemetryManager.ReportUnexpectedEvent(this, "FailedToReadDraftFile", e); return(null); } }
/// <summary> /// Helper that writes objects to the local storage /// </summary> /// <param name="name"></param> /// <param name="obj"></param> public void WriteToLocalSettings <T>(string name, T obj) { try { LocalSettings[name] = JsonConvert.SerializeObject(obj); } catch (Exception e) { _baconMan.MessageMan.DebugDia("failed to write setting " + name, e); TelemetryManager.ReportUnexpectedEvent(this, "failedToWriteSetting" + name, e); } }
public async void ShowRedditDownMessage() { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { try { var showStatus = await ShowYesNoMessage("Reddit is Down", "It looks like reddit is down right now. Go outside for a while and try again in a few minutes.", "Check Reddit's Status", "Go Outside"); if (showStatus.HasValue && showStatus.Value) { _baconMan.ShowGlobalContent("http://www.redditstatus.com/"); } } catch (Exception e) { TelemetryManager.ReportUnexpectedEvent(this, "FailedToShowMessage", e); } }); }
private async void ShowMessage(string content, string title) { // Don't show messages if we are in the background. if (_baconMan.IsBackgroundTask) { return; } await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { try { var message = new MessageDialog(content, title); await message.ShowAsync(); } catch (Exception e) { TelemetryManager.ReportUnexpectedEvent(this, "FailedToShowMessage", e); } }); }
/// <summary> /// Saves a current post to the draft file. /// </summary> /// <param name="data"></param> public async Task <bool> SavePostSubmissionDraft(PostSubmissionDraftData data) { try { // Get the folder and file. var folder = ApplicationData.Current.LocalFolder; var file = await folder.CreateFileAsync(PostDraftFile, CreationCollisionOption.ReplaceExisting); // Serialize the data var fileData = JsonConvert.SerializeObject(data); // Write to the file await FileIO.WriteTextAsync(file, fileData); } catch (Exception e) { _baconMan.MessageMan.DebugDia("failed to write draft", e); TelemetryManager.ReportUnexpectedEvent(this, "FailedToWriteDraftFile", e); return(false); } return(true); }
/// <summary> /// Used to make sure the background task is setup. /// </summary> public async Task EnsureBackgroundSetup() { // Try to find any task that we might have IBackgroundTaskRegistration foundTask = null; foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name.Equals(BackgroundTaskName)) { foundTask = task.Value; } } // For some dumb reason in WinRT we have to ask for permission to run in the background each time // our app is updated.... var currentAppVersion = $"{Package.Current.Id.Version.Build}.{Package.Current.Id.Version.Major}.{Package.Current.Id.Version.Minor}.{Package.Current.Id.Version.Revision}"; if (string.IsNullOrWhiteSpace(AppVersionWithBackgroundAccess) || !AppVersionWithBackgroundAccess.Equals(currentAppVersion)) { // Update current version AppVersionWithBackgroundAccess = currentAppVersion; // Remove access... stupid winrt. BackgroundExecutionManager.RemoveAccess(); } // Request access to run in the background. var status = await BackgroundExecutionManager.RequestAccessAsync(); LastSystemBackgroundUpdateStatus = (int)status; if (status != BackgroundAccessStatus.AllowedSubjectToSystemPolicy && status != BackgroundAccessStatus.AllowedSubjectToSystemPolicy) { _baconMan.MessageMan.DebugDia("System denied us access from running in the background"); } if (ImageUpdaterMan.IsLockScreenEnabled || ImageUpdaterMan.IsDesktopEnabled || MessageUpdaterMan.IsEnabled) { if (foundTask == null) { // We need to make a new task var builder = new BackgroundTaskBuilder { Name = BackgroundTaskName, TaskEntryPoint = "BaconBackground.BackgroundEntry" }; builder.SetTrigger(new TimeTrigger(BackgroundUpdateTime, false)); builder.SetTrigger(new MaintenanceTrigger(BackgroundUpdateTime, false)); try { builder.Register(); } catch (Exception e) { TelemetryManager.ReportUnexpectedEvent(this, "failed to register background task", e); _baconMan.MessageMan.DebugDia("failed to register background task", e); } } } else { // We need to stop and unregister the background task. foundTask?.Unregister(true); } }