davidortinau / Xappy

A mobile app to track Xamarin news and explore all the goodness that is .NET for Mobile developers
MIT License
350 stars 106 forks source link

PhotoGallery fails when updated to current NuGets #74

Open WillAutio opened 2 years ago

WillAutio commented 2 years ago

First off, thanks for the PhotoGallery part of the app. I learned lots from it. Background: Something that did not click until I came across it on a blog: http://lpains.net/articles/2020/xamarin-forms-collection-view-selected-items/ was that when the docs say to use this: public ObservableCollection<object> SelectedPhotos they actually mean object not what ever object you are using in the CollectionView. As long as I was using myObject things were failing a bit. It was almost working, but not quite. When I changed myObject to object. Things started looking up.

Issue: Now, that I was using object in the SelectedPhotos, things like the LongPress would select the photo that was receiving the LongPress. However, any Photo that I tapped after that got ignored.

Well, what actually happened is that in this code in PhotoGalleryViewModel:

private void OnPressed(Photo obj)
{
        if (_selectionMode != SelectionMode.None)
        { 
            Debug.WriteLine($"Added {obj.ImageSrc}");
            if (_selectedPhotos.Contains(obj))
                SelectedPhotos.Remove(obj);
            else
                SelectedPhotos.Add(obj);
        }
        else
        {
            Shell.Current.GoToAsync($"photo?src={obj.ImageSrc}");
        }
    }

when I was doing the second and subsequent tap of a multi-select, the program would enter OnPressed and enter the first if block since SelectionMode was Multiple. Then it wrote out the Debug stuff. Then it checked if the obj was already in _selectedPhotos. Lo and hehold, for every tap but the first LongPress, obj was contained in _selectedPhotos - and so it got removed. (this code I expect was intended to remove an entry from the list upon the Photo being tapped a second time)

After comparing your code and my code and many other things, I found the cause of the issue. My NuGets were newer than yours. I'll list what I am using: Xamarin Community Toolkit 1.3.0-pre2 Xamarin.Essentials 1.7.0 Xamarin.Forms 5.0.0.2083 Visual Studio 2019 current Win 10

When I updated this app to the same versions, it behaved the same way.

It looks like something in what got updated now takes care of the adding to the SelectedPhotos as well as removing ones that get taped a second time. When I remove this code:

              if (_selectedPhotos.Contains(obj))
                    SelectedPhotos.Remove(obj);
                else
                    SelectedPhotos.Add(obj);

it all seems to work - at least for what I tested.

toumir commented 2 years ago

.