From d0dfa0ecac57f574a5a212bc12589fda86b58c43 Mon Sep 17 00:00:00 2001 From: michael starke Date: Mon, 13 Nov 2017 18:47:36 +0100 Subject: [PATCH] Added support for tag completion --- Cartfile | 2 +- Cartfile.resolved | 2 +- MacPass/MPEntryInspectorViewController.m | 6 ++++++ MacPass/MPTagsTokenFieldDelegate.h | 3 +++ MacPass/MPTagsTokenFieldDelegate.m | 27 ++++++++++-------------- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Cartfile b/Cartfile index 70c3834b..7f0353ad 100644 --- a/Cartfile +++ b/Cartfile @@ -1,3 +1,3 @@ github "sparkle-project/Sparkle" ~> 1.18.1 -github "MacPass/KeePassKit" ~> 1.6 +github "MacPass/KeePassKit" ~> 1.7 github "mstarke/HNHUi" ~> 1.4.1 diff --git a/Cartfile.resolved b/Cartfile.resolved index 22d55993..7320ca2f 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,3 +1,3 @@ -github "MacPass/KeePassKit" "1.6" +github "MacPass/KeePassKit" "1.7" github "mstarke/HNHUi" "1.4.1" github "sparkle-project/Sparkle" "1.18.1" diff --git a/MacPass/MPEntryInspectorViewController.m b/MacPass/MPEntryInspectorViewController.m index 98f7c38b..9d20c626 100644 --- a/MacPass/MPEntryInspectorViewController.m +++ b/MacPass/MPEntryInspectorViewController.m @@ -28,6 +28,7 @@ #import "MPPasswordCreatorViewController.h" #import "MPWindowAssociationsTableViewDelegate.h" #import "MPWindowTitleComboBoxDelegate.h" +#import "MPTagsTokenFieldDelegate.h" #import "MPAutotypeBuilderViewController.h" #import "MPReferenceBuilderViewController.h" @@ -64,6 +65,7 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) { MPAttachmentTableDataSource *_attachmentDataSource; MPWindowAssociationsTableViewDelegate *_windowAssociationsTableDelegate; MPWindowTitleComboBoxDelegate *_windowTitleMenuDelegate; + MPTagsTokenFieldDelegate *_tagTokenFieldDelegate; } @property (nonatomic, assign) BOOL showPassword; @@ -93,6 +95,8 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) { _attachmentDataSource = [[MPAttachmentTableDataSource alloc] init]; _windowAssociationsTableDelegate = [[MPWindowAssociationsTableViewDelegate alloc] init]; _windowTitleMenuDelegate = [[MPWindowTitleComboBoxDelegate alloc] init]; + _tagTokenFieldDelegate = [[MPTagsTokenFieldDelegate alloc] init]; + _tagTokenFieldDelegate.viewController = self; _attachmentTableDelegate.viewController = self; _customFieldTableDelegate.viewController = self; _activeTab = MPEntryTabGeneral; @@ -159,6 +163,8 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) { [self.passwordTextField bind:NSStringFromSelector(@selector(showPassword)) toObject:self withKeyPath:NSStringFromSelector(@selector(showPassword)) options:nil]; [self.togglePassword bind:NSValueBinding toObject:self withKeyPath:NSStringFromSelector(@selector(showPassword)) options:nil]; + self.tagsTokenField.delegate = _tagTokenFieldDelegate; + [self _setupViewBindings]; } diff --git a/MacPass/MPTagsTokenFieldDelegate.h b/MacPass/MPTagsTokenFieldDelegate.h index 596af15a..0b25cda9 100644 --- a/MacPass/MPTagsTokenFieldDelegate.h +++ b/MacPass/MPTagsTokenFieldDelegate.h @@ -21,7 +21,10 @@ // #import +#import "MPEntryInspectorViewController.h" @interface MPTagsTokenFieldDelegate : NSObject +@property (weak) MPEntryInspectorViewController *viewController; + @end diff --git a/MacPass/MPTagsTokenFieldDelegate.m b/MacPass/MPTagsTokenFieldDelegate.m index 40e7c0d9..d5456ab3 100644 --- a/MacPass/MPTagsTokenFieldDelegate.m +++ b/MacPass/MPTagsTokenFieldDelegate.m @@ -22,23 +22,18 @@ #import "MPTagsTokenFieldDelegate.h" +#import "MPDocument.h" + +#import "KeePassKit/KeePassKit.h" + @implementation MPTagsTokenFieldDelegate -// Each element in the array should be an NSString or an array of NSStrings. -// substring is the partial string that is being completed. tokenIndex is the index of the token being completed. -// selectedIndex allows you to return by reference an index specifying which of the completions should be selected initially. -// The default behavior is not to have any completions. -//- (nullable NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(nullable NSInteger *)selectedIndex; - -// return an array of represented objects you want to add. -// If you want to reject the add, return an empty array. -// returning nil will cause an error. -//- (NSArray *)tokenField:(NSTokenField *)tokenField shouldAddObjects:(NSArray *)tokens atIndex:(NSUInteger)index; - -// If you return nil or don't implement these delegate methods, we will assume -// editing string = display string = represented object -//- (nullable NSString *)tokenField:(NSTokenField *)tokenField displayStringForRepresentedObject:(id)representedObject; -//- (nullable NSString *)tokenField:(NSTokenField *)tokenField editingStringForRepresentedObject:(id)representedObject; -//- (id)tokenField:(NSTokenField *)tokenField representedObjectForEditingString: (NSString *)editingString; +- (NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex { + MPDocument *document = self.viewController.windowController.document; + + return [document.tree.availableTags filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary * _Nullable bindings) { + return [evaluatedObject hasPrefix:substring]; + }]]; +} @end