From 63a3ce13e4ca028062eb55797561b7dcd4dafb6d Mon Sep 17 00:00:00 2001 From: laplamgor Date: Mon, 15 Jan 2018 15:12:48 +0800 Subject: [PATCH] Fix a crash when not enough results to shuffle. --- .../Services/AnimePersonalization.cs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/PRPR/BooruViewer/Services/AnimePersonalization.cs b/PRPR/BooruViewer/Services/AnimePersonalization.cs index 709fcfe..f67a62e 100644 --- a/PRPR/BooruViewer/Services/AnimePersonalization.cs +++ b/PRPR/BooruViewer/Services/AnimePersonalization.cs @@ -89,7 +89,27 @@ public static async Task SetBackgroundImageAsync(FilteredCollection= 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;