Skip to content

Commit

Permalink
Added state restoration
Browse files Browse the repository at this point in the history
  • Loading branch information
davbeck committed Apr 21, 2015
1 parent 473bf99 commit 737e8bb
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 18 deletions.
52 changes: 40 additions & 12 deletions Pod/Classes/TNKCollectionPickerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
#import "PHCollection+TNKThumbnail.h"


@interface TNKCollectionPickerController () <PHPhotoLibraryChangeObserver>
@interface TNKCollectionPickerController () <PHPhotoLibraryChangeObserver, UIViewControllerRestoration>
{
NSArray *_collectionsFetchResults;

NSCache *_collectionHiddenCache;
NSCache *_assetCountCache;
BOOL _needsRefetch;

BOOL _navigationBarWasHidden;
}

@end
Expand Down Expand Up @@ -56,6 +54,8 @@ - (void)_init {
_assetCountCache = [NSCache new];

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];

self.restorationIdentifier = @"TNKCollectionPickerController";
}

- (id)initWithCoder:(NSCoder *)aDecoder {
Expand Down Expand Up @@ -92,9 +92,10 @@ - (void)viewDidLoad {
[super viewDidLoad];


self.tableView.restorationIdentifier = @"TableView";
[self.tableView registerClass:[TNKCollectionCell class] forCellReuseIdentifier:@"CollectionCell"];
self.tableView.separatorInset = UIEdgeInsetsMake(0.0, 95.0, 0.0, 0.0);
self.tableView.estimatedRowHeight = 85.0 + 1.0 / self.traitCollection.displayScale;
self.tableView.estimatedRowHeight = 85.0;


[self _setNeedsReloadFetch];
Expand All @@ -106,10 +107,7 @@ - (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

if (_navigationBarWasHidden) {
_navigationBarWasHidden = NO;
self.navigationController.navigationBarHidden = YES;
}
self.navigationController.navigationBarHidden = self.collectionList == nil;
}

- (void)_setNeedsReloadFetch {
Expand Down Expand Up @@ -329,8 +327,7 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
if (hidden) {
return 0.0;
} else {
// 85.0, plus the height of the separator
return 85.0 + 1.0 / self.traitCollection.displayScale;
return 85.0;
}
}

Expand All @@ -354,9 +351,8 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
TNKCollectionPickerController *picker = [[TNKCollectionPickerController alloc] init];
picker.delegate = self.delegate;
picker.collectionList = (PHCollectionList *)collection;
picker.restorationClass = [self class];

_navigationBarWasHidden = self.navigationController.navigationBarHidden;
self.navigationController.navigationBarHidden = NO;
[self.navigationController pushViewController:picker animated:YES];
}
}
Expand All @@ -372,4 +368,36 @@ - (void)photoLibraryDidChange:(PHChange *)changeInstance {
[self _setNeedsReloadFetch];
}


#pragma mark - State Restoration

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[super encodeRestorableStateWithCoder:coder];

[coder encodeObject:self.delegate forKey:@"delegate"];
[coder encodeObject:self.collectionList.localIdentifier forKey:@"collectionList"];
}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
[super decodeRestorableStateWithCoder:coder];

id<TNKCollectionPickerControllerDelegate> delegate = [coder decodeObjectForKey:@"delegate"];
if (delegate != nil) {
self.delegate = delegate;
}

NSString *collectionListIdentifier = [coder decodeObjectForKey:@"collectionList"];
if (collectionListIdentifier != nil) {
self.collectionList = [PHCollectionList fetchCollectionListsWithLocalIdentifiers:@[ collectionListIdentifier ] options:nil].firstObject;
}
}

+ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder {
TNKCollectionPickerController *picker = [[TNKCollectionPickerController alloc] init];

return picker;
}

@end
88 changes: 82 additions & 6 deletions Pod/Classes/TNKImagePickerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#define TNKObjectSpacing 1.0


@interface TNKImagePickerController () <UIPopoverPresentationControllerDelegate, TNKCollectionPickerControllerDelegate, PHPhotoLibraryChangeObserver, TNKAssetsDetailViewControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@interface TNKImagePickerController () <UIPopoverPresentationControllerDelegate, TNKCollectionPickerControllerDelegate, PHPhotoLibraryChangeObserver, TNKAssetsDetailViewControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIViewControllerRestoration>
{
NSMutableOrderedSet *_selectedAssets;

Expand All @@ -34,6 +34,8 @@ @interface TNKImagePickerController () <UIPopoverPresentationControllerDelegate,
NSCache *_momentCache;
BOOL _windowLoaded;
NSInteger _pasteChangeCount;

TNKCollectionPickerController *_collectionPicker;
}

