Search in sources :

Example 96 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project MadMax by deviz92.

the class FirebaseUtils method removeExpenseFirebase.

public void removeExpenseFirebase(final String expenseID, final Context context) {
    databaseReference.child("expenses").child(expenseID).addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String groupID = dataSnapshot.child("groupID").getValue(String.class);
            // aggiunto da riky per mandare la notifica a tutti tranne a chi ha eliminato la spesa
            databaseReference.child("expenses").child(expenseID).child("deletedBy").setValue(MainActivity.getCurrentUID());
            // Elimino spesa dal gruppo
            databaseReference.child("groups").child(groupID).child("expenses").child(expenseID).setValue(false);
            // Per ogni participant elimino la spesa dal suo elenco spese
            for (DataSnapshot participantSnapshot : dataSnapshot.child("participants").getChildren()) {
                String participantID = participantSnapshot.getKey();
                databaseReference.child("users").child(participantID).child("expenses").child(expenseID).setValue(false);
            }
            // Elimino commenti sulla spesa
            databaseReference.child("comments").child(groupID).removeValue();
            // Elimino spesa
            databaseReference.child("expenses").child(expenseID).child("deleted").setValue(true);
            // Toast.makeText(context,context.getString(R.string.expense_removed),Toast.LENGTH_SHORT).show();
            // add event for EXPENSE_REMOVE
            databaseReference.child("expenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {

                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    User currentUser = MainActivity.getCurrentUser();
                    Event event = new Event(dataSnapshot.child("groupID").getValue(String.class), Event.EventType.EXPENSE_REMOVE, currentUser.getName() + " " + currentUser.getSurname(), dataSnapshot.child("description").getValue(String.class));
                    event.setDate(new SimpleDateFormat("yyyy.MM.dd").format(new java.util.Date()));
                    event.setTime(new SimpleDateFormat("HH:mm").format(new java.util.Date()));
                    FirebaseUtils.getInstance().addEvent(event);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.w(TAG, databaseError.toException());
                }
            });
        /*Intent intent = new Intent(context, GroupDetailActivity.class);
                intent.putExtra("groupID", groupID);
                intent.putExtra("userID", MainActivity.getCurrentUID());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);*/
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Also used : User(com.polito.mad17.madmax.entities.User) DatabaseError(com.google.firebase.database.DatabaseError) Event(com.polito.mad17.madmax.entities.Event) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot) SimpleDateFormat(java.text.SimpleDateFormat)

Example 97 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project quickstart-android by firebase.

the class NewPostActivity method submitPost.

private void submitPost() {
    final String title = mTitleField.getText().toString();
    final String body = mBodyField.getText().toString();
    // Title is required
    if (TextUtils.isEmpty(title)) {
        mTitleField.setError(REQUIRED);
        return;
    }
    // Body is required
    if (TextUtils.isEmpty(body)) {
        mBodyField.setError(REQUIRED);
        return;
    }
    // Disable button so there are no multi-posts
    setEditingEnabled(false);
    Toast.makeText(this, "Posting...", Toast.LENGTH_SHORT).show();
    // [START single_value_read]
    final String userId = getUid();
    mDatabase.child("users").child(userId).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get user value
            User user = dataSnapshot.getValue(User.class);
            // [START_EXCLUDE]
            if (user == null) {
                // User is null, error out
                Log.e(TAG, "User " + userId + " is unexpectedly null");
                Toast.makeText(NewPostActivity.this, "Error: could not fetch user.", Toast.LENGTH_SHORT).show();
            } else {
                // Write new post
                writeNewPost(userId, user.username, title, body);
            }
            // Finish this Activity, back to the stream
            setEditingEnabled(true);
            finish();
        // [END_EXCLUDE]
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, "getUser:onCancelled", databaseError.toException());
            // [START_EXCLUDE]
            setEditingEnabled(true);
        // [END_EXCLUDE]
        }
    });
// [END single_value_read]
}
Also used : 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 98 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project quickstart-android by firebase.

the class PostDetailActivity method onStart.

