Skip to content

Commit

Permalink
feat: dismiss in-app message (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahroz16 authored May 26, 2023
1 parent 267d803 commit 89794f3
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 24 deletions.
6 changes: 3 additions & 3 deletions common-test/src/main/java/io/customer/commontest/BaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import io.customer.sdk.data.store.Client
import io.customer.sdk.data.store.DeviceStore
import io.customer.sdk.di.CustomerIOComponent
import io.customer.sdk.di.CustomerIOStaticComponent
import io.customer.sdk.module.CustomerIOModuleConfig
import io.customer.sdk.module.CustomerIOModule
import io.customer.sdk.util.*
import okhttp3.ResponseBody.Companion.toResponseBody
import okhttp3.mockwebserver.MockWebServer
Expand Down Expand Up @@ -67,7 +67,7 @@ abstract class BaseTest {
backgroundQueueTaskExpiredSeconds: Double = Seconds.fromDays(3).value,
logLevel: CioLogLevel = CioLogLevel.DEBUG,
trackingApiUrl: String? = null,
configurations: Map<String, CustomerIOModuleConfig> = emptyMap()
modules: Map<String, CustomerIOModule<*>> = emptyMap()
) = CustomerIOConfig(
client = client,
siteId = siteId,
Expand All @@ -81,7 +81,7 @@ abstract class BaseTest {
backgroundQueueTaskExpiredSeconds = backgroundQueueTaskExpiredSeconds,
logLevel = logLevel,
trackingApiUrl = trackingApiUrl,
configurations = configurations
modules = modules
)

// override in test class to override SDK config for all test functions in the class.
Expand Down
10 changes: 10 additions & 0 deletions messaginginapp/api/messaginginapp.api
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,28 @@ public final class io/customer/messaginginapp/MessagingInAppModuleConfig$Compani
}

public final class io/customer/messaginginapp/ModuleMessagingInApp : io/customer/sdk/module/CustomerIOModule {
public static final field Companion Lio/customer/messaginginapp/ModuleMessagingInApp$Companion;
public static final field moduleName Ljava/lang/String;
public fun <init> ()V
public fun <init> (Lio/customer/messaginginapp/MessagingInAppModuleConfig;)V
public synthetic fun <init> (Lio/customer/messaginginapp/MessagingInAppModuleConfig;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ljava/lang/String;)V
public fun <init> (Ljava/lang/String;Lio/customer/messaginginapp/MessagingInAppModuleConfig;)V
public synthetic fun <init> (Ljava/lang/String;Lio/customer/messaginginapp/MessagingInAppModuleConfig;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun dismissMessage ()V
public fun getModuleConfig ()Lio/customer/messaginginapp/MessagingInAppModuleConfig;
public synthetic fun getModuleConfig ()Lio/customer/sdk/module/CustomerIOModuleConfig;
public fun getModuleName ()Ljava/lang/String;
public fun initialize ()V
}

public final class io/customer/messaginginapp/ModuleMessagingInApp$Companion {
}

public final class io/customer/messaginginapp/di/DIGraphMessaginIAppKt {
public static final fun inAppMessaging (Lio/customer/sdk/CustomerIO;)Lio/customer/messaginginapp/ModuleMessagingInApp;
}

public abstract interface class io/customer/messaginginapp/type/InAppEventListener {
public abstract fun errorWithMessage (Lio/customer/messaginginapp/type/InAppMessage;)V
public abstract fun messageActionTaken (Lio/customer/messaginginapp/type/InAppMessage;Ljava/lang/String;Ljava/lang/String;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import io.customer.sdk.module.CustomerIOModule
import io.customer.sdk.repository.TrackRepository

class ModuleMessagingInApp
@VisibleForTesting
@InternalCustomerIOApi
@VisibleForTesting @InternalCustomerIOApi
internal constructor(
override val moduleConfig: MessagingInAppModuleConfig = MessagingInAppModuleConfig.default(),
private val overrideDiGraph: CustomerIOComponent?
Expand All @@ -43,8 +42,7 @@ internal constructor(
overrideDiGraph = null
)

override val moduleName: String
get() = "MessagingInApp"
override val moduleName: String = ModuleMessagingInApp.moduleName

private val diGraph: CustomerIOComponent
get() = overrideDiGraph ?: CustomerIO.instance().diGraph
Expand All @@ -64,6 +62,14 @@ internal constructor(
private val config: CustomerIOConfig
get() = diGraph.sdkConfig

companion object {
const val moduleName: String = "MessagingInApp"
}

fun dismissMessage() {
gistProvider.dismissMessage()
}

override fun initialize() {
initializeGist(config)
setupHooks()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package io.customer.messaginginapp.di

import io.customer.messaginginapp.ModuleMessagingInApp
import io.customer.messaginginapp.provider.GistApi
import io.customer.messaginginapp.provider.GistApiProvider
import io.customer.messaginginapp.provider.GistInAppMessagesProvider
import io.customer.messaginginapp.provider.InAppMessagesProvider
import io.customer.sdk.CustomerIO
import io.customer.sdk.di.CustomerIOComponent

internal val CustomerIOComponent.gistApiProvider: GistApi
get() = override() ?: GistApiProvider()

internal val CustomerIOComponent.gistProvider: InAppMessagesProvider
get() = override() ?: GistInAppMessagesProvider(gistApiProvider)

fun CustomerIO.inAppMessaging(): ModuleMessagingInApp {
return diGraph.sdkConfig.modules[ModuleMessagingInApp.moduleName] as? ModuleMessagingInApp
?: throw IllegalStateException("ModuleMessagingInApp not initialized")
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal interface GistApi {
fun setCurrentRoute(route: String)
fun clearUserToken()
fun addListener(listener: GistListener)
fun dismissMessage()
fun subscribeToEvents(
onMessageShown: (deliveryId: String) -> Unit,
onAction: (deliveryId: String?, currentRoute: String, action: String, name: String) -> Unit,
Expand Down Expand Up @@ -47,6 +48,10 @@ internal class GistApiProvider : GistApi {
GistSdk.addListener(listener)
}

override fun dismissMessage() {
GistSdk.dismissMessage()
}

override fun subscribeToEvents(
onMessageShown: (String) -> Unit,
onAction: (deliveryId: String?, currentRoute: String, action: String, name: String) -> Unit,
Expand All @@ -56,7 +61,12 @@ internal class GistApiProvider : GistApi {
override fun embedMessage(message: Message, elementId: String) {
}

override fun onAction(message: Message, currentRoute: String, action: String, name: String) {
override fun onAction(
message: Message,
currentRoute: String,
action: String,
name: String
) {
val deliveryID = GistMessageProperties.getGistProperties(message).campaignId
onAction(deliveryID, currentRoute, action, name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal interface InAppMessagesProvider {
fun setCurrentRoute(route: String)
fun clearUserToken()
fun setListener(listener: InAppEventListener)
fun dismissMessage()
fun subscribeToEvents(
onMessageShown: (deliveryId: String) -> Unit,
onAction: (deliveryId: String, currentRoute: String, action: String, name: String) -> Unit,
Expand All @@ -23,7 +24,8 @@ internal interface InAppMessagesProvider {
* Wrapper around Gist SDK
*/
internal class GistInAppMessagesProvider(private val provider: GistApi) :
InAppMessagesProvider, GistListener {
InAppMessagesProvider,
GistListener {

private var listener: InAppEventListener? = null

Expand Down Expand Up @@ -51,6 +53,10 @@ internal class GistInAppMessagesProvider(private val provider: GistApi) :
this.listener = listener
}

override fun dismissMessage() {
provider.dismissMessage()
}

override fun subscribeToEvents(
onMessageShown: (String) -> Unit,
onAction: (deliveryId: String, currentRoute: String, action: String, name: String) -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package io.customer.messaginginapp

import androidx.test.ext.junit.runners.AndroidJUnit4
import io.customer.commontest.BaseTest
import io.customer.messaginginapp.di.inAppMessaging
import io.customer.messaginginapp.provider.InAppMessagesProvider
import io.customer.messaginginapp.type.InAppEventListener
import io.customer.sdk.CustomerIO
import io.customer.sdk.CustomerIOConfig
import io.customer.sdk.data.model.Region
import io.customer.sdk.extensions.random
import io.customer.sdk.hooks.HookModule
import io.customer.sdk.hooks.HooksManager
import io.customer.sdk.module.CustomerIOModule
import io.customer.sdk.repository.preference.SitePreferenceRepository
import java.lang.reflect.Field
import org.amshove.kluent.shouldBe
Expand All @@ -25,6 +30,12 @@ internal class ModuleMessagingInAppTest : BaseTest() {
private val prefRepository: SitePreferenceRepository
get() = di.sitePreferenceRepository

private val modules = hashMapOf<String, CustomerIOModule<*>>()

override fun setupConfig(): CustomerIOConfig = createConfig(
modules = modules
)

@Before
override fun setup() {
super.setup()
Expand All @@ -37,6 +48,7 @@ internal class ModuleMessagingInAppTest : BaseTest() {
.build(),
overrideDiGraph = di
)
modules[ModuleMessagingInApp.moduleName] = module
}

@Test
Expand Down Expand Up @@ -112,4 +124,23 @@ internal class ModuleMessagingInAppTest : BaseTest() {
organizationId?.isAccessible = true
(organizationId?.get(module)) shouldBe null
}

@Test
fun whenDismissMessageCalledOnCustomerIO_thenDismissMessageIsCalledOnGist() {
// initialize the SDK
val customerIO = CustomerIO.Builder(
siteId = siteId,
apiKey = String.random,
region = Region.US,
appContext = application
).apply {
overrideDiGraph = di
}.build()

// call dismissMessage on the CustomerIO instance
customerIO.inAppMessaging().dismissMessage()

// verify that the module's dismissMessage method was called
verify(gistInAppMessagesProvider).dismissMessage()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ internal val CustomerIOComponent.fcmTokenProvider: DeviceTokenProvider
get() = override() ?: FCMTokenProviderImpl(logger = logger, context = context)

internal val CustomerIOComponent.moduleConfig: MessagingPushModuleConfig
get() = override() ?: sdkConfig.configurations.getOrElse(
ModuleMessagingPushFCM.MODULE_NAME
) {
MessagingPushModuleConfig.default()
} as MessagingPushModuleConfig
get() = override()
?: sdkConfig.modules[ModuleMessagingPushFCM.MODULE_NAME]?.moduleConfig as? MessagingPushModuleConfig
?: MessagingPushModuleConfig.default()

internal val CustomerIOComponent.deepLinkUtil: DeepLinkUtil
get() = override() ?: DeepLinkUtilImpl(logger, moduleConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.customer.messagingpush.di.moduleConfig
import io.customer.sdk.CustomerIOConfig
import io.customer.sdk.CustomerIOInstance
import io.customer.sdk.device.DeviceTokenProvider
import io.customer.sdk.module.CustomerIOModuleConfig
import io.customer.sdk.module.CustomerIOModule
import org.amshove.kluent.shouldBeFalse
import org.amshove.kluent.shouldBeNull
import org.amshove.kluent.shouldBeTrue
Expand All @@ -24,10 +24,10 @@ internal class ModuleMessagingConfigTest : BaseTest() {

private val customerIOMock: CustomerIOInstance = mock()
private val fcmTokenProviderMock: DeviceTokenProvider = mock()
private val configurations = hashMapOf<String, CustomerIOModuleConfig>()
private val modules = hashMapOf<String, CustomerIOModule<*>>()

override fun setupConfig(): CustomerIOConfig = createConfig(
configurations = configurations
modules = modules
)

@Before
Expand All @@ -48,7 +48,7 @@ internal class ModuleMessagingConfigTest : BaseTest() {
overrideDiGraph = di
)

configurations[ModuleMessagingPushFCM.MODULE_NAME] = module.moduleConfig
modules[ModuleMessagingPushFCM.MODULE_NAME] = module
val moduleConfig = di.moduleConfig

moduleConfig.notificationCallback.shouldBeNull()
Expand All @@ -63,7 +63,7 @@ internal class ModuleMessagingConfigTest : BaseTest() {
overrideDiGraph = di
)

configurations[ModuleMessagingPushFCM.MODULE_NAME] = module.moduleConfig
modules[ModuleMessagingPushFCM.MODULE_NAME] = module
val moduleConfig = di.moduleConfig

moduleConfig.autoTrackPushEvents.shouldBeTrue()
Expand All @@ -83,7 +83,7 @@ internal class ModuleMessagingConfigTest : BaseTest() {
overrideDiGraph = di
)

configurations[ModuleMessagingPushFCM.MODULE_NAME] = module.moduleConfig
modules[ModuleMessagingPushFCM.MODULE_NAME] = module
val moduleConfig = di.moduleConfig

moduleConfig.autoTrackPushEvents.shouldBeFalse()
Expand Down
2 changes: 1 addition & 1 deletion sdk/api/sdk.api
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public final class io/customer/sdk/CustomerIOConfig {
public final fun getBackgroundQueueSecondsDelay ()D
public final fun getBackgroundQueueTaskExpiredSeconds ()D
public final fun getClient ()Lio/customer/sdk/data/store/Client;
public final fun getConfigurations ()Ljava/util/Map;
public final fun getLogLevel ()Lio/customer/sdk/util/CioLogLevel;
public final fun getModules ()Ljava/util/Map;
public final fun getRegion ()Lio/customer/sdk/data/model/Region;
public final fun getSiteId ()Ljava/lang/String;
public final fun getTimeout ()J
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/main/java/io/customer/sdk/CustomerIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class CustomerIO internal constructor(
backgroundQueueTaskExpiredSeconds = Seconds.fromDays(3).value,
logLevel = logLevel,
trackingApiUrl = trackingApiUrl,
configurations = modules.entries.associate { entry -> entry.key to entry.value.moduleConfig }
modules = modules.entries.associate { entry -> entry.key to entry.value }
)

sharedInstance.attachSDKConfig(sdkConfig = config, context = appContext)
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/main/java/io/customer/sdk/CustomerIOConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.customer.sdk

import io.customer.sdk.data.model.Region
import io.customer.sdk.data.store.Client
import io.customer.sdk.module.CustomerIOModuleConfig
import io.customer.sdk.module.CustomerIOModule
import io.customer.sdk.util.CioLogLevel

data class CustomerIOConfig(
Expand Down Expand Up @@ -32,7 +32,7 @@ data class CustomerIOConfig(
val backgroundQueueTaskExpiredSeconds: Double,
val logLevel: CioLogLevel,
var trackingApiUrl: String?,
val configurations: Map<String, CustomerIOModuleConfig>
val modules: Map<String, CustomerIOModule<*>>
) {
internal val trackingApiHostname: String
get() {
Expand Down

0 comments on commit 89794f3

Please sign in to comment.