Skip to content

Commit

Permalink
allow nested location props in postTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
mauron85 committed Nov 3, 2018
1 parent 1e91708 commit 5734e71
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 37 deletions.
1 change: 1 addition & 0 deletions BackgroundGeolocation/MAURLocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ typedef NS_ENUM(NSInteger, MAURLocationStatus) {
- (BOOL) isBetterLocation:(MAURLocation*)location;
- (BOOL) isBeyond:(MAURLocation*)location radius:(NSInteger)radius;
- (id) copyWithZone: (NSZone *)zone;
- (id) getValueForKey:(id)key;

@end

Expand Down
94 changes: 59 additions & 35 deletions BackgroundGeolocation/MAURLocation.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,61 @@
MAX_SECONDS_FROM_NOW = 86400
};

@interface MAURLocationMapper : NSObject
- (NSDictionary*) withDictionary:(NSDictionary*)values;
- (NSArray*) withArray:(NSArray*)values;
+ (instancetype) map:(MAURLocation*)location;
@end

@implementation MAURLocationMapper
MAURLocation* _location;

- (id) mapValue:(id)value
{
if ([value isKindOfClass:[NSString class]]) {
id locationValue = [_location getValueForKey:value];
return locationValue != nil ? locationValue : value;
} else if ([value isKindOfClass:[NSDictionary class]]) {
return [self withDictionary:value];
} else if ([value isKindOfClass:[NSArray class]]) {
return [self withArray:value];
}

return value;
}

- (NSDictionary*) withDictionary:(NSDictionary*)values
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:values.count];

for (id key in values) {
id value = [values objectForKey:key];
[dict setObject:[self mapValue:value] forKey:key];
}

return dict;
}

- (NSArray*) withArray:(NSArray*)values
{
NSMutableArray *locationArray = [[NSMutableArray alloc] initWithCapacity:values.count];

for (id value in values) {
[locationArray addObject:[self mapValue:value]];
}

return locationArray;
}

+ (instancetype) map:(MAURLocation*)location
{
MAURLocationMapper *instance = [[MAURLocationMapper alloc] init];
_location = location;
return instance;
}
@end


@implementation MAURLocation

@synthesize locationId, time, accuracy, altitudeAccuracy, speed, heading, altitude, latitude, longitude, provider, locationProvider, radius, isValid, recordedAt;
Expand Down Expand Up @@ -163,45 +218,14 @@ - (id) getValueForKey:(id)key
return nil;
}

- (NSArray*) toArrayFromTemplate:(NSArray*)locationTemplate
{
NSMutableArray *locationArray = [[NSMutableArray alloc] initWithCapacity:locationTemplate.count];

for (id key in locationTemplate) {
id value = [self getValueForKey:key];
if (value != nil) {
[locationArray addObject:value];
} else {
[locationArray addObject:key];
}
}

return locationArray;
}

- (NSDictionary*) toDictionaryFromTemplate:(NSDictionary*)locationTemplate
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:locationTemplate.count];

for (id key in locationTemplate) {
id wantedProp = [locationTemplate objectForKey:key];
id value = [self getValueForKey:wantedProp];
if (value != nil) {
[dict setObject:value forKey:key];
} else {
[dict setObject:wantedProp forKey:key];
}
}

return dict;
}

- (id) toResultFromTemplate:(id)locationTemplate
{
if ([locationTemplate isKindOfClass:[NSArray class]]) {
return [self toArrayFromTemplate:locationTemplate];
MAURLocationMapper *mapper = [MAURLocationMapper map:self];
return [mapper withArray:locationTemplate];
} else if ([locationTemplate isKindOfClass:[NSDictionary class]]) {
return [self toDictionaryFromTemplate:locationTemplate];
MAURLocationMapper *mapper = [MAURLocationMapper map:self];
return [mapper withDictionary:locationTemplate];
}

return [self toDictionary];
Expand Down
105 changes: 103 additions & 2 deletions BackgroundGeolocationTests/MAURLocationTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,109 @@ - (void)testLocationToDictionary {
@"foo": @"bar",
@"at": @"@",
@"number": @12
};

};

