From cb31205bd765ad5721b19f78a982a7c2a083a337 Mon Sep 17 00:00:00 2001 From: Michael Hohl Date: Sun, 2 Feb 2014 11:38:37 +0100 Subject: [PATCH 1/3] Cleaned up the API and made it more dynamic. Instead of using a static array the data source method now returns an array depending on the current filter. (Demo updated too of course!) --- MBContactPicker/MBContactPicker.h | 6 ++-- MBContactPicker/MBContactPicker.m | 24 ++++---------- .../Base.lproj/Main.storyboard | 21 ++---------- Sample/MBContactPicker/ViewController.m | 33 +++++++------------ 4 files changed, 22 insertions(+), 62 deletions(-) diff --git a/MBContactPicker/MBContactPicker.h b/MBContactPicker/MBContactPicker.h index eb6b214..cb1716f 100644 --- a/MBContactPicker/MBContactPicker.h +++ b/MBContactPicker/MBContactPicker.h @@ -19,8 +19,7 @@ @optional -- (NSArray *)contactModelsForContactPicker:(MBContactPicker*)contactPickerView; -- (NSArray *)selectedContactModelsForContactPicker:(MBContactPicker*)contactPickerView; +- (NSArray *)contactModelsForContactPicker:(MBContactPicker*)contactPickerView withFilter:(NSString *)filter; @end @@ -38,14 +37,13 @@ @property (nonatomic, weak) id delegate; @property (nonatomic, weak) id datasource; -@property (nonatomic, readonly) NSArray *contactsSelected; +@property (nonatomic) NSArray *contactsSelected; @property (nonatomic) NSInteger cellHeight; @property (nonatomic, copy) NSString *prompt; @property (nonatomic) CGFloat maxVisibleRows; @property (nonatomic, readonly) CGFloat currentContentHeight; @property (nonatomic, readonly) CGFloat keyboardHeight; @property (nonatomic) CGFloat animationSpeed; -@property (nonatomic) BOOL allowsCompletionOfSelectedContacts; @property (nonatomic) BOOL enabled; @property (nonatomic) BOOL showPrompt; diff --git a/MBContactPicker/MBContactPicker.m b/MBContactPicker/MBContactPicker.m index be209ac..cc9bf6b 100644 --- a/MBContactPicker/MBContactPicker.m +++ b/MBContactPicker/MBContactPicker.m @@ -17,7 +17,6 @@ @interface MBContactPicker() @property (nonatomic, weak) MBContactCollectionView *contactCollectionView; @property (nonatomic, weak) UITableView *searchTableView; @property (nonatomic) NSArray *filteredContacts; -@property (nonatomic) NSArray *contacts; @property (nonatomic) CGFloat keyboardHeight; @property (nonatomic) CGSize contactCollectionViewContentSize; @@ -70,7 +69,6 @@ - (void)setup self.originalYOffset = -1; self.maxVisibleRows = kMaxVisibleRows; self.animationSpeed = kAnimationSpeed; - self.allowsCompletionOfSelectedContacts = YES; self.translatesAutoresizingMaskIntoConstraints = NO; self.clipsToBounds = YES; self.enabled = YES; @@ -135,15 +133,6 @@ - (void)keyboardChangedStatus:(NSNotification*)notification - (void)reloadData { - self.contactCollectionView.selectedContacts = [[NSMutableArray alloc] init]; - - if ([self.datasource respondsToSelector:@selector(selectedContactModelsForContactPicker:)]) - { - [self.contactCollectionView.selectedContacts addObjectsFromArray:[self.datasource selectedContactModelsForContactPicker:self]]; - } - - self.contacts = [self.datasource contactModelsForContactPicker:self]; - [self.contactCollectionView reloadData]; [self.contactCollectionView performBatchUpdates:^{ } completion:^(BOOL finished) { @@ -158,6 +147,11 @@ - (NSArray*)contactsSelected return self.contactCollectionView.selectedContacts; } +- (void)setContactsSelected:(NSArray *)contactsSelected +{ + self.contactCollectionView.selectedContacts = [NSMutableArray arrayWithArray:contactsSelected]; +} + - (void)setCellHeight:(NSInteger)cellHeight { self.contactCollectionView.cellHeight = cellHeight; @@ -290,13 +284,7 @@ - (void)contactCollectionView:(MBContactCollectionView*)contactCollectionView en { [self showSearchTableView]; NSString *searchString = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; - NSPredicate *predicate; - if (self.allowsCompletionOfSelectedContacts) { - predicate = [NSPredicate predicateWithFormat:@"contactTitle contains[cd] %@", searchString]; - } else { - predicate = [NSPredicate predicateWithFormat:@"contactTitle contains[cd] %@ && !SELF IN %@", searchString, self.contactCollectionView.selectedContacts]; - } - self.filteredContacts = [self.contacts filteredArrayUsingPredicate:predicate]; + self.filteredContacts = [self.datasource contactModelsForContactPicker:self withFilter:searchString]; [self.searchTableView reloadData]; } } diff --git a/Sample/MBContactPicker/Base.lproj/Main.storyboard b/Sample/MBContactPicker/Base.lproj/Main.storyboard index 8134581..d884a30 100644 --- a/Sample/MBContactPicker/Base.lproj/Main.storyboard +++ b/Sample/MBContactPicker/Base.lproj/Main.storyboard @@ -1,7 +1,7 @@ - + - + @@ -158,26 +158,12 @@ - - - - - - - - - @@ -190,13 +176,10 @@ - - - diff --git a/Sample/MBContactPicker/ViewController.m b/Sample/MBContactPicker/ViewController.m index 426c0c1..e07cc0e 100644 --- a/Sample/MBContactPicker/ViewController.m +++ b/Sample/MBContactPicker/ViewController.m @@ -13,7 +13,6 @@ @interface ViewController () @property (nonatomic) NSArray *contacts; -@property (nonatomic) NSArray *selectedContacts; @property (weak, nonatomic) IBOutlet MBContactPicker *contactPickerView; @property (weak, nonatomic) IBOutlet UITextField *promptTextField; @property (nonatomic, weak) IBOutlet NSLayoutConstraint *contactPickerViewHeightConstraint; @@ -21,7 +20,8 @@ @interface ViewController () Date: Wed, 9 Apr 2014 17:03:09 +0200 Subject: [PATCH 2/3] Use a completionHandler instead of return value will allows concruent fetches (like from a website). --- MBContactPicker/MBContactPicker.h | 2 +- MBContactPicker/MBContactPicker.m | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/MBContactPicker/MBContactPicker.h b/MBContactPicker/MBContactPicker.h index cb1716f..cb04f1f 100644 --- a/MBContactPicker/MBContactPicker.h +++ b/MBContactPicker/MBContactPicker.h @@ -19,7 +19,7 @@ @optional -- (NSArray *)contactModelsForContactPicker:(MBContactPicker*)contactPickerView withFilter:(NSString *)filter; +- (void)contactModelsForContactPicker:(MBContactPicker*)contactPickerView withFilter:(NSString *)filter completionHandler:(void (^)(NSArray *results))handler; @end diff --git a/MBContactPicker/MBContactPicker.m b/MBContactPicker/MBContactPicker.m index cc9bf6b..6c61779 100644 --- a/MBContactPicker/MBContactPicker.m +++ b/MBContactPicker/MBContactPicker.m @@ -284,8 +284,10 @@ - (void)contactCollectionView:(MBContactCollectionView*)contactCollectionView en { [self showSearchTableView]; NSString *searchString = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; - self.filteredContacts = [self.datasource contactModelsForContactPicker:self withFilter:searchString]; - [self.searchTableView reloadData]; + [self.datasource contactModelsForContactPicker:self withFilter:searchString completionHandler:^(NSArray *filteredContacts) { + self.filteredContacts = filteredContacts; + [self.searchTableView reloadData]; + }]; } } From aed05d387e111e9dbff313ea6eda346b3ac19a43 Mon Sep 17 00:00:00 2001 From: Michael Hohl Date: Wed, 9 Apr 2014 18:25:41 +0200 Subject: [PATCH 3/3] Using a typedef for the completion handler. --- MBContactPicker/MBContactPicker.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/MBContactPicker/MBContactPicker.h b/MBContactPicker/MBContactPicker.h index cb04f1f..6fdc293 100644 --- a/MBContactPicker/MBContactPicker.h +++ b/MBContactPicker/MBContactPicker.h @@ -15,11 +15,13 @@ @class MBContactPicker; +typedef void (^MBContactPickerCompletionHandler) (NSArray *results); + @protocol MBContactPickerDataSource @optional -- (void)contactModelsForContactPicker:(MBContactPicker*)contactPickerView withFilter:(NSString *)filter completionHandler:(void (^)(NSArray *results))handler; +- (void)contactModelsForContactPicker:(MBContactPicker*)contactPickerView withFilter:(NSString *)filter completionHandler:(MBContactPickerCompletionHandler)handler; @end