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

chore: ensure Android component is initialized from services #451

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.customer.commontest.config.configureAndroidSDKComponent
import io.customer.commontest.config.configureSDKComponent
import io.customer.commontest.config.testConfigurationDefault
import io.customer.sdk.core.di.SDKComponent
import io.customer.sdk.core.di.registerAndroidSDKComponent
import io.customer.sdk.core.di.setupAndroidComponent
import io.mockk.clearAllMocks

/**
Expand Down Expand Up @@ -43,6 +43,6 @@ abstract class BaseTest {
private fun registerAndroidSDKComponent(testConfig: TestConfig) {
val application = testConfig.argumentOrNull<ApplicationArgument>()?.value ?: return

testConfig.configureAndroidSDKComponent(SDKComponent.registerAndroidSDKComponent(application))
testConfig.configureAndroidSDKComponent(SDKComponent.setupAndroidComponent(application))
}
}
3 changes: 1 addition & 2 deletions core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ public abstract class io/customer/sdk/core/di/DiGraph {
public fun <init> ()V
public final fun getOverrides ()Ljava/util/concurrent/ConcurrentHashMap;
public final fun getSingletons ()Ljava/util/concurrent/ConcurrentHashMap;
public final fun overrideDependency (Ljava/lang/Class;Ljava/lang/Object;)V
public fun reset ()V
}

Expand All @@ -155,7 +154,7 @@ public final class io/customer/sdk/core/di/SDKComponent : io/customer/sdk/core/d
}

public final class io/customer/sdk/core/di/SDKComponentExtKt {
public static final fun registerAndroidSDKComponent (Lio/customer/sdk/core/di/SDKComponent;Landroid/content/Context;)Lio/customer/sdk/core/di/AndroidSDKComponent;
public static final fun setupAndroidComponent (Lio/customer/sdk/core/di/SDKComponent;Landroid/content/Context;)Lio/customer/sdk/core/di/AndroidSDKComponent;
}

public abstract interface class io/customer/sdk/core/environment/BuildEnvironment {
Expand Down
14 changes: 0 additions & 14 deletions core/src/main/kotlin/io/customer/sdk/core/di/DiGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,4 @@ abstract class DiGraph {
overrides.clear()
singletons.clear()
}

// TODO: Remove deprecated functions after all usages are removed.
@Deprecated("Use overrideDependency<Dependency> instead", ReplaceWith("overrideDependency<Dependency>(value)"))
fun <Dependency : Any> overrideDependency(dependency: Class<Dependency>, value: Dependency) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed these as they were deprecated and are no longer used by any DI graph in the SDK

overrides[dependency.name] = value as Any
}

@Deprecated("Use newInstance or singleton instead", ReplaceWith("newInstance()"))
inline fun <reified DEP : Any> override(): DEP? = overrides[dependencyKey<DEP>(identifier = null)] as? DEP

@Deprecated("Use singleton instead", ReplaceWith("singleton(newInstanceCreator)"))
inline fun <reified INST : Any> getSingletonInstanceCreate(newInstanceCreator: () -> INST): INST {
return getOrCreateSingletonInstance(identifier = null, newInstanceCreator = newInstanceCreator)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import android.content.Context
/**
* Create and register an instance of AndroidSDKComponent with the provided context,
* only if it is not already initialized.
* This function should be called from all entry points of the SDK to ensure that
* AndroidSDKComponent is initialized before accessing any of its dependencies.
*/
fun SDKComponent.registerAndroidSDKComponent(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the method is intended for internal SDK use only, I renamed it to provide more clarity on its purpose and function.

fun SDKComponent.setupAndroidComponent(
context: Context
) = registerDependency<AndroidSDKComponent> {
AndroidSDKComponentImpl(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.app.Application
import com.segment.analytics.kotlin.core.platform.policies.FlushPolicy
import io.customer.datapipelines.config.DataPipelinesModuleConfig
import io.customer.sdk.core.di.SDKComponent
import io.customer.sdk.core.di.registerAndroidSDKComponent
import io.customer.sdk.core.di.setupAndroidComponent
import io.customer.sdk.core.module.CustomerIOModule
import io.customer.sdk.core.module.CustomerIOModuleConfig
import io.customer.sdk.core.util.CioLogLevel
Expand Down Expand Up @@ -33,7 +33,7 @@ class CustomerIOBuilder(
// Initialize AndroidSDKComponent as soon as the builder is created so that
// it can be used by the modules.
// Also, it is needed to override test dependencies in the test environment
private val androidSDKComponent = SDKComponent.registerAndroidSDKComponent(
private val androidSDKComponent = SDKComponent.setupAndroidComponent(
context = applicationContext
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.google.android.gms.cloudmessaging.CloudMessagingReceiver
import io.customer.messagingpush.di.pushMessageProcessor
import io.customer.messagingpush.processor.PushMessageProcessor
import io.customer.sdk.core.di.SDKComponent
import io.customer.sdk.core.di.setupAndroidComponent

/**
* Broadcast receiver for listening to push events from GoogleCloudMessaging (GCM).
Expand All @@ -26,7 +27,7 @@ class CustomerIOCloudMessagingReceiver : BroadcastReceiver() {
val extras = intent.extras
// Ignore event if no data was received in extras
if (extras == null || extras.isEmpty) return
// TODO: Make sure PushMessageProcessor works as expected even if the SDK is not initialized
SDKComponent.setupAndroidComponent(context = context)
SDKComponent.pushMessageProcessor.processGCMMessageIntent(intent = intent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.customer.messagingpush.di.pushMessageProcessor
import io.customer.sdk.communication.Event
import io.customer.sdk.core.di.SDKComponent
import io.customer.sdk.core.di.setupAndroidComponent

open class CustomerIOFirebaseMessagingService : FirebaseMessagingService() {

Expand Down Expand Up @@ -45,8 +46,9 @@
fun onNewToken(context: Context, token: String) {
handleNewToken(context = context, token = token)
}

Check warning on line 49 in messagingpush/src/main/java/io/customer/messagingpush/CustomerIOFirebaseMessagingService.kt

View workflow job for this annotation

GitHub Actions / Generate Comparison Report

Parameter 'context' is never used
private fun handleNewToken(context: Context, token: String) {
SDKComponent.setupAndroidComponent(context = context)
eventBus.publish(
Event.RegisterDeviceTokenEvent(token)
)
Expand All @@ -57,7 +59,7 @@
remoteMessage: RemoteMessage,
handleNotificationTrigger: Boolean = true
): Boolean {
// TODO: Make sure PushNotificationHandler works as expected even if the SDK is not initialized
SDKComponent.setupAndroidComponent(context = context)
val handler = CustomerIOPushNotificationHandler(
pushMessageProcessor = SDKComponent.pushMessageProcessor,
remoteMessage = remoteMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Intent
import android.os.Bundle
import io.customer.messagingpush.di.pushMessageProcessor
import io.customer.sdk.core.di.SDKComponent
import io.customer.sdk.core.di.setupAndroidComponent
import io.customer.sdk.tracking.TrackableScreen

/**
Expand All @@ -24,6 +25,7 @@ class NotificationClickReceiverActivity : Activity(), TrackableScreen {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
SDKComponent.setupAndroidComponent(context = this)
handleIntent(data = intent)
}

Expand Down
Loading