Skip to content

Commit

Permalink
Replay time ordering (#405)
Browse files Browse the repository at this point in the history
* Include locationMs, and replayRequestMs

* Ensure updatedAtMsDiff is correct

* bump version

* fix ordering

* correct version

* Add logging of visits

* simplify to just updatedAtMsDiff

* fix lint error

* always include updatedAtMsDiff

* log cleanup

* timeInMs --> locationMs, don't send updatedAtMsDiff if verified

* extra bracket

* nit

* not verified or not foreground

* version bump

* don't conditionalize for verified

* Revert "log cleanup"

This reverts commit 19dfb83.

* don't conditionalize updatedAtMsDiff at all

* make api helper logic more clear

* Reformat to more closely match android impl

* Make nil check explict

* put behind a feature setting

* Typo

* bump version

---------

Co-authored-by: KennyHuRadar <[email protected]>
Co-authored-by: Kenny Hu <[email protected]>
  • Loading branch information
3 people authored Dec 9, 2024
1 parent 1e2a594 commit 65844f2
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 19 deletions.
2 changes: 1 addition & 1 deletion RadarSDK.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'RadarSDK'
s.version = '3.19.3'
s.version = '3.19.4'
s.summary = 'iOS SDK for Radar, the leading geofencing and location tracking platform'
s.homepage = 'https://radar.com'
s.author = { 'Radar Labs, Inc.' => '[email protected]' }
Expand Down
4 changes: 2 additions & 2 deletions RadarSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 3.19.3;
MARKETING_VERSION = 3.19.4;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -1122,7 +1122,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 3.19.3;
MARKETING_VERSION = 3.19.4;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
OTHER_CFLAGS = "-fembed-bitcode";
Expand Down
13 changes: 5 additions & 8 deletions RadarSDK/RadarAPIClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ - (void)trackWithLocation:(CLLocation *_Nonnull)location
return completionHandler(RadarStatusErrorPublishableKey, nil, nil, nil, nil, nil, nil);
}
NSMutableDictionary *params = [NSMutableDictionary new];
RadarSdkConfiguration *sdkConfiguration = [RadarSettings sdkConfiguration];
BOOL anonymous = [RadarSettings anonymousTrackingEnabled];
params[@"anonymous"] = @(anonymous);
if (anonymous) {
Expand Down Expand Up @@ -251,10 +252,11 @@ - (void)trackWithLocation:(CLLocation *_Nonnull)location
params[@"floorLevel"] = @(location.floor.level);
}
long nowMs = (long)([NSDate date].timeIntervalSince1970 * 1000);
if (!foreground) {
long timeInMs = (long)(location.timestamp.timeIntervalSince1970 * 1000);
params[@"updatedAtMsDiff"] = @(nowMs - timeInMs);
long locationMs = (long)(location.timestamp.timeIntervalSince1970 * 1000);
if (sdkConfiguration.useForegroundLocationUpdatedAtMsDiff || !foreground) {
params[@"updatedAtMsDiff"] = @(nowMs - locationMs);
}
params[@"locationMs"] = @(locationMs);
params[@"foreground"] = @(foreground);
params[@"stopped"] = @(stopped);
params[@"replayed"] = @(replayed);
Expand Down Expand Up @@ -298,7 +300,6 @@ - (void)trackWithLocation:(CLLocation *_Nonnull)location
[tripParams setValue:[Radar stringForMode:tripOptions.mode] forKey:@"mode"];
params[@"tripOptions"] = tripParams;
}

RadarTrackingOptions *options = [Radar getTrackingOptions];
if (options.syncGeofences) {
params[@"nearbyGeofences"] = @(YES);
Expand Down Expand Up @@ -340,7 +341,6 @@ - (void)trackWithLocation:(CLLocation *_Nonnull)location
}
}
params[@"appId"] = [[NSBundle mainBundle] bundleIdentifier];
RadarSdkConfiguration *sdkConfiguration = [RadarSettings sdkConfiguration];
if (sdkConfiguration.useLocationMetadata) {
NSMutableDictionary *locationMetadata = [NSMutableDictionary new];
locationMetadata[@"motionActivityData"] = [RadarState lastMotionActivityData];
Expand Down Expand Up @@ -415,9 +415,6 @@ - (void)trackWithLocation:(CLLocation *_Nonnull)location
// create a copy of params that we can use to write to the buffer in case of request failure
NSMutableDictionary *bufferParams = [params mutableCopy];
bufferParams[@"replayed"] = @(YES);
bufferParams[@"updatedAtMs"] = @(nowMs);
// remove the updatedAtMsDiff key because for replays we want to rely on the updatedAtMs key for the time instead
[bufferParams removeObjectForKey:@"updatedAtMsDiff"];

[[RadarReplayBuffer sharedInstance] writeNewReplayToBuffer:bufferParams];
} else if (options.replay == RadarTrackingOptionsReplayStops && stopped &&
Expand Down
32 changes: 31 additions & 1 deletion RadarSDK/RadarAPIHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,39 @@ - (void)requestWithMethod:(NSString *)method
}

