Skip to content

Commit

Permalink
added push option to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 committed Sep 28, 2023
1 parent 06012ca commit d60b333
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,20 @@ public static Boolean parseBoolean(@Nullable String value, Boolean defaultValue)
public static String fromBoolean(@Nullable Boolean value) {
return value == null ? null : Boolean.toString(value);
}

public static <T extends Enum<T>> T parseEnum(@Nullable String value, T defaultValue, Class<T> enumClass) {
if (TextUtils.isEmpty(value)) {
return defaultValue;
}

try {
return Enum.valueOf(enumClass, value);
} catch (IllegalArgumentException e) {
return defaultValue;
}
}

public static <T extends Enum<T>> String fromEnum(@Nullable T value) {
return value == null ? null : value.name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.customer.android.sample.kotlin_compose.data.repositories.PreferenceRep
import io.customer.android.sample.kotlin_compose.data.sdk.InAppMessageEventListener
import io.customer.messaginginapp.MessagingInAppModuleConfig
import io.customer.messaginginapp.ModuleMessagingInApp
import io.customer.messagingpush.MessagingPushModuleConfig
import io.customer.messagingpush.ModuleMessagingPushFCM
import io.customer.sdk.CustomerIO
import javax.inject.Inject
Expand Down Expand Up @@ -38,7 +39,13 @@ class MainApplication : Application() {
.setEventListener(InAppMessageEventListener()).build()
)
)
addCustomerIOModule(ModuleMessagingPushFCM())
addCustomerIOModule(
ModuleMessagingPushFCM(
config = with(MessagingPushModuleConfig.Builder()) {
setNotificationClickBehavior(configuration.notificationClickBehavior)
}.build()
)
)

build()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.customer.android.sample.kotlin_compose.data.models

import io.customer.messagingpush.config.NotificationClickBehavior
import io.customer.sdk.CustomerIO
import io.customer.sdk.CustomerIOConfig.Companion.AnalyticsConstants.AUTO_TRACK_DEVICE_ATTRIBUTES
import io.customer.sdk.CustomerIOConfig.Companion.AnalyticsConstants.BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS
Expand All @@ -13,6 +14,7 @@ data class Configuration(
var trackUrl: String? = null,
var backgroundQueueSecondsDelay: Double = BACKGROUND_QUEUE_SECONDS_DELAY,
var backgroundQueueMinNumTasks: Int = BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS,
var notificationClickBehavior: NotificationClickBehavior = NotificationClickBehavior.ACTIVITY_PREVENT_RESTART,
var trackScreen: Boolean = SHOULD_AUTO_RECORD_SCREEN_VIEWS,
var trackDeviceAttributes: Boolean = AUTO_TRACK_DEVICE_ATTRIBUTES,
var debugMode: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import io.customer.android.sample.kotlin_compose.util.PreferencesKeys.API_KEY
import io.customer.android.sample.kotlin_compose.util.PreferencesKeys.BACKGROUND_QUEUE_MIN_NUM_TASKS
import io.customer.android.sample.kotlin_compose.util.PreferencesKeys.BACKGROUND_QUEUE_SECONDS_DELAY
import io.customer.android.sample.kotlin_compose.util.PreferencesKeys.DEBUG_MODE
import io.customer.android.sample.kotlin_compose.util.PreferencesKeys.NOTIFICATION_CLICK_BEHAVIOR_KEY
import io.customer.android.sample.kotlin_compose.util.PreferencesKeys.SITE_ID
import io.customer.android.sample.kotlin_compose.util.PreferencesKeys.TRACK_API_URL_KEY
import io.customer.android.sample.kotlin_compose.util.PreferencesKeys.TRACK_DEVICE_ATTRIBUTES
import io.customer.android.sample.kotlin_compose.util.PreferencesKeys.TRACK_SCREEN
import io.customer.messagingpush.config.NotificationClickBehavior
import io.customer.sdk.CustomerIOConfig
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
Expand All @@ -33,6 +35,8 @@ class PreferenceRepositoryImp(private val dataStore: DataStore<Preferences>) :
configuration.trackUrl?.let { preferences[TRACK_API_URL_KEY] = it }
preferences[BACKGROUND_QUEUE_SECONDS_DELAY] = configuration.backgroundQueueSecondsDelay
preferences[BACKGROUND_QUEUE_MIN_NUM_TASKS] = configuration.backgroundQueueMinNumTasks
preferences[NOTIFICATION_CLICK_BEHAVIOR_KEY] =
configuration.notificationClickBehavior.name
preferences[TRACK_SCREEN] = configuration.trackScreen
preferences[TRACK_DEVICE_ATTRIBUTES] = configuration.trackDeviceAttributes
preferences[DEBUG_MODE] = configuration.debugMode
Expand All @@ -46,14 +50,24 @@ class PreferenceRepositoryImp(private val dataStore: DataStore<Preferences>) :
apiKey = preferences[API_KEY] ?: BuildConfig.API_KEY
).apply {
trackUrl =
preferences[TRACK_API_URL_KEY]?.takeIf { it.isNotBlank() } ?: "https://track-sdk.customer.io/"
preferences[TRACK_API_URL_KEY]?.takeIf { it.isNotBlank() }
?: "https://track-sdk.customer.io/"

backgroundQueueSecondsDelay = preferences[BACKGROUND_QUEUE_SECONDS_DELAY]
?: CustomerIOConfig.Companion.AnalyticsConstants.BACKGROUND_QUEUE_SECONDS_DELAY

backgroundQueueMinNumTasks = preferences[BACKGROUND_QUEUE_MIN_NUM_TASKS]
?: CustomerIOConfig.Companion.AnalyticsConstants.BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS

notificationClickBehavior =
preferences[NOTIFICATION_CLICK_BEHAVIOR_KEY]?.takeIf { name ->
name.isNotBlank()
}?.let { name ->
kotlin.runCatching {
enumValueOf<NotificationClickBehavior>(name)
}.getOrNull()
} ?: NotificationClickBehavior.ACTIVITY_PREVENT_RESTART

trackScreen = preferences[TRACK_SCREEN] ?: true

trackDeviceAttributes = preferences[TRACK_DEVICE_ATTRIBUTES] ?: true
Expand All @@ -73,6 +87,7 @@ class PreferenceRepositoryImp(private val dataStore: DataStore<Preferences>) :
CustomerIOConfig.Companion.AnalyticsConstants.BACKGROUND_QUEUE_SECONDS_DELAY
backgroundQueueMinNumTasks =
CustomerIOConfig.Companion.AnalyticsConstants.BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS
notificationClickBehavior = NotificationClickBehavior.ACTIVITY_PREVENT_RESTART
trackScreen = true
trackDeviceAttributes = true
debugMode = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.customer.android.sample.kotlin_compose.ui.settings

import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
Expand All @@ -14,7 +17,10 @@ import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material3.Button
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
Expand All @@ -33,12 +39,14 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
Expand All @@ -49,6 +57,7 @@ import androidx.hilt.navigation.compose.hiltViewModel
import io.customer.android.sample.kotlin_compose.R
import io.customer.android.sample.kotlin_compose.data.models.Configuration
import io.customer.android.sample.kotlin_compose.ui.components.TrackScreenLifecycle
import io.customer.messagingpush.config.NotificationClickBehavior
import io.customer.sdk.CustomerIO

@Composable
Expand Down Expand Up @@ -337,6 +346,55 @@ fun SDKSettingsList(
},
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number)
)

