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

Added method to validate user search input, preventing app crash #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 21 additions & 2 deletions Reader/TextSearch/SearchManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,19 @@ -(void)searchForTerm:(NSString*)term page:(NSUInteger)page {
});
}

-(BOOL)isSearchTermValid:(NSString*)term {
BOOL result = YES;

// Can run mutiple validations on the search term below

// Validate against searching single or multiple blank spaces
NSCharacterSet * notWhiteSpaceSet = [[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet];
NSRange rangeOfNonWhitespaceChars = [self.searchTerm rangeOfCharacterFromSet:notWhiteSpaceSet];
result = rangeOfNonWhitespaceChars.location != NSNotFound;

return result;
}

-(void)startSearch {

if(self.running) {
Expand All @@ -239,8 +252,14 @@ -(void)startSearch {
NSNotification * notification = [NSNotification notificationWithName:kNotificationSearchDidStart object:self userInfo:info];
[[NSNotificationCenter defaultCenter] postNotification:notification];

// Start the search
[self searchForTerm:self.searchTerm page:self.currentPage];
// Validate search term before searching
if([self isSearchTermValid: self.searchTerm]) {
// Start the search
[self searchForTerm:self.searchTerm page:self.currentPage];
} else {
[self stopSearch];
return;
}
}

#pragma mark - Lifecycle
Expand Down