2014-03-20 00:09:25 +00:00
|
|
|
/**
|
|
|
|
* Copyright Maarten Billemont (http://www.lhunath.com, lhunath@lyndir.com)
|
|
|
|
*
|
|
|
|
* See the enclosed file LICENSE for license information (LGPLv3). If you did
|
|
|
|
* not receive this file, see http://www.gnu.org/licenses/lgpl-3.0.txt
|
|
|
|
*
|
|
|
|
* @author Maarten Billemont <lhunath@lyndir.com>
|
|
|
|
* @license http://www.gnu.org/licenses/lgpl-3.0.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
//
|
|
|
|
// MPPasswordsViewController.h
|
|
|
|
// MPPasswordsViewController
|
|
|
|
//
|
|
|
|
// Created by lhunath on 2014-03-08.
|
|
|
|
// Copyright, lhunath (Maarten Billemont) 2014. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "MPPasswordsViewController.h"
|
|
|
|
#import "MPiOSAppDelegate.h"
|
|
|
|
#import "MPAppDelegate_Store.h"
|
2014-04-07 03:34:18 +00:00
|
|
|
#import "MPPasswordLargeCell.h"
|
|
|
|
#import "MPPasswordTypesCell.h"
|
2014-04-20 15:09:49 +00:00
|
|
|
#import "MPPopdownSegue.h"
|
2014-04-22 03:35:29 +00:00
|
|
|
#import "MPAppDelegate_Key.h"
|
2014-04-25 01:43:47 +00:00
|
|
|
#import "MPCoachmarkViewController.h"
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-03-20 11:15:37 +00:00
|
|
|
@interface MPPasswordsViewController()<NSFetchedResultsControllerDelegate>
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
@property(nonatomic, strong) IBOutlet UINavigationBar *navigationBar;
|
2014-03-20 11:15:37 +00:00
|
|
|
@property(nonatomic, readonly) NSString *query;
|
2014-04-07 03:34:18 +00:00
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MPPasswordsViewController {
|
|
|
|
__weak id _storeObserver;
|
|
|
|
__weak id _mocObserver;
|
|
|
|
NSArray *_notificationObservers;
|
2014-03-20 20:49:33 +00:00
|
|
|
__weak UITapGestureRecognizer *_passwordsDismissRecognizer;
|
2014-04-07 03:34:18 +00:00
|
|
|
NSFetchedResultsController *_fetchedResultsController;
|
|
|
|
BOOL _exactMatch;
|
|
|
|
UIColor *_backgroundColor;
|
|
|
|
UIColor *_darkenedBackgroundColor;
|
2014-04-20 15:09:49 +00:00
|
|
|
__weak UIViewController *_popdownVC;
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-20 15:09:49 +00:00
|
|
|
#pragma mark - Life
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
- (void)viewDidLoad {
|
|
|
|
|
|
|
|
[super viewDidLoad];
|
|
|
|
|
2014-04-25 01:43:47 +00:00
|
|
|
_backgroundColor = self.passwordCollectionView.backgroundColor;
|
|
|
|
_darkenedBackgroundColor = [_backgroundColor colorWithAlphaComponent:0.6f];
|
|
|
|
_coachmark = [MPCoachmark coachmarkForClass:[self class] version:0];
|
2014-04-07 03:34:18 +00:00
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
self.view.backgroundColor = [UIColor clearColor];
|
2014-04-07 03:34:18 +00:00
|
|
|
[self.passwordCollectionView automaticallyAdjustInsetsForKeyboard];
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
|
|
|
|
|
|
|
[super viewWillAppear:animated];
|
|
|
|
|
|
|
|
[self registerObservers];
|
|
|
|
[self observeStore];
|
2014-04-26 18:03:44 +00:00
|
|
|
[self updateFromConfig];
|
2014-04-12 18:43:41 +00:00
|
|
|
[self updatePasswords];
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 01:43:47 +00:00
|
|
|
- (void)viewDidAppear:(BOOL)animated {
|
|
|
|
|
|
|
|
[super viewDidAppear:animated];
|
|
|
|
|
|
|
|
PearlMainQueueAfter( 1, ^{
|
|
|
|
if (!self.coachmark.coached)
|
|
|
|
[self performSegueWithIdentifier:@"coachmarks" sender:self];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
- (void)viewWillDisappear:(BOOL)animated {
|
|
|
|
|
|
|
|
[super viewWillDisappear:animated];
|
|
|
|
|
|
|
|
[self removeObservers];
|
|
|
|
[self stopObservingStore];
|
|
|
|
}
|
|
|
|
|
2014-04-20 15:09:49 +00:00
|
|
|
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
|
|
|
|
|
|
|
if ([segue.identifier isEqualToString:@"popdown"])
|
|
|
|
_popdownVC = segue.destinationViewController;
|
2014-04-25 01:43:47 +00:00
|
|
|
if ([segue.identifier isEqualToString:@"coachmarks"])
|
|
|
|
((MPCoachmarkViewController *)segue.destinationViewController).coachmark = self.coachmark;
|
2014-04-20 15:09:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-10 13:18:46 +00:00
|
|
|
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
|
|
|
|
|
|
|
|
[self.passwordCollectionView.collectionViewLayout invalidateLayout];
|
|
|
|
|
|
|
|
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
|
|
|
|
}
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
#pragma mark - UICollectionViewDelegateFlowLayout
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-05-11 01:42:11 +00:00
|
|
|
- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
|
|
|
|
referenceSizeForHeaderInSection:(NSInteger)section {
|
|
|
|
|
|
|
|
return CGSizeMake( collectionView.bounds.size.width, CGPointFromCGRectBottom( self.passwordsSearchBar.frame ).y );
|
|
|
|
}
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
|
|
|
|
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
if (collectionView == self.passwordCollectionView) {
|
|
|
|
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)collectionViewLayout;
|
2014-05-10 13:18:46 +00:00
|
|
|
CGFloat itemWidth = UIEdgeInsetsInsetRect(self.passwordCollectionView.bounds, layout.sectionInset).size.width;
|
2014-05-13 11:27:11 +00:00
|
|
|
return CGSizeMake( itemWidth, 100 );
|
2014-04-07 03:34:18 +00:00
|
|
|
}
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
Throw(@"Unexpected collection view: %@", collectionView);
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UICollectionViewDataSource
|
|
|
|
|
2014-03-20 11:15:37 +00:00
|
|
|
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
if (collectionView == self.passwordCollectionView)
|
2014-04-12 18:43:41 +00:00
|
|
|
return [self.fetchedResultsController.sections count];
|
2014-04-07 03:34:18 +00:00
|
|
|
|
|
|
|
Throw(@"Unexpected collection view: %@", collectionView);
|
2014-03-20 11:15:37 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
if (collectionView == self.passwordCollectionView)
|
2014-04-12 18:43:41 +00:00
|
|
|
return ![MPiOSAppDelegate get].activeUserOID? 0:
|
|
|
|
((id<NSFetchedResultsSectionInfo>)self.fetchedResultsController.sections[section]).numberOfObjects +
|
|
|
|
(!_exactMatch && [[self query] length]? 1: 0);
|
2014-03-20 11:15:37 +00:00
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
Throw(@"Unexpected collection view: %@", collectionView);
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
|
2014-03-20 11:15:37 +00:00
|
|
|
if (collectionView == self.passwordCollectionView) {
|
2014-04-07 03:34:18 +00:00
|
|
|
[UIView setAnimationsEnabled:NO];
|
2014-05-14 02:56:06 +00:00
|
|
|
MPPasswordTypesCell *cell;
|
2014-04-07 03:34:18 +00:00
|
|
|
if (indexPath.item < ((id<NSFetchedResultsSectionInfo>)self.fetchedResultsController.sections[indexPath.section]).numberOfObjects) {
|
2014-03-20 11:15:37 +00:00
|
|
|
MPElementEntity *element = [self.fetchedResultsController objectAtIndexPath:indexPath];
|
2014-05-13 11:27:11 +00:00
|
|
|
cell = [MPPasswordTypesCell dequeueCellForElement:element fromCollectionView:collectionView atIndexPath:indexPath];
|
2014-03-20 11:15:37 +00:00
|
|
|
}
|
2014-04-07 03:34:18 +00:00
|
|
|
else
|
2014-05-13 11:27:11 +00:00
|
|
|
// New Site.
|
2014-04-07 03:34:18 +00:00
|
|
|
cell = [MPPasswordTypesCell dequeueCellForTransientSite:self.query fromCollectionView:collectionView atIndexPath:indexPath];
|
2014-05-13 11:27:11 +00:00
|
|
|
cell.passwordsViewController = self;
|
2014-04-07 03:34:18 +00:00
|
|
|
|
|
|
|
[UIView setAnimationsEnabled:YES];
|
2014-05-11 01:42:11 +00:00
|
|
|
dbg_return(cell, indexPath);
|
2014-03-20 11:15:37 +00:00
|
|
|
}
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
Throw(@"Unexpected collection view: %@", collectionView);
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
2014-05-13 11:27:11 +00:00
|
|
|
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind
|
|
|
|
atIndexPath:(NSIndexPath *)indexPath {
|
2014-04-07 03:34:18 +00:00
|
|
|
|
2014-05-13 11:27:11 +00:00
|
|
|
return [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"MPPasswordHeader" forIndexPath:indexPath];
|
2014-04-07 03:34:18 +00:00
|
|
|
}
|
|
|
|
|
2014-05-13 11:27:11 +00:00
|
|
|
#pragma mark - NSFetchedResultsControllerDelegate
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath
|
|
|
|
forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
|
|
|
|
|
|
|
|
if (controller == _fetchedResultsController) {
|
2014-05-13 11:27:11 +00:00
|
|
|
[self.passwordCollectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
|
|
|
|
[self.passwordCollectionView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section]];
|
2014-04-07 03:34:18 +00:00
|
|
|
}
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo
|
|
|
|
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
|
|
|
|
|
2014-05-13 11:27:11 +00:00
|
|
|
if (controller == _fetchedResultsController)
|
|
|
|
[self.passwordCollectionView reloadData];
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 11:15:37 +00:00
|
|
|
#pragma mark - UISearchBarDelegate
|
|
|
|
|
2014-04-13 17:04:18 +00:00
|
|
|
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
|
|
|
|
|
|
|
|
if (searchBar == self.passwordsSearchBar) {
|
|
|
|
searchBar.text = nil;
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2014-03-20 11:15:37 +00:00
|
|
|
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
|
|
|
|
|
2014-05-13 11:27:11 +00:00
|
|
|
if (searchBar == self.passwordsSearchBar)
|
2014-04-07 03:34:18 +00:00
|
|
|
[UIView animateWithDuration:0.3f animations:^{
|
2014-04-25 01:43:47 +00:00
|
|
|
self.passwordCollectionView.backgroundColor = _darkenedBackgroundColor;
|
2014-04-07 03:34:18 +00:00
|
|
|
}];
|
2014-03-20 11:15:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
|
|
|
|
|
|
|
|
if (searchBar == self.passwordsSearchBar) {
|
|
|
|
self.passwordsSearchBar.showsCancelButton = NO;
|
2014-03-20 20:49:33 +00:00
|
|
|
if (_passwordsDismissRecognizer)
|
|
|
|
[self.view removeGestureRecognizer:_passwordsDismissRecognizer];
|
2014-03-20 11:15:37 +00:00
|
|
|
|
|
|
|
[UIView animateWithDuration:0.3f animations:^{
|
2014-04-25 01:43:47 +00:00
|
|
|
self.passwordCollectionView.backgroundColor = _backgroundColor;
|
2014-03-20 11:15:37 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-20 20:49:33 +00:00
|
|
|
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
|
|
|
|
|
|
|
|
[searchBar resignFirstResponder];
|
|
|
|
}
|
|
|
|
|
2014-03-20 11:15:37 +00:00
|
|
|
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
|
|
|
|
|
|
|
|
if (searchBar == self.passwordsSearchBar)
|
|
|
|
[self updatePasswords];
|
|
|
|
}
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
|
|
|
|
#pragma mark - Private
|
|
|
|
|
|
|
|
- (void)registerObservers {
|
|
|
|
|
|
|
|
if ([_notificationObservers count])
|
|
|
|
return;
|
|
|
|
|
|
|
|
Weakify(self);
|
|
|
|
_notificationObservers = @[
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserverForName:UIApplicationWillResignActiveNotification object:nil
|
|
|
|
queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
|
|
|
|
Strongify(self);
|
|
|
|
|
|
|
|
self.passwordSelectionContainer.alpha = 0;
|
|
|
|
}],
|
2014-04-07 03:34:18 +00:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserverForName:MPSignedOutNotification object:nil
|
|
|
|
queue:nil usingBlock:^(NSNotification *note) {
|
|
|
|
Strongify(self);
|
|
|
|
|
|
|
|
_fetchedResultsController = nil;
|
|
|
|
self.passwordsSearchBar.text = nil;
|
|
|
|
[self updatePasswords];
|
|
|
|
}],
|
2014-03-20 00:09:25 +00:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserverForName:UIApplicationDidBecomeActiveNotification object:nil
|
|
|
|
queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
|
|
|
|
Strongify(self);
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
[self updatePasswords];
|
2014-03-20 00:09:25 +00:00
|
|
|
[UIView animateWithDuration:1 animations:^{
|
|
|
|
self.passwordSelectionContainer.alpha = 1;
|
|
|
|
}];
|
|
|
|
}],
|
2014-04-26 18:03:44 +00:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserverForName:MPCheckConfigNotification object:nil
|
|
|
|
queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
|
|
|
|
[self updateFromConfig];
|
|
|
|
}],
|
2014-03-20 00:09:25 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeObservers {
|
|
|
|
|
|
|
|
for (id observer in _notificationObservers)
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:observer];
|
|
|
|
_notificationObservers = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)observeStore {
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
Weakify(self);
|
2014-03-20 00:09:25 +00:00
|
|
|
|
2014-03-20 11:15:37 +00:00
|
|
|
NSManagedObjectContext *mainContext = [MPiOSAppDelegate managedObjectContextForMainThreadIfReady];
|
|
|
|
if (!_mocObserver && mainContext)
|
|
|
|
_mocObserver = [[NSNotificationCenter defaultCenter]
|
|
|
|
addObserverForName:NSManagedObjectContextObjectsDidChangeNotification object:mainContext
|
|
|
|
queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
|
2014-04-13 17:04:18 +00:00
|
|
|
// Strongify(self);
|
|
|
|
// [self updatePasswords];
|
2014-03-20 11:15:37 +00:00
|
|
|
}];
|
|
|
|
if (!_storeObserver)
|
|
|
|
_storeObserver = [[NSNotificationCenter defaultCenter]
|
|
|
|
addObserverForName:USMStoreDidChangeNotification object:nil
|
|
|
|
queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
|
|
|
|
Strongify(self);
|
2014-04-07 03:34:18 +00:00
|
|
|
_fetchedResultsController = nil;
|
2014-03-20 11:15:37 +00:00
|
|
|
[self updatePasswords];
|
|
|
|
}];
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopObservingStore {
|
|
|
|
|
|
|
|
if (_mocObserver)
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:_mocObserver];
|
|
|
|
if (_storeObserver)
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:_storeObserver];
|
|
|
|
}
|
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
- (void)updateFromConfig {
|
|
|
|
|
|
|
|
self.passwordsSearchBar.keyboardType = [[MPiOSConfig get].dictationSearch boolValue]? UIKeyboardTypeDefault: UIKeyboardTypeURL;
|
|
|
|
}
|
|
|
|
|
2014-03-20 11:15:37 +00:00
|
|
|
- (void)updatePasswords {
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
NSString *query = self.query;
|
|
|
|
NSManagedObjectID *activeUserOID = [MPiOSAppDelegate get].activeUserOID;
|
2014-04-12 18:43:41 +00:00
|
|
|
if (!activeUserOID) {
|
2014-04-07 03:34:18 +00:00
|
|
|
self.passwordsSearchBar.text = nil;
|
2014-04-22 03:35:29 +00:00
|
|
|
PearlMainQueue( ^{
|
|
|
|
[self.passwordCollectionView reloadData];
|
|
|
|
[self.passwordCollectionView setContentOffset:CGPointMake( 0, -self.passwordCollectionView.contentInset.top ) animated:YES];
|
|
|
|
} );
|
2014-04-07 03:34:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-03-20 11:15:37 +00:00
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
[self.fetchedResultsController.managedObjectContext performBlock:^{
|
2014-03-20 11:15:37 +00:00
|
|
|
NSError *error = nil;
|
2014-04-12 18:43:41 +00:00
|
|
|
self.fetchedResultsController.fetchRequest.predicate =
|
|
|
|
[query length]?
|
|
|
|
[NSPredicate predicateWithFormat:@"user == %@ AND name BEGINSWITH[cd] %@", activeUserOID, query]:
|
|
|
|
[NSPredicate predicateWithFormat:@"user == %@", activeUserOID];
|
2014-03-20 11:15:37 +00:00
|
|
|
if (![self.fetchedResultsController performFetch:&error])
|
|
|
|
err(@"Couldn't fetch elements: %@", error);
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
_exactMatch = NO;
|
|
|
|
for (MPElementEntity *entity in self.fetchedResultsController.fetchedObjects)
|
|
|
|
if ([entity.name isEqualToString:query]) {
|
|
|
|
_exactMatch = YES;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
PearlMainQueue( ^{
|
|
|
|
[self.passwordCollectionView performBatchUpdates:^{
|
|
|
|
NSInteger fromSections = self.passwordCollectionView.numberOfSections;
|
|
|
|
NSInteger toSections = [self numberOfSectionsInCollectionView:self.passwordCollectionView];
|
|
|
|
for (int section = 0; section < MAX(toSections, fromSections); section++) {
|
|
|
|
if (section >= fromSections) {
|
|
|
|
dbg(@"insertSections:%d", section);
|
|
|
|
[self.passwordCollectionView insertSections:[NSIndexSet indexSetWithIndex:section]];
|
|
|
|
}
|
|
|
|
else if (section >= toSections) {
|
|
|
|
dbg(@"deleteSections:%d", section);
|
|
|
|
[self.passwordCollectionView deleteSections:[NSIndexSet indexSetWithIndex:section]];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dbg(@"reloadSections:%d", section);
|
|
|
|
[self.passwordCollectionView reloadSections:[NSIndexSet indexSetWithIndex:section]];
|
|
|
|
}
|
|
|
|
}
|
2014-04-22 03:35:29 +00:00
|
|
|
} completion:^(BOOL finished) {
|
|
|
|
if (finished)
|
|
|
|
[self.passwordCollectionView setContentOffset:CGPointMake( 0, -self.passwordCollectionView.contentInset.top )
|
|
|
|
animated:YES];
|
|
|
|
}];
|
2014-04-07 03:34:18 +00:00
|
|
|
} );
|
2014-03-20 11:15:37 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
#pragma mark - Properties
|
|
|
|
|
2014-03-20 11:15:37 +00:00
|
|
|
- (NSString *)query {
|
|
|
|
|
|
|
|
return [self.passwordsSearchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSFetchedResultsController *)fetchedResultsController {
|
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
if (!_fetchedResultsController) {
|
|
|
|
[MPiOSAppDelegate managedObjectContextForMainThreadPerformBlockAndWait:^(NSManagedObjectContext *mainContext) {
|
2014-03-20 11:15:37 +00:00
|
|
|
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass( [MPElementEntity class] )];
|
|
|
|
fetchRequest.sortDescriptors = @[
|
|
|
|
[[NSSortDescriptor alloc] initWithKey:NSStringFromSelector( @selector(lastUsed) ) ascending:NO]
|
|
|
|
];
|
|
|
|
fetchRequest.fetchBatchSize = 10;
|
|
|
|
_fetchedResultsController = [[NSFetchedResultsController alloc]
|
2014-04-07 03:34:18 +00:00
|
|
|
initWithFetchRequest:fetchRequest managedObjectContext:mainContext sectionNameKeyPath:nil cacheName:nil];
|
2014-03-20 11:15:37 +00:00
|
|
|
_fetchedResultsController.delegate = self;
|
|
|
|
}];
|
2014-04-07 03:34:18 +00:00
|
|
|
[self observeStore];
|
|
|
|
}
|
2014-03-20 11:15:37 +00:00
|
|
|
|
|
|
|
return _fetchedResultsController;
|
|
|
|
}
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
- (void)setActive:(BOOL)active {
|
|
|
|
|
2014-04-12 18:43:41 +00:00
|
|
|
[self setActive:active animated:NO completion:nil];
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-12 18:43:41 +00:00
|
|
|
- (void)setActive:(BOOL)active animated:(BOOL)animated completion:(void (^)(BOOL finished))completion {
|
2014-03-20 00:09:25 +00:00
|
|
|
|
|
|
|
_active = active;
|
|
|
|
|
2014-04-12 18:43:41 +00:00
|
|
|
[UIView animateWithDuration:animated? 0.4f: 0 animations:^{
|
2014-06-04 01:04:22 +00:00
|
|
|
[self.navigationBarToTopConstraint layoutWithPriority:active? 1: UILayoutPriorityDefaultHigh];
|
|
|
|
[self.passwordsToBottomConstraint layoutWithPriority:active? 1: UILayoutPriorityDefaultHigh];
|
2014-04-12 18:43:41 +00:00
|
|
|
} completion:completion];
|
2014-03-20 00:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Actions
|
|
|
|
|
2014-04-20 15:09:49 +00:00
|
|
|
- (IBAction)dismissPopdown:(id)sender {
|
2014-04-12 18:43:41 +00:00
|
|
|
|
2014-04-20 15:09:49 +00:00
|
|
|
if (_popdownVC)
|
|
|
|
[[[MPPopdownSegue alloc] initWithIdentifier:@"unwind-popdown" source:_popdownVC destination:self] perform];
|
|
|
|
else
|
|
|
|
self.popdownToTopConstraint.priority = UILayoutPriorityDefaultHigh;
|
2014-04-12 18:43:41 +00:00
|
|
|
}
|
|
|
|
|
2014-04-22 03:35:29 +00:00
|
|
|
- (IBAction)signOut:(id)sender {
|
|
|
|
|
|
|
|
[[MPiOSAppDelegate get] signOutAnimated:YES];
|
|
|
|
}
|
|
|
|
|
2014-03-20 00:09:25 +00:00
|
|
|
@end
|