Skip to content

Commit

Permalink
chore: cleaned up code styles in sdk classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 committed Oct 18, 2022
1 parent 2dcdfc6 commit 317715a
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ModuleMessagingInApp internal constructor(

private fun initializeGist(organizationId: String) {
gistProvider.initProvider(
application = diGraph.context as Application,
application = diGraph.context.applicationContext as Application,
organizationId = organizationId
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ class CustomerIOFirebaseMessagingService : FirebaseMessagingService() {
* Handles receiving an incoming push notification.
*
* Call this from a custom [FirebaseMessagingService] to pass push messages to
* CustomerIo SDK for tracking and rendering
* CustomerIO SDK for tracking and rendering
* @param context reference to application context
* @param remoteMessage Remote message received from Firebase in
* [FirebaseMessagingService.onMessageReceived]
* @param handleNotificationTrigger indicating if the local notification should be triggered
* @return Boolean indicating whether this will be handled by CustomerIo
* @return Boolean indicating whether this will be handled by CustomerIO
*/
@JvmOverloads
fun onMessageReceived(
Expand Down Expand Up @@ -78,12 +79,10 @@ class CustomerIOFirebaseMessagingService : FirebaseMessagingService() {
* Handles new or refreshed token
* Call this from [FirebaseMessagingService] to register the new device token
*
* @param context reference to application context
* @param token new or refreshed token
*/
fun onNewToken(
context: Context,
token: String
) {
fun onNewToken(context: Context, token: String) {
handleNewToken(context = context, token = token)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
package io.customer.messagingpush.extensions

import android.graphics.Color
import androidx.annotation.DrawableRes
import androidx.annotation.ColorInt
import io.customer.sdk.CustomerIOShared

@DrawableRes
@ColorInt
internal fun String.toColorOrNull(): Int? = try {
Color.parseColor(this)
} catch (ex: IllegalArgumentException) {
Expand Down
16 changes: 14 additions & 2 deletions sdk/api/sdk.api
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
public final class io/customer/sdk/AnalyticsConstants {
public static final field AUTO_TRACK_DEVICE_ATTRIBUTES Z
public static final field BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS I
public static final field BACKGROUND_QUEUE_SECONDS_DELAY D
public static final field HTTP_REQUEST_TIMEOUT J
public static final field INSTANCE Lio/customer/sdk/AnalyticsConstants;
public static final field SHOULD_AUTO_RECORD_SCREEN_VIEWS Z
}

public final class io/customer/sdk/CustomerIO : io/customer/sdk/CustomerIOInstance {
public static final field Companion Lio/customer/sdk/CustomerIO$Companion;
public fun clearIdentify ()V
Expand Down Expand Up @@ -126,6 +135,11 @@ public final class io/customer/sdk/CustomerIOShared$Companion {
public final fun instance ()Lio/customer/sdk/CustomerIOShared;
}

public final class io/customer/sdk/SDKConstants {
public static final field INSTANCE Lio/customer/sdk/SDKConstants;
public final fun getLOG_LEVEL_DEFAULT ()Lio/customer/sdk/util/CioLogLevel;
}

public final class io/customer/sdk/data/model/EventType : java/lang/Enum {
public static final field event Lio/customer/sdk/data/model/EventType;
public static final field screen Lio/customer/sdk/data/model/EventType;
Expand Down Expand Up @@ -189,8 +203,6 @@ public abstract interface class io/customer/sdk/device/DeviceTokenProvider {

public final class io/customer/sdk/di/CustomerIOComponent : io/customer/sdk/di/DiGraph {
public fun <init> (Lio/customer/sdk/di/CustomerIOStaticComponent;Landroid/content/Context;Lio/customer/sdk/CustomerIOConfig;)V
public final fun buildRetrofit (Ljava/lang/String;J)Lretrofit2/Retrofit;
public final fun buildStore ()Lio/customer/sdk/data/store/CustomerIOStore;
public final fun getActivityLifecycleCallbacks ()Lio/customer/sdk/CustomerIOActivityLifecycleCallbacks;
public final fun getCioHttpRetryPolicy ()Lio/customer/sdk/api/HttpRetryPolicy;
public final fun getContext ()Landroid/content/Context;
Expand Down
21 changes: 21 additions & 0 deletions sdk/src/main/java/io/customer/sdk/Constants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.customer.sdk

import io.customer.sdk.util.CioLogLevel

/**
* SDK constants to avoid repetitive configuration values
*/
object SDKConstants {
val LOG_LEVEL_DEFAULT = CioLogLevel.ERROR
}

/**
* Analytics tracking module constants to avoid repetitive configuration values
*/
object AnalyticsConstants {
const val AUTO_TRACK_DEVICE_ATTRIBUTES = true
const val BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS = 10
const val BACKGROUND_QUEUE_SECONDS_DELAY = 30.0
const val HTTP_REQUEST_TIMEOUT = 6000L
const val SHOULD_AUTO_RECORD_SCREEN_VIEWS = true
}
18 changes: 11 additions & 7 deletions sdk/src/main/java/io/customer/sdk/CustomerIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Welcome to the Customer.io Android SDK!
This class is where you begin to use the SDK.
You must have an instance of `CustomerIO` to use the features of the SDK.
Create your own instance using
`CustomerIo.Builder(siteId: "XXX", apiKey: "XXX", region: Region.US, appContext: Application context)`
`CustomerIO.Builder(siteId: "XXX", apiKey: "XXX", region: Region.US, appContext: Application context)`
It is recommended to initialize the client in the `Application::onCreate()` method.
After the instance is created you can access it via singleton instance: `CustomerIO.instance()` anywhere,
*/
Expand Down Expand Up @@ -166,16 +166,20 @@ class CustomerIO internal constructor(
) {
private val sharedInstance = CustomerIOShared.instance()
private var client: Client = Client.Android(Version.version)
private var timeout = 6000L
private var shouldAutoRecordScreenViews: Boolean = false
private var autoTrackDeviceAttributes: Boolean = true
private var timeout = AnalyticsConstants.HTTP_REQUEST_TIMEOUT
private var shouldAutoRecordScreenViews: Boolean =
AnalyticsConstants.SHOULD_AUTO_RECORD_SCREEN_VIEWS
private var autoTrackDeviceAttributes: Boolean =
AnalyticsConstants.AUTO_TRACK_DEVICE_ATTRIBUTES
private val modules: MutableMap<String, CustomerIOModule<out CustomerIOModuleConfig>> =
mutableMapOf()
private var logLevel = CioLogLevel.ERROR
private var logLevel: CioLogLevel = SDKConstants.LOG_LEVEL_DEFAULT
internal var overrideDiGraph: CustomerIOComponent? = null // set for automated tests
private var trackingApiUrl: String? = null
private var backgroundQueueMinNumberOfTasks: Int = 10
private var backgroundQueueSecondsDelay: Double = 30.0
private var backgroundQueueMinNumberOfTasks: Int =
AnalyticsConstants.BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS
private var backgroundQueueSecondsDelay: Double =
AnalyticsConstants.BACKGROUND_QUEUE_SECONDS_DELAY

fun setClient(client: Client): Builder {
this.client = client
Expand Down
16 changes: 0 additions & 16 deletions sdk/src/main/java/io/customer/sdk/api/HeadersInterceptor.kt

This file was deleted.

116 changes: 63 additions & 53 deletions sdk/src/main/java/io/customer/sdk/di/CustomerIOComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,52 +34,56 @@ class CustomerIOComponent(
) : DiGraph() {

val fileStorage: FileStorage
get() = override() ?: FileStorage(sdkConfig, context, logger)
get() = override() ?: FileStorage(config = sdkConfig, context = context, logger = logger)

val jsonAdapter: JsonAdapter
get() = override() ?: JsonAdapter(moshi)
get() = override() ?: JsonAdapter(moshi = moshi)

val queueStorage: QueueStorage
get() = override() ?: QueueStorageImpl(
sdkConfig,
fileStorage,
jsonAdapter,
dateUtil,
logger
sdkConfig = sdkConfig,
fileStorage = fileStorage,
jsonAdapter = jsonAdapter,
dateUtil = dateUtil,
logger = logger
)

val queueRunner: QueueRunner
get() = override() ?: QueueRunnerImpl(jsonAdapter, cioHttpClient, logger)
get() = override() ?: QueueRunnerImpl(
jsonAdapter = jsonAdapter,
cioHttpClient = cioHttpClient,
logger = logger
)

val dispatchersProvider: DispatchersProvider
get() = override() ?: staticComponent.dispatchersProvider

val queue: Queue
get() = override() ?: getSingletonInstanceCreate {
QueueImpl(
dispatchersProvider,
queueStorage,
queueRunRequest,
jsonAdapter,
sdkConfig,
timer,
logger,
dateUtil
dispatchersProvider = dispatchersProvider,
storage = queueStorage,
runRequest = queueRunRequest,
jsonAdapter = jsonAdapter,
sdkConfig = sdkConfig,
queueTimer = timer,
logger = logger,
dateUtil = dateUtil
)
}

internal val cleanupRepository: CleanupRepository
get() = override() ?: CleanupRepositoryImpl(queue)
get() = override() ?: CleanupRepositoryImpl(queue = queue)

val queueQueryRunner: QueueQueryRunner
get() = override() ?: QueueQueryRunnerImpl(logger)
get() = override() ?: QueueQueryRunnerImpl(logger = logger)

val queueRunRequest: QueueRunRequest
get() = override() ?: QueueRunRequestImpl(
queueRunner,
queueStorage,
logger,
queueQueryRunner
runner = queueRunner,
queueStorage = queueStorage,
logger = logger,
queryRunner = queueQueryRunner
)

val logger: Logger
Expand All @@ -88,15 +92,18 @@ class CustomerIOComponent(
val hooksManager: HooksManager
get() = override() ?: getSingletonInstanceCreate { CioHooksManager() }

internal val cioHttpClient: TrackingHttpClient
get() = override() ?: RetrofitTrackingHttpClient(buildRetrofitApi(), httpRequestRunner)
private val cioHttpClient: TrackingHttpClient
get() = override() ?: RetrofitTrackingHttpClient(
retrofitService = buildRetrofitApi(),
httpRequestRunner = httpRequestRunner
)

private val httpRequestRunner: HttpRequestRunner
get() = HttpRequestRunnerImpl(
sitePreferenceRepository,
logger,
cioHttpRetryPolicy,
jsonAdapter
prefsRepository = sitePreferenceRepository,
logger = logger,
retryPolicy = cioHttpRetryPolicy,
jsonAdapter = jsonAdapter
)

val cioHttpRetryPolicy: HttpRetryPolicy
Expand All @@ -106,48 +113,51 @@ class CustomerIOComponent(
get() = override() ?: DateUtilImpl()

val timer: SimpleTimer
get() = override() ?: AndroidSimpleTimer(logger, dispatchersProvider)
get() = override() ?: AndroidSimpleTimer(
logger = logger,
dispatchersProvider = dispatchersProvider
)

val trackRepository: TrackRepository
get() = override() ?: TrackRepositoryImpl(
sitePreferenceRepository,
queue,
logger,
hooksManager
sitePreferenceRepository = sitePreferenceRepository,
backgroundQueue = queue,
logger = logger,
hooksManager = hooksManager
)

val profileRepository: ProfileRepository
get() = override() ?: ProfileRepositoryImpl(
deviceRepository,
sitePreferenceRepository,
queue,
logger,
hooksManager
deviceRepository = deviceRepository,
sitePreferenceRepository = sitePreferenceRepository,
backgroundQueue = queue,
logger = logger,
hooksManager = hooksManager
)

val deviceRepository: DeviceRepository
get() = override() ?: DeviceRepositoryImpl(
sdkConfig,
buildStore().deviceStore,
sitePreferenceRepository,
queue,
dateUtil,
logger
config = sdkConfig,
deviceStore = buildStore().deviceStore,
sitePreferenceRepository = sitePreferenceRepository,
backgroundQueue = queue,
dateUtil = dateUtil,
logger = logger
)

val activityLifecycleCallbacks: CustomerIOActivityLifecycleCallbacks
get() = override() ?: getSingletonInstanceCreate {
CustomerIOActivityLifecycleCallbacks(sdkConfig)
CustomerIOActivityLifecycleCallbacks(config = sdkConfig)
}

fun buildStore(): CustomerIOStore {
private fun buildStore(): CustomerIOStore {
return override() ?: object : CustomerIOStore {
override val deviceStore: DeviceStore by lazy {
DeviceStoreImp(
sdkConfig,
BuildStoreImp(),
ApplicationStoreImp(context),
Version.version
sdkConfig = sdkConfig,
buildStore = BuildStoreImp(),
applicationStore = ApplicationStoreImp(context),
version = Version.version
)
}
}
Expand All @@ -163,8 +173,8 @@ class CustomerIOComponent(
private inline fun <reified T> buildRetrofitApi(): T {
val apiClass = T::class.java
return override() ?: buildRetrofit(
sdkConfig.trackingApiHostname,
sdkConfig.timeout
endpoint = sdkConfig.trackingApiHostname,
timeout = sdkConfig.timeout
).create(apiClass)
}

Expand All @@ -185,7 +195,7 @@ class CustomerIOComponent(
.build()
}

fun buildRetrofit(
private fun buildRetrofit(
endpoint: String,
timeout: Long
): Retrofit {
Expand Down
Loading

0 comments on commit 317715a

Please sign in to comment.