Skip to content
Snippets Groups Projects
Commit 6b189753 authored by cramakri's avatar cramakri
Browse files

Search/filtering implemented

SVN: 27617
parent 23cd1984
No related branches found
No related tags found
No related merge requests found
...@@ -24,16 +24,62 @@ ...@@ -24,16 +24,62 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
@class CISDOBDetailViewController, CISDOBOpenBisModel, CISDOBIpadServiceManager; @class CISDOBDetailViewController, CISDOBOpenBisModel, CISDOBIpadServiceManager;
@class CISDOBTableBrowseState, CISDOBTableFilterState, CISDOBTableDisplayState;
#import <CoreData/CoreData.h> #import <CoreData/CoreData.h>
@interface CISDOBMasterViewController : UITableViewController <NSFetchedResultsControllerDelegate, UISearchBarDelegate> @interface CISDOBMasterViewController : UITableViewController <NSFetchedResultsControllerDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
@property (strong, nonatomic) CISDOBDetailViewController *detailViewController; @property (strong, nonatomic) CISDOBDetailViewController *detailViewController;
@property (strong, nonatomic) CISDOBOpenBisModel *openBisModel; @property (strong, nonatomic) CISDOBOpenBisModel *openBisModel;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar; @property (strong, nonatomic) CISDOBTableBrowseState *browseState;
@property (strong, nonatomic) CISDOBTableFilterState *filterState;
@property (weak, nonatomic) IBOutlet UITableView *browseTableView;
// Server Communication // Server Communication
- (void)didConnectServiceManager:(CISDOBIpadServiceManager *)serviceManager; - (void)didConnectServiceManager:(CISDOBIpadServiceManager *)serviceManager;
// Table Display
- (CISDOBTableDisplayState *)displayStateForTable:(UITableView *)tableView;
@end
/**
* \brief An abstract superclass that handles interacting with and displaying the table
*/
@interface CISDOBTableDisplayState : NSObject
@property (strong, nonatomic) CISDOBOpenBisModel *openBisModel;
@property (weak, nonatomic) CISDOBMasterViewController *controller;
- (id)initWithController:(CISDOBMasterViewController *)controller;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
@end @end
/**
* \brief The state that handles results for browing.
*/
@interface CISDOBTableBrowseState : CISDOBTableDisplayState
@end
/**
* \brief The state that handles results for filtering.
*/
@interface CISDOBTableFilterState : CISDOBTableDisplayState
@property (strong, nonatomic) NSArray *filteredResults;
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString;
@end
...@@ -29,7 +29,9 @@ ...@@ -29,7 +29,9 @@
@interface CISDOBMasterViewController () @interface CISDOBMasterViewController ()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath; - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
- (void)configureCell:(UITableViewCell *)cell forEntity:(CISDOBIpadEntity *)entity;
- (void)initializeDrillDownFromParent:(CISDOBMasterViewController *)parent; - (void)initializeDrillDownFromParent:(CISDOBMasterViewController *)parent;
- (void)selectionDidChangeForModel;
@end @end
@implementation CISDOBMasterViewController @implementation CISDOBMasterViewController
...@@ -54,6 +56,9 @@ ...@@ -54,6 +56,9 @@
// UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)]; // UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
// self.navigationItem.rightBarButtonItem = addButton; // self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (CISDOBDetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController]; self.detailViewController = (CISDOBDetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
self.browseState = [[CISDOBTableBrowseState alloc] initWithController: self];
self.filterState = [[CISDOBTableFilterState alloc] initWithController: self];
} }
- (void)didReceiveMemoryWarning - (void)didReceiveMemoryWarning
...@@ -72,31 +77,32 @@ ...@@ -72,31 +77,32 @@
} }
} }
- (CISDOBTableDisplayState *)displayStateForTable:(UITableView *)tableView
{
return (tableView == self.browseTableView) ? self.browseState : self.filterState;
}
#pragma mark - Table View #pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{ {
return [self.openBisModel numberOfSections]; return [[self displayStateForTable: tableView] numberOfSectionsInTableView: tableView];
} }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ {
return [self.openBisModel numberOfEntitiesInSection: section]; return [[self displayStateForTable: tableView] tableView: tableView numberOfRowsInSection: section];
} }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ {
return [[self displayStateForTable: tableView] tableView: tableView cellForRowAtIndexPath: indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; // This only works in iOS 6,
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; // iOS5
[self configureCell:cell atIndexPath:indexPath];
return cell;
} }
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{ {
return [self.openBisModel titleForHeaderInSection: section]; return [[self displayStateForTable: tableView] tableView: tableView titleForHeaderInSection: section];
} }
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
...@@ -127,8 +133,11 @@ ...@@ -127,8 +133,11 @@
// Segue to the detail view unless we are on the ipad // Segue to the detail view unless we are on the ipad
if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPad) return; if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPad) return;
[self.openBisModel selectObjectAtIndexPath: indexPath]; [[self displayStateForTable: tableView] tableView: tableView didSelectRowAtIndexPath: indexPath];
}
- (void)selectionDidChangeForModel
{
// Figure out what to do with the detail view and the navigation view // Figure out what to do with the detail view and the navigation view
self.detailViewController.openBisModel = self.openBisModel; self.detailViewController.openBisModel = self.openBisModel;
if ([self.openBisModel selectionHasChildren]) { if ([self.openBisModel selectionHasChildren]) {
...@@ -155,10 +164,15 @@ ...@@ -155,10 +164,15 @@
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{ {
CISDOBIpadEntity *object = [self.openBisModel objectAtIndexPath: indexPath]; CISDOBIpadEntity *entity = [self.openBisModel objectAtIndexPath: indexPath];
cell.textLabel.text = object.summaryHeader; [self configureCell: cell forEntity: entity];
cell.detailTextLabel.text = object.summary; }
if ([self.openBisModel entityHasChildren: object]) {
- (void)configureCell:(UITableViewCell *)cell forEntity:(CISDOBIpadEntity *)entity
{
cell.textLabel.text = entity.summaryHeader;
cell.detailTextLabel.text = entity.summary;
if ([self.openBisModel entityHasChildren: entity]) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else { } else {
cell.accessoryType = UITableViewCellAccessoryNone; cell.accessoryType = UITableViewCellAccessoryNone;
...@@ -227,32 +241,10 @@ ...@@ -227,32 +241,10 @@
} }
*/ */
#pragma mark - UISearchBarDelegate #pragma mark - UISearchDisplayDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
// TODO Put the controller in search mode
NSLog(@"searchBarTextDidBeginEditing:");
searchBar.showsCancelButton = YES;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
// TODO Figure out what to do here
NSLog(@"searchBarTextDidEndEditing:");
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
// TODO Update the results
NSLog(@"searchBar:textDidChange:");
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{ {
// TODO Take the controller out of search mode return [self.filterState searchDisplayController: controller shouldReloadTableForSearchString: searchString];
searchBar.showsCancelButton = NO;
[self.searchBar resignFirstResponder];
[self.tableView becomeFirstResponder];
} }
#pragma mark - Server Communication #pragma mark - Server Communication
...@@ -265,7 +257,6 @@ ...@@ -265,7 +257,6 @@
#pragma mark - Properties #pragma mark - Properties
- (void)setOpenBisModel:(CISDOBOpenBisModel *)openBisModel - (void)setOpenBisModel:(CISDOBOpenBisModel *)openBisModel
{ {
_openBisModel = openBisModel; _openBisModel = openBisModel;
...@@ -279,4 +270,113 @@ ...@@ -279,4 +270,113 @@
self.title = parent.openBisModel.selectedObject.summaryHeader; self.title = parent.openBisModel.selectedObject.summaryHeader;
} }
@end
@implementation CISDOBTableDisplayState
- (id)initWithController:(CISDOBMasterViewController *)controller;
{
if (!(self = [super init])) return nil;
self.controller = controller;
self.openBisModel = controller.openBisModel;
return self;
}
// Everything is a subclass responsibility
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 0; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 0; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return nil; }
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return nil; }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }
@end
@implementation CISDOBTableBrowseState
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.openBisModel numberOfSections];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.openBisModel numberOfEntitiesInSection: section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell ;
cell = [tableView dequeueReusableCellWithIdentifier: @"Cell" forIndexPath: indexPath]; // This only works in iOS 6,
[self.controller configureCell: cell atIndexPath: indexPath];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.openBisModel titleForHeaderInSection: section];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.openBisModel selectObjectAtIndexPath: indexPath];
[self.controller selectionDidChangeForModel];
}
@end
@implementation CISDOBTableFilterState
- (CISDOBIpadEntity *)entityAtIndexPath:(NSIndexPath *)indexPath
{
return [self.filteredResults objectAtIndex: [indexPath indexAtPosition: 1]];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (self.filteredResults) ? [self.filteredResults count] : 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellId = @"FilterCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: cellId];
}
CISDOBIpadEntity *entity = [self entityAtIndexPath: indexPath];
[self.controller configureCell: cell forEntity: entity];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"Results";
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
NSArray *fullResult = [self.openBisModel.fetchedResultsController fetchedObjects];
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat: @"SELF.refconJson contains[cd] %@", searchString];
self.filteredResults = [fullResult filteredArrayUsingPredicate: filterPredicate];
return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.openBisModel.selectedObject = [self entityAtIndexPath: indexPath];
[self.controller selectionDidChangeForModel];
}
@end @end
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
- (CISDOBIpadEntity *)objectAtIndexPath:(NSIndexPath *)indexPath; - (CISDOBIpadEntity *)objectAtIndexPath:(NSIndexPath *)indexPath;
// Selection // Selection
@property (readonly) CISDOBIpadEntity *selectedObject; @property (strong, nonatomic) CISDOBIpadEntity *selectedObject;
//! Select the object and return it //! Select the object and return it
- (CISDOBIpadEntity *)selectObjectAtIndexPath:(NSIndexPath *)indexPath; - (CISDOBIpadEntity *)selectObjectAtIndexPath:(NSIndexPath *)indexPath;
......
...@@ -97,14 +97,14 @@ ...@@ -97,14 +97,14 @@
#pragma mark - Selection #pragma mark - Selection
- (CISDOBIpadEntity *)selectObjectAtIndexPath:(NSIndexPath *)indexPath - (CISDOBIpadEntity *)selectObjectAtIndexPath:(NSIndexPath *)indexPath
{ {
_selectedObject = [self objectAtIndexPath: indexPath]; self.selectedObject = [self objectAtIndexPath: indexPath];
return _selectedObject; return self.selectedObject;
} }
- (BOOL)selectionHasChildren - (BOOL)selectionHasChildren
{ {
if (!_selectedObject) return NO; if (!self.selectedObject) return NO;
return [self entityHasChildren: _selectedObject]; return [self entityHasChildren: self.selectedObject];
} }
- (BOOL)entityHasChildren:(CISDOBIpadEntity *)entity - (BOOL)entityHasChildren:(CISDOBIpadEntity *)entity
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment