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

The following examples show how to use com.google.firebase.database.DataSnapshot#getKey() . 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: DumpFeedback.java    From white-label-event-app with Apache License 2.0 6 votes vote down vote up
private void writeSessionRow(final DataSnapshot session) throws IOException {
    final String session_id = session.getKey();
    for (final DataSnapshot session_user : session.getChildren()) {
        writer.write(session_id);
        final String user_id = session_user.getKey();
        writer.write('\t');
        writer.write(user_id);
        for (DataSnapshot rating : session_user.getChildren()) {
            writer.write('\t');
            final Object value = rating.getValue();
            if (value != null) {
                writer.write(value.toString());
            }
        }
        writer.write('\n');
    }
}
 
Example 2
Source File: FavSessionButtonManager.java    From white-label-event-app with Apache License 2.0 5 votes vote down vote up
@Override
public void onChildChanged(DataSnapshot data, String previousChildName) {
    final String session_id = data.getKey();
    boolean is_fav = data.getValue(Boolean.class);
    if (is_fav) {
        favorites.add(session_id);
    }
    else {
        favorites.remove(session_id);
    }
    updateButtons(session_id);
}
 
Example 3
Source File: GeoQuery.java    From geofire-java with MIT License 5 votes vote down vote up
private void childChanged(DataSnapshot dataSnapshot) {
    GeoLocation location = GeoFire.getLocationValue(dataSnapshot);
    if (location != null) {
        this.updateLocationInfo(dataSnapshot, location);
    } else {
        throw new AssertionError("Got Datasnapshot without location with key " + dataSnapshot.getKey());
    }
}
 
Example 4
Source File: GeoQuery.java    From geofire-java with MIT License 5 votes vote down vote up
private void childAdded(DataSnapshot dataSnapshot) {
    GeoLocation location = GeoFire.getLocationValue(dataSnapshot);
    if (location != null) {
        this.updateLocationInfo(dataSnapshot, location);
    } else {
        throw new AssertionError("Got Datasnapshot without location with key " + dataSnapshot.getKey());
    }
}
 
Example 5
Source File: FirebaseIndexArray.java    From FirebaseUI-Android with Apache License 2.0 5 votes vote down vote up
@Override
public void onDataChange(DataSnapshot snapshot) {
    String key = snapshot.getKey();
    int index = currentIndex = returnOrFindIndexForKey(currentIndex, key);

    if (snapshot.getValue() != null) {
        if (isKeyAtIndex(key, index)) {
            // We already know about this data, just update it
            mDataSnapshots.set(index, snapshot);
            notifyOnChildChanged(ChangeEventType.CHANGED, snapshot, index, -1);
        } else {
            // We don't already know about this data, add it
            mDataSnapshots.add(index, snapshot);
            notifyOnChildChanged(ChangeEventType.ADDED, snapshot, index, -1);
        }
    } else {
        if (isKeyAtIndex(key, index)) {
            // This data has disappeared, remove it
            mDataSnapshots.remove(index);
            notifyOnChildChanged(ChangeEventType.REMOVED, snapshot, index, -1);
        } else {
            // Data does not exist
            Log.w(TAG, "Key not found at ref: " + snapshot.getRef());
        }
    }

    // In theory, we would only want to pop the queue if this listener was just added
    // i.e. `snapshot.value != null && isKeyAtIndex(...)`. However, if the developer makes a
    // mistake and `snapshot.value == null`, we will never pop the queue and
    // `notifyOnDataChanged()` will never be called. Thus, we pop the queue anytime
    // an update is received.
    mKeysWithPendingUpdate.remove(key);
    if (mKeysWithPendingUpdate.isEmpty()) notifyOnDataChanged();
}
 
