diff options
author | Fredrik Karlsson <bjorn.fredrik.karlsson@gmail.com> | 2016-08-17 13:38:02 +0200 |
---|---|---|
committer | Fredrik Karlsson <bjorn.fredrik.karlsson@gmail.com> | 2016-09-02 22:42:05 +0200 |
commit | d77a13eb7320722c48c8a18240adf99615c4b85f (patch) | |
tree | 1017781b310e434f943e4ad38b57f319ad7a3a58 /platform/darwin/src/NSCompoundPredicate+MGLAdditions.mm | |
parent | 6a39cf5aaece81c7a531b12321dd503004cc45b8 (diff) | |
download | qtlocation-mapboxgl-d77a13eb7320722c48c8a18240adf99615c4b85f.tar.gz |
[ios] Added support for filters (NSPredicate)
[ios, macos] cleaned up filters
[ios] added a filter example
[ios] utest filters
[ios, macos] nested predicates
[ios] refactored
[ios] filter -> NSPredicate
[ios] fixed mbgl::Any/AllFilter -> NSPredicate
[ios] translate nested mbgl::NotIn filters
[ios] cleanup and added more utests
[ios] fixed a bug in the None filter conversion and improved utests
[ios, macos] doc
[macos] added missing category
[ios, macos] additional utests
[ios, macos] updated documentation
Diffstat (limited to 'platform/darwin/src/NSCompoundPredicate+MGLAdditions.mm')
-rw-r--r-- | platform/darwin/src/NSCompoundPredicate+MGLAdditions.mm | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/platform/darwin/src/NSCompoundPredicate+MGLAdditions.mm b/platform/darwin/src/NSCompoundPredicate+MGLAdditions.mm new file mode 100644 index 0000000000..07114308c9 --- /dev/null +++ b/platform/darwin/src/NSCompoundPredicate+MGLAdditions.mm @@ -0,0 +1,56 @@ +#import "NSCompoundPredicate+MGLAdditions.h" + +#import "NSPredicate+MGLAdditions.h" +#import "NSExpression+MGLAdditions.h" + +@implementation NSCompoundPredicate (MGLAdditions) + +- (std::vector<mbgl::style::Filter>)mgl_subfilters +{ + std::vector<mbgl::style::Filter>filters; + for (NSPredicate *predicate in self.subpredicates) { + filters.push_back(predicate.mgl_filter); + } + return filters; +} + +- (mbgl::style::Filter)mgl_filter +{ + switch (self.compoundPredicateType) { + case NSNotPredicateType: { + + // Translate a nested NSComparisonPredicate with operator type NSInPredicateOperatorType into a flat mbgl::NotIn filter. + NSArray<NSComparisonPredicate *> *comparisonPredicates = [self.subpredicates filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"class == %@", [NSComparisonPredicate class]]]; + NSArray<NSComparisonPredicate *> *notInPredicates = [comparisonPredicates filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSComparisonPredicate *_Nonnull predicate, NSDictionary<NSString *,id> * _Nullable bindings) { + return predicate.predicateOperatorType == NSInPredicateOperatorType; + }]]; + + if (notInPredicates.count) { + auto filter = mbgl::style::NotInFilter(); + filter.key = notInPredicates.firstObject.leftExpression.keyPath.UTF8String; + filter.values = notInPredicates.firstObject.rightExpression.mgl_filterValues; + return filter; + } else { + auto filter = mbgl::style::NoneFilter(); + filter.filters = [self mgl_subfilters]; + return filter; + } + } + case NSAndPredicateType: { + auto filter = mbgl::style::AllFilter(); + filter.filters = [self mgl_subfilters]; + return filter; + } + case NSOrPredicateType: { + auto filter = mbgl::style::AnyFilter(); + filter.filters = [self mgl_subfilters]; + return filter; + } + } + + [NSException raise:@"Compound predicate type not handled" + format:@""]; + return {}; +} + +@end |