Search in sources :

Example 1 with DatabaseError

use of com.google.firebase.database.DatabaseError in project FirebaseUI-Android by firebase.

the class ChatActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);
    mAuth = FirebaseAuth.getInstance();
    mAuth.addAuthStateListener(this);
    mSendButton = (Button) findViewById(R.id.sendButton);
    mMessageEdit = (EditText) findViewById(R.id.messageEdit);
    mEmptyListMessage = (TextView) findViewById(R.id.emptyTextView);
    mRef = FirebaseDatabase.getInstance().getReference();
    mChatRef = mRef.child("chats");
    mSendButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String uid = mAuth.getCurrentUser().getUid();
            String name = "User " + uid.substring(0, 6);
            Chat chat = new Chat(name, mMessageEdit.getText().toString(), uid);
            mChatRef.push().setValue(chat, new DatabaseReference.CompletionListener() {

                @Override
                public void onComplete(DatabaseError error, DatabaseReference reference) {
                    if (error != null) {
                        Log.e(TAG, "Failed to write message", error.toException());
                    }
                }
            });
            mMessageEdit.setText("");
        }
    });
    mManager = new LinearLayoutManager(this);
    mManager.setReverseLayout(false);
    mMessages = (RecyclerView) findViewById(R.id.messagesList);
    mMessages.setHasFixedSize(false);
    mMessages.setLayoutManager(mManager);
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View)

Example 2 with DatabaseError

use of com.google.firebase.database.DatabaseError in project FirebaseUI-Android by firebase.

the class TestUtils method runAndWaitUntil.

public static void runAndWaitUntil(FirebaseArray array, Runnable task, Callable<Boolean> done) throws InterruptedException {
    final Semaphore semaphore = new Semaphore(0);
    array.setOnChangedListener(new ChangeEventListener() {

        @Override
        public void onChildChanged(ChangeEventListener.EventType type, int index, int oldIndex) {
            semaphore.release();
        }

        @Override
        public void onDataChanged() {
        }

        @Override
        public void onCancelled(DatabaseError error) {
            throw new IllegalStateException(error.toException());
        }
    });
    task.run();
    boolean isDone = false;
    long startedAt = System.currentTimeMillis();
    while (!isDone && System.currentTimeMillis() - startedAt < TIMEOUT) {
        semaphore.tryAcquire(1, TimeUnit.SECONDS);
        try {
            isDone = done.call();
        } catch (Exception e) {
            e.printStackTrace();
        // and we're not done
        }
    }
    assertTrue("Timed out waiting for expected results on FirebaseArray", isDone);
    array.setOnChangedListener(null);
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Semaphore(java.util.concurrent.Semaphore)

Example 3 with DatabaseError

use of com.google.firebase.database.DatabaseError 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 4 with DatabaseError

use of com.google.firebase.database.DatabaseError 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 5 with DatabaseError

use of com.google.firebase.database.DatabaseError 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

DatabaseError (com.google.firebase.database.DatabaseError)42 DataSnapshot (com.google.firebase.database.DataSnapshot)40 ValueEventListener (com.google.firebase.database.ValueEventListener)34 View (android.view.View)14 DatabaseReference (com.google.firebase.database.DatabaseReference)14 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)12 RecyclerView (android.support.v7.widget.RecyclerView)12 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 TextView (android.widget.TextView)5 Event (com.polito.mad17.madmax.entities.Event)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 ActionBar (android.support.v7.app.ActionBar)3 MenuItem (android.view.MenuItem)3 ImageView (android.widget.ImageView)3