Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaned up the API and made it more dynamic. ... #58

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions MBContactPicker/MBContactPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@

@class MBContactPicker;

typedef void (^MBContactPickerCompletionHandler) (NSArray *results);

@protocol MBContactPickerDataSource <NSObject>

@optional

- (NSArray *)contactModelsForContactPicker:(MBContactPicker*)contactPickerView;
- (NSArray *)selectedContactModelsForContactPicker:(MBContactPicker*)contactPickerView;
- (void)contactModelsForContactPicker:(MBContactPicker*)contactPickerView withFilter:(NSString *)filter completionHandler:(MBContactPickerCompletionHandler)handler;

@end

Expand All @@ -38,14 +39,13 @@

@property (nonatomic, weak) id<MBContactPickerDelegate> delegate;
@property (nonatomic, weak) id<MBContactPickerDataSource> 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;

Expand Down
28 changes: 9 additions & 19 deletions MBContactPicker/MBContactPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -290,14 +284,10 @@ - (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.searchTableView reloadData];
[self.datasource contactModelsForContactPicker:self withFilter:searchString completionHandler:^(NSArray *filteredContacts) {
self.filteredContacts = filteredContacts;
[self.searchTableView reloadData];
}];
}
}

Expand Down
21 changes: 2 additions & 19 deletions Sample/MBContactPicker/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="4514" systemVersion="13B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="4511" systemVersion="13B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3747"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3745"/>
</dependencies>
<scenes>
<!--Navigation Controller-->
Expand Down Expand Up @@ -158,26 +158,12 @@
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="aXv-M5-Fwb">
<rect key="frame" x="255" y="262" width="51" height="31"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<connections>
<action selector="completeDuplicatesSwitched:" destination="vXZ-lx-hvc" eventType="valueChanged" id="rcJ-KS-eSF"/>
</connections>
</switch>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Complete selected contacts?" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="WH8-5t-xyt">
<rect key="frame" x="20" y="267" width="225" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
<constraints>
<constraint firstItem="M5x-NO-v9J" firstAttribute="top" secondItem="8iv-gJ-RhI" secondAttribute="bottom" constant="13" id="16G-CU-O5w"/>
<constraint firstItem="7t9-ye-HPo" firstAttribute="top" secondItem="gVn-GW-f9z" secondAttribute="bottom" constant="8" id="1id-Hq-URW"/>
<constraint firstAttribute="trailing" secondItem="8iv-gJ-RhI" secondAttribute="trailing" constant="20" id="1x6-e1-fZI"/>
<constraint firstAttribute="trailing" secondItem="aXv-M5-Fwb" secondAttribute="trailing" constant="18" id="2DV-3T-HIb"/>
<constraint firstAttribute="trailing" secondItem="Zhw-4x-QkP" secondAttribute="trailing" constant="20" id="7eA-Ys-DQs"/>
<constraint firstItem="gVn-GW-f9z" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" id="7mJ-0V-NnG"/>
<constraint firstItem="5DW-Oo-iRm" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="20" id="8PC-xg-VtW"/>
Expand All @@ -190,13 +176,10 @@
<constraint firstItem="ot2-3j-aSE" firstAttribute="top" secondItem="8iv-gJ-RhI" secondAttribute="bottom" constant="8" id="LIJ-ac-u6n"/>
<constraint firstItem="8X5-rA-60e" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="18" id="Lyg-kR-fmH"/>
<constraint firstItem="5DW-Oo-iRm" firstAttribute="width" secondItem="7t9-ye-HPo" secondAttribute="width" id="MVX-xi-h72"/>
<constraint firstItem="WH8-5t-xyt" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="20" symbolic="YES" id="PxN-Mt-lgr"/>
<constraint firstItem="aXv-M5-Fwb" firstAttribute="top" secondItem="ot2-3j-aSE" secondAttribute="bottom" constant="8" id="SdK-Iz-LDW"/>
<constraint firstItem="M5x-NO-v9J" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="20" id="VSj-WJ-fGg"/>
<constraint firstAttribute="trailing" secondItem="gVn-GW-f9z" secondAttribute="trailing" id="WZN-GZ-kBf"/>
<constraint firstItem="8X5-rA-60e" firstAttribute="top" secondItem="VDA-XU-QaJ" secondAttribute="bottom" constant="15" id="b1v-ye-jzN"/>
<constraint firstItem="Zhw-4x-QkP" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="20" id="eb9-2M-AV0"/>
<constraint firstItem="WH8-5t-xyt" firstAttribute="centerY" secondItem="aXv-M5-Fwb" secondAttribute="centerY" id="gvY-Yp-Mgg"/>
<constraint firstItem="5DW-Oo-iRm" firstAttribute="top" secondItem="gVn-GW-f9z" secondAttribute="bottom" constant="8" id="p5K-M5-IQY"/>
<constraint firstItem="8iv-gJ-RhI" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="20" id="rue-ND-WIx"/>
<constraint firstAttribute="trailing" secondItem="7t9-ye-HPo" secondAttribute="trailing" constant="20" id="vRw-cs-uDf"/>
Expand Down
33 changes: 12 additions & 21 deletions Sample/MBContactPicker/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,34 @@
@interface ViewController () <MBContactPickerDataSource, MBContactPickerDelegate>

@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;

- (IBAction)resignFirstResponder:(id)sender;
- (IBAction)takeFirstResponder:(id)sender;
- (IBAction)enabledSwitched:(id)sender;
- (IBAction)completeDuplicatesSwitched:(id)sender;
- (IBAction)clearSelectedButtonTouchUpInside:(id)sender;
- (IBAction)addContactsButtonTouchUpInside:(id)sender;

@end

@implementation ViewController

- (IBAction)clearSelectedButtonTouchUpInside:(id)sender
{
self.selectedContacts = @[];
self.contactPickerView.contactsSelected = @[];
[self.contactPickerView reloadData];
}

- (IBAction)addContactsButtonTouchUpInside:(id)sender
{
self.selectedContacts = @[
self.contacts[0],
self.contacts[1],
self.contacts[2],
self.contacts[3]
];
self.contactPickerView.contactsSelected = @[
self.contacts[0],
self.contacts[1],
self.contacts[2],
self.contacts[3]
];

[self.contactPickerView reloadData];
}
Expand Down Expand Up @@ -82,14 +82,10 @@ - (void)viewDidLoad

#pragma mark - MBContactPickerDataSource

- (NSArray *)contactModelsForContactPicker:(MBContactPicker*)contactPickerView
- (NSArray *)contactModelsForContactPicker:(MBContactPicker*)contactPickerView withFilter:(NSString *)filter
{
return self.contacts;
}

- (NSArray *)selectedContactModelsForContactPicker:(MBContactPicker*)contactPickerView
{
return self.selectedContacts;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"contactTitle contains[cd] %@", filter];
return [self.contacts filteredArrayUsingPredicate:predicate];
}

#pragma mark - MBContactPickerDelegate
Expand Down Expand Up @@ -163,11 +159,6 @@ - (IBAction)enabledSwitched:(id)sender
self.contactPickerView.enabled = ((UISwitch *)sender).isOn;
}

- (IBAction)completeDuplicatesSwitched:(id)sender
{
self.contactPickerView.allowsCompletionOfSelectedContacts = ((UISwitch *)sender).isOn;
}

- (void)promptTextFieldDidChange:(UITextField *)textField
{
self.contactPickerView.prompt = textField.text;
Expand Down