Search in sources :

Example 1 with DataSnapshot

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);
    }
}
Also used : DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 2 with DataSnapshot

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) {
        }
    });
}
Also used : Comment(com.google.firebase.quickstart.database.models.Comment) User(com.google.firebase.quickstart.database.models.User) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 3 with DataSnapshot

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);
    }
}
Also used : FirebaseUser(com.google.firebase.auth.FirebaseUser) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 4 with DataSnapshot

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);
            }
        }
    });
}
Also used : ChangeEventListenerBase(com.supercilex.robotscouter.util.DatabaseHelper.ChangeEventListenerBase) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 5 with DataSnapshot

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) {
        }
    });
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Aggregations

DataSnapshot (com.google.firebase.database.DataSnapshot)47 DatabaseError (com.google.firebase.database.DatabaseError)40 ValueEventListener (com.google.firebase.database.ValueEventListener)34 DatabaseReference (com.google.firebase.database.DatabaseReference)14 View (android.view.View)13 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)11 RecyclerView (android.support.v7.widget.RecyclerView)11 Intent (android.content.Intent)10 Bundle (android.os.Bundle)10 User (com.polito.mad17.madmax.entities.User)10 MutableData (com.google.firebase.database.MutableData)6 Transaction (com.google.firebase.database.Transaction)6 SimpleDateFormat (java.text.SimpleDateFormat)6 Event (com.polito.mad17.madmax.entities.Event)5 ChatMessageHelper (ingage.ingage20.helpers.ChatMessageHelper)5 HashMap (java.util.HashMap)5 TextView (android.widget.TextView)4 ArrayList (java.util.ArrayList)4 ActionBar (android.support.v7.app.ActionBar)3 MenuItem (android.view.MenuItem)3