0

I am working in the Xamarin. I have one Observable Collection. This observable collection has some pictures. In the collection, 1 picture is a cover photo. I can pin any photo as a cover photo. For that, I have a function called "UpdatePinned".

In my observable collection, index 0 is for adding pictures (which means camera or gallery options). index 1 is for a cover photo. Every time when I add a picture, I store created date time in the local DB.

Previously, when I update the pin I just changed the index position of the picture. Remove the picture from the collection and inserting at the index position 1.

await App.RunOnMainThread(() =>
            {
                PicturesDetails.Remove(picture);
                PicturesDetails.Insert(1, picture);
            });

but now whenever the user updates the pin and makes any photo as a cover photo, I want to sort the rest of the pictures (after index 1) in descending order by created date.

So, I update the code like this,

//Move pinned picture to index 1 and Sort unpinned pictures (all the items after index 1) in descending order by created date property.  
            var PictureVMs = PicturesDetails.ToList();
            PictureVMs.Remove(picture);
            PictureVMs.Insert(1, picture);
            var PictureVMsWithNoCoverPhoto = PictureVMs.Skip(2).OrderByDescending(x => x.CreatedDateTime).ToList();
            PictureVMs.RemoveRange(2, PicturesDetails.Count-2);
            PictureVMs.AddRange(PictureVMsWithNoCoverPhoto);
            
            await App.RunOnMainThread(() =>
            {
                PicturesDetails.Clear();
                PicturesDetails = new ObservableCollection<PictureVM>(PictureVMs);
            });

I have to convert it to the temporary list object and after operations again convert it back to an observable collection.

Is there any optimized way to do the same thing?

RMR
  • 599
  • 3
  • 12
  • 35
  • any alternative I can think of seems very similar in terms of complexity and overhead – Jason Mar 27 '23 at 18:00
  • Before you add a picture, the unpinned are correctly sorted, right? (from a previous sort.) If so, then all you need to do is find the index at which to insert the newly added picture. Given a sorted list, this is as simple as finding the first picture which is AFTER where the new one should go. (Has a higher/lower creation date, depending on your sort order.) Its index is the insertion index. – ToolmakerSteve Mar 27 '23 at 18:15
  • Why are you not using skip? that way your main collection won't be affected – FreakyAli Mar 27 '23 at 19:52

1 Answers1

0
var PictureVMs = PicturesDetails.ToList();
PictureVMs.Remove(picture);
PictureVMs.Insert(1, picture);
PictureVMs = PictureVMs.Take(2).Concat(
    PictureVMs.Skip(2)
    .OrderByDescending(x => x.CreatedDateTime));

await App.RunOnMainThread(() =>
        {
            PicturesDetails.Clear();
            PicturesDetails = new ObservableCollection<PictureVM>(PictureVMs);
        });
Igor Buchelnikov
  • 505
  • 4
  • 14