-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
only refresh User when app is foregrounded
Ensure cache for the user is refreshed once per cold start when app is in the foreground. This saves resources as there are a number of events (such as push received or non-OneSignal events) that start the app in the background but will never read/write any user properties.
- Loading branch information
Showing
3 changed files
with
53 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...DK/onesignal/core/src/main/java/com/onesignal/user/internal/service/UserRefreshService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.onesignal.user.internal.service | ||
|
||
import com.onesignal.core.internal.application.IApplicationLifecycleHandler | ||
import com.onesignal.core.internal.application.IApplicationService | ||
import com.onesignal.core.internal.config.ConfigModelStore | ||
import com.onesignal.core.internal.operations.IOperationRepo | ||
import com.onesignal.core.internal.startup.IStartableService | ||
import com.onesignal.user.internal.identity.IdentityModelStore | ||
import com.onesignal.user.internal.operations.RefreshUserOperation | ||
|
||
// Ensure cache for the user is refreshed once per cold start when app | ||
// is in the foreground. This saves resources as there are a number of | ||
// events (such as push received or non-OneSignal events) that start | ||
// the app in the background but will never read/write any user | ||
// properties. | ||
class UserRefreshService( | ||
private val _applicationService: IApplicationService, | ||
private val _operationRepo: IOperationRepo, | ||
private val _configModelStore: ConfigModelStore, | ||
private val _identityModelStore: IdentityModelStore, | ||
) : IStartableService, | ||
IApplicationLifecycleHandler { | ||
private fun refreshUser() { | ||
_operationRepo.enqueue( | ||
RefreshUserOperation( | ||
_configModelStore.model.appId, | ||
_identityModelStore.model.onesignalId, | ||
), | ||
) | ||
} | ||
|
||
override fun start() { | ||
if (_applicationService.isInForeground) { | ||
refreshUser() | ||
} else { | ||
_applicationService.addApplicationLifecycleHandler(this) | ||
} | ||
} | ||
|
||
private var onFocusCalled: Boolean = false | ||
|
||
override fun onFocus() { | ||
if (onFocusCalled) return | ||
onFocusCalled = true | ||
refreshUser() | ||
} | ||
|
||
override fun onUnfocused() { } | ||
} |