Skip to content

Commit

Permalink
Allow for createTrip without starting tracking immediately (#411)
Browse files Browse the repository at this point in the history
* Allow for createTrip without starting tracking immediately

* tripOptions.scheduled --> tripOptions.startTracking (defaults to YES)

* Fix deps

* more deps fixing

* Fix class impl

* add back declaration

* fix typo

* Fix conditional

* Add example app testing

* null out startTrackingAfter and stopTrackingAfter to prevent indadvertent starts and stops of tracking

* Fixes

* version bump

---------

Co-authored-by: KennyHuRadar <139801512+KennyHuRadar@users.noreply.github.com>
Co-authored-by: Kenny Hu <kenny.hu@radar.com>
  • Loading branch information
3 people authored Nov 8, 2024
1 parent 8abf09d commit fe1f485
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 48 deletions.
34 changes: 32 additions & 2 deletions Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIWindowSceneDelegate, UN
let radarInitializeOptions = RadarInitializeOptions()
// Uncomment to enable automatic setup for notification conversions
// radarInitializeOptions.autoSetupNotificationConversion = true
Radar.initialize(publishableKey: "prj_test_pk_0000000000000000000000000000000000000000", options: radarInitializeOptions )
Radar.initialize(publishableKey: "prj_test_pk_0000000000000", options: radarInitializeOptions )
Radar.setUserId("testUserId")
Radar.setMetadata([ "foo": "bar" ])
Radar.setDelegate(self)
Expand Down Expand Up @@ -271,12 +271,42 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIWindowSceneDelegate, UN
}

demoButton(text: "startTrip") {
let tripOptions = RadarTripOptions(externalId: "299", destinationGeofenceTag: "store", destinationGeofenceExternalId: "123")
let tripOptions = RadarTripOptions(externalId: "300", destinationGeofenceTag: "store", destinationGeofenceExternalId: "123")
tripOptions.mode = .car
tripOptions.approachingThreshold = 9
Radar.startTrip(options: tripOptions)
}

demoButton(text: "startTrip with start tracking false") {
let tripOptions = RadarTripOptions(externalId: "301", destinationGeofenceTag: "store", destinationGeofenceExternalId: "123", scheduledArrivalAt: nil, startTracking: false)
tripOptions.mode = .car
tripOptions.approachingThreshold = 9
Radar.startTrip(options: tripOptions)
}

demoButton(text: "startTrip with tracking options") {
let tripOptions = RadarTripOptions(externalId: "301", destinationGeofenceTag: "store", destinationGeofenceExternalId: "123", scheduledArrivalAt: nil, startTracking: false)
tripOptions.mode = .car
tripOptions.approachingThreshold = 9
let trackingOptions = RadarTrackingOptions.presetContinuous
Radar.startTrip(options: tripOptions, trackingOptions: trackingOptions)
}

demoButton(text: "startTrip with tracking options and startTrackingAfter") {
let tripOptions = RadarTripOptions(externalId: "303", destinationGeofenceTag: "store", destinationGeofenceExternalId: "123", scheduledArrivalAt: nil)
tripOptions.startTracking = false
tripOptions.mode = .car
tripOptions.approachingThreshold = 9
let trackingOptions = RadarTrackingOptions.presetContinuous
// startTrackingAfter 3 minutes from now
trackingOptions.startTrackingAfter = Date().addingTimeInterval(180)
Radar.startTrip(options: tripOptions, trackingOptions: trackingOptions)
}

demoButton(text: "completeTrip") {
Radar.completeTrip()
}

