Skip to content

Improved fallback mechanism when user is not using hilt/dagger #43

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

Merged
merged 5 commits into from
Feb 3, 2025
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
4 changes: 2 additions & 2 deletions chat-sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ dependencies {
implementation(libs.loggingInterceptor)

//Hilt
implementation(libs.hiltAndroid)
implementation(libs.lifecycleProcess)
compileOnly(libs.hiltAndroid)
compileOnly(libs.lifecycleProcess)
kapt(libs.hiltCompiler)
kapt(libs.hiltAndroidCompiler)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,50 @@ object ChatSessionProvider {
}
}

// Private method to initialize ChatSession using Hilt or manual fallback
private fun initializeChatSession(context: Context): ChatSession {
return if (isHiltAvailable()) {
// Use Hilt's EntryPoint mechanism if Hilt is available
val entryPoint = EntryPointAccessors.fromApplication(
context.applicationContext,
ChatModule.ChatSessionEntryPoint::class.java
)
entryPoint.getChatSession()
} else {
// Fallback to manual initialization
createChatSessionManually(context)
}
}

// Method to check if Hilt is available
private fun isHiltAvailable(): Boolean {
return try {
Class.forName("dagger.hilt.EntryPoints")
// Check for the presence of Hilt's core class
val hiltClass = Class.forName("dagger.hilt.android.EntryPointAccessors")
SDKLogger.logger.logDebug { "Hilt detected: $hiltClass" }
true
} catch (e: ClassNotFoundException) {
SDKLogger.logger.logDebug {"Hilt is not available"}
SDKLogger.logger.logDebug { "Hilt is not available: ${e.message}" }
false
} catch (e: Exception) {
// Catch any unexpected issues during class detection
SDKLogger.logger.logDebug { "Error while checking Hilt availability: ${e.message}" }
Copy link

Choose a reason for hiding this comment

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

why this is logged as Debug not err ?

false
}
}

private fun initializeChatSession(context: Context): ChatSession {
return try {
if (isHiltAvailable()) {
SDKLogger.logger.logDebug { "Attempting Hilt-based ChatSession initialization." }
val entryPoint = EntryPointAccessors.fromApplication(
context.applicationContext,
ChatModule.ChatSessionEntryPoint::class.java
)
entryPoint.getChatSession().also {
SDKLogger.logger.logDebug { "ChatSession initialized using Hilt." }
}
} else {
SDKLogger.logger.logDebug { "Hilt not available. Falling back to manual initialization." }
createChatSessionManually(context).also {
SDKLogger.logger.logDebug { "ChatSession initialized manually." }
}
}
} catch (e: Exception) {
// Handle unexpected errors during initialization
SDKLogger.logger.logDebug { "Error initializing ChatSession: ${e.message}" }
Copy link

Choose a reason for hiding this comment

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

Is this as debug statement correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes we aim to print it as a debug which won't interrupt the flow

SDKLogger.logger.logDebug { "Falling back to manual initialization due to error." }
createChatSessionManually(context).also {
SDKLogger.logger.logDebug { "ChatSession initialized manually after Hilt failure." }
}
}
}

// Manual initialization of ChatSession for non-Hilt users
private fun createChatSessionManually(context: Context): ChatSession {
val appContext = context.applicationContext
Expand Down
2 changes: 1 addition & 1 deletion chat-sdk/version.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
sdkVersion=1.0.5
sdkVersion=1.0.6
groupId=software.aws.connect
artifactId=amazon-connect-chat-android
Loading