Skip to content

Commit

Permalink
Merge pull request #79 from devshackio/android-fixes
Browse files Browse the repository at this point in the history
Android fixes - convert firebase longs to doubles, implement ServerValue.TIMESTAMP
  • Loading branch information
auser authored Oct 21, 2016
2 parents df340f1 + 887be01 commit c5092b9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,8 @@ private <Any> Any castSnapshotValue(DataSnapshot snapshot) {
case "java.lang.Boolean":
data.putBoolean(child.getKey(), (Boolean) castedChild);
break;
case "java.lang.Integer":
data.putInt(child.getKey(), (Integer) castedChild);
case "java.lang.Long":
data.putDouble(child.getKey(), (Long) castedChild);
break;
case "java.lang.Double":
data.putDouble(child.getKey(), (Double) castedChild);
Expand All @@ -704,7 +704,7 @@ private <Any> Any castSnapshotValue(DataSnapshot snapshot) {
case "java.lang.Boolean":
return (Any)((Boolean) snapshot.getValue());
case "java.lang.Long":
return (Any)((Integer)(((Long) snapshot.getValue()).intValue()));
return (Any) ((Long) snapshot.getValue());
case "java.lang.Double":
return (Any)((Double) snapshot.getValue());
case "java.lang.String":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,13 @@ public String setKeyOrDefault(

@ReactMethod
public void serverValue(@Nullable final Callback onComplete) {
WritableMap timestampMap = Arguments.createMap();
for (Map.Entry<String, String> entry : ServerValue.TIMESTAMP.entrySet()) {
timestampMap.putString(entry.getKey(), entry.getValue());
}

WritableMap map = Arguments.createMap();
// TODO
map.putString("TIMESTAMP", "ServerValue.TIMESTAMP");
map.putMap("TIMESTAMP", timestampMap);
onComplete.invoke(null, map);
}

Expand Down
36 changes: 21 additions & 15 deletions android/src/main/java/io/fullstack/firestack/FirestackUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,27 @@ public static WritableMap dataSnapshotToMap(String name,
data.putBoolean("hasChildren", dataSnapshot.hasChildren());

data.putDouble("childrenCount", dataSnapshot.getChildrenCount());
if (!dataSnapshot.hasChildren() && dataSnapshot.getValue() != null) {
String type = dataSnapshot.getValue().getClass().getName();
if (!dataSnapshot.hasChildren()) {
Object value = dataSnapshot.getValue();
String type = value!=null ? value.getClass().getName() : "";
switch (type) {
case "java.lang.Boolean":
data.putBoolean("value", (Boolean) dataSnapshot.getValue());
data.putBoolean("value", (Boolean)value);
break;
case "java.lang.Long":
data.putInt("value",(Integer)(((Long) dataSnapshot.getValue()).intValue()));
Long longVal = (Long) value;
data.putDouble("value", (double)longVal);
break;
case "java.lang.Double":
data.putDouble("value",(Double) dataSnapshot.getValue());
data.putDouble("value", (Double) value);
break;
case "java.lang.String":
data.putString("value",(String) dataSnapshot.getValue());
data.putString("value",(String) value);
break;
default:
data.putString("value", null);
}
}else{
} else{
WritableMap valueMap = FirestackUtils.castSnapshotValue(dataSnapshot);
data.putMap("value", valueMap);
}
Expand Down Expand Up @@ -104,8 +106,9 @@ public static <Any> Any castSnapshotValue(DataSnapshot snapshot) {
case "java.lang.Boolean":
data.putBoolean(child.getKey(), (Boolean) castedChild);
break;
case "java.lang.Integer":
data.putInt(child.getKey(), (Integer) castedChild);
case "java.lang.Long":
Long longVal = (Long) castedChild;
data.putDouble(child.getKey(), (double)longVal);
break;
case "java.lang.Double":
data.putDouble(child.getKey(), (Double) castedChild);
Expand All @@ -116,6 +119,9 @@ public static <Any> Any castSnapshotValue(DataSnapshot snapshot) {
case "com.facebook.react.bridge.WritableNativeMap":
data.putMap(child.getKey(), (WritableMap) castedChild);
break;
default:
Log.w(TAG, "Invalid type: "+type);
break;
}
}
return (Any) data;
Expand All @@ -124,19 +130,19 @@ public static <Any> Any castSnapshotValue(DataSnapshot snapshot) {
String type = snapshot.getValue().getClass().getName();
switch (type) {
case "java.lang.Boolean":
return (Any)((Boolean) snapshot.getValue());
return (Any)(snapshot.getValue());
case "java.lang.Long":
return (Any)((Integer)(((Long) snapshot.getValue()).intValue()));
return (Any)(snapshot.getValue());
case "java.lang.Double":
return (Any)((Double) snapshot.getValue());
return (Any)(snapshot.getValue());
case "java.lang.String":
return (Any)((String) snapshot.getValue());
return (Any)(snapshot.getValue());
default:
Log.w(TAG, "Invalid type: "+type);
return (Any) null;
}
} else {
return (Any) null;
}
return (Any) null;
}
}

Expand Down

0 comments on commit c5092b9

Please sign in to comment.