demoButton(text: "mockTracking") {
let origin = CLLocation(latitude: 40.78382, longitude: -73.97536)
let destination = CLLocation(latitude: 40.70390, longitude: -73.98670)
Expand Down
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.18.4'
s.version = '3.18.5'
s.summary = 'iOS SDK for Radar, the leading geofencing and location tracking platform'
s.homepage = 'https://radar.com'
s.author = { 'Radar Labs, Inc.' => 'support@radar.com' }
Expand Down
8 changes: 6 additions & 2 deletions RadarSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
53CCD783275E579800F79CC8 /* RadarLogBuffer.m in Sources */ = {isa = PBXBuildFile; fileRef = 53CCD782275E579800F79CC8 /* RadarLogBuffer.m */; };
78D8CE3C23AD78A1009E91F5 /* geocode.json in Resources */ = {isa = PBXBuildFile; fileRef = 78D8CE3B23AD78A1009E91F5 /* geocode.json */; };
78D8CE3E23AD7FEE009E91F5 /* geocode_ip.json in Resources */ = {isa = PBXBuildFile; fileRef = 78D8CE3D23AD7FEE009E91F5 /* geocode_ip.json */; };
8227EF0C2CDAB69B00C47290 /* RadarRouteMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 8227EF0B2CDAB69B00C47290 /* RadarRouteMode.m */; };
825732512B72BE1900DF8B88 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 825732502B72BE1900DF8B88 /* PrivacyInfo.xcprivacy */; };
825732522B72BE1900DF8B88 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 825732502B72BE1900DF8B88 /* PrivacyInfo.xcprivacy */; };
82D04ABB29722ED20036619F /* RadarReplay.h in Headers */ = {isa = PBXBuildFile; fileRef = 82D04AB829722ED20036619F /* RadarReplay.h */; };
Expand Down Expand Up @@ -204,6 +205,7 @@
78156B9623A8210E0094410E /* RadarAddress.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RadarAddress.m; sourceTree = "<group>"; };
78D8CE3B23AD78A1009E91F5 /* geocode.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = geocode.json; sourceTree = "<group>"; };
78D8CE3D23AD7FEE009E91F5 /* geocode_ip.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = geocode_ip.json; sourceTree = "<group>"; };
8227EF0B2CDAB69B00C47290 /* RadarRouteMode.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RadarRouteMode.m; sourceTree = "<group>"; };
825732502B72BE1900DF8B88 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
82D04AB829722ED20036619F /* RadarReplay.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RadarReplay.h; sourceTree = "<group>"; };
82D04AB929722ED20036619F /* RadarReplayBuffer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RadarReplayBuffer.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -518,6 +520,7 @@
82D04ABA29722ED20036619F /* RadarReplayBuffer.h */,
82D04AB929722ED20036619F /* RadarReplayBuffer.m */,
DD236C782308797B00EB88F9 /* RadarSDK.h */,
8227EF0B2CDAB69B00C47290 /* RadarRouteMode.m */,
F667F8282BFBF3D1001F2F67 /* RadarSdkConfiguration.h */,
F667F8262BFBF3C8001F2F67 /* RadarSdkConfiguration.m */,
DD236CFB230895D400EB88F9 /* RadarSettings.h */,
Expand Down Expand Up @@ -862,6 +865,7 @@
0107AAD426220190008AB52F /* RadarRouteGeometry.m in Sources */,
0107AB2C262201F7008AB52F /* RadarTripOptions.m in Sources */,
0107AB0B262201D1008AB52F /* RadarAPIHelper.m in Sources */,
8227EF0C2CDAB69B00C47290 /* RadarRouteMode.m in Sources */,
9683FD6527B36C26009EBB6B /* RadarMeta.m in Sources */,
0107AA9B26220153008AB52F /* RadarContext.m in Sources */,
015C53AE29B8E8BA004F53A6 /* (null) in Sources */,
Expand Down Expand Up @@ -1062,7 +1066,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MARKETING_VERSION = 3.18.4;
MARKETING_VERSION = 3.18.5;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -1120,7 +1124,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MARKETING_VERSION = 3.18.4;
MARKETING_VERSION = 3.18.5;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
OTHER_CFLAGS = "-fembed-bitcode";
Expand Down
2 changes: 0 additions & 2 deletions RadarSDK/Include/Radar.h
Original file line number Diff line number Diff line change
Expand Up @@ -1200,9 +1200,7 @@ typedef void (^_Nullable RadarLogConversionCompletionHandler)(RadarStatus status

/**
Returns a display string for a travel mode value.
@param mode A travel mode value.
@return A display string for the travel mode value.
*/
+ (NSString *)stringForMode:(RadarRouteMode)mode NS_SWIFT_NAME(stringForMode(_:));
Expand Down
26 changes: 14 additions & 12 deletions RadarSDK/Include/RadarRouteMode.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
//
// RadarRouteMode.h
// RadarSDK
//
// Copyright © 2022 Radar Labs, Inc. All rights reserved.
//

// RadarRouteMode.h
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/**
The travel modes for routes.
@see https://radar.com/documentation/api#routing
*/
/** The travel modes for routes.
@see https://radar.com/documentation/api#routing
*/
typedef NS_OPTIONS(NSInteger, RadarRouteMode) {
/// Foot
RadarRouteModeFoot NS_SWIFT_NAME(foot) = 1 << 0,
Expand All @@ -27,4 +19,14 @@ typedef NS_OPTIONS(NSInteger, RadarRouteMode) {
RadarRouteModeMotorbike NS_SWIFT_NAME(motorbike) = 1 << 4
};

@interface RadarRouteModeUtils : NSObject

/** Returns a display string for a travel mode value.
@param mode A travel mode value.
@return A display string for the travel mode value.
*/
+ (NSString *)stringForMode:(RadarRouteMode)mode NS_SWIFT_NAME(stringForMode(_:));

@end

NS_ASSUME_NONNULL_END
10 changes: 9 additions & 1 deletion RadarSDK/Include/RadarTripOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

#import <Foundation/Foundation.h>

#import "Radar.h"
#import "RadarRouteMode.h"


NS_ASSUME_NONNULL_BEGIN

Expand All @@ -27,6 +28,11 @@ NS_ASSUME_NONNULL_BEGIN
destinationGeofenceExternalId:(NSString *_Nullable)destinationGeofenceExternalId
scheduledArrivalAt:(NSDate *_Nullable)scheduledArrivalAt;

- (instancetype)initWithExternalId:(NSString *_Nonnull)externalId
destinationGeofenceTag:(NSString *_Nullable)destinationGeofenceTag
destinationGeofenceExternalId:(NSString *_Nullable)destinationGeofenceExternalId
scheduledArrivalAt:(NSDate *_Nullable)scheduledArrivalAt
startTracking:(BOOL)startTracking;
/**
A stable unique ID for the trip.
*/
Expand Down Expand Up @@ -59,6 +65,8 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, assign) UInt16 approachingThreshold;

@property (nonatomic, assign) BOOL startTracking;

+ (RadarTripOptions *_Nullable)tripOptionsFromDictionary:(NSDictionary *)dict;
- (NSDictionary *)dictionaryValue;

Expand Down
29 changes: 7 additions & 22 deletions RadarSDK/Radar.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import "RadarVerificationManager.h"
#import "RadarReplayBuffer.h"
#import "RadarNotificationHelper.h"
#import "RadarTripOptions.h"

@interface Radar ()

Expand Down Expand Up @@ -315,6 +316,7 @@ + (void)setExpectedJurisdictionWithCountryCode:(NSString *)countryCode stateCode

+ (void)startTrackingWithOptions:(RadarTrackingOptions *)options {
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelInfo type:RadarLogTypeSDKCall message:@"startTracking()"];

[[RadarLocationManager sharedInstance] startTrackingWithOptions:options];
}

Expand Down Expand Up @@ -584,13 +586,14 @@ + (void)startTripWithOptions:(RadarTripOptions *)tripOptions
[RadarSettings removePreviousTrackingOptions];
}

if (trackingOptions) {
if (trackingOptions && trackingOptions.startTrackingAfter == nil) {
[self startTrackingWithOptions:trackingOptions];
} else if (!Radar.isTracking) {
} else if (trackingOptions) {
[RadarSettings setTrackingOptions:trackingOptions];
} else if (!Radar.isTracking && tripOptions && tripOptions.startTracking) {
[self startTrackingWithOptions:[RadarSettings remoteTrackingOptions] ?: [RadarSettings trackingOptions]];
}


// flush location update to generate events
[[RadarLocationManager sharedInstance] getLocationWithCompletionHandler:nil];
}
Expand Down Expand Up @@ -1251,25 +1254,7 @@ + (NSString *)stringForLocationSource:(RadarLocationSource)source {
}

+ (NSString *)stringForMode:(RadarRouteMode)mode {
NSString *str;
switch (mode) {
case RadarRouteModeFoot:
str = @"foot";
break;
case RadarRouteModeBike:
str = @"bike";
break;
case RadarRouteModeCar:
str = @"car";
break;
case RadarRouteModeTruck:
str = @"truck";
break;
case RadarRouteModeMotorbike:
str = @"motorbike";
break;
}
return str;
return [RadarRouteModeUtils stringForMode:mode];
}

+ (NSString *)stringForTripStatus:(RadarTripStatus)status {
Expand Down
12 changes: 10 additions & 2 deletions RadarSDK/RadarLocationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ - (void)stopTracking {
[self stopActivityAndMotionUpdates];
}

// null out startTrackingAfter and stopTrackingAfter in local tracking options
// so that subsequent trackOnce calls don't restart tracking
RadarTrackingOptions *trackingOptions = [RadarSettings trackingOptions];
trackingOptions.startTrackingAfter = nil;
trackingOptions.stopTrackingAfter = nil;
[RadarSettings setTrackingOptions:trackingOptions];

[self updateTracking];
}

Expand Down Expand Up @@ -304,17 +311,18 @@ - (void)updateTracking:(CLLocation *)location fromInitialize:(BOOL)fromInitializ
dispatch_async(dispatch_get_main_queue(), ^{
BOOL tracking = [RadarSettings tracking];
RadarTrackingOptions *options = [Radar getTrackingOptions];
RadarTrackingOptions *localOptions = [RadarSettings trackingOptions];

[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug
message:[NSString stringWithFormat:@"Updating tracking | options = %@; location = %@", [options dictionaryValue], location]];

if (!tracking && [options.startTrackingAfter timeIntervalSinceNow] < 0) {
if (!tracking && [localOptions.startTrackingAfter timeIntervalSinceNow] < 0) {
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug
message:[NSString stringWithFormat:@"Starting time-based tracking | startTrackingAfter = %@", options.startTrackingAfter]];

[RadarSettings setTracking:YES];
tracking = YES;
} else if (tracking && [options.stopTrackingAfter timeIntervalSinceNow] < 0) {
} else if (tracking && [localOptions.stopTrackingAfter timeIntervalSinceNow] < 0) {
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug
message:[NSString stringWithFormat:@"Stopping time-based tracking | stopTrackingAfter = %@", options.stopTrackingAfter]];

Expand Down
23 changes: 23 additions & 0 deletions RadarSDK/RadarRouteMode.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RadarRouteMode.m
#import "RadarRouteMode.h"

@implementation RadarRouteModeUtils

+ (NSString *)stringForMode:(RadarRouteMode)mode {
switch (mode) {
case RadarRouteModeFoot:
return @"foot";
case RadarRouteModeBike:
return @"bike";
case RadarRouteModeCar:
return @"car";
case RadarRouteModeTruck:
return @"truck";
case RadarRouteModeMotorbike:
return @"motorbike";
default:
return @"unknown";
}
}

@end
30 changes: 28 additions & 2 deletions RadarSDK/RadarTripOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ @implementation RadarTripOptions
static NSString *const kMode = @"mode";
static NSString *const kScheduledArrivalAt = @"scheduledArrivalAt";
static NSString *const kApproachingThreshold = @"approachingThreshold";
static NSString *const kStartTracking = @"startTracking";


- (instancetype)initWithExternalId:(NSString *_Nonnull)externalId
destinationGeofenceTag:(NSString *_Nullable)destinationGeofenceTag
Expand All @@ -27,6 +29,8 @@ - (instancetype)initWithExternalId:(NSString *_Nonnull)externalId
_destinationGeofenceTag = destinationGeofenceTag;
_destinationGeofenceExternalId = destinationGeofenceExternalId;
_mode = RadarRouteModeCar;
_startTracking = YES;

}
return self;
}
Expand All @@ -44,6 +48,23 @@ - (instancetype)initWithExternalId:(NSString *_Nonnull)externalId
return self;
}

- (instancetype)initWithExternalId:(NSString *_Nonnull)externalId
destinationGeofenceTag:(NSString *_Nullable)destinationGeofenceTag
destinationGeofenceExternalId:(NSString *_Nullable)destinationGeofenceExternalId
scheduledArrivalAt:(NSDate *_Nullable)scheduledArrivalAt
startTracking:(BOOL)startTracking {
self = [self initWithExternalId:externalId
destinationGeofenceTag:destinationGeofenceTag
destinationGeofenceExternalId:destinationGeofenceExternalId
scheduledArrivalAt:scheduledArrivalAt];

if (self) {
_startTracking = startTracking;
}

return self;
}

+ (RadarTripOptions *)tripOptionsFromDictionary:(NSDictionary *)dict {
if (!dict) {
return nil;
Expand Down Expand Up @@ -79,6 +100,8 @@ + (RadarTripOptions *)tripOptionsFromDictionary:(NSDictionary *)dict {
options.mode = RadarRouteModeCar;
}
options.approachingThreshold = [dict[kApproachingThreshold] intValue];
options.startTracking = dict[kStartTracking] ? [dict[kStartTracking] boolValue] : YES;

return options;
}

Expand All @@ -88,11 +111,13 @@ - (NSDictionary *)dictionaryValue {
dict[kMetadata] = self.metadata;
dict[kDestinationGeofenceTag] = self.destinationGeofenceTag;
dict[kDestinationGeofenceExternalId] = self.destinationGeofenceExternalId;
dict[kMode] = [Radar stringForMode:self.mode];
dict[kMode] = [RadarRouteModeUtils stringForMode:self.mode];
dict[kScheduledArrivalAt] = [RadarUtils.isoDateFormatter stringFromDate:self.scheduledArrivalAt];
if (self.approachingThreshold && self.approachingThreshold > 0) {
dict[kApproachingThreshold] = @(self.approachingThreshold);
}
dict[kStartTracking] = @(self.startTracking);

return dict;
}

Expand Down Expand Up @@ -120,7 +145,8 @@ - (BOOL)isEqual:(id)object {
[self.destinationGeofenceExternalId isEqualToString:options.destinationGeofenceExternalId])) &&
((!self.scheduledArrivalAt && !options.scheduledArrivalAt) ||
(self.scheduledArrivalAt != nil && options.scheduledArrivalAt != nil && [self.scheduledArrivalAt isEqualToDate:options.scheduledArrivalAt])) &&
self.mode == options.mode && ((!self.approachingThreshold && !options.approachingThreshold) || (self.approachingThreshold == options.approachingThreshold));
self.mode == options.mode && ((!self.approachingThreshold && !options.approachingThreshold) || (self.approachingThreshold == options.approachingThreshold)) &&
self.startTracking == options.startTracking;
}

@end
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.18.4";
return @"3.18.5";
}

+ (NSString *)deviceId {
Expand Down
Loading

0 comments on commit fe1f485

Please sign in to comment.