Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ensure ConnectivityManager is available before other plugins #101

Open
wants to merge 6 commits into
base: feat/sdk-2850-implementation-network-observer
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,29 @@ class Analytics(
internal val activityLifecycleManagementPlugin = ActivityLifecycleManagementPlugin()
internal val processLifecycleManagementPlugin = ProcessLifecycleManagementPlugin()
private val sessionTrackingPlugin = SessionTrackingPlugin()
override lateinit var connectivityObserver: ConnectivityObserver
override val connectivityObserver: ConnectivityObserver

/**
* Initializes the core components of the class upon instantiation.
*
* This `init` block ensures that essential components are set up in the correct order.
* The following components are initialized:
*
* - **Logger**: Initializes the logging mechanism using `AndroidLogger`. This must be called first to ensure proper logging for all subsequent setup tasks.
* - **Connectivity Observer**: Configures an observer to monitor network connectivity changes, utilizing the application context and core analytics instance.
* - **Core Analytics Setup**: Initializes core analytics components, ensuring they are ready before any other setup or operations begin.
* - **Android-Specific Setup**: Sets up other Android-specific components and dependencies needed for the class.
* - **Source Configuration Setup**: Setup the SourceConfig.
*/
init {
setLogger(logger = AndroidLogger())
connectivityObserver = AndroidConnectivityObserver(
application = configuration.application,
analyticsScope = this.analyticsScope
)
setupCoreAnalytics()
setup()
setupSourceConfig()
}

/**
Expand Down Expand Up @@ -205,7 +224,6 @@ class Analytics(
}

private fun setup() {
setLogger(logger = AndroidLogger())
add(DeviceInfoPlugin())
add(AppInfoPlugin())
add(NetworkInfoPlugin())
Expand All @@ -223,8 +241,6 @@ class Analytics(
// adding lifecycle management plugins last so that lifecycle callbacks are invoked after all the observers in plugins are added.
add(processLifecycleManagementPlugin)
add(activityLifecycleManagementPlugin)

connectivityObserver = AndroidConnectivityObserver((configuration as Configuration).application, this.analyticsScope)
}

override fun getPlatformType(): PlatformType = PlatformType.Mobile
Expand Down
50 changes: 41 additions & 9 deletions core/src/main/kotlin/com/rudderstack/sdk/kotlin/core/Analytics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,51 @@ open class Analytics protected constructor(
override val connectivityObserver: ConnectivityObserver by lazy { DefaultConnectivityObserver() }

init {
runForBaseTypeOnly()
}

/**
* Initializes core components when the class is of the base type (`Analytics`).
*
* This function performs the following tasks:
* - **Logger Setup**: Initializes the logger by calling `setLogger()`, ensuring that the logger is ready to log events.
* - **Core Analytics Setup**: Configures the core analytics components by calling `setupCoreAnalytics()`.
* - **Source Configuration**: Sets up the source configuration by calling `setupSourceConfig()`.
*/
private fun runForBaseTypeOnly() {
if (this::class == Analytics::class) {
setLogger(logger = KotlinLogger())
setupCoreAnalytics()
setupSourceConfig()
}
}

/**
* Initializes the core analytics components during the class setup.
*
* This function performs the following tasks:
* - **Event Processing Channel**: Initializes the event processing channel by calling `processEvents()`. This ensures the channel is ready to process events.
* - **Plugin Setup**: Configures core plugins by calling `setup()`. This sets up all the necessary plugins for analytics functionality.
* - **Anonymous ID Storage**: Stores the anonymous ID by calling `storeAnonymousId()`.
*/
protected fun setupCoreAnalytics() {
processEvents()
setup()
storeAnonymousId()
}

/**
* This function is called during initialization to set up the source config.
*/
protected fun setupSourceConfig() {
analyticsScope.launch(analyticsDispatcher) {
SourceConfigManager(
analytics = this@Analytics,
sourceConfigState = sourceConfigState
).fetchAndUpdateSourceConfig()
}
}

/**
* Configures the logger for analytics with a specified `Logger` instance.
*
Expand All @@ -96,7 +136,7 @@ open class Analytics protected constructor(
*
* @param logger The `Logger` instance to use for logging. Defaults to an instance of `KotlinLogger`.
*/
fun setLogger(logger: Logger) {
protected fun setLogger(logger: Logger) {
if (!isAnalyticsActive()) return
LoggerAnalytics.setup(logger = logger, logLevel = configuration.logLevel)
}
Expand Down Expand Up @@ -307,16 +347,8 @@ open class Analytics protected constructor(
* and `RudderStackDataplanePlugin`. This function is called during initialization.
*/
private fun setup() {
setLogger(logger = KotlinLogger())
add(LibraryInfoPlugin())
add(RudderStackDataplanePlugin())

analyticsScope.launch(analyticsDispatcher) {
SourceConfigManager(
analytics = this@Analytics,
sourceConfigState = sourceConfigState
).fetchAndUpdateSourceConfig()
}
}

/**
Expand Down