Example 6
Source File: FirebaseIndexArray.java    From FirebaseUI-Android with Apache License 2.0 5 votes vote down vote up
private void onKeyRemoved(DataSnapshot data, int index) {
    String key = data.getKey();
    ValueEventListener listener = mRefs.remove(mDataRef.getRef().child(key));
    if (listener != null) mDataRef.child(key).removeEventListener(listener);

    int realIndex = returnOrFindIndexForKey(index, key);
    if (isKeyAtIndex(key, realIndex)) {
        DataSnapshot snapshot = mDataSnapshots.remove(realIndex);
        mHasPendingMoveOrDelete = true;
        notifyOnChildChanged(ChangeEventType.REMOVED, snapshot, realIndex, -1);
    }
}
 
Example 7
Source File: FirebaseIndexArray.java    From FirebaseUI-Android with Apache License 2.0 5 votes vote down vote up
private void onKeyMoved(DataSnapshot data, int index, int oldIndex) {
    String key = data.getKey();

    // We can't use `returnOrFindIndexForKey(...)` for `oldIndex` or it might find the updated
    // index instead of the old one. Unfortunately, this does mean move events will be
    // incorrectly ignored if our list is a subset of the key list e.g. a key has null data.
    if (isKeyAtIndex(key, oldIndex)) {
        DataSnapshot snapshot = mDataSnapshots.remove(oldIndex);
        int realIndex = returnOrFindIndexForKey(index, key);
        mHasPendingMoveOrDelete = true;

        mDataSnapshots.add(realIndex, snapshot);
        notifyOnChildChanged(ChangeEventType.MOVED, snapshot, realIndex, oldIndex);
    }
}
 
Example 8
Source File: FirebaseIndexArray.java    From FirebaseUI-Android with Apache License 2.0 5 votes vote down vote up
private void onKeyAdded(DataSnapshot data, int newIndex) {
    String key = data.getKey();
    DatabaseReference ref = mDataRef.child(key);

    mKeysWithPendingUpdate.add(key);
    // Start listening
    mRefs.put(ref, ref.addValueEventListener(new DataRefListener(newIndex)));
}
 
Example 9
Source File: GeoQuery.java    From geofire-android with Apache License 2.0 5 votes vote down vote up
private void childAdded(DataSnapshot dataSnapshot) {
    GeoLocation location = GeoFire.getLocationValue(dataSnapshot);
    if (location != null) {
        this.updateLocationInfo(dataSnapshot, location);
    } else {
        throw new AssertionError("Got Datasnapshot without location with key " + dataSnapshot.getKey());
    }
}
 
Example 10
Source File: FavSessionButtonManager.java    From white-label-event-app with Apache License 2.0 5 votes vote down vote up
@Override
public void onChildAdded(DataSnapshot data, String previousChildName) {
    final String session_id = data.getKey();
    boolean is_fav = data.getValue(Boolean.class);
    if (is_fav) {
        favorites.add(session_id);
    }
    else {
        favorites.remove(session_id);
    }
    updateButtons(session_id);
}
 
Example 11
Source File: DataSnapshotMapper.java    From RxFirebase with Apache License 2.0 5 votes vote down vote up
@Override
public RxFirebaseChildEvent<U> call(final RxFirebaseChildEvent<DataSnapshot> rxFirebaseChildEvent) {
    DataSnapshot dataSnapshot = rxFirebaseChildEvent.getValue();
    if (dataSnapshot.exists()) {
        return new RxFirebaseChildEvent<U>(
                dataSnapshot.getKey(),
                getDataSnapshotTypedValue(dataSnapshot, clazz),
                rxFirebaseChildEvent.getPreviousChildName(),
                rxFirebaseChildEvent.getEventType());
    } else {
        throw Exceptions.propagate(new RuntimeException("child dataSnapshot doesn't exist"));
    }
}
 