@end
Expand Down Expand Up @@ -420,21 +422,29 @@ - (IBAction)deselectAll:(id)sender
}

- (IBAction)changeCollection:(id)sender {
TNKCollectionPickerController *collectionPicker = [[TNKCollectionPickerController alloc] init];
collectionPicker.assetFetchOptions = [self _assetFetchOptions];
if (_collectionPicker == nil) {
_collectionPicker = [[TNKCollectionPickerController alloc] init];
_collectionPicker.delegate = self;
}

_collectionPicker.assetFetchOptions = [self _assetFetchOptions];
if (_selectedAssets.count > 0) {
PHAssetCollection *collection = [PHAssetCollection transientAssetCollectionWithAssets:_selectedAssets.array title:NSLocalizedString(@"Selected", @"Collection name for selected photos")];
collectionPicker.additionalAssetCollections = @[ collection ];
_collectionPicker.additionalAssetCollections = @[ collection ];
} else {
_collectionPicker.additionalAssetCollections = @[];
}
collectionPicker.delegate = self;

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:collectionPicker];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_collectionPicker];
navigationController.restorationIdentifier = @"TNKCollectionPickerController.NavigationController";
navigationController.restorationClass = [self class];
navigationController.navigationBarHidden = YES;

navigationController.modalPresentationStyle = UIModalPresentationPopover;
navigationController.popoverPresentationController.sourceView = _collectionButton;
navigationController.popoverPresentationController.sourceRect = _collectionButton.bounds;
navigationController.popoverPresentationController.delegate = self;

[self presentViewController:navigationController animated:YES completion:nil];
}

Expand Down Expand Up @@ -825,4 +835,70 @@ - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - State Restoration

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[super encodeRestorableStateWithCoder:coder];

[coder encodeObject:self.mediaTypes forKey:@"mediaTypes"];
[coder encodeObject:self.assetCollection.localIdentifier forKey:@"assetCollection"];
[coder encodeObject:[self.selectedAssets valueForKey:@"localIdentifier"] forKey:@"selectedAssets"];
[coder encodeObject:_collectionPicker forKey:@"collectionPicker"];
[coder encodeObject:_collectionPicker.navigationController forKey:@"collectionPickerNavigationController"];
}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
[super decodeRestorableStateWithCoder:coder];

self.mediaTypes = [coder decodeObjectForKey:@"mediaTypes"];

NSString *assetCollectionIdentifier = [coder decodeObjectForKey:@"assetCollection"];
if (assetCollectionIdentifier != nil) {
self.assetCollection = [[PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[ assetCollectionIdentifier ] options:nil] firstObject];
}

NSOrderedSet *selectedAssetsIdentifiers = [coder decodeObjectForKey:@"selectedAssets"];
if (selectedAssetsIdentifiers != nil) {
NSMutableOrderedSet *assets = [NSMutableOrderedSet new];
for (PHAsset *asset in [PHAsset fetchAssetsWithLocalIdentifiers:selectedAssetsIdentifiers.array options:nil]) {
[assets addObject:asset];
}
[self addSelectedAssets:assets];
}

TNKCollectionPickerController *collectionPicker = [coder decodeObjectForKey:@"collectionPicker"];
if (collectionPicker != nil) {
_collectionPicker = collectionPicker;
_collectionPicker.assetFetchOptions = [self _assetFetchOptions];
if (_selectedAssets.count > 0) {
PHAssetCollection *collection = [PHAssetCollection transientAssetCollectionWithAssets:_selectedAssets.array title:NSLocalizedString(@"Selected", @"Collection name for selected photos")];
_collectionPicker.additionalAssetCollections = @[ collection ];
}
_collectionPicker.delegate = self;
}

UINavigationController *navigationController = [coder decodeObjectForKey:@"collectionPickerNavigationController"];
navigationController.modalPresentationStyle = UIModalPresentationPopover;
navigationController.popoverPresentationController.sourceView = _collectionButton;
navigationController.popoverPresentationController.sourceRect = _collectionButton.bounds;
navigationController.popoverPresentationController.delegate = self;
}

+ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
{
if ([identifierComponents.lastObject isEqual:@"TNKCollectionPickerController.NavigationController"]) {
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[TNKCollectionPickerController alloc] init]];
navigationController.restorationIdentifier = @"TNKCollectionPickerController.NavigationController";
navigationController.restorationClass = self;
navigationController.navigationBarHidden = YES;

return navigationController;
}

return nil;
}

@end

0 comments on commit 737e8bb

Please sign in to comment.