-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added shared instance for independent components
- Loading branch information
Showing
13 changed files
with
267 additions
and
24 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
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
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
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,54 @@ | ||
package io.customer.sdk | ||
|
||
import androidx.annotation.VisibleForTesting | ||
import io.customer.base.internal.InternalCustomerIOApi | ||
import io.customer.sdk.CustomerIOShared.Companion.instance | ||
import io.customer.sdk.di.CustomerIOSharedComponent | ||
import io.customer.sdk.util.LogcatLogger | ||
|
||
/** | ||
* Singleton static instance of Customer.io SDK that is initialized exactly when | ||
* [instance] is called the first time. The class should be lightweight and only | ||
* be used to hold code that might be required before initializing the SDK. | ||
* <p/> | ||
* Some use cases of the class may include: | ||
* - access selected SDK methods even when SDK is not initialized | ||
* - contains code that cannot guarantee SDK initialization | ||
* - notify user and prevent unwanted SDK crashes in case of late initialization | ||
* - hold callbacks/values that might be needed post-initialization of the SDK | ||
* - reduce challenges of communication when wrapping the SDK for non native | ||
* platforms | ||
* | ||
* @property diGraph instance of DI graph to satisfy dependencies | ||
*/ | ||
class CustomerIOShared private constructor( | ||
val diGraph: CustomerIOSharedComponent | ||
) { | ||
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) | ||
fun attachSDKConfig(sdkConfig: CustomerIOConfig) { | ||
(diGraph.logger as? LogcatLogger)?.setPreferredLogLevel(logLevel = sdkConfig.logLevel) | ||
} | ||
|
||
companion object { | ||
private var INSTANCE: CustomerIOShared? = null | ||
|
||
@JvmStatic | ||
@OptIn(InternalCustomerIOApi::class) | ||
fun instance(): CustomerIOShared = createInstance(diGraph = null) | ||
|
||
@Synchronized | ||
@InternalCustomerIOApi | ||
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) | ||
fun createInstance( | ||
diGraph: CustomerIOSharedComponent? = null | ||
): CustomerIOShared = INSTANCE ?: CustomerIOShared( | ||
diGraph = diGraph ?: CustomerIOSharedComponent() | ||
).apply { INSTANCE = this } | ||
|
||
@InternalCustomerIOApi | ||
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) | ||
fun clearInstance() { | ||
INSTANCE = null | ||
} | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
sdk/src/main/java/io/customer/sdk/di/CustomerIOSharedComponent.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,25 @@ | ||
package io.customer.sdk.di | ||
|
||
import io.customer.sdk.util.* | ||
|
||
/** | ||
* Static/shared component dependency graph to satisfy independent dependencies | ||
* from single place. All other graphs should never redefine dependencies defined | ||
* here unless extremely necessary. | ||
* <p/> | ||
* The class should only contain dependencies matching the following criteria: | ||
* - dependencies that may be required without SDK initialization | ||
* - dependencies that are lightweight and are not dependent on SDK initialization | ||
*/ | ||
@Suppress("MemberVisibilityCanBePrivate") | ||
class CustomerIOSharedComponent : DiGraph() { | ||
val staticSettingsProvider: StaticSettingsProvider by lazy { | ||
override() ?: StaticSettingsProviderImpl() | ||
} | ||
|
||
val logger: Logger by lazy { | ||
override() ?: LogcatLogger(staticSettingsProvider = staticSettingsProvider) | ||
} | ||
|
||
val dispatchersProvider: DispatchersProvider by lazy { override() ?: SdkDispatchers() } | ||
} |
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
18 changes: 18 additions & 0 deletions
18
sdk/src/main/java/io/customer/sdk/util/StaticSettingsProvider.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,18 @@ | ||
package io.customer.sdk.util | ||
|
||
import io.customer.sdk.BuildConfig | ||
|
||
/** | ||
* Wrapper class to hold static/only one time defined properties from | ||
* [BuildConfig] and other Android classes to achieve the following: | ||
* - making it easier to test classes relying on these properties | ||
* - create abstraction and reduce dependency from native Android properties; | ||
* can be helpful in SDK wrappers | ||
*/ | ||
interface StaticSettingsProvider { | ||
val isDebuggable: Boolean | ||
} | ||
|
||
class StaticSettingsProviderImpl : StaticSettingsProvider { | ||
override val isDebuggable: Boolean = BuildConfig.DEBUG | ||
} |
Oops, something went wrong.