private static async Task <Post> GetPostAtAsync(FilteredCollection <Post, Posts> posts, int postPointer) { while ((postPointer >= posts.Count) && (posts.HasMoreItems)) { await posts.LoadMoreItemsAsync(1); } return(posts[postPointer]); }
public static async Task SetTileAsync(FilteredCollection <Post, Posts> posts) { List <string> faces = new List <string>(); int postPointer = 0; // Remove all old face file first try { var imageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(TILE_FOLDER_NAME, CreationCollisionOption.ReplaceExisting); } catch (Exception ex) { } // Download up to 20 faces while (faces.Count < 20 && ((posts.Count - 1 > postPointer) || (posts.HasMoreItems))) { if ((posts.Count - 1 < postPointer) && (posts.HasMoreItems)) { // Need to download more posts data await posts.LoadMoreItemsAsync(1); } else { // No need to download more posts data, read next post directly Post post = await GetPostAtAsync(posts, postPointer); var previewBuffer = await(new Windows.Web.Http.HttpClient()).GetBufferAsync(new Uri(post.PreviewUrl)); var rects = (await DetechFromBufferAsync(previewBuffer, 5, 55)).Faces; await SaveFacesForTileAsync(rects, previewBuffer, post.Id); foreach (var rect in rects) { faces.Add($"{post.Id}-{rects.IndexOf(rect)}.jpg"); } postPointer++; } } // Update the rile notification if (true) { UpdateTile(faces); } }
public static async Task <string> SetBackgroundImageAsync(FilteredCollection <Post, Posts> posts, uint postShuffle, CropMethod method, Size screenSize, bool isLockscreen, int qualityLevel) { var folderName = isLockscreen ? LOCKSCREEN_FOLDER_NAME : WALLPAPER_FOLDER_NAME; Random rnd = new Random(); int pointer = rnd.Next((int)postShuffle); // Load as many post as possible until reaching the pointer while ((pointer >= posts.Count) && (posts.HasMoreItems)) { await posts.LoadMoreItemsAsync(1); } if (posts.Count == 0) { // No result at all, cannot update background return(""); } if (posts.Count - 1 < pointer) { // Not enough posts to shuffle, shuffle for a lower amount of posts pointer = rnd.Next((int)posts.Count - 1); } // Select a post var post = posts[pointer]; var previewBuffer = await(new Windows.Web.Http.HttpClient()).GetBufferAsync(new Uri(post.PreviewUrl)); var largeBufferUrl = ""; int largeWidth = 0; int largeHeight = 0; switch (qualityLevel) { case 2: if (!String.IsNullOrEmpty(post.FileUrl)) { largeBufferUrl = post.FileUrl; largeWidth = post.Width; largeHeight = post.Height; } else { largeBufferUrl = post.JpegUrl; largeWidth = post.JpegWidth; largeHeight = post.JpegHeight; } break; case 1: largeBufferUrl = post.JpegUrl; largeWidth = post.JpegWidth; largeHeight = post.JpegHeight; break; default: largeBufferUrl = post.SampleUrl; largeWidth = post.SampleWidth; largeHeight = post.SampleHeight; break; } // Try to delete all files of previous images var imageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists); var imageFiles = await imageFolder.GetFilesAsync(); foreach (var oldImageFile in imageFiles) { try { await oldImageFile.DeleteAsync(StorageDeleteOption.PermanentDelete); } catch (Exception ex) { } } // Downlaod the image and create a copy ready to crop var largeBuffer = await(new Windows.Web.Http.HttpClient()).GetBufferAsync(new Uri(largeBufferUrl)); var imageFile = await imageFolder.CreateFileAsync($"{post.Id}.jpg", CreationCollisionOption.ReplaceExisting); await FileIO.WriteBufferAsync(imageFile, largeBuffer); var jpegFile = await imageFolder.CreateFileAsync($"{post.Id}-original.jpg", CreationCollisionOption.ReplaceExisting); await FileIO.WriteBufferAsync(jpegFile, largeBuffer); // Crop the image var imageSize = new Size(largeWidth, largeHeight); await CropImageFile(imageFile, imageSize, method, screenSize, previewBuffer); var id = ""; if (UserProfilePersonalizationSettings.IsSupported()) { if (isLockscreen) { var b = await UserProfilePersonalizationSettings.Current.TrySetLockScreenImageAsync(imageFile); if (b) { using (var db = new AppDbContext()) { db.LockScreenRecords.Add(LockScreenRecord.Create(post)); db.SaveChanges(); } id = post.Id.ToString(); } } else { var b = await UserProfilePersonalizationSettings.Current.TrySetWallpaperImageAsync(imageFile); if (b) { using (var db = new AppDbContext()) { db.WallpaperRecords.Add(WallpaperRecord.Create(post)); db.SaveChanges(); } id = post.Id.ToString(); } } } // Return the latest ID if there is an update return(id); }