Skip to content

Commit

Permalink
Added filterTerm to ABXFAQsViewController
Browse files Browse the repository at this point in the history
If filterTerm is set, no search UI is added, and only FAQs matching the filterTerm are shown.
  • Loading branch information
blackp committed Jul 7, 2014
1 parent da78bc2 commit c72733f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
5 changes: 5 additions & 0 deletions Classes/Controllers/ABXFAQsViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@
@property (nonatomic, assign) BOOL hideContactButton;
@property (nonatomic, strong) NSDictionary *contactMetaData;

/**
* Filter term. If set, no search bar will be added, but only FAQs matching the filter term will be shown.
*/
@property (nonatomic, strong) NSString *filterTerm;

@end
26 changes: 17 additions & 9 deletions Classes/Controllers/ABXFAQsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ - (void)viewDidLoad
[super viewDidLoad];

// Title
self.title = [@"FAQs" localizedString];
self.title = self.title ?: [@"FAQs" localizedString];

// Setup our UI components
[self setupFaqUI];
Expand Down Expand Up @@ -79,12 +79,15 @@ - (void)didReceiveMemoryWarning

- (void)setupFaqUI
{
// Search bar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 44)];
self.searchBar.delegate = self;
self.searchBar.placeholder = [@"Search..." localizedString];
self.tableView.tableHeaderView = self.searchBar;
if (!self.filterTerm) {
// Search bar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 44)];
self.searchBar.delegate = self;
self.searchBar.placeholder = [@"Search..." localizedString];
self.tableView.tableHeaderView = self.searchBar;
}


// Nav buttons
if (!self.hideContactButton) {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
Expand All @@ -104,11 +107,16 @@ - (void)fetchFAQs
[ABXFaq fetch:^(NSArray *faqs, ABXResponseCode responseCode, NSInteger httpCode, NSError *error) {
[self.activityView stopAnimating];
if (responseCode == ABXResponseCodeSuccess) {
self.faqs = faqs;
self.filteredFaqs = faqs;
if (self.filterTerm) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.question contains[cd] %@ OR SELF.answer contains[cd] %@", self.filterTerm, self.filterTerm];
self.faqs = [faqs filteredArrayUsingPredicate:predicate];
} else {
self.faqs = faqs;
}
self.filteredFaqs = self.faqs;
[self.tableView reloadData];

if (faqs.count == 0) {
if (self.faqs.count == 0) {
[self showError:[@"No FAQs" localizedString]];
}
else {
Expand Down

0 comments on commit c72733f

Please sign in to comment.