@Override
public void onStart() {
    super.onStart();
    // Add value event listener to the post
    // [START post_value_event_listener]
    ValueEventListener postListener = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get Post object and use the values to update the UI
            Post post = dataSnapshot.getValue(Post.class);
            // [START_EXCLUDE]
            mAuthorView.setText(post.author);
            mTitleView.setText(post.title);
            mBodyView.setText(post.body);
        // [END_EXCLUDE]
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Getting Post failed, log a message
            Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
            // [START_EXCLUDE]
            Toast.makeText(PostDetailActivity.this, "Failed to load post.", Toast.LENGTH_SHORT).show();
        // [END_EXCLUDE]
        }
    };
    mPostReference.addValueEventListener(postListener);
    // [END post_value_event_listener]
    // Keep copy of post listener so we can remove it when app stops
    mPostListener = postListener;
    // Listen for comments
    mAdapter = new CommentAdapter(this, mCommentsReference);
    mCommentsRecycler.setAdapter(mAdapter);
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Post(com.google.firebase.quickstart.database.models.Post) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 99 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project priend by TakoJ.

the class NewPostActivity method submitPost.

private void submitPost() {
    final String title = mTitleField.getText().toString();
    final String body = mBodyField.getText().toString();
    // Title is required
    if (TextUtils.isEmpty(title)) {
        mTitleField.setError(REQUIRED);
        return;
    }
    // Body is required
    if (TextUtils.isEmpty(body)) {
        mBodyField.setError(REQUIRED);
        return;
    }
    // Disable button so there are no multi-posts
    setEditingEnabled(false);
    Toast.makeText(this, "Posting...", Toast.LENGTH_SHORT).show();
    // [START single_value_read]
    final String userId = mAuth.getCurrentUser().getUid();
    mDatabase.child("users").child(userId).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get user value
            User user = dataSnapshot.getValue(User.class);
            // [START_EXCLUDE]
            if (user == null) {
                // User is null, error out
                Log.e(TAG, "User " + userId + " is unexpectedly null");
                Toast.makeText(NewPostActivity.this, "Error: could not fetch user.", Toast.LENGTH_SHORT).show();
            } else {
                // Write new post
                writeNewPost(userId, user.username, title, body);
            }
            // Finish this Activity, back to the stream
            setEditingEnabled(true);
            finish();
        // [END_EXCLUDE]
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, "getUser:onCancelled", databaseError.toException());
            // [START_EXCLUDE]
            setEditingEnabled(true);
        // [END_EXCLUDE]
        }
    });
// [END single_value_read]
}
Also used : User(com.example.management.models.User) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 100 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project priend by TakoJ.

the class PostDetailActivity method onStart.

@Override
public void onStart() {
    super.onStart();
    // Add value event listener to the post
    // [START post_value_event_listener]
    ValueEventListener postListener = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get Post object and use the values to update the UI
            Post post = dataSnapshot.getValue(Post.class);
            // [START_EXCLUDE]
            mAuthorView.setText(post.author);
            mTitleView.setText(post.title);
            mBodyView.setText(post.body);
        // [END_EXCLUDE]
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Getting Post failed, log a message
            Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
            // [START_EXCLUDE]
            Toast.makeText(PostDetailActivity.this, "Failed to load post.", Toast.LENGTH_SHORT).show();
        // [END_EXCLUDE]
        }
    };
    mPostReference.addValueEventListener(postListener);
    // [END post_value_event_listener]
    // Keep copy of post listener so we can remove it when app stops
    mPostListener = postListener;
    // Listen for comments
    mAdapter = new CommentAdapter(this, mCommentsReference);
    mCommentsRecycler.setAdapter(mAdapter);
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Post(com.example.management.models.Post) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Aggregations

DataSnapshot (com.google.firebase.database.DataSnapshot)211 ValueEventListener (com.google.firebase.database.ValueEventListener)211 DatabaseError (com.google.firebase.database.DatabaseError)210 DatabaseReference (com.google.firebase.database.DatabaseReference)62 View (android.view.View)47 Intent (android.content.Intent)43 TextView (android.widget.TextView)30 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)24 RecyclerView (android.support.v7.widget.RecyclerView)20 FirebaseUser (com.google.firebase.auth.FirebaseUser)20 HashMap (java.util.HashMap)20 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)19 Bundle (android.os.Bundle)16 ImageView (android.widget.ImageView)15 ArrayList (java.util.ArrayList)15 User (com.jexapps.bloodhub.m_Model.User)11 Map (java.util.Map)11 Date (java.util.Date)10 Query (com.google.firebase.database.Query)9 User (com.polito.mad17.madmax.entities.User)9