Skip to content

Commit

Permalink
Fix a crash when not enough results to shuffle.
Browse files Browse the repository at this point in the history
  • Loading branch information
laplamgor committed Jan 15, 2018
1 parent ded1bd5 commit 63a3ce1
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion PRPR/BooruViewer/Services/AnimePersonalization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,27 @@ public static async Task<string> SetBackgroundImageAsync(FilteredCollection<Post
Random rnd = new Random();
int pointer = rnd.Next((int)postShuffle);

var post = await GetPostAtAsync(posts, pointer);
// 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;
Expand Down

1 comment on commit 63a3ce1

@laplamgor
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the background task crash mentioned in #1.

Please sign in to comment.