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

First attempt for searchable QRadioElement #644

Open
wants to merge 2 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
3 changes: 3 additions & 0 deletions quickdialog/QRadioElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
@property(nonatomic, assign, readwrite) NSInteger selected;
@property(nonatomic, retain) NSArray *values;
@property(nonatomic, strong) NSArray *itemsImageNames;
@property(nonatomic) BOOL searchable;

- (QRadioElement *)initWithDict:(NSDictionary *)valuesDictionary selected:(int)selected title:(NSString *)title;

- (void)createElements;
- (void)hideElementsNotMatchingSearchKey:(NSString*)searchKey;
- (void)showAllElements;

- (NSObject *)selectedValue;
- (void)setSelectedValue:(NSObject *)aSelected;
Expand Down
29 changes: 29 additions & 0 deletions quickdialog/QRadioElement.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ @implementation QRadioElement {
@synthesize values = _values;
@synthesize items = _items;
@synthesize itemsImageNames = _itemsImageNames;
@synthesize searchable = _searchable;


- (void)createElements {
Expand All @@ -42,6 +43,34 @@ - (void)createElements {
}
}

// NOTE: I'm not preserving "old" hidden property values, but it's important as they could've been
// hidden on purpose in other context
- (void)hideElementsNotMatchingSearchKey:(NSString *)searchKey {

for (QRadioItemElement *element in _internalRadioItemsSection.elements) {

NSRange range = [element.title rangeOfString:searchKey
options:NSCaseInsensitiveSearch];

if (range.location == NSNotFound) {

[element setHidden:YES];
} else {

[element setHidden:NO];
}
}
}

// NOTE: similar as above
- (void)showAllElements {

for (QRadioItemElement *element in _internalRadioItemsSection.elements) {

[element setHidden:NO];
}
}

-(void)setItems:(NSArray *)items {
_items = items;
[self createElements];
Expand Down
2 changes: 2 additions & 0 deletions quickdialog/QRadioItemElement.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ - (void)selected:(QuickDialogTableView *)tableView controller:(QuickDialogContro
[_radioElement fieldDidEndEditing];
tableView.userInteractionEnabled = NO;

[_radioElement showAllElements];

[NSTimer scheduledTimerWithTimeInterval:0.3
target:controller
selector:@selector(popToPreviousRootElement)
Expand Down
3 changes: 2 additions & 1 deletion quickdialog/QuickDialogTableView.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
@class QElement;
@class QRootElement;

@interface QuickDialogTableView : UITableView {
// NOTE: Conforming to protocols here, but could be refactored similar as quickDialogDatasource and delegate
@interface QuickDialogTableView : UITableView <UISearchDisplayDelegate, UISearchBarDelegate> {


@private
Expand Down
48 changes: 48 additions & 0 deletions quickdialog/QuickDialogTableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

@implementation QuickDialogTableView {
BOOL _deselectRowWhenViewAppears;

UISearchBar *_searchBar;
UISearchDisplayController *_searchDisplayController;
}

@synthesize root = _root;
Expand All @@ -38,12 +41,57 @@ - (QuickDialogTableView *)initWithController:(QuickDialogController *)controller

quickDialogDelegate = [[QuickDialogTableDelegate alloc] initForTableView:self];
self.delegate = quickDialogDelegate;

if ([self.root respondsToSelector:@selector(searchable)]) {

BOOL isSearchable = [[self.root valueForKey:@"searchable"] boolValue];

if (isSearchable) {

_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
_searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar
contentsController:_controller];

_searchBar.delegate = self;
_searchDisplayController.delegate = self;
_searchDisplayController.searchResultsDataSource = self.dataSource;
_searchDisplayController.searchResultsDelegate = self.delegate;

self.tableHeaderView = _searchBar;
}
}

self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
}
return self;
}

#pragma mark UISearchBarDelegate members

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

if ([self.root respondsToSelector:@selector(showAllElements)]) {

[self.root performSelector:@selector(showAllElements)];
}

[self reloadData];
}

#pragma mark UISearchDisplayDelegate memebers

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

if ([self.root respondsToSelector:@selector(hideElementsNotMatchingSearchKey:)]) {

[self.root performSelector:@selector(hideElementsNotMatchingSearchKey:)
withObject:searchString];
}

return YES;
}


-(void)setRoot:(QRootElement *)root{
_root = root;
for (QSection *section in _root.sections) {
Expand Down
5 changes: 5 additions & 0 deletions sample/SampleDataBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ + (QElement *)createRadioRoot {
QRadioElement *elementWithAction = [[QRadioElement alloc] initWithItems:[NSArray arrayWithObjects:@"Ferrari", @"McLaren", @"Lotus", nil] selected:0 title:@"WithAction"];
elementWithAction.controllerAction = @"exampleAction:";
[section1 addElement:elementWithAction];

QRadioElement *elementWithFilter = [[QRadioElement alloc] initWithItems:@[@"Ferrari", @"McLaren", @"Lotus"] selected:0 title:@"Filtered"];
elementWithFilter.searchable = YES;
[section1 addElement:elementWithFilter];

[root addSection:section1];

QRadioSection *section2 = [[QRadioSection alloc] initWithItems:[NSArray arrayWithObjects:@"Football", @"Soccer", @"Formula 1", nil] selected:0 title:@"Sport"];
Expand Down