var pushBehaviorDropdownExpanded by remember { mutableStateOf(false) }
Box(modifier = Modifier.fillMaxWidth()) {
Column {
OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
readOnly = true,
value = configuration.notificationClickBehavior.name,
onValueChange = {},
label = { Text(text = "Notification Click Behavior") },
trailingIcon = {
Icon(
rememberVectorPainter(Icons.Default.ArrowDropDown),
contentDescription = "Dropdown Arrow"
)
}
)
DropdownMenu(
expanded = pushBehaviorDropdownExpanded,
modifier = Modifier.fillMaxWidth(),
onDismissRequest = { pushBehaviorDropdownExpanded = true }
) {
NotificationClickBehavior.values().forEach { value ->
DropdownMenuItem(
modifier = Modifier.padding(8.dp),
onClick = {
pushBehaviorDropdownExpanded = !pushBehaviorDropdownExpanded
onConfigurationChange(
configuration.copy(
notificationClickBehavior = value
)
)
},
text = { Text(value.name) }
)
}
}
}
Spacer(
modifier = Modifier
.matchParentSize()
.background(Color.Transparent)
.padding(10.dp)
.clickable(
role = Role.DropdownList,
onClick = { pushBehaviorDropdownExpanded = !pushBehaviorDropdownExpanded }
)
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ object PreferencesKeys {
val BACKGROUND_QUEUE_SECONDS_DELAY = doublePreferencesKey("backgroundQueueSecondsDelay")
val BACKGROUND_QUEUE_MIN_NUM_TASKS = intPreferencesKey("backgroundQueueMinNumTasks")

val NOTIFICATION_CLICK_BEHAVIOR_KEY = stringPreferencesKey("notificationClickBehaviorKey")

val TRACK_SCREEN = booleanPreferencesKey("trackScreen")
val TRACK_DEVICE_ATTRIBUTES = booleanPreferencesKey("trackDeviceAttributes")
val DEBUG_MODE = booleanPreferencesKey("debugMode")
Expand Down

0 comments on commit d60b333

Please sign in to comment.