From ae09860445545f7f2234a880cc97c1443e53b3f9 Mon Sep 17 00:00:00 2001 From: Muhammad Rehan Date: Tue, 26 Nov 2024 12:39:01 +0500 Subject: [PATCH] chore: rename android classes and method extensions (#179) --- .../customer_io/CustomerIOExtensions.kt | 11 ---- ...ustomerIoPlugin.kt => CustomerIOPlugin.kt} | 57 ++++++++++--------- .../NativeModuleBridge.kt} | 4 +- .../CustomerIOInAppMessaging.kt | 10 ++-- .../messagingpush/CustomerIOPushMessaging.kt | 24 ++++---- ...tensions.kt => PushMessagingExtensions.kt} | 14 ++--- .../customer_io/utils/MapExtensions.kt | 12 ++++ .../customer_io/utils/StringExtensions.kt | 6 ++ pubspec.yaml | 2 +- 9 files changed, 74 insertions(+), 66 deletions(-) rename android/src/main/kotlin/io/customer/customer_io/{CustomerIoPlugin.kt => CustomerIOPlugin.kt} (80%) rename android/src/main/kotlin/io/customer/customer_io/{CustomerIOPluginModule.kt => bridge/NativeModuleBridge.kt} (92%) rename android/src/main/kotlin/io/customer/customer_io/messagingpush/{Extensions.kt => PushMessagingExtensions.kt} (78%) create mode 100644 android/src/main/kotlin/io/customer/customer_io/utils/MapExtensions.kt create mode 100644 android/src/main/kotlin/io/customer/customer_io/utils/StringExtensions.kt diff --git a/android/src/main/kotlin/io/customer/customer_io/CustomerIOExtensions.kt b/android/src/main/kotlin/io/customer/customer_io/CustomerIOExtensions.kt index 138a65a..b6256fc 100644 --- a/android/src/main/kotlin/io/customer/customer_io/CustomerIOExtensions.kt +++ b/android/src/main/kotlin/io/customer/customer_io/CustomerIOExtensions.kt @@ -3,17 +3,6 @@ package io.customer.customer_io import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel -/** - * Returns the value corresponding to the given key after casting to the generic type provided, or - * null if such key is not present in the map or value cannot be casted to the given type. - */ -internal inline fun Map.getAsTypeOrNull(key: String): T? { - if (containsKey(key)) { - return get(key) as? T - } - return null -} - /** * Invokes lambda method that can be used to call matching native method conveniently. The lambda * expression receives function parameters as arguments and should return the desired result. Any diff --git a/android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt b/android/src/main/kotlin/io/customer/customer_io/CustomerIOPlugin.kt similarity index 80% rename from android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt rename to android/src/main/kotlin/io/customer/customer_io/CustomerIOPlugin.kt index 399a387..9da23c2 100644 --- a/android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt +++ b/android/src/main/kotlin/io/customer/customer_io/CustomerIOPlugin.kt @@ -3,9 +3,11 @@ package io.customer.customer_io import android.app.Application import android.content.Context import androidx.annotation.NonNull +import io.customer.customer_io.bridge.NativeModuleBridge import io.customer.customer_io.constant.Keys import io.customer.customer_io.messaginginapp.CustomerIOInAppMessaging import io.customer.customer_io.messagingpush.CustomerIOPushMessaging +import io.customer.customer_io.utils.getAs import io.customer.sdk.CustomerIO import io.customer.sdk.CustomerIOBuilder import io.customer.sdk.core.di.SDKComponent @@ -26,7 +28,7 @@ import io.flutter.plugin.common.MethodChannel.Result * Android implementation of plugin that will let Flutter developers to * interact with a Android platform * */ -class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { +class CustomerIOPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { /// The MethodChannel that will the communication between Flutter and native Android /// /// This local reference serves to register the plugin with the Flutter Engine and unregister it @@ -34,7 +36,7 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { private lateinit var flutterCommunicationChannel: MethodChannel private lateinit var context: Context - private lateinit var modules: List + private lateinit var modules: List private val logger: Logger = SDKComponent.logger @@ -133,8 +135,8 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } private fun identify(params: Map) { - val userId = params.getAsTypeOrNull(Keys.Tracking.USER_ID) - val traits = params.getAsTypeOrNull>(Keys.Tracking.TRAITS) ?: emptyMap() + val userId = params.getAs(Keys.Tracking.USER_ID) + val traits = params.getAs>(Keys.Tracking.TRAITS) ?: emptyMap() if (userId == null && traits.isEmpty()) { logger.error("Please provide either an ID or traits to identify.") @@ -151,10 +153,10 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } private fun track(params: Map) { - val name = requireNotNull(params.getAsTypeOrNull(Keys.Tracking.NAME)) { + val name = requireNotNull(params.getAs(Keys.Tracking.NAME)) { "Event name is missing in params: $params" } - val properties = params.getAsTypeOrNull>(Keys.Tracking.PROPERTIES) + val properties = params.getAs>(Keys.Tracking.PROPERTIES) if (properties.isNullOrEmpty()) { CustomerIO.instance().track(name) @@ -164,16 +166,16 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } private fun registerDeviceToken(params: Map) { - val token = requireNotNull(params.getAsTypeOrNull(Keys.Tracking.TOKEN)) { + val token = requireNotNull(params.getAs(Keys.Tracking.TOKEN)) { "Device token is missing in params: $params" } CustomerIO.instance().registerDeviceToken(token) } private fun trackMetric(params: Map) { - val deliveryId = params.getAsTypeOrNull(Keys.Tracking.DELIVERY_ID) - val deliveryToken = params.getAsTypeOrNull(Keys.Tracking.DELIVERY_TOKEN) - val eventName = params.getAsTypeOrNull(Keys.Tracking.METRIC_EVENT) + val deliveryId = params.getAs(Keys.Tracking.DELIVERY_ID) + val deliveryToken = params.getAs(Keys.Tracking.DELIVERY_TOKEN) + val eventName = params.getAs(Keys.Tracking.METRIC_EVENT) if (deliveryId == null || deliveryToken == null || eventName == null) { throw IllegalArgumentException("Missing required parameters") @@ -191,7 +193,7 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } private fun setDeviceAttributes(params: Map) { - val attributes = params.getAsTypeOrNull>(Keys.Tracking.ATTRIBUTES) + val attributes = params.getAs>(Keys.Tracking.ATTRIBUTES) if (attributes.isNullOrEmpty()) { logger.error("Device attributes are missing in params: $params") @@ -202,7 +204,7 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } private fun setProfileAttributes(params: Map) { - val attributes = params.getAsTypeOrNull>(Keys.Tracking.ATTRIBUTES) + val attributes = params.getAs>(Keys.Tracking.ATTRIBUTES) if (attributes.isNullOrEmpty()) { logger.error("Profile attributes are missing in params: $params") @@ -213,10 +215,10 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } private fun screen(params: Map) { - val title = requireNotNull(params.getAsTypeOrNull(Keys.Tracking.TITLE)) { + val title = requireNotNull(params.getAs(Keys.Tracking.TITLE)) { "Screen title is missing in params: $params" } - val properties = params.getAsTypeOrNull>(Keys.Tracking.PROPERTIES) + val properties = params.getAs>(Keys.Tracking.PROPERTIES) if (properties.isNullOrEmpty()) { CustomerIO.instance().screen(title) @@ -227,12 +229,12 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { private fun initialize(args: Map): kotlin.Result = runCatching { val application: Application = context.applicationContext as Application - val cdpApiKey = requireNotNull(args.getAsTypeOrNull("cdpApiKey")) { + val cdpApiKey = requireNotNull(args.getAs("cdpApiKey")) { "CDP API Key is required to initialize Customer.io" } - val logLevelRawValue = args.getAsTypeOrNull("logLevel") - val regionRawValue = args.getAsTypeOrNull("region") + val logLevelRawValue = args.getAs("logLevel") + val regionRawValue = args.getAs("region") val givenRegion = regionRawValue.let { Region.getRegion(it) } CustomerIOBuilder( @@ -242,28 +244,27 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { logLevelRawValue?.let { logLevel(CioLogLevel.getLogLevel(it)) } regionRawValue?.let { region(givenRegion) } - args.getAsTypeOrNull("migrationSiteId")?.let(::migrationSiteId) - args.getAsTypeOrNull("autoTrackDeviceAttributes") - ?.let(::autoTrackDeviceAttributes) - args.getAsTypeOrNull("trackApplicationLifecycleEvents") + args.getAs("migrationSiteId")?.let(::migrationSiteId) + args.getAs("autoTrackDeviceAttributes")?.let(::autoTrackDeviceAttributes) + args.getAs("trackApplicationLifecycleEvents") ?.let(::trackApplicationLifecycleEvents) - args.getAsTypeOrNull("flushAt")?.let(::flushAt) - args.getAsTypeOrNull("flushInterval")?.let(::flushInterval) + args.getAs("flushAt")?.let(::flushAt) + args.getAs("flushInterval")?.let(::flushInterval) - args.getAsTypeOrNull("apiHost")?.let(::apiHost) - args.getAsTypeOrNull("cdnHost")?.let(::cdnHost) + args.getAs("apiHost")?.let(::apiHost) + args.getAs("cdnHost")?.let(::cdnHost) // Configure in-app messaging module based on config provided by customer app - args.getAsTypeOrNull>(key = "inApp")?.let { inAppConfig -> + args.getAs>(key = "inApp")?.let { inAppConfig -> modules.filterIsInstance().forEach { it.configureModule( builder = this, - config = inAppConfig.plus("region" to givenRegion), + config = inAppConfig.plus("region" to givenRegion.code), ) } } // Configure push messaging module based on config provided by customer app - args.getAsTypeOrNull>(key = "push").let { pushConfig -> + args.getAs>(key = "push").let { pushConfig -> modules.filterIsInstance().forEach { it.configureModule( builder = this, diff --git a/android/src/main/kotlin/io/customer/customer_io/CustomerIOPluginModule.kt b/android/src/main/kotlin/io/customer/customer_io/bridge/NativeModuleBridge.kt similarity index 92% rename from android/src/main/kotlin/io/customer/customer_io/CustomerIOPluginModule.kt rename to android/src/main/kotlin/io/customer/customer_io/bridge/NativeModuleBridge.kt index ebf0482..d891ad5 100644 --- a/android/src/main/kotlin/io/customer/customer_io/CustomerIOPluginModule.kt +++ b/android/src/main/kotlin/io/customer/customer_io/bridge/NativeModuleBridge.kt @@ -1,4 +1,4 @@ -package io.customer.customer_io +package io.customer.customer_io.bridge import io.customer.sdk.CustomerIOBuilder import io.flutter.embedding.engine.plugins.FlutterPlugin @@ -11,7 +11,7 @@ import io.flutter.plugin.common.MethodChannel * should be treated as module in Flutter SDK and should be used to hold all relevant methods at * single place. */ -internal interface CustomerIOPluginModule : MethodChannel.MethodCallHandler, ActivityAware { +internal interface NativeModuleBridge : MethodChannel.MethodCallHandler, ActivityAware { /** * Unique name of module to identify between other modules */ diff --git a/android/src/main/kotlin/io/customer/customer_io/messaginginapp/CustomerIOInAppMessaging.kt b/android/src/main/kotlin/io/customer/customer_io/messaginginapp/CustomerIOInAppMessaging.kt index 5603003..ece79f6 100644 --- a/android/src/main/kotlin/io/customer/customer_io/messaginginapp/CustomerIOInAppMessaging.kt +++ b/android/src/main/kotlin/io/customer/customer_io/messaginginapp/CustomerIOInAppMessaging.kt @@ -1,10 +1,10 @@ package io.customer.customer_io.messaginginapp import android.app.Activity -import io.customer.customer_io.CustomerIOPluginModule +import io.customer.customer_io.bridge.NativeModuleBridge import io.customer.customer_io.constant.Keys -import io.customer.customer_io.getAsTypeOrNull import io.customer.customer_io.invokeNative +import io.customer.customer_io.utils.getAs import io.customer.messaginginapp.MessagingInAppModuleConfig import io.customer.messaginginapp.ModuleMessagingInApp import io.customer.messaginginapp.di.inAppMessaging @@ -27,7 +27,7 @@ import java.lang.ref.WeakReference */ internal class CustomerIOInAppMessaging( pluginBinding: FlutterPlugin.FlutterPluginBinding, -) : CustomerIOPluginModule, MethodChannel.MethodCallHandler, ActivityAware { +) : NativeModuleBridge, MethodChannel.MethodCallHandler, ActivityAware { override val moduleName: String = "InAppMessaging" override val flutterCommunicationChannel: MethodChannel = MethodChannel(pluginBinding.binaryMessenger, "customer_io_messaging_in_app") @@ -74,8 +74,8 @@ internal class CustomerIOInAppMessaging( builder: CustomerIOBuilder, config: Map ) { - val siteId = config.getAsTypeOrNull("siteId") - val regionRawValue = config.getAsTypeOrNull("region") + val siteId = config.getAs("siteId") + val regionRawValue = config.getAs("region") val givenRegion = regionRawValue.let { Region.getRegion(it) } if (siteId.isNullOrBlank()) { diff --git a/android/src/main/kotlin/io/customer/customer_io/messagingpush/CustomerIOPushMessaging.kt b/android/src/main/kotlin/io/customer/customer_io/messagingpush/CustomerIOPushMessaging.kt index f625be8..ae78ef8 100644 --- a/android/src/main/kotlin/io/customer/customer_io/messagingpush/CustomerIOPushMessaging.kt +++ b/android/src/main/kotlin/io/customer/customer_io/messagingpush/CustomerIOPushMessaging.kt @@ -1,10 +1,11 @@ package io.customer.customer_io.messagingpush import android.content.Context -import io.customer.customer_io.CustomerIOPluginModule +import io.customer.customer_io.bridge.NativeModuleBridge import io.customer.customer_io.constant.Keys -import io.customer.customer_io.getAsTypeOrNull import io.customer.customer_io.invokeNative +import io.customer.customer_io.utils.getAs +import io.customer.customer_io.utils.takeIfNotBlank import io.customer.messagingpush.CustomerIOFirebaseMessagingService import io.customer.messagingpush.MessagingPushModuleConfig import io.customer.messagingpush.ModuleMessagingPushFCM @@ -16,7 +17,7 @@ import io.customer.sdk.core.util.Logger import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel -import java.util.* +import java.util.UUID /** * Flutter module implementation for messaging push module in native SDKs. All functionality @@ -24,7 +25,7 @@ import java.util.* */ internal class CustomerIOPushMessaging( pluginBinding: FlutterPlugin.FlutterPluginBinding, -) : CustomerIOPluginModule, MethodChannel.MethodCallHandler { +) : NativeModuleBridge, MethodChannel.MethodCallHandler { override val moduleName: String = "PushMessaging" private val applicationContext: Context = pluginBinding.applicationContext override val flutterCommunicationChannel: MethodChannel = @@ -42,8 +43,8 @@ internal class CustomerIOPushMessaging( Keys.Methods.ON_MESSAGE_RECEIVED -> { call.invokeNative(result) { args -> return@invokeNative onMessageReceived( - message = args.getAsTypeOrNull>("message"), - handleNotificationTrigger = args.getAsTypeOrNull("handleNotificationTrigger") + message = args.getAs>("message"), + handleNotificationTrigger = args.getAs("handleNotificationTrigger") ) } } @@ -75,8 +76,8 @@ internal class CustomerIOPushMessaging( } // Generate destination string, see docs on receiver method for more details - val destination = (message["to"] as? String)?.takeIf { it.isNotBlank() } - ?: UUID.randomUUID().toString() + val destination = + (message["to"] as? String)?.takeIfNotBlank() ?: UUID.randomUUID().toString() return CustomerIOFirebaseMessagingService.onMessageReceived( context = applicationContext, remoteMessage = message.toFCMRemoteMessage(destination = destination), @@ -99,14 +100,13 @@ internal class CustomerIOPushMessaging( builder: CustomerIOBuilder, config: Map ) { - val androidConfig = - config.getAsTypeOrNull>(key = "android") ?: emptyMap() + val androidConfig = config.getAs>(key = "android") ?: emptyMap() // Prefer `android` object for push configurations as it's more specific to Android // For common push configurations, use `config` object instead of `android` // Default push click behavior is to prevent restart of activity in Flutter apps - val pushClickBehavior = androidConfig.getAsTypeOrNull("pushClickBehavior") - ?.takeIf { it.isNotBlank() } + val pushClickBehavior = androidConfig.getAs("pushClickBehavior") + ?.takeIfNotBlank() ?.let { value -> runCatching { enumValueOf(value) }.getOrNull() } ?: PushClickBehavior.ACTIVITY_PREVENT_RESTART diff --git a/android/src/main/kotlin/io/customer/customer_io/messagingpush/Extensions.kt b/android/src/main/kotlin/io/customer/customer_io/messagingpush/PushMessagingExtensions.kt similarity index 78% rename from android/src/main/kotlin/io/customer/customer_io/messagingpush/Extensions.kt rename to android/src/main/kotlin/io/customer/customer_io/messagingpush/PushMessagingExtensions.kt index b51fa91..6b1f071 100644 --- a/android/src/main/kotlin/io/customer/customer_io/messagingpush/Extensions.kt +++ b/android/src/main/kotlin/io/customer/customer_io/messagingpush/PushMessagingExtensions.kt @@ -1,7 +1,7 @@ package io.customer.customer_io.messagingpush import com.google.firebase.messaging.RemoteMessage -import io.customer.customer_io.getAsTypeOrNull +import io.customer.customer_io.utils.getAs /** * Safely transforms any value to string @@ -23,8 +23,8 @@ private fun Any.toStringOrNull(): String? = try { * string for it. */ internal fun Map.toFCMRemoteMessage(destination: String): RemoteMessage { - val notification = getAsTypeOrNull>("notification") - val data = getAsTypeOrNull>("data") + val notification = getAs>("notification") + val data = getAs>("data") val messageParams = buildMap { notification?.let { result -> putAll(result) } // Adding `data` after `notification` so `data` params take more value as we mainly use @@ -42,10 +42,10 @@ internal fun Map.toFCMRemoteMessage(destination: String): RemoteMes value.toStringOrNull()?.let { v -> addData(key, v) } } } - getAsTypeOrNull("messageId")?.let { id -> setMessageId(id) } - getAsTypeOrNull("messageType")?.let { type -> setMessageType(type) } - getAsTypeOrNull("collapseKey")?.let { key -> setCollapseKey(key) } - getAsTypeOrNull("ttl")?.let { time -> ttl = time } + getAs("messageId")?.let { id -> setMessageId(id) } + getAs("messageType")?.let { type -> setMessageType(type) } + getAs("collapseKey")?.let { key -> setCollapseKey(key) } + getAs("ttl")?.let { time -> ttl = time } return@with build() } } diff --git a/android/src/main/kotlin/io/customer/customer_io/utils/MapExtensions.kt b/android/src/main/kotlin/io/customer/customer_io/utils/MapExtensions.kt new file mode 100644 index 0000000..d0a2785 --- /dev/null +++ b/android/src/main/kotlin/io/customer/customer_io/utils/MapExtensions.kt @@ -0,0 +1,12 @@ +package io.customer.customer_io.utils + +/** + * Returns the value corresponding to the given key after casting to the generic type provided, or + * null if such key is not present in the map or value cannot be casted to the given type. + */ +internal inline fun Map.getAs(key: String): T? { + if (containsKey(key)) { + return get(key) as? T + } + return null +} diff --git a/android/src/main/kotlin/io/customer/customer_io/utils/StringExtensions.kt b/android/src/main/kotlin/io/customer/customer_io/utils/StringExtensions.kt new file mode 100644 index 0000000..42ac132 --- /dev/null +++ b/android/src/main/kotlin/io/customer/customer_io/utils/StringExtensions.kt @@ -0,0 +1,6 @@ +package io.customer.customer_io.utils + +/** + * Extension function to return the string if it is not null or blank. + */ +internal fun String?.takeIfNotBlank(): String? = takeIf { !it.isNullOrBlank() } diff --git a/pubspec.yaml b/pubspec.yaml index d91304d..c77b0b5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -39,7 +39,7 @@ flutter: platforms: android: package: io.customer.customer_io - pluginClass: CustomerIoPlugin + pluginClass: CustomerIOPlugin ios: pluginClass: CustomerIoPlugin native_sdk_version: 3.5.1