public void PhotoLibraryDidChange(PHChange changeInstance) { // Change notifications may be made on a background queue. Re-dispatch to the // main queue before acting on the change as we'll be updating the UI. DispatchQueue.MainQueue.DispatchSync(() => { // Check each of the three top-level fetches for changes. // Update the cached fetch result. var changeDetails = changeInstance.GetFetchResultChangeDetails(allPhotos); if (changeDetails != null) { // Update the cached fetch result. allPhotos = changeDetails.FetchResultAfterChanges; // (The table row for this one doesn't need updating, it always says "All Photos".) } // Update the cached fetch results, and reload the table sections to match. changeDetails = changeInstance.GetFetchResultChangeDetails(smartAlbums); if (changeDetails != null) { smartAlbums = changeDetails.FetchResultAfterChanges; TableView.ReloadSections(NSIndexSet.FromIndex((int)Section.SmartAlbums), UITableViewRowAnimation.Automatic); } changeDetails = changeInstance.GetFetchResultChangeDetails(userCollections); if (changeDetails != null) { userCollections = changeDetails.FetchResultAfterChanges; TableView.ReloadSections(NSIndexSet.FromIndex((int)Section.UserCollections), UITableViewRowAnimation.Automatic); } }); }
public void PhotoLibraryDidChange(PHChange changeInstance) { Debug.WriteLine($"{this.GetType().Name}: PhotoLibraryDidChange"); // Call might come on any background queue. Re-dispatch to the main queue to handle it. DispatchQueue.MainQueue.DispatchAsync(() => { List <PHFetchResult> updatedCollectionsFetchResults = null; foreach (var collectionsFetchResult in _collectionsFetchResults) { var changeDetails = changeInstance.GetFetchResultChangeDetails(collectionsFetchResult); if (changeDetails != null) { if (updatedCollectionsFetchResults == null) { updatedCollectionsFetchResults = _collectionsFetchResults.ToList(); } updatedCollectionsFetchResults[updatedCollectionsFetchResults.IndexOf(collectionsFetchResult)] = changeDetails.FetchResultAfterChanges; } // This only affects to changes in albums level (add/remove/edit album) if (updatedCollectionsFetchResults != null) { _collectionsFetchResults = updatedCollectionsFetchResults; } } // Search for new photos and select them if camera is turned on if (_picker.AutoSelectCameraImages && _picker.ShowCameraButton) { foreach (var collection in _collectionsFetchResultsAssets) { foreach (var fetchResult in collection) { var changeDetails = changeInstance.GetFetchResultChangeDetails(fetchResult); if (changeDetails != null && changeDetails.InsertedObjects != null) { foreach (var asset in changeDetails.InsertedObjects.OfType <PHAsset>()) { _picker.SelectAsset(asset); } } } } } // However, we want to update if photos are added, so the counts of items & thumbnails are updated too. // Maybe some checks could be done here , but for now is OKey. UpdateFetchResults(); TableView.ReloadData(); }); }
public override void PhotoLibraryDidChange(PHChange changeInstance) { DispatchQueue.MainQueue.DispatchAsync(() => { var changes = changeInstance.GetFetchResultChangeDetails(controller.Images); controller.Images = changes.FetchResultAfterChanges; }); }
public override void PhotoLibraryDidChange(PHChange changeInstance) { DispatchQueue.MainQueue.DispatchAsync(() => { var changes = changeInstance.GetFetchResultChangeDetails(controller.fetchResults); controller.fetchResults = changes.FetchResultAfterChanges; controller.CollectionView.ReloadData(); }); }
public void PhotoLibraryDidChange (PHChange changeInstance) { // Call might come on any background queue. Re-dispatch to the main queue to handle it. DispatchQueue.MainQueue.DispatchAsync (() => { List<PHFetchResult> updatedCollectionsFetchResults = null; foreach (var collectionsFetchResult in _collectionsFetchResults){ var changeDetails = changeInstance.GetFetchResultChangeDetails(collectionsFetchResult); if (changeDetails != null) { if (updatedCollectionsFetchResults == null) { updatedCollectionsFetchResults = _collectionsFetchResults.ToList(); } updatedCollectionsFetchResults[updatedCollectionsFetchResults.IndexOf(collectionsFetchResult)] = changeDetails.FetchResultAfterChanges; } // This only affects to changes in albums level (add/remove/edit album) if (updatedCollectionsFetchResults != null) { _collectionsFetchResults = updatedCollectionsFetchResults; } } // Search for new photos and select them if camera is turned on if (_picker.AutoSelectCameraImages && _picker.ShowCameraButton) { foreach (var collection in _collectionsFetchResultsAssets) { foreach (var fetchResult in collection) { var changeDetails = changeInstance.GetFetchResultChangeDetails (fetchResult); if (changeDetails != null && changeDetails.InsertedObjects != null) { foreach (var asset in changeDetails.InsertedObjects.OfType<PHAsset>()) { _picker.SelectAsset (asset); } } } } } // However, we want to update if photos are added, so the counts of items & thumbnails are updated too. // Maybe some checks could be done here , but for now is OKey. UpdateFetchResults(); TableView.ReloadData(); }); }
NSIndexSet[] SynchronizeAlbums(PHChange changeInstance) { // updated index set var updatedIndexSets = new List <NSIndexSet>(); // notify changes of albums foreach (var i in AlbumsFetchArray.Select((value, index) => KeyValuePair.Create(index, value))) { var section = i.Key; var albumsFetchResult = i.Value; var updatedIndexSet = new NSMutableIndexSet(); updatedIndexSets.Add(updatedIndexSet); var albumsChangeDetail = changeInstance.GetFetchResultChangeDetails(albumsFetchResult); if (albumsChangeDetail == null) { continue; } // update albumsFetchArray AlbumsFetchArray[section] = albumsChangeDetail.FetchResultAfterChanges; if (!albumsChangeDetail.HasIncrementalChanges) { NotifySubscribers((_) => _.ReloadedAlbumsInSection(this, section)); continue; } // sync removed albums var removedIndexes = albumsChangeDetail.RemovedIndexes; removedIndexes.EnumerateIndexes((nuint insertedIndex, ref bool stop) => { RemoveAt(indexPath: NSIndexPath.FromRowSection((nint)insertedIndex, section)); }); // sync inserted albums var insertedIndexes = albumsChangeDetail.InsertedIndexes; insertedIndexes.EnumerateIndexes((nuint insertedIndex, ref bool stop) => { var insertedAlbum = albumsChangeDetail.FetchResultAfterChanges[(nint)insertedIndex] as PHAssetCollection; FetchAlbum(insertedAlbum); FetchedAlbumsArray[section].Insert((int)insertedIndex, insertedAlbum); updatedIndexSet.Add(insertedIndex); }); // sync updated albums var updatedIndexes = albumsChangeDetail.ChangedIndexes; updatedIndexes.EnumerateIndexes((nuint updatedIndex, ref bool stop) => { var updatedAlbum = albumsChangeDetail.FetchResultAfterChanges[(nint)updatedIndex] as PHAssetCollection; FetchAlbum(updatedAlbum); updatedIndexSet.Add(updatedIndex); }); } return(updatedIndexSets.ToArray()); }
public void PhotoLibraryDidChange(PHChange changeInstance) { var changes = changeInstance.GetFetchResultChangeDetails(FetchResult); if (changes == null) { return; } DispatchQueue.MainQueue.DispatchSync(() => { // Hang on to the new fetch result. FetchResult = changes.FetchResultAfterChanges; if (changes.HasIncrementalChanges) { // If we have incremental diffs, animate them in the collection view. CollectionView.PerformBatchUpdates(() => { // For indexes to make sense, updates must be in this order: // delete, insert, reload, move var removed = changes.RemovedIndexes; if (removed != null && removed.Count > 0) { CollectionView.DeleteItems(ToNSIndexPaths(removed)); } var inserted = changes.InsertedIndexes; if (inserted != null && inserted.Count > 0) { CollectionView.InsertItems(ToNSIndexPaths(inserted)); } var changed = changes.ChangedIndexes; if (changed != null && changed.Count > 0) { CollectionView.ReloadItems(ToNSIndexPaths(changed)); } changes.EnumerateMoves((fromIndex, toIndex) => { var start = NSIndexPath.FromItemSection((nint)fromIndex, 0); var end = NSIndexPath.FromItemSection((nint)toIndex, 0); CollectionView.MoveItem(start, end); }); }, null); } else { // Reload the collection view if incremental diffs are not available. CollectionView.ReloadData(); } ResetCachedAssets(); }); }
public override void PhotoLibraryDidChange(PHChange changeInstance) { PHFetchResultChangeDetails imageDetails = changeInstance.GetFetchResultChangeDetails(_imageFetchResult); PHFetchResultChangeDetails videoDetails = changeInstance.GetFetchResultChangeDetails(_videoFetchResult); List <PHObject> insertedObjects = new List <PHObject>(); if (imageDetails != null && imageDetails.InsertedObjects.Any()) { insertedObjects.AddRange(imageDetails.InsertedObjects); _imageFetchResult = imageDetails.FetchResultAfterChanges; } if (videoDetails != null && videoDetails.InsertedObjects.Any()) { insertedObjects.AddRange(videoDetails.InsertedObjects); _videoFetchResult = videoDetails.FetchResultAfterChanges; } HandleImage(insertedObjects); }
public void PhotoLibraryDidChange(PHChange changeInstance) { DispatchQueue.MainQueue.DispatchAsync(() => { // Loop through the section fetch results, replacing any fetch results that have been updated. for (int i = 0; i < sectionFetchResults.Length; i++) { PHFetchResultChangeDetails changeDetails = changeInstance.GetFetchResultChangeDetails(sectionFetchResults [i]); if (changeDetails != null) { sectionFetchResults [i] = changeDetails.FetchResultAfterChanges; } } TableView.ReloadData(); }); }
public void PhotoLibraryDidChange(PHChange changeInstance) { // Check if there are changes to the assets we are showing. var collectionChanges = changeInstance.GetFetchResultChangeDetails(AssetsFetchResults); if (collectionChanges == null) { return; } DispatchQueue.MainQueue.DispatchAsync(() => { // Get the new fetch result. AssetsFetchResults = collectionChanges.FetchResultAfterChanges; UICollectionView collectionView = CollectionView; if (collectionChanges.HasIncrementalChanges || !collectionChanges.HasMoves) { collectionView.PerformBatchUpdates(() => { var removedIndexes = collectionChanges.RemovedIndexes; if (removedIndexes != null && removedIndexes.Count > 0) { collectionView.DeleteItems(removedIndexes.GetIndexPaths(0)); } var insertedIndexes = collectionChanges.InsertedIndexes; if (insertedIndexes != null && insertedIndexes.Count > 0) { collectionView.InsertItems(insertedIndexes.GetIndexPaths(0)); } var changedIndexes = collectionChanges.ChangedIndexes; if (changedIndexes != null && changedIndexes.Count > 0) { collectionView.ReloadItems(changedIndexes.GetIndexPaths(0)); } }, null); } else { collectionView.ReloadData(); } ResetCachedAssets(); }); }
private PHFetchResult TryAdjustAssets(PHFetchResult assets, PHChange changeInstance) { if (assets == null) { return(null); } if (changeInstance == null) { return(assets); } var collectionChanges = changeInstance.GetFetchResultChangeDetails(assets); if (collectionChanges == null) { return(assets); } return(AdjustAssets(assets, collectionChanges)); }
public void PhotoLibraryDidChange(PHChange changeInstance) { var fetchResult = _collectionViewDataSource.AssetsModel.FetchResult; var changes = changeInstance.GetFetchResultChangeDetails(fetchResult); if (fetchResult == null || changes == null) { return; } _collectionViewCoordinator.PerformDataSourceUpdate(() => { //update old fetch result with these updates _collectionViewDataSource.AssetsModel.UpdateFetchResult(changes.FetchResultAfterChanges); //update layout model because it changed _collectionViewDataSource.UpdateLayoutModel(new LayoutModel(LayoutConfiguration, (int)_collectionViewDataSource.AssetsModel.FetchResult.Count)); }); //perform update animations _collectionViewCoordinator.PerformChanges(changes, LayoutConfiguration.SectionIndexForAssets); }
public void PhotoLibraryDidChange(PHChange changeInstance) { Debug.WriteLine($"{this.GetType().Name}: PhotoLibraryDidChange"); // Call might come on any background queue. Re-dispatch to the main queue to handle it. DispatchQueue.MainQueue.DispatchAsync(() => { // check if there are changes to the assets (insertions, deletions, updates) var collectionChanges = changeInstance.GetFetchResultChangeDetails(AssetsFetchResults); if (collectionChanges != null) { var collectionView = CollectionView; if (CollectionView == null) { return; } // get the new fetch result AssetsFetchResults = collectionChanges.FetchResultAfterChanges; if (!collectionChanges.HasIncrementalChanges || collectionChanges.HasMoves) { // we need to reload all if the incremental diffs are not available collectionView.ReloadData(); } else { // if we have incremental diffs, tell the collection view to animate insertions and deletions collectionView.PerformBatchUpdates(() => { var removedIndexes = collectionChanges.RemovedIndexes; if (removedIndexes != null && removedIndexes.Count > 0) { collectionView.DeleteItems(GetIndexesWithSection(removedIndexes, 0)); } if (collectionChanges.InsertedIndexes != null && collectionChanges.InsertedIndexes.Count > 0) { collectionView.InsertItems(GetIndexesWithSection(collectionChanges.InsertedIndexes, 0)); var changedIndexes = collectionChanges.ChangedIndexes; if (changedIndexes != null && changedIndexes.Count > 0) { collectionView.ReloadItems(GetIndexesWithSection(changedIndexes, 0)); } } }, (x) => { if (_picker.GridSortOrder == SortOrder.Ascending) { var item = collectionView.NumberOfItemsInSection(0) - 1; if (item >= 0) { var path = NSIndexPath.FromItemSection(item, 0); collectionView.ScrollToItem(path, UICollectionViewScrollPosition.Bottom, true); } } else { var path = NSIndexPath.FromItemSection(0, 0); collectionView.ScrollToItem(path, UICollectionViewScrollPosition.Top, true); } }); if (collectionChanges.InsertedIndexes != null && collectionChanges.InsertedIndexes.Count > 0) { if (_picker.ShowCameraButton && _picker.AutoSelectCameraImages) { foreach (var path in GetIndexesWithSection(collectionChanges.InsertedIndexes, 0)) { ItemSelected(collectionView, path); } } } } ResetCachedAssets(); } }); }
public void PhotoLibraryDidChange (PHChange changeInstance) { BeginInvokeOnMainThread(() => { // check if there are changes to the assets (insertions, deletions, updates) var collectionChanges = changeInstance.GetFetchResultChangeDetails(assetsFetchResults); if (collectionChanges != null) { assetsFetchResults = collectionChanges.FetchResultAfterChanges; if (!collectionChanges.HasIncrementalChanges || collectionChanges.HasMoves) { CollectionView.ReloadData(); } else { var removedIndexes = indexPathsFromIndexSet(collectionChanges.RemovedIndexes); var insertedIndexes = indexPathsFromIndexSet(collectionChanges.InsertedIndexes); var changedIndexes = indexPathsFromIndexSet(collectionChanges.ChangedIndexes); // if we have incremental diffs, tell the collection view to animate insertions and deletions CollectionView.PerformBatchUpdates(() => { if (!removedIndexes.IsEmpty()) CollectionView.DeleteItems(removedIndexes.ToArray()); if (!insertedIndexes.IsEmpty()) CollectionView.InsertItems(insertedIndexes.ToArray()); if (!changedIndexes.IsEmpty()) CollectionView.ReloadItems(changedIndexes.ToArray()); }, null); } } }); }
public void PhotoLibraryDidChange (PHChange changeInstance) { // Check if there are changes to the assets we are showing. var collectionChanges = changeInstance.GetFetchResultChangeDetails (AssetsFetchResults); if (collectionChanges == null) return; DispatchQueue.MainQueue.DispatchAsync (() => { // Get the new fetch result. AssetsFetchResults = collectionChanges.FetchResultAfterChanges; UICollectionView collectionView = CollectionView; if (collectionChanges.HasIncrementalChanges || !collectionChanges.HasMoves) { collectionView.PerformBatchUpdates (() => { var removedIndexes = collectionChanges.RemovedIndexes; if (removedIndexes != null && removedIndexes.Count > 0) collectionView.DeleteItems (removedIndexes.GetIndexPaths (0)); var insertedIndexes = collectionChanges.InsertedIndexes; if (insertedIndexes != null && insertedIndexes.Count > 0) collectionView.InsertItems (insertedIndexes.GetIndexPaths (0)); var changedIndexes = collectionChanges.ChangedIndexes; if (changedIndexes != null && changedIndexes.Count > 0) collectionView.ReloadItems (changedIndexes.GetIndexPaths (0)); }, null); } else { collectionView.ReloadData (); } ResetCachedAssets (); }); }
public void PhotoLibraryDidChange (PHChange changeInstance) { DispatchQueue.MainQueue.DispatchAsync (() => { // Loop through the section fetch results, replacing any fetch results that have been updated. for (int i = 0; i < sectionFetchResults.Length; i++) { PHFetchResultChangeDetails changeDetails = changeInstance.GetFetchResultChangeDetails (sectionFetchResults [i]); if(changeDetails != null) { sectionFetchResults [i] = changeDetails.FetchResultAfterChanges; } } TableView.ReloadData (); }); }
public override void PhotoLibraryDidChange(PHChange changeInstance) { PHFetchResultChangeDetails changes = changeInstance.GetFetchResultChangeDetails(_imageCache._fetchResult); if (changes == null) { return; } _imageCache.OnPhotoLibraryDidChange(changes.FetchResultAfterChanges); }
public void PhotoLibraryDidChange (PHChange changeInstance) { // Change notifications may be made on a background queue. Re-dispatch to the // main queue before acting on the change as we'll be updating the UI. DispatchQueue.MainQueue.DispatchSync (() => { // Check each of the three top-level fetches for changes. // Update the cached fetch result. var changeDetails = changeInstance.GetFetchResultChangeDetails (allPhotos); if (changeDetails != null) { allPhotos = changeDetails.FetchResultAfterChanges; // (The table row for this one doesn't need updating, it always says "All Photos".) } // Update the cached fetch results, and reload the table sections to match. changeDetails = changeInstance.GetFetchResultChangeDetails (smartAlbums); if (changeDetails != null) { smartAlbums = changeDetails.FetchResultAfterChanges; TableView.ReloadSections (NSIndexSet.FromIndex ((int)Section.SmartAlbums), UITableViewRowAnimation.Automatic); } changeDetails = changeInstance.GetFetchResultChangeDetails (userCollections); if (changeDetails != null) { userCollections = changeDetails.FetchResultAfterChanges; TableView.ReloadSections (NSIndexSet.FromIndex ((int)Section.UserCollections), UITableViewRowAnimation.Automatic); } }); }
public override void PhotoLibraryDidChange(PHChange changeInstance) { DispatchQueue.MainQueue.DispatchAsync (() => { var changes = changeInstance.GetFetchResultChangeDetails (controller.fetchResults); controller.fetchResults = changes.FetchResultAfterChanges; controller.CollectionView.ReloadData (); }); }
void SynchronizeAssets(NSIndexSet[] updatedAlbumIndexSets, IDictionary <string, PHFetchResult> fetchMapBeforeChanges, PHChange changeInstance) { var updatedIndexSets = updatedAlbumIndexSets; foreach (var i in FetchedAlbumsArray.Select((value, index) => KeyValuePair.Create(index, value))) { var section = i.Key; var albums = i.Value; var updatedIndexSet = updatedIndexSets[section] as NSMutableIndexSet; foreach (var album in albums) { Debug.WriteLine($"Looping album: {album.LocalizedTitle}"); var fetchResult = fetchMapBeforeChanges[album.LocalIdentifier]; var assetsChangeDetails = changeInstance.GetFetchResultChangeDetails(fetchResult); if (fetchResult == null || assetsChangeDetails == null) { continue; } // check thumbnail if (IsThumbnailChanged(assetsChangeDetails) || IsCountChanged(assetsChangeDetails)) { var updateRow = fetchedAlbumsArray[section].IndexOf(album); if (updateRow > -1) { updatedIndexSet.Add((uint)updateRow); } } // update fetch result for each album fetchMap[album.LocalIdentifier] = assetsChangeDetails.FetchResultAfterChanges; if (!assetsChangeDetails.HasIncrementalChanges) { NotifySubscribers((subscriber) => { var indexPathForAlbum = this.IndexPathForAlbumInAlbumsArray(album, this.SortedAlbumsArray); if (indexPathForAlbum != null) { subscriber.ReloadedAlbum(this, album, indexPathForAlbum); } }); continue; } var selectedAlbum = this.SelectedAlbum; if (selectedAlbum == null || selectedAlbum.LocalIdentifier != album.LocalIdentifier) { continue; } // sync removed assets var removedIndexesSet = assetsChangeDetails.RemovedIndexes; if (removedIndexesSet != null) { var removedIndexes = removedIndexesSet.AsArray().OrderBy(x => x.Row); var removeAssets = new List <PHAsset>(); foreach (var removedIndex in removedIndexes.Reverse()) { removeAssets.Insert(0, AssetArray.RemoveAndGet(removedIndex.Row)); } // stop caching for removed assets StopCache(removeAssets.ToArray(), PickerConfig.AssetCacheSize); NotifySubscribers((_) => _.RemovedAssets(this, removeAssets.ToArray(), removedIndexes.ToArray()), removeAssets.Count > 0); } // sync inserted assets var insertedIndexesSet = assetsChangeDetails.InsertedIndexes; if (insertedIndexesSet != null) { var insertedIndexes = insertedIndexesSet.AsArray().OrderBy(x => x.Row); var insertedAssets = new List <PHAsset>(); foreach (var insertedIndex in insertedIndexes) { var insertedAsset = assetsChangeDetails.FetchResultAfterChanges[insertedIndex.Row] as PHAsset; insertedAssets.Add(insertedAsset); AssetArray.Insert(insertedIndex.Row, insertedAsset); } // stop caching for removed assets Cache(insertedAssets.ToArray(), PickerConfig.AssetCacheSize); NotifySubscribers((_) => _.InsertedAssets(this, insertedAssets.ToArray(), insertedIndexes.ToArray()), insertedAssets.Count > 0); } // sync updated assets var updatedIndexes = assetsChangeDetails.ChangedIndexes; if (updatedIndexes != null) { var updatedAssets = new List <PHAsset>(); foreach (var updatedIndex in updatedIndexes.AsArray()) { var updatedAsset = assetsChangeDetails.FetchResultAfterChanges[updatedIndex.Row] as PHAsset; updatedAssets.Add(updatedAsset); } // stop caching for removed assets Cache(updatedAssets.ToArray(), PickerConfig.AssetCacheSize); StopCache(updatedAssets.ToArray(), PickerConfig.AssetCacheSize); NotifySubscribers((_) => _.UpdatedAssets(this, updatedAssets.ToArray(), updatedIndexes.AsArray()), updatedAssets.Count > 0); } } // update final changes in albums var oldSortedAlbums = SortedAlbumsArray[section]; var newSortedAlbums = SortedAlbumFromAlbums(fetchedAlbumsArray[section].ToArray()); /* 1. find & notify removed albums. */ var removedInfo = RemovedIndexPaths(newSortedAlbums, oldSortedAlbums.ToArray(), section); foreach (var item in removedInfo.Item1.Select((value, index) => new { Value = value, Index = index })) { var fetchedIndexPath = IndexPathForAlbumInAlbumsArray(removedInfo.Item2[item.Index], fetchedAlbumsArray); if (fetchedIndexPath != null) { updatedIndexSet.Remove((uint)fetchedIndexPath.Row); } } SortedAlbumsArray[section] = oldSortedAlbums; NotifySubscribers((_) => _.InsertedAlbums(this, removedInfo.Item2, removedInfo.Item1), removedInfo.Item1.Length > 0); /* 2. find & notify inserted albums. */ } }
public void PhotoLibraryDidChange (PHChange changeInstance) { var changes = changeInstance.GetFetchResultChangeDetails (FetchResult); if (changes == null) return; DispatchQueue.MainQueue.DispatchSync (() => { // Hang on to the new fetch result. FetchResult = changes.FetchResultAfterChanges; if (changes.HasIncrementalChanges) { // If we have incremental diffs, animate them in the collection view. CollectionView.PerformBatchUpdates (() => { // For indexes to make sense, updates must be in this order: // delete, insert, reload, move var removed = changes.RemovedIndexes; if (removed != null && removed.Count > 0) CollectionView.DeleteItems (ToNSIndexPaths (removed)); var inserted = changes.InsertedIndexes; if (inserted != null && inserted.Count > 0) CollectionView.InsertItems (ToNSIndexPaths (inserted)); var changed = changes.ChangedIndexes; if (changed != null && changed.Count > 0) CollectionView.ReloadItems (ToNSIndexPaths (changed)); changes.EnumerateMoves ((fromIndex, toIndex) => { var start = NSIndexPath.FromItemSection ((nint)fromIndex, 0); var end = NSIndexPath.FromItemSection ((nint)toIndex, 0); CollectionView.MoveItem (start, end); }); }, null); } else { // Reload the collection view if incremental diffs are not available. CollectionView.ReloadData (); } ResetCachedAssets (); }); }
public void PhotoLibraryDidChange (PHChange changeInstance) { // Call might come on any background queue. Re-dispatch to the main queue to handle it. DispatchQueue.MainQueue.DispatchAsync (() => { // check if there are changes to the assets (insertions, deletions, updates) var collectionChanges = changeInstance.GetFetchResultChangeDetails(AssetsFetchResults); if (collectionChanges != null) { var collectionView = CollectionView; if (collectionView == null) { return; } // get the new fetch result AssetsFetchResults = collectionChanges.FetchResultAfterChanges; if (!collectionChanges.HasIncrementalChanges || collectionChanges.HasMoves) { // we need to reload all if the incremental diffs are not available collectionView.ReloadData(); } else { // if we have incremental diffs, tell the collection view to animate insertions and deletions collectionView.PerformBatchUpdates(() => { var removedIndexes = collectionChanges.RemovedIndexes; if (removedIndexes != null && removedIndexes.Count > 0) { collectionView.DeleteItems(GetIndexesWithSection(removedIndexes, 0)); } var insertedIndexes = collectionChanges.InsertedIndexes; if (insertedIndexes != null && insertedIndexes.Count > 0) { collectionView.InsertItems(GetIndexesWithSection(insertedIndexes, 0)); if (_picker.ShowCameraButton && _picker.AutoSelectCameraImages) { foreach (var path in GetIndexesWithSection(insertedIndexes, 0)) { collectionView.Source.ItemSelected(collectionView, path); } } } var changedIndexes = collectionChanges.ChangedIndexes; if (changedIndexes != null && changedIndexes.Count > 0) { collectionView.ReloadItems(GetIndexesWithSection(changedIndexes, 0)); } }, (x) => { if (_picker.GridSortOrder == SortOrder.Ascending) { var item = collectionView.NumberOfItemsInSection(0) - 1; var path = NSIndexPath.FromItemSection(item, 0); collectionView.ScrollToItem(path, UICollectionViewScrollPosition.Bottom, true); } else { var path = NSIndexPath.FromItemSection(0, 0); collectionView.ScrollToItem(path, UICollectionViewScrollPosition.Top, true); } }); } ResetCachedAssets(); } }); }