Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.

Commit 21b6248

Browse files
author
Sérgio Estêvão
committed
Merge pull request #88 from wordpress-mobile/issue/73_fix_crash_when_adding_photo
Issue/73 fix crash when adding photo
2 parents 338fc9b + 28373ec commit 21b6248

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

Pod/Classes/WPMediaCollectionViewCell.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ - (void)prepareForReuse
3535
[self setImage:nil];
3636
[self setCaption:@""];
3737
[self setPosition:NSNotFound];
38+
[self setSelected:NO];
3839
}
3940

4041
- (void)commonInit

Pod/Classes/WPMediaCollectionViewController.m

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ - (void)refreshData
237237
[self.collectionView setContentOffset:CGPointMake(0, - [[self topLayoutGuide] length] - (self.refreshControl.frame.size.height)) animated:YES];
238238
[self.refreshControl beginRefreshing];
239239
}
240+
// NOTE: Sergio Estevao (2015-11-19)
241+
// Clean all assets and refresh collection view when the group was changed
242+
// This avoid to see data from previous group while the new one is loading.
240243
[self.collectionView reloadData];
241244
}
242245
self.collectionView.allowsSelection = NO;
@@ -657,13 +660,15 @@ - (void)showAlertAboutMediaCapturePermission
657660

658661
- (void)processMediaCaptured:(NSDictionary *)info
659662
{
663+
NSInteger mediaItemsBefore = [self.dataSource numberOfAssets];
660664
self.ignoreMediaTimestamp = [NSDate timeIntervalSinceReferenceDate];
661665
WPMediaAddedBlock completionBlock = ^(id<WPMediaAsset> media, NSError *error) {
662666
if (error || !media) {
663-
NSLog(@"%@", error);
667+
NSLog(@"Adding media failed: %@", [error localizedDescription]);
664668
return;
665669
}
666-
[self addMedia:media];
670+
NSInteger mediaItemsAfter = [self.dataSource numberOfAssets];
671+
[self addMedia:media animated:mediaItemsAfter != mediaItemsBefore];
667672
};
668673
if ([info[UIImagePickerControllerMediaType] isEqual:(NSString *)kUTTypeImage]) {
669674
UIImage *image = (UIImage *)info[UIImagePickerControllerOriginalImage];
@@ -675,7 +680,7 @@ - (void)processMediaCaptured:(NSDictionary *)info
675680
}
676681
}
677682

678-
- (void)addMedia:(id<WPMediaAsset>)asset
683+
- (void)addMedia:(id<WPMediaAsset>)asset animated:(BOOL)animated
679684
{
680685
BOOL willBeSelected = YES;
681686
if ([self.picker.delegate respondsToSelector:@selector(mediaPickerController:shouldSelectAsset:)]) {
@@ -687,21 +692,21 @@ - (void)addMedia:(id<WPMediaAsset>)asset
687692
} else {
688693
[self.selectedAssets addObject:asset];
689694
}
690-
691695
NSUInteger insertPosition = [self showMostRecentFirst] ? 1 : [self.dataSource numberOfAssets]-1;
696+
if (animated){
697+
[self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:insertPosition inSection:0]]];
692698

693-
[self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:insertPosition inSection:0]]];
694-
695-
if ( ![self showMostRecentFirst] ){
696-
NSUInteger reloadPosition = [self.dataSource numberOfAssets];
697-
[self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:reloadPosition inSection:0]]];
698-
} else {
699-
NSUInteger reloadPosition = MIN([self.dataSource numberOfAssets], 2);
700-
[self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:reloadPosition inSection:0]]];
701-
}
702-
if (!self.showMostRecentFirst) {
703-
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:[self.dataSource numberOfAssets] inSection:0]
704-
atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
699+
if ( ![self showMostRecentFirst] ){
700+
NSUInteger reloadPosition = [self.dataSource numberOfAssets];
701+
[self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:reloadPosition inSection:0]]];
702+
} else {
703+
NSUInteger reloadPosition = MIN([self.dataSource numberOfAssets], 2);
704+
[self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:reloadPosition inSection:0]]];
705+
}
706+
if (!self.showMostRecentFirst) {
707+
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:[self.dataSource numberOfAssets] inSection:0]
708+
atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
709+
}
705710
}
706711

707712
if (!willBeSelected) {

0 commit comments

Comments
 (0)