Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/handle-assets-library-changes' into 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
nek023 committed Apr 5, 2015
2 parents 3a77f2a + e7d3397 commit 3d2f3a7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
26 changes: 26 additions & 0 deletions QBImagePicker/QBAlbumsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ - (void)viewDidLoad
[self updateAssetsGroupsWithCompletion:^{
[self.tableView reloadData];
}];

// Register observer
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(assetsLibraryChanged:)
name:ALAssetsLibraryChangedNotification
object:nil];
}

- (void)viewWillAppear:(BOOL)animated
Expand All @@ -62,6 +68,14 @@ - (void)viewWillAppear:(BOOL)animated
[self updateSelectionInfo];
}

- (void)dealloc
{
// Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:ALAssetsLibraryChangedNotification
object:nil];
}


#pragma mark - Storyboard

Expand All @@ -73,6 +87,18 @@ - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
}


#pragma mark - Handling Assets Library Changes

- (void)assetsLibraryChanged:(NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^{
[self updateAssetsGroupsWithCompletion:^{
[self.tableView reloadData];
}];
});
}


#pragma mark - Actions

- (IBAction)cancel:(id)sender
Expand Down
51 changes: 40 additions & 11 deletions QBImagePicker/QBAssetsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ - (void)viewDidLoad
[super viewDidLoad];

[self setUpToolbarItems];

// Register observer
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(assetsLibraryChanged:)
name:ALAssetsLibraryChangedNotification
object:nil];
}

- (void)viewWillAppear:(BOOL)animated
Expand All @@ -68,8 +74,8 @@ - (void)viewWillAppear:(BOOL)animated
[self updateSelectionInfo];

// Scroll to bottom
if (self.assetsGroup.numberOfAssets > 0 && self.isMovingToParentViewController && !self.disableScrollToBottom) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:(self.assetsGroup.numberOfAssets - 1) inSection:0];
if (self.numberOfAssets > 0 && self.isMovingToParentViewController && !self.disableScrollToBottom) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:(self.numberOfAssets - 1) inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:NO];
}
}
Expand All @@ -88,6 +94,25 @@ - (void)viewDidAppear:(BOOL)animated
self.disableScrollToBottom = NO;
}

- (void)dealloc
{
// Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:ALAssetsLibraryChangedNotification
object:nil];
}


#pragma mark - Accessors

- (void)setAssetsGroup:(ALAssetsGroup *)assetsGroup
{
_assetsGroup = assetsGroup;

[self updateAssets];
[self.collectionView reloadData];
}


#pragma mark - Handling Device Rotation

Expand Down Expand Up @@ -121,17 +146,21 @@ - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIVi
}


#pragma mark - Accessors
#pragma mark - Handling Assets Library Changes

- (void)setAssetsGroup:(ALAssetsGroup *)assetsGroup
- (void)assetsLibraryChanged:(NSNotification *)notification
{
_assetsGroup = assetsGroup;

// Load assets
[self updateAssets];

// Update collection view
[self.collectionView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
NSSet *updatedAssetsGroups = notification.userInfo[ALAssetLibraryUpdatedAssetGroupsKey];
NSURL *assetsGroupURL = [self.assetsGroup valueForProperty:ALAssetsGroupPropertyURL];

for (NSURL *updatedAssetsGroupURL in updatedAssetsGroups) {
if ([updatedAssetsGroupURL isEqual:assetsGroupURL]) {
[self updateAssets];
[self.collectionView reloadData];
}
}
});
}


Expand Down

0 comments on commit 3d2f3a7

Please sign in to comment.