diff --git a/Pod/Classes/TNKCollectionPickerController.m b/Pod/Classes/TNKCollectionPickerController.m index be1e41c..eebc160 100644 --- a/Pod/Classes/TNKCollectionPickerController.m +++ b/Pod/Classes/TNKCollectionPickerController.m @@ -16,15 +16,13 @@ #import "PHCollection+TNKThumbnail.h" -@interface TNKCollectionPickerController () +@interface TNKCollectionPickerController () { NSArray *_collectionsFetchResults; NSCache *_collectionHiddenCache; NSCache *_assetCountCache; BOOL _needsRefetch; - - BOOL _navigationBarWasHidden; } @end @@ -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 { @@ -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]; @@ -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 { @@ -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; } } @@ -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]; } } @@ -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 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 diff --git a/Pod/Classes/TNKImagePickerController.m b/Pod/Classes/TNKImagePickerController.m index 0691354..bc62097 100644 --- a/Pod/Classes/TNKImagePickerController.m +++ b/Pod/Classes/TNKImagePickerController.m @@ -24,7 +24,7 @@ #define TNKObjectSpacing 1.0 -@interface TNKImagePickerController () +@interface TNKImagePickerController () { NSMutableOrderedSet *_selectedAssets; @@ -34,6 +34,8 @@ @interface TNKImagePickerController () 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]; } @@ -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