forked from aksonov/react-native-router-flux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix to support complex types in Android version for GoogleTagManager.…
…pushDataLayerEvent (aksonov#91) * Fix issue around complex types https://github.com/idehub/react-native-google-analytics-bridge/issues/90 * Remove previous commented out method
- Loading branch information
1 parent
c8bb460
commit 5365c85
Showing
2 changed files
with
83 additions
and
12 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
android/src/main/java/com/idehub/GoogleAnalyticsBridge/ConvertReadableToMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.idehub.GoogleAnalyticsBridge; | ||
|
||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.bridge.ReadableType; | ||
import com.facebook.react.bridge.ReadableArray; | ||
import com.facebook.react.bridge.ReadableMapKeySetIterator; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* Converts a ReadableMap to a HashMap<String, Object> | ||
*/ | ||
public class ConvertReadableToMap { | ||
|
||
public static HashMap<String, Object> getMap(ReadableMap dictionary) { | ||
HashMap<String, Object> parsedMap = new HashMap<String, Object>(); | ||
ReadableMapKeySetIterator iterator = dictionary.keySetIterator(); | ||
while (iterator.hasNextKey()) { | ||
String key = iterator.nextKey(); | ||
ReadableType type = dictionary.getType(key); | ||
switch (type) { | ||
case Null: | ||
parsedMap.put(key, null); | ||
break; | ||
case Boolean: | ||
parsedMap.put(key, dictionary.getBoolean(key)); | ||
break; | ||
case Number: | ||
parsedMap.put(key, dictionary.getDouble(key)); | ||
break; | ||
case String: | ||
parsedMap.put(key, dictionary.getString(key)); | ||
break; | ||
case Map: | ||
parsedMap.put(key, ConvertReadableToMap.getMap(dictionary.getMap(key))); | ||
break; | ||
case Array: | ||
parsedMap.put(key, ConvertReadableToMap.getArray(dictionary.getArray(key))); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Could not convert of type: " + type + " and key: " + key + "."); | ||
} | ||
} | ||
return parsedMap; | ||
} | ||
|
||
private static List<Object> getArray(ReadableArray readableArray) { | ||
List<Object> parsedList = new ArrayList<Object>(readableArray.size()); | ||
for (int i = 0; i < readableArray.size(); i++) { | ||
ReadableType type = readableArray.getType(i); | ||
switch(type) { | ||
case Null: | ||
parsedList.add(i, null); | ||
break; | ||
case Boolean: | ||
parsedList.add(i, readableArray.getBoolean(i)); | ||
break; | ||
case Number: | ||
parsedList.add(i, readableArray.getDouble(i)); | ||
break; | ||
case String: | ||
parsedList.add(i, readableArray.getString(i)); | ||
break; | ||
case Map: | ||
parsedList.add(i, ConvertReadableToMap.getMap(readableArray.getMap(i))); | ||
break; | ||
case Array: | ||
parsedList.add(i, ConvertReadableToMap.getArray(readableArray.getArray(i))); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Could not convert of type: " + type + " and index: " + i + "."); | ||
} | ||
} | ||
return parsedList; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters