diff --git a/PassiveDataKit.xcodeproj/project.pbxproj b/PassiveDataKit.xcodeproj/project.pbxproj index ea306ba..a47e907 100644 --- a/PassiveDataKit.xcodeproj/project.pbxproj +++ b/PassiveDataKit.xcodeproj/project.pbxproj @@ -981,7 +981,7 @@ 3842F7281CDA2021007F843D /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0730; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = "Audacious Software"; TargetAttributes = { 3842F7301CDA2021007F843D = { @@ -1187,8 +1187,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -1235,8 +1237,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -1267,6 +1271,7 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -1285,6 +1290,7 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; diff --git a/PassiveDataKit.xcodeproj/xcshareddata/xcschemes/PassiveDataKit.xcscheme b/PassiveDataKit.xcodeproj/xcshareddata/xcschemes/PassiveDataKit.xcscheme index d63610a..c2f5213 100644 --- a/PassiveDataKit.xcodeproj/xcshareddata/xcschemes/PassiveDataKit.xcscheme +++ b/PassiveDataKit.xcodeproj/xcshareddata/xcschemes/PassiveDataKit.xcscheme @@ -1,6 +1,6 @@ )listener { } } +- (void) updateOptions:(NSDictionary *) options { + if (options == nil) { + options = @{}; //!OCLINT + } + + NSArray * existingListeners = [NSArray arrayWithArray:self.listeners]; + + for (id listener in existingListeners) { + [self removeListener:listener]; + } + + for (id listener in existingListeners) { + [self addListener:listener options:options]; + } +} + - (void) addListener:(id)listener options:(NSDictionary *) options { if (options == nil) { options = @{}; //!OCLINT @@ -74,6 +90,8 @@ - (void) addListener:(id)listener options:(NSDictionary *) opti CLLocation * location = self.lastOptions[PDKGooglePlacesSpecificLocation]; [self transmitPlacesForLocation:location]; + } else if (self.lastOptions[PDKGooglePlacesFreetextQuery] != nil) { + [self transmitPlacesForFreetextQuery:self.lastOptions[PDKGooglePlacesFreetextQuery]]; } else { if (self.listeners.count == 1) { [[PassiveDataKit sharedInstance] registerListener:self forGenerator:PDKLocation options:options]; @@ -103,6 +121,26 @@ - (NSURL *) urlForLocation:(CLLocation *) location { return [NSURL URLWithString:urlString]; } +- (NSURL *) urlForFreetextQuery:(NSString *) query { + NSMutableString * urlString = [NSMutableString stringWithString:@"https://maps.googleapis.com/maps/api/place/textsearch/json?"]; + + [urlString appendFormat:@"key=%@&", self.lastOptions[PDKGooglePlacesAPIKey]]; + + NSMutableCharacterSet * charSet = [[NSMutableCharacterSet alloc] init]; + [charSet formUnionWithCharacterSet:[NSCharacterSet URLQueryAllowedCharacterSet]]; + [charSet removeCharactersInString:@"?&="]; + + query = [query stringByAddingPercentEncodingWithAllowedCharacters:charSet]; + + [urlString appendFormat:@"query=%@&", query]; + + if (self.lastOptions[PDKGooglePlacesType] != nil) { + [urlString appendFormat:@"type=%@&", self.lastOptions[PDKGooglePlacesType]]; + } + + return [NSURL URLWithString:urlString]; +} + - (NSURL *) urlForPlaceId:(NSString *) placeId { NSMutableString * urlString = [NSMutableString stringWithString:@"https://maps.googleapis.com/maps/api/place/details/json?"]; @@ -200,6 +238,68 @@ - (void) transmitPlacesForLocation:(CLLocation *) location { [self.reach startMonitoring]; } +- (void) transmitPlacesForFreetextQuery:(NSString *) query { + self.reach = [PDKAFNetworkReachabilityManager managerForDomain:@"maps.googleapis.com"]; + + __unsafe_unretained PDKGooglePlacesGenerator * weakSelf = self; + + [self.reach setReachabilityStatusChangeBlock:^(PDKAFNetworkReachabilityStatus status){ + if (status == PDKAFNetworkReachabilityStatusReachableViaWWAN || status == PDKAFNetworkReachabilityStatusReachableViaWiFi) + { + PDKAFHTTPSessionManager *manager = [PDKAFHTTPSessionManager manager]; + + manager.responseSerializer = [PDKAFHTTPResponseSerializer serializer]; + manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil]; + + [manager GET:[weakSelf urlForFreetextQuery:query].absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) { + + NSError * error = nil; + + NSDictionary * response = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&error]; + + NSLog(@"response: %@", response); + NSLog(@"count: %d", (int) [response[@"results"] count]); + + NSMutableDictionary * log = [NSMutableDictionary dictionary]; + [log setValue:[NSDate date] forKey:@"recorded"]; + [log setValue:response[@"results"] forKey:@"response"]; + + [PDKGooglePlacesGenerator logForReview:log]; + + NSMutableDictionary * data = [NSMutableDictionary dictionary]; + + [data setValue:response[@"results"] forKey:PDKGooglePlacesInstance]; + + if (self.lastOptions[PDKGooglePlacesIncludeFullDetails] != nil && [self.lastOptions[PDKGooglePlacesIncludeFullDetails] boolValue]) { + for (NSDictionary * place in response[@"results"]) { + NSData * placeData = [[NSData alloc] initWithContentsOfURL:[weakSelf urlForPlaceId:place[@"place_id"]]]; + + NSDictionary * placeResponse = [NSJSONSerialization JSONObjectWithData:placeData options:0 error:&error]; + + [data setValue:placeResponse[@"result"] forKey:place[@"place_id"]]; + } + } + + NSLog(@"DATA: %@", data); + + for (id listener in weakSelf.listeners) { + [listener receivedData:data forGenerator:PDKGooglePlaces]; + } + } failure:^(NSURLSessionTask *operation, NSError *error) { + NSLog(@"ERROR: %@", error); + // TODO: Log failure... + }]; + } + + [weakSelf.reach stopMonitoring]; + weakSelf.reach = nil; + }]; + + [self.reach startMonitoring]; +} + + + - (void) receivedData:(NSDictionary *) data forGenerator:(PDKDataGenerator) dataGenerator { [self transmitPlacesForLocation:[data valueForKey:PDKLocationInstance]]; } diff --git a/PassiveDataKit/PDKLocationGenerator.m b/PassiveDataKit/PDKLocationGenerator.m index 4a2fee7..6556e19 100644 --- a/PassiveDataKit/PDKLocationGenerator.m +++ b/PassiveDataKit/PDKLocationGenerator.m @@ -101,6 +101,15 @@ - (void) removeListener:(id)listener { } } +- (void) updateOptions:(NSDictionary *) options { + if (options == nil) { + options = @{}; //!OCLINT + } + + NSLog(@"TODO: Update options and refresh generator!"); + +} + - (void) addListener:(id)listener options:(NSDictionary *) options { if (options == nil) { diff --git a/PassiveDataKit/PassiveDataKit.h b/PassiveDataKit/PassiveDataKit.h index c9376bc..9f7e34f 100644 --- a/PassiveDataKit/PassiveDataKit.h +++ b/PassiveDataKit/PassiveDataKit.h @@ -23,6 +23,7 @@ extern NSString *const PDKLocationInstance; extern NSString *const PDKMixpanelToken; extern NSString *const PDKGooglePlacesSpecificLocation; +extern NSString *const PDKGooglePlacesFreetextQuery; extern NSString *const PDKGooglePlacesAPIKey; extern NSString *const PDKGooglePlacesType; extern NSString *const PDKGooglePlacesRadius; @@ -44,6 +45,8 @@ typedef NS_ENUM(NSInteger, PDKDataGenerator) { - (void) removeListener:(id)listener; - (void) addListener:(id)listener options:(NSDictionary *) options; +- (void) updateOptions:(NSDictionary *) options; + + (UIView *) visualizationForSize:(CGSize) size; @end diff --git a/PassiveDataKit/PassiveDataKit.m b/PassiveDataKit/PassiveDataKit.m index 56713ec..74b634a 100644 --- a/PassiveDataKit/PassiveDataKit.m +++ b/PassiveDataKit/PassiveDataKit.m @@ -42,6 +42,7 @@ @interface PassiveDataKit () NSString * const PDKGooglePlacesType = @"PDKGooglePlacesType"; //!OCLINT NSString * const PDKGooglePlacesRadius = @"PDKGooglePlacesRadius"; //!OCLINT NSString * const PDKGooglePlacesIncludeFullDetails = @"PDKGooglePlacesIncludeFullDetails"; //!OCLINT +NSString * const PDKGooglePlacesFreetextQuery = @"PDKGooglePlacesFreetextQuery"; //!OCLINT @implementation PassiveDataKit @@ -89,6 +90,8 @@ - (BOOL) registerListener:(id) listener forGenerator:(PDKDataGe [dataListeners addObject:listener]; [self incrementGenerator:dataGenerator withListener:listener options:options]; + } else { + [self updateGenerator:dataGenerator withOptions:options]; } return YES; @@ -146,6 +149,19 @@ - (void) incrementGenerator:(PDKDataGenerator) generator withListener:(id