Created
February 7, 2014 14:24
-
-
Save andrei512/8863482 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSPredicate *)predicateFromSearchOptions:(SearchOptions *)searchOptions { | |
NSMutableArray *formatParts = [NSMutableArray array]; | |
NSMutableArray *args = [NSMutableArray array]; | |
// viewport | |
[formatParts addObjectsFromArray:@[ | |
@"latitude >= %@", | |
@"latitude <= %@", | |
@"longitude >= %@", | |
@"longitude <= %@" | |
]]; | |
[args addObjectsFromArray:@[ | |
searchOptions.minLat, | |
searchOptions.maxLat, | |
searchOptions.minLong, | |
searchOptions.maxLong | |
]]; | |
// tagets | |
NSMutableArray *targets = [NSMutableArray array]; | |
if (searchOptions.foreclosures.boolValue == YES) { | |
[targets addObject:@"foreclosure"]; | |
} | |
if (searchOptions.homesForSale.boolValue == YES) { | |
[targets addObject:@"sale"]; | |
} | |
if (targets.count > 0) { | |
[formatParts addObject:@"status IN %@"]; | |
[args addObject:targets]; | |
} else { | |
[formatParts addObject:@"propertyType == %@"]; | |
[args addObject:targets[0]]; | |
} | |
// open house ? | |
if (searchOptions.onlyOpenHouse != nil && | |
[searchOptions.onlyOpenHouse isEqualToString:@"no"] == NO) { | |
NSDictionary *timeInterval = [NSDate timeIntervalNamed:searchOptions.onlyOpenHouse]; | |
if (timeInterval[@"begin_date"] != nil) { | |
[formatParts addObject:@"openHouseDate >= %@"]; | |
[args addObject:timeInterval[@"begin_date"]]; | |
} | |
if (timeInterval[@"end_date"] != nil) { | |
[formatParts addObject:@"openHouseDate >= %@"]; | |
[args addObject:timeInterval[@"end_date"]]; | |
} | |
} | |
// beds | |
if (searchOptions.bedroomsMin.intValue > 1) { | |
[formatParts addObject:@"bedrooms >= %@"]; | |
[args addObject:searchOptions.bedroomsMin]; | |
} | |
// baths | |
if (searchOptions.bathroomsMin.intValue > 1) { | |
[formatParts addObject:@"bathrooms >= %@"]; | |
[args addObject:searchOptions.bathroomsMin]; | |
} | |
// price | |
if (searchOptions.priceMin.intValue > 0) { | |
[formatParts addObject:@"price >= %@"]; | |
[args addObject:searchOptions.priceMin]; | |
} | |
if (searchOptions.priceMax.intValue < 1111111111) { | |
[formatParts addObject:@"price <= %@"]; | |
[args addObject:searchOptions.priceMax]; | |
} | |
// sqft | |
if (searchOptions.sqftMin.intValue > 0) { | |
[formatParts addObject:@"sqft >= %@"]; | |
[args addObject:searchOptions.sqftMin]; | |
} | |
if (searchOptions.sqftMax.intValue < 1111111111) { | |
[formatParts addObject:@"sqft <= %@"]; | |
[args addObject:searchOptions.sqftMax]; | |
} | |
// year built | |
if (searchOptions.yearBuiltMin.intValue > 0) { | |
[formatParts addObject:@"yearBuilt >= %@"]; | |
[args addObject:searchOptions.yearBuiltMin]; | |
} | |
if (searchOptions.yearBuiltMax.intValue < 1111111111) { | |
[formatParts addObject:@"yearBuilt <= %@"]; | |
[args addObject:searchOptions.yearBuiltMax]; | |
} | |
NSString *format = [formatParts mapWithBlock:^id(NSString *condition) { | |
return [NSString stringWithFormat:@"(%@)", condition]; | |
}].join(@" AND "); | |
NSPredicate *predicate = | |
[NSPredicate predicateWithFormat:format argumentArray:args]; | |
return predicate; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment