Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move getTimeFocusedElapsed from OneSignalTracker to `OSSessionMan…
…ager`

Motivation: move into `OSSessionManager` to be accessible from other modules
  • Loading branch information
Rodrigo Gomez Palacio committed Oct 23, 2024
commit b07df39a50353f45e73656da0faf395c3f47cd59
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@

+ (void)resetSharedSessionManager;

- (NSTimeInterval)getTimeFocusedElapsed;

- (void)setLastOpenedTime:(NSTimeInterval)lastOpened;

@property (nonatomic) id<SessionStatusDelegate> _Nullable delegate;
@property AppEntryAction appEntryState;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,32 @@ @implementation OSSessionManager

NSDate *_sessionLaunchTime;
AppEntryAction _appEntryState = APP_CLOSE;
static NSTimeInterval lastOpenedTime;

+ (OSSessionManager*)sharedSessionManager {
if (!_sessionManager)
_sessionManager = [[OSSessionManager alloc] init:nil withTrackerFactory:[OSTrackerFactory sharedTrackerFactory]];
return _sessionManager;
}

- (void)setLastOpenedTime:(NSTimeInterval)lastOpened {
lastOpenedTime = lastOpened;
}

- (NSTimeInterval)getTimeFocusedElapsed {
if (!lastOpenedTime)
return -1;

NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
NSTimeInterval timeElapsed = now - lastOpenedTime;

// Time is invalid if below 0 or over a day (86400 seconds)
if (timeElapsed < 0 || timeElapsed > 86400)
return -1;

return timeElapsed;
}

+ (void)resetSharedSessionManager {
_sessionManager = nil;
}
Expand Down
33 changes: 4 additions & 29 deletions iOS_SDK/OneSignalSDK/Source/OneSignalTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,8 @@ + (NSString *)mExternalIdAuthToken;

@implementation OneSignalTracker

static NSTimeInterval lastOpenedTime;
static BOOL lastOnFocusWasToBackground = YES;

+ (void)resetLocals {
[OSFocusTimeProcessorFactory resetUnsentActiveTime];
lastOpenedTime = 0;
lastOnFocusWasToBackground = YES;
}

+ (void)setLastOpenedTime:(NSTimeInterval)lastOpened {
lastOpenedTime = lastOpened;
}

+ (void)onFocus:(BOOL)toBackground {
// return if the user has not granted privacy permissions
if ([OSPrivacyConsentController requiresUserPrivacyConsent])
Expand All @@ -88,7 +77,7 @@ + (void)applicationBecameActive {
if (OSSessionManager.sharedSessionManager.appEntryState != NOTIFICATION_CLICK)
OSSessionManager.sharedSessionManager.appEntryState = APP_OPEN;

lastOpenedTime = [NSDate date].timeIntervalSince1970;
[OSSessionManager.sharedSessionManager setLastOpenedTime:[[NSDate date] timeIntervalSince1970]];

// on_session tracking when resumming app.
if ([OneSignal shouldStartNewSession])
Expand All @@ -104,7 +93,7 @@ + (void)applicationBackgrounded {
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"Application Backgrounded started"];
[self updateLastClosedTime];

let timeElapsed = [self getTimeFocusedElapsed];
let timeElapsed = [OSSessionManager.sharedSessionManager getTimeFocusedElapsed];
if (timeElapsed < -1)
return;

Expand All @@ -124,7 +113,7 @@ + (void)applicationBackgrounded {
// The on_focus call is made right away.
+ (void)onSessionEnded:(NSArray<OSInfluence *> *)lastInfluences {
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"onSessionEnded started"];
let timeElapsed = [self getTimeFocusedElapsed];
let timeElapsed = [OSSessionManager.sharedSessionManager getTimeFocusedElapsed];
let focusCallParams = [self createFocusCallParams:lastInfluences onSessionEnded:true];
let timeProcessor = [OSFocusTimeProcessorFactory createTimeProcessorWithInfluences:lastInfluences focusEventType:END_SESSION];

Expand All @@ -141,7 +130,7 @@ + (void)onSessionEnded:(NSArray<OSInfluence *> *)lastInfluences {
}

+ (OSFocusCallParams *)createFocusCallParams:(NSArray<OSInfluence *> *)lastInfluences onSessionEnded:(BOOL)onSessionEnded {
let timeElapsed = [self getTimeFocusedElapsed];
let timeElapsed = [OSSessionManager.sharedSessionManager getTimeFocusedElapsed];
NSMutableArray<OSFocusInfluenceParam *> *focusInfluenceParams = [NSMutableArray new];

for (OSInfluence *influence in lastInfluences) {
Expand All @@ -159,20 +148,6 @@ + (OSFocusCallParams *)createFocusCallParams:(NSArray<OSInfluence *> *)lastInflu
onSessionEnded:onSessionEnded];
}

+ (NSTimeInterval)getTimeFocusedElapsed {
if (!lastOpenedTime)
return -1;

let now = [NSDate date].timeIntervalSince1970;
let timeElapsed = now - (int)(lastOpenedTime + 0.5);

// Time is invalid if below 1 or over a day
if (timeElapsed < 0 || timeElapsed > 86400)
return -1;

return timeElapsed;
}

+ (void)updateLastClosedTime {
let now = [NSDate date].timeIntervalSince1970;
[OneSignalUserDefaults.initStandard saveDoubleForKey:OSUD_APP_LAST_CLOSED_TIME withValue:now];
Expand Down