From 0b6de0840fce8f2a884bb47b1b930276c6d8d7cc Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Wed, 26 Jul 2023 21:56:27 -0400 Subject: [PATCH] Move photoIndex to wherever the previously active photo was --- Program.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/Program.cs b/Program.cs index 7ce50ac..13e2ba5 100644 --- a/Program.cs +++ b/Program.cs @@ -714,9 +714,22 @@ public class Game : GameWindow { void FilterByRating(int rating) { Console.WriteLine("filter to " + rating); + Photo previouslyActive = photos[photoIndex]; photos = allPhotos.Where(p => p.Rating >= rating).ToList(); - // TODO: put this closest to wherever the previously active photo was. - photoIndex = 0; + // Move photoIndex to wherever the previously active photo was, or if it + // was filtered out, to whichever unfiltered photo comes before it. This + // is O(n) in the length of allPhotos, but how bad can it be? :) + photoIndex = -1; + for (int i = 0; i < allPhotos.Count; i++) { + Photo candidate = allPhotos[i]; + if (candidate.Rating >= rating) { + photoIndex++; + } + if (candidate == previouslyActive) { + break; + } + } + photoIndex = Math.Max(0, photoIndex); } protected override void OnLoad() {