/// <summary> /// Callback when we get the image. /// </summary> /// <param name="response"></param> public async void OnRequestComplete(object sender, ImageManager.ImageManagerResponseEventArgs response) { // Remove the event ImageManager.ImageManagerRequest request = (ImageManager.ImageManagerRequest)sender; request.OnRequestComplete -= OnRequestComplete; // Jump back to the UI thread await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (!response.Success) { App.BaconMan.TelemetryMan.ReportUnExpectedEvent(this, "BasicImageControlNoImageUrl"); m_base.FireOnFallbackToBrowser(); return; } lock (this) { if (m_base.IsDestoryed) { // Get out of here if we should be destroyed. return; } // Set the current size of the control, this will also be set // by the size changed listener but we want to make sure it is correct. m_currentControlSize.Height = ui_contentRoot.ActualHeight; m_currentControlSize.Width = ui_contentRoot.ActualWidth; // Grab the source, we need this to make the image m_imageSourceStream = response.ImageStream; // Add the image to the UI m_image = new Image(); // Add a loaded listener so we can size the image when loaded m_image.Loaded += Image_Loaded; // Set the image. ReloadImage(false); // Set the image into the UI. ui_scrollViewer.Content = m_image; // Setup the save image tap m_image.RightTapped += ContentRoot_RightTapped; m_image.Holding += ContentRoot_Holding; m_base.FireOnLoading(false); } }); }
public async void OnRequestComplete(object sender, ImageManager.ImageManagerResponseEventArgs response) { // Remove event ImageManager.ImageManagerRequest request = (ImageManager.ImageManagerRequest)sender; request.OnRequestComplete -= OnRequestComplete; // Make sure we were successfully. if (!response.Success) { return; } // Get a unique name for this image int imageCount = 0; lock (m_getImageLock) { imageCount = m_imageReturnCount; m_imageReturnCount++; } // Get the file name string imageFileName = imageCount + ".jpg"; try { // Write the file await WriteImageToFile(response.ImageStream, imageFileName, m_isCurrentImageRequestLockScreen); } catch (Exception e) { m_baconMan.MessageMan.DebugDia("Failed to write background image", e); m_baconMan.TelemetryMan.ReportUnExpectedEvent(this, "Failed to write background image", e); } // Add the file to the list lock (m_getImageLock) { // Report we are done m_imageDoneCount++; // Check to see if we were last if (m_imageDoneCount == c_maxImageCacheCount) { m_autoReset.Set(); } } }
/// <summary> /// Callback when we get the image. /// </summary> /// <param name="response"></param> public async void OnRequestComplete(object sender, ImageManager.ImageManagerResponseEventArgs response) { // Remove the event ImageManager.ImageManagerRequest request = (ImageManager.ImageManagerRequest)sender; request.OnRequestComplete -= OnRequestComplete; // Jump back to the UI thread await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (!response.Success) { App.BaconMan.TelemetryMan.ReportUnExpectedEvent(this, "BasicImageControlNoImageUrl"); m_host.FallbackToWebBrowser(m_post); return; } lock (m_host) { if (m_isDestoryed) { // Get out of here if we should be destroyed. return; } // Create a bitmap and set the source // #todo, initially we can use a bitmap here decoded to the size of the control // then when the user zooms in we can swap it for a larger image. BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(response.ImageStream); // Add the image to the UI m_image = new Image(); // Add a loaded listener so we can size the image when loaded m_image.Loaded += Image_Loaded; // Set the image. m_image.Source = bitmapImage; // Set the image into the UI. ui_scrollViewer.Content = m_image; // Setup the save image tap m_image.RightTapped += ContentRoot_RightTapped; m_image.Holding += ContentRoot_Holding; } }); }
/// <summary> /// Callback when we get the image. /// </summary> /// <param name="response"></param> public async void OnRequestComplete(object sender, ImageManager.ImageManagerResponseEventArgs response) { // Remove the event ImageManager.ImageManagerRequest request = (ImageManager.ImageManagerRequest)sender; request.OnRequestComplete -= OnRequestComplete; // Jump back to the UI thread await global::Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (!response.Success) { m_base.FireOnFallbackToBrowser(); return; } lock (this) { if (m_base.IsDestoryed) { // Get out of here if we should be destroyed. return; } // Grab the source, we need this to make the image m_imageSourceStream = response.ImageStream; // Add the image to the UI m_image = new Image(); // We don't want to wait on this. #pragma warning disable CS4014 // Set the image. ReloadImage(false); #pragma warning restore CS4014 // Set the image into the UI. ui_scrollViewer.Content = m_image; // Setup the save image tap m_image.RightTapped += ContentRoot_RightTapped; m_image.Holding += ContentRoot_Holding; m_image.Width = ui_scrollViewer.ViewportWidth; m_image.Height = ui_scrollViewer.ViewportHeight; } }); }
/// <summary> /// Callback when we get the image. /// </summary> /// <param name="sender"></param> /// <param name="response"></param> private async void OnRequestComplete(object sender, ImageManager.ImageManagerResponseEventArgs response) { // Remove the event var request = (ImageManager.ImageManagerRequest)sender; request.OnRequestComplete -= OnRequestComplete; // Jump back to the UI thread await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (!response.Success) { TelemetryManager.ReportUnexpectedEvent(this, "BasicImageControlNoImageUrl"); _baseContentPanel.FireOnFallbackToBrowser(); return; } lock (this) { if (_baseContentPanel.IsDestroyed) { // Get out of here if we should be destroyed. return; } // Grab the source, we need this to make the image _imageSourceStream = response.ImageStream; // Add the image to the UI _image = new Image(); // We don't want to wait on this. #pragma warning disable CS4014 // Set the image. ReloadImage(false); #pragma warning restore CS4014 // Set the image into the UI. ui_scrollViewer.Content = _image; // Setup the save image tap _image.RightTapped += ContentRoot_RightTapped; _image.Holding += ContentRoot_Holding; } }); }
/// <summary> /// Callback when we get the image. /// </summary> /// <param name="response"></param> public async void OnRequestComplete(object sender, ImageManager.ImageManagerResponseEventArgs response) { // Remove the event ImageManager.ImageManagerRequest request = (ImageManager.ImageManagerRequest)sender; request.OnRequestComplete -= OnRequestComplete; // Jump back to the UI thread await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (!response.Success) { App.BaconMan.TelemetryMan.ReportUnExpectedEvent(this, "BasicImageControlNoImageUrl"); m_host.ShowError(); return; } lock (m_host) { if (m_isDestoryed) { // Get out of here if we should be destroyed. return; } // Create a bitmap and set the source BitmapImage bitImage = new BitmapImage(); bitImage.SetSource(response.ImageStream); // Add the image to the UI m_image = new Image(); m_image.Source = bitImage; ui_contentRoot.Children.Add(m_image); // Setup the save image tap m_image.RightTapped += ContentRoot_RightTapped; m_image.Holding += ContentRoot_Holding; // Hide the loading screen m_host.HideLoading(); // Show the image ui_storyContentRoot.Begin(); } }); }
public async void OnRequestComplete(object sender, ImageManager.ImageManagerResponseEventArgs response) { // Remove the event var request = (ImageManager.ImageManagerRequest)sender; request.OnRequestComplete -= OnRequestComplete; // Make sure we were successful. if (response.Success) { // Try to find the post Post owningPost = null; // Lock the list lock (_postsLists) { foreach (var post in _postsLists) { if (post.Id.Equals(response.Request.ImageId)) { owningPost = post; break; } } } // If we didn't find it just return if (owningPost == null) { return; } // Dispatch to the UI thread await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Create a bitmap and set the source owningPost.Image = new BitmapImage(); owningPost.Image.SetSource(response.ImageStream); // Set the image visible owningPost.ImageVisibility = Visibility.Visible; }); } }
/// <summary> /// Fired when an image request is done. /// </summary> /// <param name="sender"></param> /// <param name="response"></param> public async void OnRequestComplete(object sender, ImageManager.ImageManagerResponseEventArgs response) { // Remove event listener ImageManager.ImageManagerRequest request = (ImageManager.ImageManagerRequest)sender; request.OnRequestComplete -= OnRequestComplete; UpdateTypes type = (UpdateTypes)response.Request.Context; // Make sure we were successfully. if (response.Success) { try { // Get the target size Size targetImageSize = LastKnownScreenResoultion; if (type == UpdateTypes.Band) { if (m_baconMan.BackgroundMan.BandMan.BandVersion == BandVersions.V1) { targetImageSize = new Size(310, 102); } else { targetImageSize = new Size(310, 128); } } // Resize the image to fit nicely InMemoryRandomAccessStream image = await ResizeImage(response.ImageStream, targetImageSize); // Write the file await WriteImageToFile(image, type); } catch (Exception e) { m_baconMan.MessageMan.DebugDia("Failed to write background image", e); m_baconMan.TelemetryMan.ReportUnExpectedEvent(this, "Failed to write background image", e); } } // Indicate that this image is done. bool isDone = false; lock (m_getImageLock) { if (type == UpdateTypes.LockScreen) { m_lockScreenDoneCount++; isDone = m_lockScreenDoneCount >= m_lockScreenRequestCount; } else if (type == UpdateTypes.Band) { m_bandDoneCount++; isDone = m_bandDoneCount >= m_bandRequestCount; } else { m_desktopDoneCount++; isDone = m_desktopDoneCount >= m_desktopRequestCount; } } // if we are done done then tell the images to rotate and clean up. if (isDone) { // Set this high so we roll over. if (type == UpdateTypes.LockScreen) { CurrentLockScreenRotationIndex = 99; } else if (type == UpdateTypes.Band) { CurrentBandRotationIndex = 99; } else { CurrentDesktopRotationIndex = 99; } // Do the rotate await DoImageRotation(type); // If this is a lock screen update this might also be a desktop update. This happens when the lock screen and desktop // share the same subreddit, they share the same images. if (type == UpdateTypes.LockScreen && IsDeskopEnabled && LockScreenSubredditName.Equals(DesktopSubredditName)) { // Off set the two so they don't show the same image CurrentDesktopRotationIndex = 1; // We also need to update the desktop. await DoImageRotation(UpdateTypes.Desktop); } // We are done, set the last update time LastFullUpdate = DateTime.Now; // And kill the deferral ReleaseDeferral(type); // And set us to stopped. UnSetIsRunningIfDone(); } }
/// <summary> /// Fired when an image request is done. /// </summary> /// <param name="sender"></param> /// <param name="response"></param> private async void OnRequestComplete(object sender, ImageManager.ImageManagerResponseEventArgs response) { // Remove event listener var request = (ImageManager.ImageManagerRequest)sender; request.OnRequestComplete -= OnRequestComplete; var type = (UpdateTypes)response.Request.Context; // Make sure we were successfully. if (response.Success) { try { // Get the target size var targetImageSize = LastKnownScreenResolution; switch (type) { case UpdateTypes.Band when _baconMan.BackgroundMan.BandMan.BandVersion == BandVersions.V1: targetImageSize = new Size(310, 102); break; case UpdateTypes.Band: targetImageSize = new Size(310, 128); break; case UpdateTypes.Desktop when DeviceHelper.CurrentDevice() == DeviceTypes.Mobile: // If we are desktop on mobile we want to do a bit larger than the screen res because // there is a sliding image animation when you switch to all apps. Lets make the width 30% larger. targetImageSize.Width *= 1.3; break; case UpdateTypes.All: break; case UpdateTypes.LockScreen: break; default: throw new ArgumentOutOfRangeException(); } // Resize the image to fit nicely var image = await ResizeImage(response.ImageStream, targetImageSize); // Write the file await WriteImageToFile(image, type); } catch (Exception e) { _baconMan.MessageMan.DebugDia("Failed to write background image", e); TelemetryManager.ReportUnexpectedEvent(this, "Failed to write background image", e); } } // Indicate that this image is done. var isDone = false; lock (_getImageLock) { switch (type) { case UpdateTypes.LockScreen: _lockScreenDoneCount++; isDone = _lockScreenDoneCount >= _lockScreenRequestCount; break; case UpdateTypes.Band: _bandDoneCount++; isDone = _bandDoneCount >= _bandRequestCount; break; case UpdateTypes.All: break; case UpdateTypes.Desktop: break; default: _desktopDoneCount++; isDone = _desktopDoneCount >= _desktopRequestCount; break; } } // if we are done done then tell the images to rotate and clean up. if (!isDone) { return; } switch (type) { // Set this high so we roll over. case UpdateTypes.LockScreen: CurrentLockScreenRotationIndex = 99; break; case UpdateTypes.Band: CurrentBandRotationIndex = 99; break; case UpdateTypes.All: break; case UpdateTypes.Desktop: break; default: CurrentDesktopRotationIndex = 99; break; } // Do the rotate await DoImageRotation(type); // If this is a lock screen update this might also be a desktop update. This happens when the lock screen and desktop // share the same subreddit, they share the same images. if (type == UpdateTypes.LockScreen && IsDesktopEnabled && LockScreenSubredditName.Equals(DesktopSubredditName)) { // Off set the two so they don't show the same image CurrentDesktopRotationIndex = 1; // We also need to update the desktop. await DoImageRotation(UpdateTypes.Desktop); } // We are done, set the last update time LastFullUpdate = DateTime.Now; // And kill the deferral ReleaseDeferral(type); // And set us to stopped. UnSetIsRunningIfDone(); }