use of com.google.firebase.database.DataSnapshot in project FirebaseUI-Android by firebase.
the class FirebaseIndexArray method onChildMoved.
@Override
public void onChildMoved(DataSnapshot keySnapshot, String previousChildKey) {
String key = keySnapshot.getKey();
int oldIndex = getIndexForKey(key);
super.setOnChangedListener(null);
super.onChildMoved(keySnapshot, previousChildKey);
super.setOnChangedListener(mListener);
if (isKeyAtIndex(key, oldIndex)) {
DataSnapshot snapshot = mDataSnapshots.remove(oldIndex);
int newIndex = getIndexForKey(key);
mDataSnapshots.add(newIndex, snapshot);
notifyChangedListeners(ChangeEventListener.EventType.MOVED, newIndex, oldIndex);
}
}
use of com.google.firebase.database.DataSnapshot in project quickstart-android by firebase.
the class PostDetailActivity method postComment.
private void postComment() {
final String uid = getUid();
FirebaseDatabase.getInstance().getReference().child("users").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get user information
User user = dataSnapshot.getValue(User.class);
String authorName = user.username;
// Create new comment object
String commentText = mCommentField.getText().toString();
Comment comment = new Comment(uid, authorName, commentText);
// Push the comment, it will appear in the list
mCommentsReference.push().setValue(comment);
// Clear the field
mCommentField.setText(null);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
use of com.google.firebase.database.DataSnapshot in project SocialRec by Jkuras.
the class MainActivity method setUser.
/*
This function sets the database to reference the current users User profile info
When it is called, a data listener is set to the corrent db reference, and
if there is indeed a user signed in, it will set the variable mUser equal to their
user information.
*/
public void setUser() {
if (mAuth.getCurrentUser() != null) {
mfirebaseUser = mAuth.getCurrentUser();
mDataBase = FirebaseDatabase.getInstance();
mref = mDataBase.getReference("users/" + mfirebaseUser.getUid());
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mUser = dataSnapshot.getValue(User.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
};
mref.addValueEventListener(postListener);
}
}
use of com.google.firebase.database.DataSnapshot in project Robot-Scouter by SUPERCILEX.
the class AppIndexingService method onDataChange.
@Override
public void onDataChange(DataSnapshot snapshot) {
long numOfExpectedTeams = snapshot.getChildrenCount();
if (numOfExpectedTeams == 0) {
FirebaseAppIndex.getInstance().update();
return;
}
Constants.sFirebaseTeams.addChangeEventListener(new ChangeEventListenerBase() {
@Override
public void onChildChanged(EventType type, DataSnapshot snapshot, int index, int oldIndex) {
if (type == EventType.ADDED) {
mIndexables.add(Constants.sFirebaseTeams.getObject(index).getHelper().getIndexable());
}
if (mIndexables.size() >= numOfExpectedTeams) {
FirebaseAppIndex.getInstance().update(mIndexables.toArray(new Indexable[mIndexables.size()]));
Constants.sFirebaseTeams.removeChangeEventListener(this);
}
}
});
}
use of com.google.firebase.database.DataSnapshot in project MadMax by deviz92.
the class PayExpenseActivity method payDebtForExpense.
//money = cifra che ho a disposizione per ripianare i debiti
void payDebtForExpense(final String userID, final String expenseID, Double money) {
myMoney = money;
databaseReference.child("expenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String creatorID = dataSnapshot.child("creatorID").getValue(String.class);
Double alreadyPaidByCreator = dataSnapshot.child("participants").child(creatorID).child("alreadyPaid").getValue(Double.class);
//dice se user contribuisce o no a quella spesa
Boolean involved = false;
for (DataSnapshot participantSnapshot : dataSnapshot.child("participants").getChildren()) {
//todo poi gestire caso in cui utente viene tolto dai participant alla spesa
if (participantSnapshot.getKey().equals(userID))
involved = true;
}
//se user ha partecipato alla spesa
if (involved) {
//alreadyPaid = soldi già messi dallo user per quella spesa
//dueImport = quota che user deve mettere per quella spesa
Double alreadyPaid = dataSnapshot.child("participants").child(userID).child("alreadyPaid").getValue(Double.class);
Log.d(TAG, "Fraction: " + Double.parseDouble(String.valueOf(dataSnapshot.child("participants").child(userID).child("fraction").getValue())));
Double amount = dataSnapshot.child("amount").getValue(Double.class);
Double dueImport = Double.parseDouble(String.valueOf(dataSnapshot.child("participants").child(userID).child("fraction").getValue())) * amount;
Double stillToPay = dueImport - alreadyPaid;
//Se questa spesa non è già stata ripagata in toto
if (stillToPay > 0) {
//Se ho ancora abbastanza soldi per ripagare in toto questa spesa, la ripago in toto AL CREATOR!!
if (myMoney >= stillToPay) {
//Quota già pagata DA ME per questa spesa aumenta
databaseReference.child("expenses").child(expenseID).child("participants").child(userID).child("alreadyPaid").setValue(dueImport);
//Quota già pagata DAL CREATOR per questa spesa diminuisce, perchè gli sto dando dei soldi
databaseReference.child("expenses").child(expenseID).child("participants").child(creatorID).child("alreadyPaid").setValue(alreadyPaidByCreator - stillToPay);
//Adesso ho meno soldi a disposizione, perchè li ho usati in parte per ripagare questa spesa
myMoney -= stillToPay;
} else //Altrimenti la ripago solo in parte
{
databaseReference.child("expenses").child(expenseID).child("participants").child(userID).child("alreadyPaid").setValue(alreadyPaid + myMoney);
databaseReference.child("expenses").child(expenseID).child("participants").child(creatorID).child("alreadyPaid").setValue(alreadyPaidByCreator - myMoney);
myMoney = 0d;
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Aggregations