Skip to content

Commit

Permalink
Merge pull request #78 from rees46/feat/connect-in-app-notification
Browse files Browse the repository at this point in the history
Feat/connect in app notification
  • Loading branch information
TorinAsakura authored Dec 2, 2024
2 parents 26d0b33 + c34b237 commit f77bb6b
Show file tree
Hide file tree
Showing 37 changed files with 804 additions and 341 deletions.

Large diffs are not rendered by default.

99 changes: 19 additions & 80 deletions personalization-sdk/src/main/kotlin/com/personalization/SDK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ open class SDK {

internal lateinit var context: Context
private lateinit var segment: String
lateinit var fragmentManager: FragmentManager

private var onMessageListener: OnMessageListener? = null
private var search: Search = Search(JSONObject())
Expand Down Expand Up @@ -114,28 +113,40 @@ open class SDK {
notificationId: String,
autoSendPushToken: Boolean = true
) {
val sdkComponent =
DaggerSdkComponent.factory().create(AppModule(applicationContext = context))
sdkComponent.inject(this)
val sdkComponent = DaggerSdkComponent.factory().create(
appModule = AppModule(applicationContext = context)
)
sdkComponent.inject(sdk = this)

initPreferencesUseCase.invoke(
context = context, preferencesKey = preferencesKey
context = context,
preferencesKey = preferencesKey
)

this.context = context
TAG = tag

segment = getPreferencesValueUseCase.getSegment()

notificationHandler.initialize(context)
notificationHandler.initialize(context = context)

initUserSettingsUseCase.invoke(
shopId = shopId, shopSecretKey = shopSecretKey, segment = segment, stream = stream
shopId = shopId,
shopSecretKey = shopSecretKey,
segment = segment,
stream = stream
)
initNetworkUseCase.invoke(
baseUrl = apiUrl
)
registerManager.initialize(context.contentResolver, autoSendPushToken)
registerManager.initialize(
contentResolver = context.contentResolver,
autoSendPushToken = autoSendPushToken
)
}

fun initializeFragmentManager(fragmentManager: FragmentManager) {
inAppNotificationManager.initFragmentManager(fragmentManager = fragmentManager)
}

fun initializeStoriesView(storiesView: StoriesView) {
Expand All @@ -158,78 +169,6 @@ open class SDK {
storiesManager.showStories(context.mainLooper, code)
}

fun initializeFragmentManager(fragmentManager: FragmentManager) {
this.fragmentManager = fragmentManager
}

fun showAlertDialog(
title: String,
message: String,
imageUrl: String,
buttonNegativeText: String,
buttonPositiveText: String,
buttonPositiveColor: Int,
buttonNegativeColor: Int,
onPositiveClick: () -> Unit,
onNegativeClick: () -> Unit
) = inAppNotificationManager.showAlertDialog(
fragmentManager = fragmentManager,
title = title,
message = message,
imageUrl = imageUrl,
buttonPositiveColor = buttonPositiveColor,
buttonNegativeColor = buttonNegativeColor,
buttonNegativeText = buttonNegativeText,
buttonPositiveText = buttonPositiveText,
onNegativeClick = onNegativeClick,
onPositiveClick = onPositiveClick,
)

fun showFullScreenDialog(
title: String,
message: String,
imageUrl: String,
buttonPositiveColor: Int,
buttonNegativeColor: Int,
buttonNegativeText: String,
buttonPositiveText: String,
onPositiveClick: () -> Unit,
onNegativeClick: () -> Unit
) = inAppNotificationManager.showFullScreenDialog(
fragmentManager = fragmentManager,
title = title,
message = message,
imageUrl = imageUrl,
buttonPositiveColor = buttonPositiveColor,
buttonNegativeColor = buttonNegativeColor,
buttonNegativeText = buttonNegativeText,
buttonPositiveText = buttonPositiveText,
onNegativeClick = onNegativeClick,
onPositiveClick = onPositiveClick,
)

fun showBottomSheetDialog(
title: String,
message: String,
imageUrl: String?,
buttonPositiveText: String,
buttonNegativeText: String?,
buttonPositiveColor: Int,
buttonNegativeColor: Int,
onPositiveClick: () -> Unit,
onNegativeClick: () -> Unit
) = inAppNotificationManager.showBottomSheetDialog(
fragmentManager = fragmentManager,
title = title,
message = message,
imageUrl = imageUrl,
buttonNegativeText = buttonNegativeText,
buttonPositiveText = buttonPositiveText,
buttonPositiveColor = buttonPositiveColor,
buttonNegativeColor = buttonNegativeColor,
onNegativeClick = onNegativeClick,
onPositiveClick = onPositiveClick,
)

/**
* Triggers a story event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,45 @@ package com.personalization.api.managers

import android.view.View
import androidx.fragment.app.FragmentManager
import com.personalization.sdk.data.models.dto.popUp.PopupDto

interface InAppNotificationManager {

fun initFragmentManager(fragmentManager: FragmentManager)

fun shopPopUp(popupDto: PopupDto)

fun showAlertDialog(
fragmentManager: FragmentManager,
title: String,
message: String,
imageUrl: String,
buttonPositiveText: String,
buttonNegativeText: String,
buttonPositiveColor: Int,
buttonNegativeColor: Int,
onPositiveClick: () -> Unit,
onNegativeClick: () -> Unit
onPositiveClick: () -> Unit
)

fun showFullScreenDialog(
fragmentManager: FragmentManager,
title: String,
message: String,
imageUrl: String?,
buttonPositiveColor: Int,
buttonNegativeColor: Int,
buttonPositiveText: String,
buttonNegativeText: String,
onPositiveClick: () -> Unit,
onNegativeClick: () -> Unit
onPositiveClick: () -> Unit
)

fun showBottomSheetDialog(
fragmentManager: FragmentManager,
title: String,
message: String,
imageUrl: String?,
buttonPositiveText: String,
buttonNegativeText: String?,
buttonPositiveColor: Int,
buttonNegativeColor: Int,
onPositiveClick: () -> Unit,
onNegativeClick: () -> Unit
onPositiveClick: () -> Unit
)

fun showSnackBar(
Expand All @@ -52,4 +51,5 @@ interface InAppNotificationManager {
onPositiveClick: () -> Unit,
onNegativeClick: () -> Unit
)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.personalization.api.responses.initialization

import com.personalization.sdk.data.models.dto.popUp.PopupDto
import com.personalization.sdk.data.models.dto.popUp.WebPushSettings
import com.personalization.sdk.data.models.dto.search.Search

data class SdkInitializationResponse(
val did: String,
val seance: String,
val currency: String,
val emailCollector: Boolean,
val hasEmail: Boolean,
val recommendations: Boolean,
val lazyLoad: Boolean,
val autoCssRecommender: Boolean,
val cms: String,
val snippets: List<String>,
val popupDto: PopupDto?,
val search: Search?,
val webPushSettings: WebPushSettings?,
val recone: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ interface SdkComponent {

@Component.Factory
interface Factory {
fun create(appModule: AppModule): SdkComponent
fun create(
appModule: AppModule
): SdkComponent
}

fun inject(sdk: SDK)

fun inject(service: NotificationService)

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.personalization.di

import android.content.Context
import com.personalization.RegisterManager
import com.personalization.api.managers.InAppNotificationManager
import com.personalization.api.managers.ProductsManager
Expand Down Expand Up @@ -36,14 +37,16 @@ class SdkModule {
updateUserSettingsValueUseCase: UpdateUserSettingsValueUseCase,
getUserSettingsValueUseCase: GetUserSettingsValueUseCase,
sendNetworkMethodUseCase: SendNetworkMethodUseCase,
executeQueueTasksUseCase: ExecuteQueueTasksUseCase
executeQueueTasksUseCase: ExecuteQueueTasksUseCase,
inAppNotificationManager: InAppNotificationManager
): RegisterManager = RegisterManager(
getPreferencesValueUseCase = getPreferencesValueUseCase,
savePreferencesValueUseCase = savePreferencesValueUseCase,
updateUserSettingsValueUseCase = updateUserSettingsValueUseCase,
getUserSettingsValueUseCase = getUserSettingsValueUseCase,
sendNetworkMethodUseCase = sendNetworkMethodUseCase,
executeQueueTasksUseCase = executeQueueTasksUseCase
executeQueueTasksUseCase = executeQueueTasksUseCase,
inAppNotificationManager = inAppNotificationManager
)

@Singleton
Expand Down Expand Up @@ -94,7 +97,11 @@ class SdkModule {

@Singleton
@Provides
fun provideInAppNotificationManager(): InAppNotificationManager = InAppNotificationManagerImpl()
fun provideInAppNotificationManager(
context: Context
): InAppNotificationManager {
return InAppNotificationManagerImpl(context)
}

@Singleton
@Provides
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.personalization.errors

import android.util.Log

class BaseInfoError(
private val tag: String,
private val exception: Exception? = null,
private val message: String
) {

fun logError() {
Log.e(
/* tag = */ tag,
/* msg = */"$message: ${exception?.message ?: "Unknown error"}"
)
}
}
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
@@ -0,0 +1,17 @@
package com.personalization.errors

import android.util.Log

class FirebaseError(
private val tag: String,
private val exception: Exception?
) {

fun logError() {
Log.e(
/* tag = */ tag,
/* msg = */
"Failed to retrieve Firebase token: ${exception?.message ?: "Unknown error"}"
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.personalization.errors

import android.util.Log
import org.json.JSONObject

/**
* 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
*/
fun validateResponse(): Boolean {
return if (response == null) {
logError("Response is null or incorrect")
false
} else {
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
*/
fun getRequiredField(fieldName: String): String? {
val value = response?.optString(fieldName)
if (value.isNullOrEmpty()) {
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
*/
fun logError(message: String, exception: Exception? = null) {
Log.e(tag, message, exception)
}
}
Loading

0 comments on commit f77bb6b

Please sign in to comment.