Example 12
Source File: ContactsSynchronizer.java    From chat21-android-sdk with GNU Affero General Public License v3.0 5 votes vote down vote up
public static IChatUser decodeContactSnapShop(DataSnapshot dataSnapshot) throws ChatFieldNotFoundException {
    Log.v(DEBUG_CONTACTS_SYNC, "decodeContactSnapShop called");

    Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();

    String contactId = dataSnapshot.getKey();

    String uid = (String) map.get("uid");
    if (uid == null) {
        throw new ChatFieldNotFoundException("Required uid field is null for contact id : " + contactId);
    }

    String firstName = (String) map.get("firstname");
    String lastName = (String) map.get("lastname");
    String imageUrl = (String) map.get("imageurl");
    String email = (String) map.get("email");


    Long timestamp = null;
    if (map.containsKey("timestamp")) {
        timestamp = (Long) map.get("timestamp");
    }

    IChatUser contact = new ChatUser();
    contact.setId(uid);
    contact.setFullName(firstName + " " + lastName);
    contact.setProfilePictureUrl(imageUrl);
    contact.setEmail(email);

    Log.v(DEBUG_CONTACTS_SYNC, "decodeContactSnapShop.contact : " + contact);

    return contact;
}
 
Example 13
Source File: FirebaseQueryLiveDataSet.java    From budgetto with MIT License 5 votes vote down vote up
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
    T item = dataSnapshot.getValue(genericTypeClass);
    String key = dataSnapshot.getKey();

    int index = liveDataSetIndexes.indexOf(key);
    if(index == -1) return;
    liveDataSetEntries.remove(index);
    liveDataSetIndexes.remove(index);
    int newPosition;
    if (previousChildName == null) {
        liveDataSetEntries.add(0, item);
        liveDataSetIndexes.add(0, key);
        newPosition = 0;
    } else {
        int previousIndex = liveDataSetIndexes.indexOf(previousChildName);
        int nextIndex = previousIndex + 1;
        if (nextIndex == liveDataSetEntries.size()) {
            liveDataSetEntries.add(item);
            liveDataSetIndexes.add(key);
        } else {
            liveDataSetEntries.add(nextIndex, item);
            liveDataSetIndexes.add(nextIndex, key);
        }
        newPosition = nextIndex;
    }
    liveDataSet.setItemMoved(index, newPosition);
    setValue(new FirebaseElement<>(liveDataSet));


}
 
Example 14
Source File: FirebaseQueryLiveDataSet.java    From budgetto with MIT License 5 votes vote down vote up
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
    String key = dataSnapshot.getKey();

    if (liveDataSetIndexes.contains(key)) {
        int index = liveDataSetIndexes.indexOf(key);
        if (index == -1) return;
        T item = liveDataSetEntries.get(index);

        liveDataSetIndexes.remove(index);
        liveDataSetEntries.remove(index);
        liveDataSet.setItemRemoved(index);
        setValue(new FirebaseElement<>(liveDataSet));
    }
}
 
Example 15
Source File: FirebaseQueryLiveDataSet.java    From budgetto with MIT License 5 votes vote down vote up
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
    T item = dataSnapshot.getValue(genericTypeClass);
    String key = dataSnapshot.getKey();

    if (liveDataSetIndexes.contains(key)) {
        int index = liveDataSetIndexes.indexOf(key);
        T oldItem = liveDataSetEntries.get(index);
        liveDataSetEntries.set(index, item);
        liveDataSet.setItemChanged(index);
        setValue(new FirebaseElement<>(liveDataSet));

    }
}
 
Example 16
Source File: FirebaseQueryLiveDataSet.java    From budgetto with MIT License 5 votes vote down vote up
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
    T item = dataSnapshot.getValue(genericTypeClass);

    String key = dataSnapshot.getKey();
    if (!liveDataSetIndexes.contains(key)) {
        int insertedPosition;
        if (previousChildName == null) {
            liveDataSetEntries.add(0, item);
            liveDataSetIndexes.add(0, key);
            insertedPosition = 0;
        } else {
            int previousIndex = liveDataSetIndexes.indexOf(previousChildName);
            int nextIndex = previousIndex + 1;
            if (nextIndex == liveDataSetEntries.size()) {
                liveDataSetEntries.add(item);
                liveDataSetIndexes.add(key);
            } else {
                liveDataSetEntries.add(nextIndex, item);
                liveDataSetIndexes.add(nextIndex, key);
            }
            insertedPosition = nextIndex;
        }

        liveDataSet.setItemInserted(insertedPosition);
        setValue(new FirebaseElement<>(liveDataSet));
    }
}
 