XCTAssertEqualObjects(actual, expected);
}

- (void)testNestedLocationTemplateToArray {
MAURLocation *location = [[MAURLocation alloc] init];
location.locationId = [NSNumber numberWithInt:1];
location.time = [NSDate dateWithTimeIntervalSince1970:1000];
location.accuracy = [NSNumber numberWithInt:40];
location.altitudeAccuracy = [NSNumber numberWithInt:50];
location.speed = [NSNumber numberWithInt:60];
location.heading = [NSNumber numberWithInt:1];
location.altitude = [NSNumber numberWithInt:100];
location.latitude = [NSNumber numberWithInt:49];
location.longitude = [NSNumber numberWithInt:20];
location.provider = @"TEST";
location.locationProvider = [NSNumber numberWithInt:-1];
location.radius = [NSNumber numberWithInt:111];
location.isValid = @YES;
location.recordedAt = [NSDate dateWithTimeIntervalSince1970:2000];

NSArray *template = @[@"@time", @{ @"lat": @"@latitude", @"lon": @"@longitude" }, @"@recordedAt", @"@accuracy", @"", @"custom", @"@heading", @"@speed", @"@provider", @"@id", @"@radius"];

NSArray *actual = [location toResultFromTemplate:template];
NSArray *expected = @[
[NSNumber numberWithDouble:([location.time timeIntervalSince1970] * 1000)],
@{
@"lat": location.latitude,
@"lon": location.longitude,
},
[NSNumber numberWithDouble:([location.recordedAt timeIntervalSince1970] * 1000)],
location.accuracy,
@"",
@"custom",
location.heading,
location.speed,
location.provider,
location.locationId,
location.radius
];

XCTAssertEqualObjects(actual, expected);
}

- (void)testNestedLocationTemplateToDictionary {
MAURLocation *location = [[MAURLocation alloc] init];
location.locationId = [NSNumber numberWithInt:1];
location.time = [NSDate dateWithTimeIntervalSince1970:1000];
location.accuracy = [NSNumber numberWithInt:40];
location.altitudeAccuracy = [NSNumber numberWithInt:50];
location.speed = [NSNumber numberWithInt:60];
location.heading = [NSNumber numberWithInt:1];
location.altitude = [NSNumber numberWithInt:100];
location.latitude = [NSNumber numberWithInt:49];
location.longitude = [NSNumber numberWithInt:20];
location.provider = @"TEST";
location.locationProvider = [NSNumber numberWithInt:-1];
location.radius = [NSNumber numberWithInt:111];
location.isValid = @YES;
location.recordedAt = [NSDate dateWithTimeIntervalSince1970:2000];

NSDictionary *template = @{
@"data": @{
@"id": @"@id",
@"t": @"@time",
@"acu": @"@accuracy",
@"aacu": @"@altitudeAccuracy",
@"s": @"@speed",
@"h": @"@heading",
@"alt": @"@altitude",
@"lat": @"@latitude",
@"lon": @"@longitude",
@"p": @"@provider",
@"lp": @"@locationProvider",
@"r": @"@radius",
@"rt": @"@recordedAt",
@"foo": @"bar",
@"at": @"@",
@"number": @12
}};

NSDictionary *actual = [location toResultFromTemplate:template];
NSDictionary *expected = @{
@"data": @{
@"id": location.locationId,
@"t": [NSNumber numberWithDouble:([location.time timeIntervalSince1970] * 1000)],
@"acu": location.accuracy,
@"aacu": location.altitudeAccuracy,
@"s": location.speed,
@"h": location.heading,
@"alt": location.altitude,
@"lat": location.latitude,
@"lon": location.longitude,
@"p": location.provider,
@"lp": location.locationProvider,
@"r": location.radius,
@"rt": [NSNumber numberWithDouble:([location.recordedAt timeIntervalSince1970] * 1000)],
@"foo": @"bar",
@"at": @"@",
@"number": @12
}};

XCTAssertEqualObjects(actual, expected);
}

Expand Down

0 comments on commit 5734e71

Please sign in to comment.