Skip to content

Commit

Permalink
TAP-5070/TAP-5900/TAP-5167: Refactored SDK to be more modular (#12)
Browse files Browse the repository at this point in the history
* Refactored SDK to be more modular

* Removed TaplyticsProvider imports

* Removed unused and undocumented native methods

* Added _newSessionCallback native method and exported it as part of `setTaplyticsNewSessionListener` method

* Changed newSyncObject to take in NSDictionary

* Added getVariables and registerVaraiblesChangedListener methods and removed lodash and only added lodash.clonedeep

* Add library to gitignore and prettierignore

* Added extra line on ignore files

* Added push method types and cleaned up the push methods

* Removed old index files and added push types to index

* Fixed typo in console.error
  • Loading branch information
hamzahayat authored Mar 24, 2021
1 parent 3ca480a commit d29d280
Show file tree
Hide file tree
Showing 22 changed files with 5,244 additions and 599 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,9 @@ iOSInjectionProject/

# End of https://www.gitignore.io/api/objective-c
ios/RNTaplyticsReact.xcodeproj/project.xcworkspace/xcshareddata/

# Generated Library
lib/

# Watchman config
.watchmanconfig
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ ios
android
node_modules
.circleci
lib
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.taplytics.sdk.TLGcmBroadcastReceiver;

import org.json.JSONException;
import org.json.JSONObject;

import static com.taplytics.react.TaplyticsReactHelper.convertJsonToMap;

/**
* Created by Adam Wootton on 2017-01-30.
*/

public class TRNBroadcastReceiver extends TLGcmBroadcastReceiver {
private static final String LOG_TAG_NAME = "TaplyticsBR";

@Override
public void pushOpened(Context context, Intent intent) {
if (TaplyticsReactModule.getInstance() != null) {
Expand Down Expand Up @@ -52,7 +58,11 @@ private WritableMap getIntentData(Intent intent) {
} catch (Throwable ignored) {
//Ignore the bad JSON for now. Log here as you please.
}
data.putString("value", dataObject.toString());
try {
data.putMap("value", convertJsonToMap(dataObject));
} catch (JSONException e) {
Log.e(LOG_TAG_NAME, e.getMessage());
}
return data;
}
}
146 changes: 146 additions & 0 deletions android/src/main/java/com/taplytics/react/TaplyticsReactHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.taplytics.react;


import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.Map;

public class TaplyticsReactHelper {

public static WritableMap convertJsonToMap(JSONObject jsonObject) throws JSONException {
WritableMap map = new WritableNativeMap();

Iterator<String> iterator = jsonObject.keys();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = jsonObject.get(key);
if (value instanceof JSONObject) {
map.putMap(key, convertJsonToMap((JSONObject) value));
} else if (value instanceof JSONArray) {
map.putArray(key, convertJsonToArray((JSONArray) value));
} else if (value instanceof Boolean) {
map.putBoolean(key, (Boolean) value);
} else if (value instanceof Integer) {
map.putInt(key, (Integer) value);
} else if (value instanceof Double) {
map.putDouble(key, (Double) value);
} else if (value instanceof String) {
map.putString(key, (String) value);
} else {
map.putString(key, value.toString());
}
}
return map;
}

public static WritableArray convertJsonToArray(JSONArray jsonArray) throws JSONException {
WritableArray array = new WritableNativeArray();

for (int i = 0; i < jsonArray.length(); i++) {
Object value = jsonArray.get(i);
if (value instanceof JSONObject) {
array.pushMap(convertJsonToMap((JSONObject) value));
} else if (value instanceof JSONArray) {
array.pushArray(convertJsonToArray((JSONArray) value));
} else if (value instanceof Boolean) {
array.pushBoolean((Boolean) value);
} else if (value instanceof Integer) {
array.pushInt((Integer) value);
} else if (value instanceof Double) {
array.pushDouble((Double) value);
} else if (value instanceof String) {
array.pushString((String) value);
} else {
array.pushString(value.toString());
}
}
return array;
}


public static JSONArray convertArrayToJson(ReadableArray readableArray) throws JSONException {
JSONArray array = new JSONArray();
for (int i = 0; i < readableArray.size(); i++) {
switch (readableArray.getType(i)) {
case Null:
break;
case Boolean:
array.put(readableArray.getBoolean(i));
break;
case Number:
array.put(readableArray.getDouble(i));
break;
case String:
array.put(readableArray.getString(i));
break;
case Map:
array.put(convertMapToJson(readableArray.getMap(i)));
break;
case Array:
array.put(convertArrayToJson(readableArray.getArray(i)));
break;
}
}
return array;
}

public static JSONObject convertMapToJson(ReadableMap readableMap) throws JSONException {
JSONObject object = new JSONObject();
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
switch (readableMap.getType(key)) {
case Null:
object.put(key, JSONObject.NULL);
break;
case Boolean:
object.put(key, readableMap.getBoolean(key));
break;
case Number:
object.put(key, readableMap.getDouble(key));
break;
case String:
object.put(key, readableMap.getString(key));
break;
case Map:
object.put(key, convertMapToJson(readableMap.getMap(key)));
break;
case Array:
object.put(key, convertArrayToJson(readableMap.getArray(key)));
break;
}
}
return object;
}

public static WritableMap getWritableMap(Map<String, String> map) {
WritableMap writeMap = Arguments.createMap();
if (map.isEmpty()) {
return writeMap;
}
try {
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
writeMap.putString((String) pair.getKey(), (String) pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
} catch (Throwable e) {
writeMap = Arguments.createMap();
}
return writeMap;
}

}
Loading

0 comments on commit d29d280

Please sign in to comment.