Skip to content

Commit

Permalink
fix: in-app remove gist org id (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahroz16 authored Feb 9, 2023
1 parent 5d8bb95 commit ce4cc9e
Show file tree
Hide file tree
Showing 25 changed files with 452 additions and 227 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,7 @@ healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/

# End of https://www.gitignore.io/api/git,dart,flutter,intellij,webstorm,visualstudio
# Remove env files
*.env

# End of https://www.gitignore.io/api/git,dart,flutter,intellij,webstorm,visualstudio
11 changes: 10 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,21 @@ android {
defaultConfig {
minSdkVersion 21
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
freeCompilerArgs += [
'-Xopt-in=kotlin.RequiresOptIn',
'-Xopt-in=io.customer.base.internal.InternalCustomerIOApi',
]
}
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// Customer.io SDK
def cioVersion = "3.2.0-alpha.1"
def cioVersion = "3.3.0-beta.1"
implementation "io.customer.android:tracking:$cioVersion"
implementation "io.customer.android:messaging-push-fcm:$cioVersion"
implementation "io.customer.android:messaging-in-app:$cioVersion"
Expand Down
136 changes: 89 additions & 47 deletions android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package io.customer.customer_io

import android.app.Activity
import android.app.Application
import android.content.Context
import androidx.annotation.NonNull
import io.customer.customer_io.constant.Keys
import io.customer.customer_io.extension.*
import io.customer.messaginginapp.MessagingInAppModuleConfig
import io.customer.messaginginapp.ModuleMessagingInApp
import io.customer.messaginginapp.type.InAppEventListener
import io.customer.messaginginapp.type.InAppMessage
import io.customer.messagingpush.MessagingPushModuleConfig
import io.customer.messagingpush.ModuleMessagingPushFCM
import io.customer.sdk.CustomerIO
import io.customer.sdk.CustomerIOConfig
import io.customer.sdk.CustomerIOShared
import io.customer.sdk.data.store.Client
import io.customer.sdk.data.model.Region
import io.customer.sdk.extensions.getProperty
import io.customer.sdk.extensions.getString
import io.customer.sdk.extensions.takeIfNotBlank
import io.customer.sdk.util.Logger
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
Expand All @@ -22,17 +31,34 @@ 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 {
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
/// when the Flutter Engine is detached from the Activity
private lateinit var flutterCommunicationChannel: MethodChannel
private lateinit var context: Context
private var activity: Activity? = null

private val logger: Logger
get() = CustomerIOShared.instance().diStaticGraph.logger

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
this.activity = binding.activity
}

override fun onDetachedFromActivityForConfigChanges() {
onDetachedFromActivity()
}

override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
onAttachedToActivity(binding)
}

override fun onDetachedFromActivity() {
this.activity = null
}

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
context = flutterPluginBinding.applicationContext
flutterCommunicationChannel =
Expand All @@ -41,15 +67,14 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler {
}

private fun MethodCall.toNativeMethodCall(
result: Result,
performAction: (params: Map<String, Any>) -> Unit
result: Result, performAction: (params: Map<String, Any>) -> Unit
) {
try {
val params = this.arguments as? Map<String, Any> ?: emptyMap()
performAction(params)
result.success(true)
} catch (e: Exception) {
result.error(this.method, e.localizedMessage, null);
result.error(this.method, e.localizedMessage, null)
}
}

Expand Down Expand Up @@ -105,7 +130,7 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler {
CustomerIO.instance().identify(identifier, attributes)
}

fun track(params: Map<String, Any>) {
private fun track(params: Map<String, Any>) {
val name = params.getString(Keys.Tracking.EVENT_NAME)
val attributes =
params.getProperty<Map<String, Any>>(Keys.Tracking.ATTRIBUTES) ?: emptyMap()
Expand All @@ -117,21 +142,20 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler {
}
}

fun setDeviceAttributes(params: Map<String, Any>) {
private fun setDeviceAttributes(params: Map<String, Any>) {
val attributes =
params.getProperty<Map<String, Any>>(Keys.Tracking.ATTRIBUTES) ?: emptyMap()

CustomerIO.instance().deviceAttributes = attributes
}

fun setProfileAttributes(params: Map<String, Any>) {
val attributes =
params.getProperty<Map<String, Any>>(Keys.Tracking.ATTRIBUTES) ?: return
private fun setProfileAttributes(params: Map<String, Any>) {
val attributes = params.getProperty<Map<String, Any>>(Keys.Tracking.ATTRIBUTES) ?: return

CustomerIO.instance().profileAttributes = attributes
}

fun screen(params: Map<String, Any>) {
private fun screen(params: Map<String, Any>) {
val name = params.getString(Keys.Tracking.EVENT_NAME)
val attributes =
params.getProperty<Map<String, Any>>(Keys.Tracking.ATTRIBUTES) ?: emptyMap()
Expand All @@ -149,24 +173,28 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler {
val apiKey = configData.getString(Keys.Environment.API_KEY)
val region = configData.getProperty<String>(
Keys.Environment.REGION
)?.takeIfNotBlank().toRegion()
val organizationId = configData.getProperty<String>(
Keys.Environment.ORGANIZATION_ID
)?.takeIfNotBlank()
val enableInApp = configData.getProperty<Boolean>(
Keys.Environment.ENABLE_IN_APP
)

CustomerIO.Builder(
siteId = siteId,
apiKey = apiKey,
region = region,
region = Region.getRegion(region),
appContext = application,
config = configData
).apply {
setClient(client = getUserAgentClient(packageConfig = configData))
setupConfig(configData)
addCustomerIOModule(module = configureModuleMessagingPushFCM(configData))
if (!organizationId.isNullOrBlank()) {
if (enableInApp == true) {
addCustomerIOModule(
module = ModuleMessagingInApp(
organizationId = organizationId,
config = MessagingInAppModuleConfig.Builder()
.setEventListener(CustomerIOInAppEventListener { method, args ->
this@CustomerIoPlugin.activity?.runOnUiThread {
flutterCommunicationChannel.invokeMethod(method, args)
}
}).build(),
)
)
}
Expand All @@ -177,41 +205,55 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler {
private fun configureModuleMessagingPushFCM(config: Map<String, Any?>?): ModuleMessagingPushFCM {
return ModuleMessagingPushFCM(
config = MessagingPushModuleConfig.Builder().apply {
config?.getProperty<Boolean>(Keys.Config.AUTO_TRACK_PUSH_EVENTS)?.let { value ->
setAutoTrackPushEvents(autoTrackPushEvents = value)
}
config?.getProperty<Boolean>(CustomerIOConfig.Companion.Keys.AUTO_TRACK_PUSH_EVENTS)
?.let { value ->
setAutoTrackPushEvents(autoTrackPushEvents = value)
}
}.build(),
)
}

private fun getUserAgentClient(packageConfig: Map<String, Any?>?): Client {
val sourceSDKVersion = packageConfig?.getProperty<String>(
Keys.PackageConfig.SOURCE_SDK_VERSION
)?.takeIfNotBlank() ?: "n/a"
return Client.Flutter(sdkVersion = sourceSDKVersion)
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
flutterCommunicationChannel.setMethodCallHandler(null)
}
}

class CustomerIOInAppEventListener(private val invokeMethod: (String, Any?) -> Unit) :
InAppEventListener {
override fun errorWithMessage(message: InAppMessage) {
invokeMethod(
"errorWithMessage", mapOf(
"messageId" to message.messageId, "deliveryId" to message.deliveryId
)
)
}

private fun CustomerIO.Builder.setupConfig(config: Map<String, Any?>?): CustomerIO.Builder {
if (config == null) return this
override fun messageActionTaken(
message: InAppMessage, actionValue: String, actionName: String
) {
invokeMethod(
"messageActionTaken", mapOf(
"messageId" to message.messageId,
"deliveryId" to message.deliveryId,
"actionValue" to actionValue,
"actionName" to actionName
)
)
}

val logLevel = config.getProperty<String>(Keys.Config.LOG_LEVEL).toCIOLogLevel()
setLogLevel(level = logLevel)
config.getProperty<String>(Keys.Config.TRACKING_API_URL)?.takeIfNotBlank()?.let { value ->
setTrackingApiURL(value)
}
config.getProperty<Boolean>(Keys.Config.AUTO_TRACK_DEVICE_ATTRIBUTES)?.let { value ->
autoTrackDeviceAttributes(shouldTrackDeviceAttributes = value)
}
config.getProperty<Int>(Keys.Config.BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS)?.let { value ->
setBackgroundQueueMinNumberOfTasks(backgroundQueueMinNumberOfTasks = value)
}
config.getProperty<Double>(Keys.Config.BACKGROUND_QUEUE_SECONDS_DELAY)?.let { value ->
setBackgroundQueueSecondsDelay(backgroundQueueSecondsDelay = value)
}
return this
override fun messageDismissed(message: InAppMessage) {
invokeMethod(
"messageDismissed", mapOf(
"messageId" to message.messageId, "deliveryId" to message.deliveryId
)
)
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
flutterCommunicationChannel.setMethodCallHandler(null)
override fun messageShown(message: InAppMessage) {
invokeMethod(
"messageShown", mapOf(
"messageId" to message.messageId, "deliveryId" to message.deliveryId
)
)
}
}
14 changes: 1 addition & 13 deletions android/src/main/kotlin/io/customer/customer_io/constant/Keys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,7 @@ internal object Keys {
const val SITE_ID = "siteId"
const val API_KEY = "apiKey"
const val REGION = "region"
const val ORGANIZATION_ID = "organizationId"
const val ENABLE_IN_APP = "enableInApp"
}

object Config {
const val TRACKING_API_URL = "trackingApiUrl"
const val AUTO_TRACK_PUSH_EVENTS = "autoTrackPushEvents"
const val AUTO_TRACK_DEVICE_ATTRIBUTES = "autoTrackDeviceAttributes"
const val LOG_LEVEL = "logLevel"
const val BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS = "backgroundQueueMinNumberOfTasks"
const val BACKGROUND_QUEUE_SECONDS_DELAY = "backgroundQueueSecondsDelay"
}

object PackageConfig {
const val SOURCE_SDK_VERSION = "version"
}
}

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 2 additions & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath 'com.android.tools.build:gradle:7.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

allprojects {
repositories {
mavenLocal()
google()
mavenCentral()
}
Expand Down
6 changes: 3 additions & 3 deletions example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Jun 23 08:50:38 CEST 2017
#Fri Jan 20 23:52:06 EST 2023
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
zipStoreBase=GRADLE_USER_HOME
2 changes: 2 additions & 0 deletions example/credentials.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
siteId=SITE_ID
apiKey=API_KEY
Loading

0 comments on commit ce4cc9e

Please sign in to comment.