Skip to content

Commit

Permalink
feat: Change usecase to manager and use error for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielGreenEngineer committed Dec 2, 2024
1 parent 6759912 commit c34b237
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import android.util.Log
import com.google.android.gms.tasks.Task
import com.google.firebase.messaging.FirebaseMessaging
import com.personalization.api.OnApiCallbackListener
import com.personalization.api.managers.InAppNotificationManager
import com.personalization.errors.BaseInfoError
import com.personalization.errors.FirebaseError
import com.personalization.errors.JsonResponseErrorHandler
import com.personalization.sdk.data.mappers.SdkInitializationMapper.mapToSdkInitResponse
import com.personalization.sdk.domain.usecases.inAppNotification.InAppNotificationUseCase
import com.personalization.sdk.domain.usecases.network.ExecuteQueueTasksUseCase
import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase
import com.personalization.sdk.domain.usecases.preferences.GetPreferencesValueUseCase
Expand All @@ -37,7 +37,7 @@ class RegisterManager @Inject constructor(
private val getUserSettingsValueUseCase: GetUserSettingsValueUseCase,
private val sendNetworkMethodUseCase: SendNetworkMethodUseCase,
private val executeQueueTasksUseCase: ExecuteQueueTasksUseCase,
private val inAppNotificationUseCase: InAppNotificationUseCase
private val inAppNotificationManager: InAppNotificationManager
) {

private var autoSendPushToken = false
Expand Down Expand Up @@ -190,7 +190,7 @@ class RegisterManager @Inject constructor(
} else {
val popUpData = response?.mapToSdkInitResponse()?.popupDto
if (popUpData != null) {
inAppNotificationUseCase.execute(popupDto = popUpData)
inAppNotificationManager.shopPopUp(popupDto = popUpData)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.personalization.features.products.impl.ProductsManagerImpl
import com.personalization.features.recommendation.impl.RecommendationManagerImpl
import com.personalization.features.search.impl.SearchManagerImpl
import com.personalization.features.trackEvent.impl.TrackEventManagerImpl
import com.personalization.sdk.domain.usecases.inAppNotification.InAppNotificationUseCase
import com.personalization.sdk.domain.usecases.network.ExecuteQueueTasksUseCase
import com.personalization.sdk.domain.usecases.network.SendNetworkMethodUseCase
import com.personalization.sdk.domain.usecases.preferences.GetPreferencesValueUseCase
Expand All @@ -39,15 +38,15 @@ class SdkModule {
getUserSettingsValueUseCase: GetUserSettingsValueUseCase,
sendNetworkMethodUseCase: SendNetworkMethodUseCase,
executeQueueTasksUseCase: ExecuteQueueTasksUseCase,
inAppNotificationUseCase: InAppNotificationUseCase
inAppNotificationManager: InAppNotificationManager
): RegisterManager = RegisterManager(
getPreferencesValueUseCase = getPreferencesValueUseCase,
savePreferencesValueUseCase = savePreferencesValueUseCase,
updateUserSettingsValueUseCase = updateUserSettingsValueUseCase,
getUserSettingsValueUseCase = getUserSettingsValueUseCase,
sendNetworkMethodUseCase = sendNetworkMethodUseCase,
executeQueueTasksUseCase = executeQueueTasksUseCase,
inAppNotificationUseCase = inAppNotificationUseCase
inAppNotificationManager = inAppNotificationManager
)

@Singleton
Expand Down Expand Up @@ -96,14 +95,6 @@ class SdkModule {
sendNetworkMethodUseCase = sendNetworkMethodUseCase
)

@Singleton
@Provides
fun provideSendPopupToNotificationManagerUseCase(
inAppNotificationManager: InAppNotificationManager
): InAppNotificationUseCase = InAppNotificationUseCase(
inAppNotificationManager = inAppNotificationManager
)

@Singleton
@Provides
fun provideInAppNotificationManager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import android.util.Log
class EmptyFieldError(
private val tag: String,
private val functionName: String,
private val message: String,
private val message: String? = null,
) {

fun logError() {
Log.e(
/* tag = */ tag,
/* msg = */ "Error caught in $functionName : $message"
/* msg = */ "Field is empty or null in $functionName : $message"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,45 @@ import android.util.Log
import org.json.JSONObject

/**
* Class for handling JSON response errors and validation.
* Class for handling JSON response errors and validation
*/
class JsonResponseErrorHandler(
private val tag: String,
private val response: JSONObject?,
) {

/**
* Validates if the response is not null.
* @return true if the response is valid, false otherwise.
* Validates if the response is not null
* @return true if the response is valid, false otherwise
*/
fun validateResponse(): Boolean {
return if (response == null) {
logError("Response is null or incorrect.")
logError("Response is null or incorrect")
false
} else {
Log.i(tag, "Response is : $response")
Log.i(tag, "Response is: $response")
true
}
}

/**
* Extracts a required string field from the JSON response.
* Logs an error and returns null if the field is missing or empty.
* @param fieldName The name of the field to extract.
* @return The field value, or null if invalid.
* Extracts a required string field from the JSON response
* Logs an error and returns null if the field is missing or empty
* @param fieldName The name of the field to extract
* @return The field value, or null if invalid
*/
fun getRequiredField(fieldName: String): String? {
val value = response?.optString(fieldName)
if (value.isNullOrEmpty()) {
logError("Response does not contain the correct field: $fieldName.")
logError("Response does not contain the correct field: $fieldName")
return null
}
return value
}

/**
* Logs the error message with the associated tag.
* @param message The error message to log.
* Logs the error message with the associated tag
* @param message The error message to log
*/
fun logError(message: String, exception: Exception? = null) {
Log.e(tag, message, exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class InAppNotificationManagerImpl @Inject constructor(
EmptyFieldError(
tag = TAG,
functionName = FUNC_OPENING_BROWSER,
message = "Deep link is empty or null"
)
return
}
Expand Down

This file was deleted.

0 comments on commit c34b237

Please sign in to comment.