Search in sources :

Example 31 with DatabaseError

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

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

the class FirebaseUtils method removePendingExpenseFirebase.

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

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String groupID = dataSnapshot.child("groupID").getValue(String.class);
            // Elimino spesa pending dal gruppo
            databaseReference.child("groups").child(groupID).child("proposedExpenses").child(expenseID).setValue(false);
            // Per ogni participant elimino la spesa pending dal suo elenco spese pending
            for (DataSnapshot participantSnapshot : dataSnapshot.child("participants").getChildren()) {
                String participantID = participantSnapshot.getKey();
                databaseReference.child("users").child(participantID).child("proposedExpenses").child(expenseID).setValue(false);
            }
            // todo Elimino commenti sulla spesa pending
            // databaseReference.child("comments").child(groupID).removeValue();
            // Elimino spesa pending
            databaseReference.child("proposedExpenses").child(expenseID).child("deleted").setValue(true);
            // Toast.makeText(context, context.getString(R.string.expense_removed), Toast.LENGTH_SHORT).show();
            // add event for PENDING_EXPENSE_REMOVE
            databaseReference.child("proposedExpenses").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.PENDING_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, MainActivity.class);
                intent.putExtra("currentFragment", 2);
                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 33 with DatabaseError

use of com.google.firebase.database.DatabaseError in project iNGAGE by davis123123.

the class PostThreadActivity method checkPageExist.

public void checkPageExist(String threadTitle) {
    Log.d("CHECKPAGE", "yes3");
    final DatabaseReference post_root = FirebaseDatabase.getInstance().getReference().child(threadTitle);
    post_root.runTransaction(new Transaction.Handler() {

        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            Log.d("ROOT", String.valueOf(mutableData));
            if (mutableData.hasChildren()) {
                Log.d("ROOTCHILDREN", "yes");
            } else {
                Log.d("ROOTCHILDREN", "no");
                Map<String, Object> map_page = new HashMap<String, Object>();
                map_page.put("1", "");
                String fPage = "1";
                post_root.updateChildren(map_page);
            }
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
        }
    });
}
Also used : DatabaseReference(com.google.firebase.database.DatabaseReference) DataSnapshot(com.google.firebase.database.DataSnapshot) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.google.firebase.database.Transaction) MutableData(com.google.firebase.database.MutableData) HashMap(java.util.HashMap) Map(java.util.Map)

Example 34 with DatabaseError

use of com.google.firebase.database.DatabaseError in project iNGAGE by davis123123.

the class ChatFragment method removeDownvote.

@Override
public void removeDownvote(int p) {
    ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
    String chat_key = chatMessageHelper.getMessageID();
    DatabaseReference message_root = currPageData.child(chat_key);
    // get upvote data
    DatabaseReference downvote_count = message_root.child("downvotes");
    downvote_count.runTransaction(new Transaction.Handler() {

        @Override
        public Transaction.Result doTransaction(MutableData currentData) {
            Log.d("Data", String.valueOf(currentData));
            if (currentData.getValue() == null) {
                currentData.setValue(0);
            } else {
                currentData.setValue((Long) currentData.getValue() - 1);
            }
            // we can also abort by calling Transaction.abort()
            return Transaction.success(currentData);
        }

        // TODO:Error handle here
        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
        }
    });
}
Also used : ChatMessageHelper(ingage.ingage20.helpers.ChatMessageHelper) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.google.firebase.database.Transaction) DatabaseReference(com.google.firebase.database.DatabaseReference) MutableData(com.google.firebase.database.MutableData) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 35 with DatabaseError

use of com.google.firebase.database.DatabaseError in project iNGAGE by davis123123.

the class ChatFragment method onDownvoteClick.

@Override
public void onDownvoteClick(int p) {
    // Log.d("vote" , "down : ");
    ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
    String chat_key = chatMessageHelper.getMessageID();
    DatabaseReference message_root = currPageData.child(chat_key);
    // get upvote data
    DatabaseReference downvote_count = message_root.child("downvotes");
    downvote_count.runTransaction(new Transaction.Handler() {

        @Override
        public Transaction.Result doTransaction(MutableData currentData) {
            Log.d("Data", String.valueOf(currentData));
            if (currentData.getValue() == null) {
                currentData.setValue(1);
            } else {
                currentData.setValue((Long) currentData.getValue() + 1);
            }
            // we can also abort by calling Transaction.abort()
            return Transaction.success(currentData);
        }

        // TODO:Error handle here
        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
        }
    });
}
Also used : ChatMessageHelper(ingage.ingage20.helpers.ChatMessageHelper) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.google.firebase.database.Transaction) DatabaseReference(com.google.firebase.database.DatabaseReference) MutableData(com.google.firebase.database.MutableData) DataSnapshot(com.google.firebase.database.DataSnapshot)

Aggregations

DatabaseError (com.google.firebase.database.DatabaseError)61 DataSnapshot (com.google.firebase.database.DataSnapshot)59 ValueEventListener (com.google.firebase.database.ValueEventListener)44 DatabaseReference (com.google.firebase.database.DatabaseReference)22 View (android.view.View)21 Intent (android.content.Intent)15 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)13 RecyclerView (android.support.v7.widget.RecyclerView)13 User (com.polito.mad17.madmax.entities.User)12 MutableData (com.google.firebase.database.MutableData)11 Transaction (com.google.firebase.database.Transaction)11 Bundle (android.os.Bundle)10 TextView (android.widget.TextView)9 ChatMessageHelper (ingage.ingage20.helpers.ChatMessageHelper)8 ImageView (android.widget.ImageView)6 SimpleDateFormat (java.text.SimpleDateFormat)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ChildEventListener (com.google.firebase.database.ChildEventListener)5 Event (com.polito.mad17.madmax.entities.Event)5