mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-14 09:22:33 +00:00
filter bar now works
This commit is contained in:
@@ -17,15 +17,27 @@
|
||||
|
||||
#define STATUS_BAR_ANIMATION_TIME 0.2
|
||||
|
||||
typedef enum {
|
||||
MPFilterNone = 0,
|
||||
MPFilterUrls = 2,
|
||||
MPFilterUsernames = 4,
|
||||
MPFilterTitles = 8,
|
||||
} MPFilterModeType;
|
||||
|
||||
NSString *const MPEntryTableUserNameColumnIdentifier = @"MPUserNameColumnIdentifier";
|
||||
NSString *const MPEntryTableTitleColumnIdentifier = @"MPTitleColumnIdentifier";
|
||||
NSString *const MPEntryTablePasswordColumnIdentifier = @"MPPasswordColumnIdentifier";
|
||||
NSString *const MPEntryTableParentColumnIdentifier = @"MPParentColumnIdentifier";
|
||||
NSString *const MPEntryTableURLColumnIdentifier = @"MPEntryTableURLColumnIdentifier";
|
||||
|
||||
NSString *const _MPTableImageCellView = @"ImageCell";
|
||||
NSString *const _MPTableStringCellView = @"StringCell";
|
||||
NSString *const _MPTAbleSecurCellView = @"PasswordCell";
|
||||
|
||||
NSString *const _toggleSearchURLButton = @"SearchURL";
|
||||
NSString *const _toggleSearchTitleButton = @"SearchTitle";
|
||||
NSString *const _toggleSearchUsernameButton = @"SearchUsername";
|
||||
|
||||
@interface MPEntryViewController ()
|
||||
|
||||
@property (retain) NSArrayController *entryArrayController;
|
||||
@@ -36,7 +48,20 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell";
|
||||
@property (assign) BOOL isStatusBarVisible;
|
||||
@property (retain) IBOutlet NSLayoutConstraint *statusBarToTop;
|
||||
@property (retain) IBOutlet NSLayoutConstraint *tableToTop;
|
||||
@property (assign) IBOutlet NSProgressIndicator *progressIndicator;
|
||||
|
||||
@property (assign) IBOutlet NSButton *searchTitleButton;
|
||||
@property (assign) IBOutlet NSButton *searchUsernameButton;
|
||||
@property (assign) IBOutlet NSButton *searchURLButton;
|
||||
|
||||
@property (assign, nonatomic) MPFilterModeType filterMode;
|
||||
@property (retain, nonatomic) NSDictionary *filterButtonToMode;
|
||||
|
||||
- (IBAction)toggleFilterSpace:(id)sender;
|
||||
|
||||
- (BOOL)shouldFilterURLs;
|
||||
- (BOOL)shouldFilterTitles;
|
||||
- (BOOL)shouldFilterUsernames;
|
||||
|
||||
- (BOOL)hasFilter;
|
||||
- (void)updateFilter;
|
||||
@@ -57,6 +82,11 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell";
|
||||
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
|
||||
if(self) {
|
||||
_isStatusBarVisible = YES;
|
||||
_filterMode = MPFilterTitles;
|
||||
_filterButtonToMode = [@{ _toggleSearchUsernameButton : @(MPFilterUsernames),
|
||||
_toggleSearchTitleButton : @(MPFilterTitles),
|
||||
_toggleSearchURLButton : @(MPFilterUrls)
|
||||
} retain];
|
||||
_entryArrayController = [[NSArrayController alloc] init];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(didChangeGroupSelectionInOutlineView:)
|
||||
@@ -67,26 +97,33 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell";
|
||||
}
|
||||
|
||||
- (void)didLoadView {
|
||||
|
||||
[self.view setWantsLayer:YES];
|
||||
[self hideStatusBarAnimated:NO];
|
||||
[[self.searchLabelTextField cell] setBackgroundStyle:NSBackgroundStyleRaised];
|
||||
|
||||
[self.searchURLButton setIdentifier:_toggleSearchURLButton];
|
||||
[self.searchUsernameButton setIdentifier:_toggleSearchUsernameButton];
|
||||
[self.searchTitleButton setIdentifier:_toggleSearchTitleButton];
|
||||
|
||||
[self.entryTable setDelegate:self];
|
||||
|
||||
NSTableColumn *parentColumn = [self.entryTable tableColumns][0];
|
||||
NSTableColumn *titleColumn = [self.entryTable tableColumns][1];
|
||||
NSTableColumn *userNameColumn = [self.entryTable tableColumns][2];
|
||||
NSTableColumn *passwordColumn = [self.entryTable tableColumns][3];
|
||||
NSTableColumn *urlColumn = [self.entryTable tableColumns][4];
|
||||
|
||||
[parentColumn setIdentifier:MPEntryTableParentColumnIdentifier];
|
||||
[titleColumn setIdentifier:MPEntryTableTitleColumnIdentifier];
|
||||
[userNameColumn setIdentifier:MPEntryTableUserNameColumnIdentifier];
|
||||
[passwordColumn setIdentifier:MPEntryTablePasswordColumnIdentifier];
|
||||
[urlColumn setIdentifier:MPEntryTableURLColumnIdentifier];
|
||||
|
||||
[[parentColumn headerCell] setStringValue:@"Group"];
|
||||
[[titleColumn headerCell] setStringValue:@"Title"];
|
||||
[[userNameColumn headerCell] setStringValue:@"Username"];
|
||||
[[passwordColumn headerCell] setStringValue:@"Password"];
|
||||
[[urlColumn headerCell] setStringValue:@"URL"];
|
||||
|
||||
[self.entryTable bind:NSContentBinding toObject:self.entryArrayController withKeyPath:@"arrangedObjects" options:nil];
|
||||
|
||||
@@ -102,6 +139,7 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell";
|
||||
const BOOL isGroupColumn = [[tableColumn identifier] isEqualToString:MPEntryTableParentColumnIdentifier];
|
||||
const BOOL isPasswordColum = [[tableColumn identifier] isEqualToString:MPEntryTablePasswordColumnIdentifier];
|
||||
const BOOL isUsernameColumn = [[tableColumn identifier] isEqualToString:MPEntryTableUserNameColumnIdentifier];
|
||||
const BOOL isURLColumn = [[tableColumn identifier] isEqualToString:MPEntryTableURLColumnIdentifier];
|
||||
|
||||
NSTableCellView *view = nil;
|
||||
if(isTitleColumn || isGroupColumn) {
|
||||
@@ -113,19 +151,19 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell";
|
||||
else {
|
||||
[[view textField] setStringValue:entry.parent.name];
|
||||
}
|
||||
return view;
|
||||
}
|
||||
|
||||
if( isPasswordColum ) {
|
||||
else if( isPasswordColum ) {
|
||||
view = [tableView makeViewWithIdentifier:_MPTAbleSecurCellView owner:self];
|
||||
[[view textField] setStringValue:entry.password];
|
||||
return view;
|
||||
}
|
||||
|
||||
if( isUsernameColumn ) {
|
||||
else if( isUsernameColumn || isURLColumn ) {
|
||||
view = [tableView makeViewWithIdentifier:_MPTableStringCellView owner:self];
|
||||
[[view textField] setStringValue:entry.username];
|
||||
return view;
|
||||
if(isURLColumn) {
|
||||
[[view textField] setStringValue:entry.url];
|
||||
}
|
||||
else {
|
||||
[[view textField] setStringValue:entry.username];
|
||||
}
|
||||
}
|
||||
|
||||
return view;
|
||||
@@ -165,11 +203,22 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell";
|
||||
if(openDatabase && [self hasFilter]) {
|
||||
[self showStatusBarAnimated:YES];
|
||||
|
||||
|
||||
dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
||||
dispatch_async(backgroundQueue, ^{
|
||||
|
||||
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF.title CONTAINS[cd] %@", self.filter];
|
||||
self.filteredEntries = [[openDatabase.root childEntries] filteredArrayUsingPredicate:filterPredicate];
|
||||
NSMutableArray *prediactes = [NSMutableArray arrayWithCapacity:3];
|
||||
if( [self shouldFilterTitles] ) {
|
||||
[prediactes addObject:[NSPredicate predicateWithFormat:@"SELF.title CONTAINS[cd] %@", self.filter]];
|
||||
}
|
||||
if( [self shouldFilterUsernames] ) {
|
||||
[prediactes addObject:[NSPredicate predicateWithFormat:@"SELF.username CONTAINS[cd] %@", self.filter]];
|
||||
}
|
||||
if( [self shouldFilterURLs] ) {
|
||||
[prediactes addObject:[NSPredicate predicateWithFormat:@"SELF.url CONTAINS[cd] %@", self.filter]];
|
||||
}
|
||||
NSPredicate *fullFilter = [NSCompoundPredicate orPredicateWithSubpredicates:prediactes];
|
||||
self.filteredEntries = [[openDatabase.root childEntries] filteredArrayUsingPredicate:fullFilter];
|
||||
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
[self.entryArrayController setContent:self.filteredEntries];
|
||||
@@ -192,10 +241,15 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell";
|
||||
if(self.isStatusBarVisible) {
|
||||
return; // nothign to to
|
||||
}
|
||||
|
||||
[self.searchTitleButton setState:[self shouldFilterTitles] ? NSOnState : NSOffState];
|
||||
[self.searchURLButton setState:[self shouldFilterURLs] ? NSOnState : NSOffState ];
|
||||
[self.searchUsernameButton setState:[self shouldFilterUsernames] ? NSOnState : NSOffState];
|
||||
|
||||
self.isStatusBarVisible = YES;
|
||||
self.statusBarToTop.constant = 0;
|
||||
self.tableToTop.constant = [self.statusBar frame].size.height;
|
||||
|
||||
|
||||
if(animate) {
|
||||
[NSAnimationContext runAnimationGroup:^(NSAnimationContext* context) {
|
||||
context.duration = STATUS_BAR_ANIMATION_TIME;
|
||||
@@ -230,5 +284,43 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell";
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark Actions
|
||||
|
||||
- (void)toggleFilterSpace:(id)sender {
|
||||
NSButton *button = sender;
|
||||
NSNumber *value = self.filterButtonToMode[[button identifier]];
|
||||
MPFilterModeType toggledMode = (MPFilterModeType)[value intValue];
|
||||
switch ([button state]) {
|
||||
case NSOnState:
|
||||
self.filterMode |= toggledMode;
|
||||
break;
|
||||
|
||||
case NSOffState:
|
||||
self.filterMode ^= toggledMode;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setFilterMode:(MPFilterModeType)newFilterMode {
|
||||
if(_filterMode != newFilterMode) {
|
||||
_filterMode = newFilterMode;
|
||||
[self updateFilter];
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)shouldFilterTitles {
|
||||
return ( MPFilterNone != (self.filterMode & MPFilterTitles));
|
||||
}
|
||||
|
||||
- (BOOL)shouldFilterURLs {
|
||||
return ( MPFilterNone != (self.filterMode & MPFilterUrls));
|
||||
}
|
||||
|
||||
- (BOOL)shouldFilterUsernames {
|
||||
return ( MPFilterNone != (self.filterMode & MPFilterUsernames));
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user