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?