Move photoIndex to wherever the previously active photo was

This commit is contained in:
Colin McMillen 2023-07-26 21:56:27 -04:00
parent ca5b2d94f5
commit 0b6de0840f

View File

@ -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() {