Example 17
Source File: GeoQuery.java    From geofire-android with Apache License 2.0 5 votes vote down vote up
private void childChanged(DataSnapshot dataSnapshot) {
    GeoLocation location = GeoFire.getLocationValue(dataSnapshot);
    if (location != null) {
        this.updateLocationInfo(dataSnapshot, location);
    } else {
        throw new AssertionError("Got Datasnapshot without location with key " + dataSnapshot.getKey());
    }
}
 
Example 18
Source File: FavSessionButtonManager.java    From white-label-event-app with Apache License 2.0 4 votes vote down vote up
@Override
public void onChildRemoved(DataSnapshot data) {
    final String session_id = data.getKey();
    favorites.remove(session_id);
    updateButtons(session_id);
}
 
Example 19
Source File: ConversationMessagesHandler.java    From chat21-android-sdk with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
     * @param dataSnapshot the datasnapshot to decode
     * @return the decoded message
     */
    public static Message decodeMessageSnapShop(DataSnapshot dataSnapshot) throws ChatFieldNotFoundException {
        Log.v(TAG, "decodeMessageSnapShop called");

        Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();

        String messageId = dataSnapshot.getKey();

        String sender = (String) map.get("sender");
        if (sender == null) {
            throw new ChatFieldNotFoundException("Required sender field is null for message id : " + messageId);
        }

        String recipient = (String) map.get("recipient");
        if (recipient == null) {
            throw new ChatFieldNotFoundException("Required recipient field is null for message id : " + messageId);
        }

        String sender_fullname = (String) map.get("sender_fullname");
        String recipient_fullname = (String) map.get("recipient_fullname");

        Long status = null;
        if (map.containsKey("status")) {
            status = (Long) map.get("status");
        }

        String text = (String) map.get("text");

        Long timestamp = null;
        if (map.containsKey("timestamp")) {
            timestamp = (Long) map.get("timestamp");
        }

        String type = (String) map.get("type");

        String channelType = (String) map.get("channel_type");

        // if metadata is a string ignore it
        Map<String, Object> metadata = null;
        if (map.containsKey("metadata") && !(map.get("metadata") instanceof String)) {
            metadata = (Map<String, Object>) map.get("metadata");
        }

        // if metadata is a string ignore it
        Map<String, Object> attributes = null;
        if (map.containsKey("attributes") && !(map.get("attributes") instanceof String)) {
            attributes = (Map<String, Object>) map.get("attributes");
        }

        Message message = new Message();

        message.setId(messageId);
        message.setSender(sender);
        message.setSenderFullname(sender_fullname);
        message.setRecipient(recipient);
        message.setRecipientFullname(recipient_fullname);
        message.setStatus(status);
        message.setText(text);
        message.setTimestamp(timestamp);
        message.setType(type);
        message.setChannelType(channelType);
        if (metadata != null) message.setMetadata(metadata);
        if (attributes != null) message.setAttributes(attributes);

        Log.v(TAG, "decodeMessageSnapShop.message : " + message);

//        Log.d(TAG, "message >: " + dataSnapshot.toString());

        return message;
    }
 
Example 20
Source File: CachingSnapshotParser.java    From FirebaseUI-Android with Apache License 2.0 4 votes vote down vote up
@NonNull
@Override
public String getId(@NonNull DataSnapshot snapshot) {
    return snapshot.getKey();
}