Skip to content

Commit

Permalink
chore: update screen view to sealed class
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 committed Dec 17, 2024
1 parent 6655fd4 commit 4baa79a
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 32 deletions.
16 changes: 11 additions & 5 deletions datapipelines/api/datapipelines.api
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ public final class io/customer/datapipelines/config/DataPipelinesModuleConfig :
public final fun getTrackApplicationLifecycleEvents ()Z
}

public final class io/customer/datapipelines/config/ScreenView : java/lang/Enum {
public static final field Analytics Lio/customer/datapipelines/config/ScreenView;
public abstract class io/customer/datapipelines/config/ScreenView {
public static final field Companion Lio/customer/datapipelines/config/ScreenView$Companion;
public static final field InApp Lio/customer/datapipelines/config/ScreenView;
public static fun valueOf (Ljava/lang/String;)Lio/customer/datapipelines/config/ScreenView;
public static fun values ()[Lio/customer/datapipelines/config/ScreenView;
public synthetic fun <init> (Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getName ()Ljava/lang/String;
}

public final class io/customer/datapipelines/config/ScreenView$All : io/customer/datapipelines/config/ScreenView {
public static final field INSTANCE Lio/customer/datapipelines/config/ScreenView$All;
}

public final class io/customer/datapipelines/config/ScreenView$Companion {
Expand All @@ -29,6 +31,10 @@ public final class io/customer/datapipelines/config/ScreenView$Companion {
public static synthetic fun getScreenView$default (Lio/customer/datapipelines/config/ScreenView$Companion;Ljava/lang/String;Lio/customer/datapipelines/config/ScreenView;ILjava/lang/Object;)Lio/customer/datapipelines/config/ScreenView;
}

public final class io/customer/datapipelines/config/ScreenView$InApp : io/customer/datapipelines/config/ScreenView {
public static final field INSTANCE Lio/customer/datapipelines/config/ScreenView$InApp;
}

public final class io/customer/datapipelines/extensions/JsonExtensionsKt {
public static final fun toJsonArray (Lorg/json/JSONArray;)Lkotlinx/serialization/json/JsonArray;
public static final fun toJsonObject (Lorg/json/JSONObject;)Lkotlinx/serialization/json/JsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,34 @@ package io.customer.datapipelines.config
/**
* Enum class to define how CustomerIO SDK should handle screen view events.
*/
enum class ScreenView {
sealed class ScreenView(val name: String) {
/**
* Screen view events are sent to destinations for analytics purposes.
*/
Analytics,
object All : ScreenView(name = "all")

/**
* Screen view events are kept on device only. They are used to display in-app messages based on page rules. Events are not sent to our back end servers.
*/
InApp;

object InApp : ScreenView(name = "inapp")

companion object {
/**
* Returns the [ScreenView] enum constant for the given name.
* Returns fallback if the specified enum type has no constant with the given name.
* Defaults to [Analytics].
* Defaults to [All].
*/
@JvmOverloads
fun getScreenView(screenView: String?, fallback: ScreenView = Analytics): ScreenView {
return values().firstOrNull { it.name.equals(screenView, ignoreCase = true) } ?: fallback
fun getScreenView(screenView: String?, fallback: ScreenView = All): ScreenView {
if (screenView.isNullOrBlank()) {
return fallback
}

return listOf(
All,
InApp
).find { value -> value.name.equals(screenView, ignoreCase = true) } ?: fallback
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class ScreenFilterPlugin(private val screenViewUse: ScreenView) : Event
// Filter out screen events based on the configuration provided by customer app
// Using when expression so it enforce right check for all possible values of ScreenView in future
return when (screenViewUse) {
ScreenView.Analytics -> payload
ScreenView.All -> payload
// Do not send screen events to server if ScreenView is not Analytics
ScreenView.InApp -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class CustomerIOBuilder(
private var migrationSiteId: String? = null

// Determines how SDK should handle screen view events
private var screenViewUse: ScreenView = ScreenView.Analytics
private var screenViewUse: ScreenView = ScreenView.All

/**
* Specifies the log level for the SDK.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.customer.datapipelines.config

import io.customer.commontest.core.JUnit5Test
import org.amshove.kluent.shouldBeEqualTo
import org.junit.jupiter.api.Test

class ScreenViewTest : JUnit5Test() {
@Test
fun getScreenView_givenNamesWithMatchingCase_expectCorrectScreenView() {
val screenViewAnalytics = ScreenView.getScreenView("All")
val screenViewInApp = ScreenView.getScreenView("InApp")

screenViewAnalytics shouldBeEqualTo ScreenView.All
screenViewInApp shouldBeEqualTo ScreenView.InApp
}

@Test
fun getScreenView_givenNamesWithDifferentCase_expectCorrectScreenView() {
val screenViewAnalytics = ScreenView.getScreenView("all")
val screenViewInApp = ScreenView.getScreenView("inapp")

screenViewAnalytics shouldBeEqualTo ScreenView.All
screenViewInApp shouldBeEqualTo ScreenView.InApp
}

@Test
fun getScreenView_givenInvalidValue_expectFallbackScreenView() {
val parsedValue = ScreenView.getScreenView("none")

parsedValue shouldBeEqualTo ScreenView.All
}

@Test
fun getScreenView_givenEmptyValue_expectFallbackScreenView() {
val parsedValue = ScreenView.getScreenView(screenView = "", fallback = ScreenView.InApp)

parsedValue shouldBeEqualTo ScreenView.InApp
}

@Test
fun getScreenView_givenNull_expectFallbackScreenView() {
val parsedValue = ScreenView.getScreenView(null)

parsedValue shouldBeEqualTo ScreenView.All
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ScreenFilterPluginTest : JUnitTest() {

@Test
fun process_givenScreenViewUseAnalytics_expectScreenEventWithoutPropertiesProcessed() {
setupWithConfig(screenViewUse = ScreenView.Analytics)
setupWithConfig(screenViewUse = ScreenView.All)

val givenScreenTitle = String.random
sdkInstance.screen(givenScreenTitle)
Expand All @@ -48,7 +48,7 @@ class ScreenFilterPluginTest : JUnitTest() {

@Test
fun process_givenScreenViewUseAnalytics_expectScreenEventWithPropertiesProcessed() {
setupWithConfig(screenViewUse = ScreenView.Analytics)
setupWithConfig(screenViewUse = ScreenView.All)

val givenScreenTitle = String.random
val givenProperties: CustomAttributes = mapOf("source" to "push", "discount" to 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class CustomerIOBuilderTest : RobolectricTest() {
dataPipelinesModuleConfig.apiHost shouldBe "cdp.customer.io/v1"
dataPipelinesModuleConfig.cdnHost shouldBe "cdp.customer.io/v1"
dataPipelinesModuleConfig.autoAddCustomerIODestination shouldBe true
dataPipelinesModuleConfig.screenViewUse shouldBe ScreenView.Analytics
dataPipelinesModuleConfig.screenViewUse shouldBe ScreenView.All
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static CustomerIOSDKConfig getDefaultConfigurations() {
true,
CioLogLevel.DEBUG,
Region.US.INSTANCE,
ScreenView.Analytics,
ScreenView.All.INSTANCE,
true,
false,
true);
Expand Down Expand Up @@ -96,7 +96,7 @@ public static Map<String, String> toMap(@NonNull CustomerIOSDKConfig config) {
bundle.put(Keys.TRACK_DEVICE_ATTRIBUTES, StringUtils.fromBoolean(config.deviceAttributesTrackingEnabled));
bundle.put(Keys.LOG_LEVEL, config.logLevel.name());
bundle.put(Keys.REGION, config.getRegion().getCode());
bundle.put(Keys.SCREEN_VIEW_USE, config.getScreenViewUse().name());
bundle.put(Keys.SCREEN_VIEW_USE, config.getScreenViewUse().getName());
bundle.put(Keys.TRACK_APPLICATION_LIFECYCLE, StringUtils.fromBoolean(config.applicationLifecycleTrackingEnabled));
bundle.put(Keys.TEST_MODE_ENABLED, StringUtils.fromBoolean(config.testModeEnabled));
bundle.put(Keys.IN_APP_MESSAGING_ENABLED, StringUtils.fromBoolean(config.inAppMessagingEnabled));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected void onDestroy() {

@Override
protected void setupContent() {
prepareViews();
prepareViewsForAutomatedTests();
setupViews();
setupObservers();
Expand Down Expand Up @@ -85,6 +86,11 @@ private void parseLinkParams() {
isLinkParamsPopulated = true;
}

private void prepareViews() {
binding.settingsScreenViewUseAllButton.setText(ScreenView.All.INSTANCE.getName());
binding.settingsScreenViewUseInAppButton.setText(ScreenView.InApp.INSTANCE.getName());
}

private void prepareViewsForAutomatedTests() {
ViewUtils.prepareForAutomatedTests(binding.topAppBar);
ViewUtils.prepareForAutomatedTests(binding.settingsCdpApiKeyLabel, R.string.acd_cdp_api_key_input);
Expand Down Expand Up @@ -221,10 +227,10 @@ private Region getSelectedRegion() {
@NonNull
private ScreenView getSelectedScreenViewUse() {
int checkedButton = binding.screenViewUseSettingsValuesGroup.getCheckedButtonId();
if (checkedButton == R.id.settings_screen_view_use_analytics_button) {
return ScreenView.Analytics;
if (checkedButton == R.id.settings_screen_view_use_all_button) {
return ScreenView.All.INSTANCE;
} else if (checkedButton == R.id.settings_screen_view_use_in_app_button) {
return ScreenView.InApp;
return ScreenView.InApp.INSTANCE;
}
throw new IllegalStateException();
}
Expand Down Expand Up @@ -273,13 +279,10 @@ private int getCheckedRegionButtonId(@NonNull Region region) {
}

private int getCheckedScreenViewUseButtonId(@NonNull ScreenView screenViewUse) {
switch (screenViewUse) {
case InApp:
return R.id.settings_screen_view_use_in_app_button;
case Analytics:
default:
return R.id.settings_screen_view_use_analytics_button;
if (screenViewUse instanceof ScreenView.InApp) {
return R.id.settings_screen_view_use_in_app_button;
}
return R.id.settings_screen_view_use_all_button;
}

private boolean updateErrorState(TextInputLayout textInputLayout,
Expand Down
6 changes: 3 additions & 3 deletions samples/java_layout/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -259,20 +259,20 @@
app:singleSelection="true">

<Button
android:id="@+id/settings_screen_view_use_analytics_button"
android:id="@+id/settings_screen_view_use_all_button"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/screen_view_use_settings_analytics" />
tools:text="All" />

<Button
android:id="@+id/settings_screen_view_use_in_app_button"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/screen_view_use_settings_in_app" />
tools:text="InApp" />
</com.google.android.material.button.MaterialButtonToggleGroup>

<TextView
Expand Down
2 changes: 0 additions & 2 deletions samples/java_layout/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,5 @@
<string name="label_internal_settings_activity">InternalSettingsActivity</string>
<string name="error_url_input_field">This field must be a valid URL</string>
<string name="screen_view_use_settings_label">ScreenView use</string>
<string name="screen_view_use_settings_analytics">Analytics</string>
<string name="screen_view_use_settings_in_app">InApp</string>

</resources>

0 comments on commit 4baa79a

Please sign in to comment.