Skip to content

Commit

Permalink
Comments Updates
Browse files Browse the repository at this point in the history
Refactor native Kotlin + Dart code into separated functions and classes

Switch to positional / required parameters for Dart

Switch method channel to analytics_pinpoint

Basic unit tests in amplify_analytics_pinpoint_test.dart
  • Loading branch information
fjnoyp committed Jul 18, 2020
1 parent 098ed15 commit a27219d
Show file tree
Hide file tree
Showing 20 changed files with 530 additions and 310 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.amazonaws.amplify.amplify_analytics_pinpoint

import androidx.annotation.NonNull

import com.amplifyframework.core.Amplify

import io.flutter.plugin.common.MethodChannel


class AmplifyAnalyticsBridge {
companion object Bridge {

fun recordEvent(@NonNull arguments: Any, @NonNull result: MethodChannel.Result){
try {
val argumentsMap = arguments as HashMap<*,*>
val name = argumentsMap["name"] as String
val properties = argumentsMap["propertiesMap"] as HashMap<String, Any>;
Amplify.Analytics.recordEvent(
AmplifyAnalyticsConstructor.createAnalyticsEvent(name, properties) );
result.success(true);
}
catch(e: Exception) {
result.error("AmplifyException", "Error", e.message )
}
}

fun flushEvents(@NonNull result: MethodChannel.Result){
try {
Amplify.Analytics.flushEvents();
}
catch(e: Exception){
result.error("AmplifyException", "Error", e.message )
}
}

fun registerGlobalProperties(@NonNull arguments: Any, @NonNull result: MethodChannel.Result){
try{
val globalProperties = arguments as HashMap<String, Any>;
Amplify.Analytics.registerGlobalProperties(
AmplifyAnalyticsConstructor.createAnalyticsProperties(globalProperties) );
}
catch(e: Exception){
result.error("AmplifyException", "Error", e.message )
}
}

fun unregisterGlobalProperties(@NonNull arguments: Any, @NonNull result: MethodChannel.Result){
try{
val propertyNames = (arguments as ArrayList<String>).toSet<String>();

for(name in propertyNames){
Amplify.Analytics.unregisterGlobalProperties( name );
}
}
catch(e: Exception){
result.error("AmplifyException", "Error", e.message )
}
}

fun unregisterAllGlobalProperties(@NonNull result: MethodChannel.Result){
try {
Amplify.Analytics.unregisterGlobalProperties()
}
catch(e: Exception){
result.error("AmplifyException", "Error", e.message )
}
}

fun enable(@NonNull result: MethodChannel.Result) {
try {
Amplify.Analytics.enable();
} catch (e: Exception) {
result.error("AmplifyException", "Error", e.message)
}
}

fun disable(@NonNull result: MethodChannel.Result){
try {
Amplify.Analytics.disable();
} catch (e: Exception) {
result.error("AmplifyException", "Error", e.message)
}
}

fun identifyUser(@NonNull arguments: Any, @NonNull result: MethodChannel.Result){
try {
val argumentsMap = arguments as HashMap<*, *>
val userId = argumentsMap["userId"] as String
val userProfileMap = argumentsMap["userProfileMap"] as HashMap<String, Object>;

Amplify.Analytics.identifyUser(userId,
AmplifyAnalyticsConstructor.createUserProfile(userProfileMap));
}
catch(e: Exception){
result.error("AmplifyException", "Error", e.message )
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.amazonaws.amplify.amplify_analytics_pinpoint

import com.amplifyframework.analytics.AnalyticsEvent
import com.amplifyframework.analytics.AnalyticsProperties
import com.amplifyframework.analytics.UserProfile

class AmplifyAnalyticsConstructor {
companion object Constructor {

fun createAnalyticsProperties(propertiesMap : HashMap<String, Any>) : AnalyticsProperties {

val propertiesBuilder: AnalyticsProperties.Builder = AnalyticsProperties.builder()

for( (key, value ) in propertiesMap){

when (value) {
is String -> {
propertiesBuilder.add(key, value);
}
is Double -> {
propertiesBuilder.add(key, value);
}
is Boolean -> {
propertiesBuilder.add(key, value);
}
is Int -> {
propertiesBuilder.add(key, value);
}
else -> {
throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties");
}
}

}

return propertiesBuilder.build();
}

fun createAnalyticsEvent(name: String, propertiesMap: HashMap<String, Any>) : AnalyticsEvent{

val eventBuilder: AnalyticsEvent.Builder = AnalyticsEvent.builder()
.name( name );

for( (key, value ) in propertiesMap){

when (value) {
is String -> {
eventBuilder.addProperty(key, value);
}
is Double -> {
eventBuilder.addProperty(key, value);
}
is Boolean -> {
eventBuilder.addProperty(key, value);
}
is Int -> {
eventBuilder.addProperty(key, value);
}
else -> {
throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties");
}
}
}

return eventBuilder.build();
}

fun createUserProfile(userProfileMap: HashMap<String, *>) : UserProfile {

val userProfileBuilder = UserProfile.builder();

for (item in userProfileMap) {
when (item.key) {
"name" ->
userProfileBuilder.name(item.value as String);
"email" ->
userProfileBuilder.email(item.value as String);
"plan" ->
userProfileBuilder.plan(item.value as String);
"location" -> {
val locationMap = item.value as HashMap<String, String>;
userProfileBuilder.location(createUserLocation(locationMap));
}
"properties" -> {
val propertiesMap = item.value as HashMap<String, Any>;
userProfileBuilder.customProperties(createAnalyticsProperties(propertiesMap));
}
else -> {
throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties");
}
}
}

return userProfileBuilder.build();
}

fun createUserLocation(userLocationMap: HashMap<String, *>) : UserProfile.Location{

val locationBuilder = UserProfile.Location.builder()

for(item in userLocationMap){
when(item.key){
"latitude" ->
locationBuilder.latitude(item.value as Double);
"longitude" ->
locationBuilder.longitude(item.value as Double);
"postalCode" ->
locationBuilder.postalCode(item.value as String);
"city" ->
locationBuilder.city(item.value as String);
"region" ->
locationBuilder.region(item.value as String);
"country" ->
locationBuilder.country(item.value as String);
else -> {
throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties");
}
}
}

return locationBuilder.build();
}
}

}

0 comments on commit a27219d

Please sign in to comment.