2011-11-30 21:42:40 +00:00
|
|
|
//
|
|
|
|
// OPMainViewController.m
|
|
|
|
// OnePassword
|
|
|
|
//
|
|
|
|
// Created by Maarten Billemont on 24/11/11.
|
|
|
|
// Copyright (c) 2011 Lyndir. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "OPMainViewController.h"
|
2012-01-05 00:44:15 +00:00
|
|
|
#import "OPAppDelegate.h"
|
|
|
|
#import "OPContentViewController.h"
|
|
|
|
|
|
|
|
#import <MobileCoreServices/MobileCoreServices.h>
|
|
|
|
|
|
|
|
|
|
|
|
@interface OPMainViewController (Private)
|
|
|
|
|
|
|
|
- (void)updateAnimated:(BOOL)animated;
|
|
|
|
- (void)updateWasAnimated:(BOOL)animated;
|
|
|
|
|
|
|
|
@end
|
2011-11-30 21:42:40 +00:00
|
|
|
|
|
|
|
@implementation OPMainViewController
|
2012-01-05 00:44:15 +00:00
|
|
|
@synthesize activeElement = _activeElement;
|
|
|
|
@synthesize searchResultsController = _searchResultsController;
|
|
|
|
@synthesize typeLabel = _typeLabel;
|
|
|
|
@synthesize contentType = _contentType;
|
|
|
|
@synthesize contentField = _contentField;
|
|
|
|
@synthesize contentTextView = _contentTextView;
|
2011-11-30 21:42:40 +00:00
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
#pragma mark - View lifecycle
|
2011-11-30 21:42:40 +00:00
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
|
|
|
|
|
|
|
|
return [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad || interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
|
2011-11-30 21:42:40 +00:00
|
|
|
}
|
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
|
|
|
|
|
|
|
if ([[segue identifier] isEqualToString:@"OP_Main_ChooseType"])
|
|
|
|
[[segue destinationViewController] setDelegate:self];
|
|
|
|
if ([[segue identifier] isEqualToString:@"OP_Main_Content"])
|
|
|
|
((OPContentViewController *)[segue destinationViewController]).activeElement = self.activeElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
|
|
|
|
|
|
|
[super viewWillAppear:animated];
|
|
|
|
|
|
|
|
[self updateAnimated:NO];
|
|
|
|
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated];
|
|
|
|
}
|
2011-11-30 21:42:40 +00:00
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
- (void)viewDidLoad {
|
|
|
|
|
|
|
|
// Because IB's edit button doesn't auto-toggle self.editable like editButtonItem does.
|
|
|
|
self.navigationItem.rightBarButtonItem = self.editButtonItem;
|
|
|
|
|
2011-11-30 21:42:40 +00:00
|
|
|
[super viewDidLoad];
|
|
|
|
}
|
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
- (void)viewDidUnload {
|
|
|
|
|
|
|
|
[self setContentField:nil];
|
|
|
|
[self setTypeLabel:nil];
|
|
|
|
|
|
|
|
[self setContentType:nil];
|
|
|
|
[self setContentTextView:nil];
|
|
|
|
[self setSearchResultsController:nil];
|
2011-11-30 21:42:40 +00:00
|
|
|
[super viewDidUnload];
|
|
|
|
}
|
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
|
|
|
|
|
|
|
|
[super setEditing:editing animated:animated];
|
|
|
|
|
|
|
|
[self updateAnimated:animated];
|
2011-11-30 21:42:40 +00:00
|
|
|
}
|
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
- (void)updateAnimated:(BOOL)animated {
|
|
|
|
|
|
|
|
[[OPAppDelegate get] saveContext];
|
|
|
|
|
|
|
|
if (animated)
|
|
|
|
[UIView animateWithDuration:0.2 animations:^{
|
|
|
|
[self updateWasAnimated:YES];
|
|
|
|
}];
|
|
|
|
else
|
|
|
|
[self updateWasAnimated:NO];
|
2011-11-30 21:42:40 +00:00
|
|
|
}
|
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
- (void)updateWasAnimated:(BOOL)animated {
|
|
|
|
|
|
|
|
self.typeLabel.text = self.activeElement? NSStringFromOPElementType(self.activeElement.type): @"moo";
|
|
|
|
|
|
|
|
self.contentTextView.alpha = self.contentType.selectedSegmentIndex == OPElementContentTypeNote? 1: 0;
|
|
|
|
self.contentTextView.editable = self.editing && self.activeElement.type & OPElementTypeStored;
|
|
|
|
|
|
|
|
self.contentType.alpha = self.editing && self.activeElement.type & OPElementTypeStored? 1: 0;
|
|
|
|
self.contentType.selectedSegmentIndex = self.activeElement.contentType;
|
|
|
|
|
|
|
|
self.contentField.alpha = self.contentType.selectedSegmentIndex == OPElementContentTypePassword? 1: 0;
|
|
|
|
self.contentField.enabled = self.editing && self.activeElement.type & OPElementTypeStored;
|
|
|
|
self.contentField.clearButtonMode = self.contentField.enabled? UITextFieldViewModeAlways: UITextFieldViewModeNever;
|
|
|
|
self.contentField.text = @"...";
|
|
|
|
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
|
|
|
|
NSString *contentDescription = self.activeElement.contentDescription;
|
|
|
|
if (contentDescription)
|
|
|
|
[self.activeElement use];
|
|
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
self.contentField.text = contentDescription;
|
|
|
|
});
|
|
|
|
});
|
2011-11-30 21:42:40 +00:00
|
|
|
}
|
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
#pragma mark - Protocols
|
|
|
|
|
|
|
|
- (IBAction)didChangeContentType:(UISegmentedControl *)sender {
|
|
|
|
|
|
|
|
self.activeElement.contentType = self.contentType.selectedSegmentIndex;
|
|
|
|
[self updateAnimated:YES];
|
2011-11-30 21:42:40 +00:00
|
|
|
}
|
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
- (void)didSelectType:(OPElementType)type {
|
|
|
|
|
|
|
|
self.activeElement.type = type;
|
|
|
|
[self updateAnimated:YES];
|
2011-11-30 21:42:40 +00:00
|
|
|
}
|
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
- (void)didSelectElement:(OPElementEntity *)element {
|
|
|
|
|
|
|
|
self.activeElement = element;
|
|
|
|
[self updateAnimated:YES];
|
|
|
|
|
|
|
|
[self.searchDisplayController setActive:NO animated:YES];
|
|
|
|
self.searchDisplayController.searchBar.text = self.activeElement.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
|
|
|
|
|
|
|
|
[self updateAnimated:YES];
|
2011-11-30 21:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|