Java Code Examples for com.google.firebase.database.DataSnapshot#hasChildren()

The following examples show how to use com.google.firebase.database.DataSnapshot#hasChildren() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestorePresenter.java    From FastAccess with GNU General Public License v3.0 6 votes vote down vote up
@Override public void onDataChange(DataSnapshot dataSnapshot) {
    if (!isAttached()) return;
    if (dataSnapshot != null && dataSnapshot.hasChildren()) {
        FirebaseUser user = getView().user();
        if (InputHelper.isEmpty(userId)) {
            if (user != null) userId = user.getUid();
        }
        if (userId == null) {
            getView().onHideProgress();
            getView().onShowMessage(R.string.login_first_msg);
            getView().finishOnError();
            return;
        }
        Logger.e(dataSnapshot);
        BackupRestoreModel.restore(dataSnapshot.getValue(BackupRestoreModel.class));
        getView().onHideProgress();
        getView().onRestoreCompleted();
    } else {
        getView().onHideProgress();
        getView().onShowMessage(R.string.no_data_to_restore);
        getView().finishOnError();
    }
}
 
Example 2
Source File: FirestackUtils.java    From react-native-firestack with MIT License 5 votes vote down vote up
public static WritableArray getChildKeys(DataSnapshot snapshot) {
  WritableArray childKeys = Arguments.createArray();

  if (snapshot.hasChildren()) {
    for (DataSnapshot child : snapshot.getChildren()) {
      childKeys.pushString(child.getKey());
    }
  }

  return childKeys;
}
 
Example 3
Source File: FirestackDatabase.java    From react-native-firestack with MIT License 4 votes vote down vote up
private <Any> Any castSnapshotValue(DataSnapshot snapshot) {
  if (snapshot.hasChildren()) {
    WritableMap data = Arguments.createMap();
    for (DataSnapshot child : snapshot.getChildren()) {
      Any castedChild = castSnapshotValue(child);
      switch (castedChild.getClass().getName()) {
        case "java.lang.Boolean":
          data.putBoolean(child.getKey(), (Boolean) castedChild);
          break;
        case "java.lang.Long":
          data.putDouble(child.getKey(), (Long) castedChild);
          break;
        case "java.lang.Double":
          data.putDouble(child.getKey(), (Double) castedChild);
          break;
        case "java.lang.String":
          data.putString(child.getKey(), (String) castedChild);
          break;
        case "com.facebook.react.bridge.WritableNativeMap":
          data.putMap(child.getKey(), (WritableMap) castedChild);
          break;
      }
    }
    return (Any) data;
  } else {
    if (snapshot.getValue() != null) {
      String type = snapshot.getValue().getClass().getName();
      switch (type) {
        case "java.lang.Boolean":
          return (Any)((Boolean) snapshot.getValue());
        case "java.lang.Long":
          return (Any) ((Long) snapshot.getValue());
        case "java.lang.Double":
          return (Any)((Double) snapshot.getValue());
        case "java.lang.String":
          return (Any)((String) snapshot.getValue());
        default:
          return (Any) null;
      }
    } else {
      return (Any) null;
    }
  }
}
 
Example 4
Source File: FirestackUtils.java    From react-native-firestack with MIT License 4 votes vote down vote up
public static WritableMap dataSnapshotToMap(String name, 
  String path, 
  DataSnapshot dataSnapshot) {
    WritableMap data = Arguments.createMap();

    data.putString("key", dataSnapshot.getKey());
    data.putBoolean("exists", dataSnapshot.exists());
    data.putBoolean("hasChildren", dataSnapshot.hasChildren());

    data.putDouble("childrenCount", dataSnapshot.getChildrenCount());
    if (!dataSnapshot.hasChildren()) {
      Object value = dataSnapshot.getValue();
      String type = value!=null ? value.getClass().getName() : "";
      switch (type) {
        case "java.lang.Boolean":
          data.putBoolean("value", (Boolean)value);
          break;
        case "java.lang.Long":
          Long longVal = (Long) value;
          data.putDouble("value", (double)longVal);
          break;
        case "java.lang.Double":
          data.putDouble("value", (Double) value);
          break;
        case "java.lang.String":
          data.putString("value",(String) value);
          break;
        default:
          data.putString("value", null);
      }
    } else{
      WritableMap valueMap = FirestackUtils.castSnapshotValue(dataSnapshot);
      data.putMap("value", valueMap);
    }

    // Child keys
    WritableArray childKeys = FirestackUtils.getChildKeys(dataSnapshot);
    data.putArray("childKeys", childKeys);

    Object priority = dataSnapshot.getPriority();
    if (priority == null) {
      data.putString("priority", null);
    } else {
      data.putString("priority", priority.toString());
    }

    WritableMap eventMap = Arguments.createMap();
    eventMap.putString("eventName", name);
    eventMap.putMap("snapshot", data);
    eventMap.putString("path", path);
    return eventMap;
}
 
Example 5
Source File: FirestackUtils.java    From react-native-firestack with MIT License 4 votes vote down vote up
public static <Any> Any castSnapshotValue(DataSnapshot snapshot) {
  if (snapshot.hasChildren()) {
    WritableMap data = Arguments.createMap();
    for (DataSnapshot child : snapshot.getChildren()) {
      Any castedChild = castSnapshotValue(child);
      switch (castedChild.getClass().getName()) {
        case "java.lang.Boolean":
          data.putBoolean(child.getKey(), (Boolean) castedChild);
          break;
        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);
          break;
        case "java.lang.String":
          data.putString(child.getKey(), (String) castedChild);
          break;
        case "com.facebook.react.bridge.WritableNativeMap":
          data.putMap(child.getKey(), (WritableMap) castedChild);
          break;
        default:
          Log.w(TAG, "Invalid type: " + castedChild.getClass().getName());
          break;
      }
    }
    return (Any) data;
  } else {
    if (snapshot.getValue() != null) {
      String type = snapshot.getValue().getClass().getName();
      switch (type) {
        case "java.lang.Boolean":
          return (Any)(snapshot.getValue());
        case "java.lang.Long":
          return (Any)(snapshot.getValue());
        case "java.lang.Double":
          return (Any)(snapshot.getValue());
        case "java.lang.String":
          return (Any)(snapshot.getValue());
        default:
          Log.w(TAG, "Invalid type: "+type);
          return (Any) null;
      }
    }
    return (Any) null;
  }
}