if (params) {
[req setHTTPBody:[NSJSONSerialization dataWithJSONObject:params options:0 error:NULL]];
NSNumber *prevUpdatedAtMsDiff = params[@"updatedAtMsDiff"];
NSArray *replays = params[@"replays"];
if (prevUpdatedAtMsDiff || replays) {
NSMutableDictionary *requestParams = [params mutableCopy];
long nowMs = (long)([NSDate date].timeIntervalSince1970 * 1000);
NSNumber *locationMs = params[@"locationMs"];

if (locationMs != nil && prevUpdatedAtMsDiff != nil) {
long updatedAtMsDiff = nowMs - [locationMs longValue];
requestParams[@"updatedAtMsDiff"] = @(updatedAtMsDiff);
}

if (replays) {
NSMutableArray *updatedReplays = [NSMutableArray arrayWithCapacity:replays.count];
for (NSDictionary *replay in replays) {
NSMutableDictionary *updatedReplay = [replay mutableCopy];
NSNumber *replayLocationMs = replay[@"locationMs"];
if (replayLocationMs != nil) {
long replayUpdatedAtMsDiff = nowMs - [replayLocationMs longValue];
updatedReplay[@"updatedAtMsDiff"] = @(replayUpdatedAtMsDiff);
}
[updatedReplays addObject:updatedReplay];
}
requestParams[@"replays"] = updatedReplays;
}

[req setHTTPBody:[NSJSONSerialization dataWithJSONObject:requestParams options:0 error:NULL]];
} else {
[req setHTTPBody:[NSJSONSerialization dataWithJSONObject:params options:0 error:NULL]];
}
}


NSURLSessionConfiguration *configuration;
if (extendedTimeout) {
configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
Expand Down
4 changes: 4 additions & 0 deletions RadarSDK/RadarLocationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,10 @@ - (void)locationManager:(CLLocationManager *)manager didVisit:(CLVisit *)visit {
return;
}

[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug
message:[NSString stringWithFormat:@"Visit detected | arrival = %@; departure = %@; horizontalAccuracy = %f; visit.coordinate = (%f, %f); manager.location = %@",
visit.arrivalDate, visit.departureDate, visit.horizontalAccuracy, visit.coordinate.latitude, visit.coordinate.longitude, manager.location]];

BOOL tracking = [RadarSettings tracking];
if (!tracking) {
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug message:@"Ignoring visit: not tracking"];
Expand Down
3 changes: 3 additions & 0 deletions RadarSDK/RadarSdkConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign) BOOL useLocationMetadata;

@property (nonatomic, assign) BOOL useOpenedAppConversion;

@property (nonatomic, assign) BOOL useForegroundLocationUpdatedAtMsDiff;

/**
Initializes a new RadarSdkConfiguration object with given value.
*/
Expand Down
13 changes: 10 additions & 3 deletions RadarSDK/RadarSdkConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,16 @@ - (instancetype)initWithDict:(NSDictionary *)dict {
_useLocationMetadata = [(NSNumber *)useLocationMetadataObj boolValue];
}

NSObject *useOpenedAppConversion = dict[@"useOpenedAppConversion"];
NSObject *useOpenedAppConversionObj = dict[@"useOpenedAppConversion"];
_useOpenedAppConversion = NO;
if (useOpenedAppConversion && [useOpenedAppConversion isKindOfClass:[NSNumber class]]) {
_useOpenedAppConversion = [(NSNumber *)useOpenedAppConversion boolValue];
if (useOpenedAppConversionObj && [useOpenedAppConversionObj isKindOfClass:[NSNumber class]]) {
_useOpenedAppConversion = [(NSNumber *)useOpenedAppConversionObj boolValue];
}

NSObject *useForegroundLocationUpdatedAtMsDiffObj = dict[@"foregroundLocationUseUpdatedAtMsDiff"];
_useForegroundLocationUpdatedAtMsDiff = NO;
if (useForegroundLocationUpdatedAtMsDiffObj && [useForegroundLocationUpdatedAtMsDiffObj isKindOfClass:[NSNumber class]]) {
_useForegroundLocationUpdatedAtMsDiff = [(NSNumber *)useForegroundLocationUpdatedAtMsDiffObj boolValue];
}

return self;
Expand All @@ -90,6 +96,7 @@ - (NSDictionary *)dictionaryValue {
dict[@"useRadarModifiedBeacon"] = @(_useRadarModifiedBeacon);
dict[@"useLocationMetadata"] = @(_useLocationMetadata);
dict[@"useOpenedAppConversion"] = @(_useOpenedAppConversion);
dict[@"useForegroundLocationUpdatedAtMsDiff"] = @(_useForegroundLocationUpdatedAtMsDiff);

return dict;
}
Expand Down
2 changes: 1 addition & 1 deletion RadarSDK/RadarUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ + (NSNumber *)timeZoneOffset {
}

+ (NSString *)sdkVersion {
return @"3.19.3";
return @"3.19.4";
}

+ (NSString *)deviceId {
Expand Down
2 changes: 1 addition & 1 deletion RadarSDKMotion.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'RadarSDKMotion'
s.version = '3.19.3'
s.version = '3.19.4'
s.summary = 'Motion detection plugin for RadarSDK, the leading geofencing and location tracking platform'
s.homepage = 'https://radar.com'
s.author = { 'Radar Labs, Inc.' => '[email protected]' }
Expand Down
4 changes: 2 additions & 2 deletions RadarSDKMotion/RadarSDKMotion.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MARKETING_VERSION = 3.19.3;
MARKETING_VERSION = 3.19.4;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -348,7 +348,7 @@
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MARKETING_VERSION = 3.19.3;
MARKETING_VERSION = 3.19.4;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = iphoneos;
Expand Down

0 comments on commit 65844f2

Please sign in to comment.