From c72733fe129e54442d11e34097605df3e24c75af Mon Sep 17 00:00:00 2001
From: Philip Blackwell
Date: Mon, 7 Jul 2014 13:34:26 +1000
Subject: [PATCH] Added filterTerm to ABXFAQsViewController
If filterTerm is set, no search UI is added, and only FAQs matching the filterTerm are shown.
---
Classes/Controllers/ABXFAQsViewController.h | 5 ++++
Classes/Controllers/ABXFAQsViewController.m | 26 ++++++++++++++-------
2 files changed, 22 insertions(+), 9 deletions(-)
diff --git a/Classes/Controllers/ABXFAQsViewController.h b/Classes/Controllers/ABXFAQsViewController.h
index fc9db4d..1dae729 100644
--- a/Classes/Controllers/ABXFAQsViewController.h
+++ b/Classes/Controllers/ABXFAQsViewController.h
@@ -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
diff --git a/Classes/Controllers/ABXFAQsViewController.m b/Classes/Controllers/ABXFAQsViewController.m
index a9ffb8b..e6a4773 100644
--- a/Classes/Controllers/ABXFAQsViewController.m
+++ b/Classes/Controllers/ABXFAQsViewController.m
@@ -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];
@@ -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]
@@ -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 {