Search in sources :

Example 31 with DataSnapshot

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

the class FriendsFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final String activityName = getActivity().getClass().getSimpleName();
    Log.d(TAG, "Sono nella activity: " + activityName);
    final View view = inflater.inflate(R.layout.skeleton_list, container, false);
    databaseReference = FirebaseDatabase.getInstance().getReference();
    friends.clear();
    setInterface((OnItemClickInterface) getActivity(), (OnItemLongClickInterface) getActivity());
    RecyclerView.ItemDecoration divider = new InsetDivider.Builder(getContext()).orientation(InsetDivider.VERTICAL_LIST).dividerHeight(getResources().getDimensionPixelSize(R.dimen.divider_height)).color(ContextCompat.getColor(getContext(), R.color.colorDivider)).insets(getResources().getDimensionPixelSize(R.dimen.divider_inset), 0).overlay(true).build();
    recyclerView = (RecyclerView) view.findViewById(R.id.rv_skeleton);
    layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.addItemDecoration(divider);
    friendsViewAdapter = new FriendsViewAdapter(this.getContext(), this, this, friends, groupDetail);
    recyclerView.setAdapter(friendsViewAdapter);
    // Se sono in MainActivity visualizzo lista degli amici
    if (activityName.equals("MainActivity")) {
        query = databaseReference.child("users").child(MainActivity.getCurrentUID()).child("friends");
    } else // Se sono dentro un gruppo, visualizzo lista membri del gruppo
    if (activityName.equals("GroupDetailActivity")) {
        Bundle b = this.getArguments();
        if (b != null) {
            groupID = b.getString("groupID");
            query = databaseReference.child("groups").child(groupID).child("members");
        }
    }
    Log.d(TAG, "query: " + query);
    groupMembersListener = query.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(final DataSnapshot externalDataSnapshot) {
            // svuoto la map, così se viene eliminato uno user dal db, non viene tenuto nella map che si ricarica da zero
            friends.clear();
            for (final DataSnapshot friendSnapshot : externalDataSnapshot.getChildren()) {
                // getFriend(friendSnapshot.getKey());
                if (activityName.equals("MainActivity")) {
                    Log.d(TAG, "key: " + friendSnapshot.getKey());
                    Log.d(TAG, "value: " + friendSnapshot.getValue());
                    if (!listenedFriends)
                        listenedFriends = true;
                    // Log.d(TAG,  friendSnapshot.child("deleted").getValue() + " ");
                    deleted = friendSnapshot.child("deleted").getValue(Boolean.class);
                    if (deleted == null) {
                        deleted = true;
                    }
                    if (deleted)
                        Log.d(TAG, friendSnapshot.getKey() + " is cancelled");
                } else if (activityName.equals("GroupDetailActivity")) {
                    deleted = friendSnapshot.child("deleted").getValue(Boolean.class);
                    if (deleted == null) {
                        deleted = true;
                    }
                    // Se sono negli amici "generali" e non nei membri di un gruppo, non c'è il campo deleted, quindi sarà null
                    if (!listenedGroups.contains(groupID))
                        listenedGroups.add(groupID);
                }
                if (!deleted) {
                    final String id = friendSnapshot.getKey();
                    databaseReference.child("users").child(id).addValueEventListener(new ValueEventListener() {

                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            String name = dataSnapshot.child("name").getValue(String.class);
                            String surname = dataSnapshot.child("surname").getValue(String.class);
                            String profileImage = dataSnapshot.child("image").getValue(String.class);
                            if (activityName.equals("MainActivity")) {
                                User u = new User();
                                u.setID(friendSnapshot.getKey());
                                u.setName(name);
                                u.setSurname(surname);
                                u.setProfileImage(profileImage);
                                if (!deleted)
                                    friends.put(id, u);
                                else
                                    friends.remove(id);
                                friendsViewAdapter.update(friends);
                                friendsViewAdapter.notifyDataSetChanged();
                            } else if (activityName.equals("GroupDetailActivity")) {
                                getUserAndGroupBalance(id, name, surname, profileImage, groupID);
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                        }
                    });
                }
            }
            friendsViewAdapter.update(friends);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, databaseError.getMessage());
        }
    });
    Log.d(TAG, "dopo setAdapter");
    return view;
}
Also used : User(com.polito.mad17.madmax.entities.User) Bundle(android.os.Bundle) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DataSnapshot(com.google.firebase.database.DataSnapshot) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) DatabaseError(com.google.firebase.database.DatabaseError) RecyclerView(android.support.v7.widget.RecyclerView) ValueEventListener(com.google.firebase.database.ValueEventListener)

Example 32 with DataSnapshot

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

the class User method joinGroup.

public void joinGroup(String groupID, String inviterID) {
    final DatabaseReference databaseReference = FirebaseUtils.getDatabaseReference();
    final String currentUID = this.getID();
    // Aggiungo gruppo alla lista gruppi dello user
    databaseReference.child("users").child(currentUID).child("groups").push();
    databaseReference.child("users").child(currentUID).child("groups").child(groupID).setValue("true");
    // Aggiungo user (con sottocampi admin e timestamp) alla lista membri del gruppo
    databaseReference.child("groups").child(groupID).child("members").push();
    databaseReference.child("groups").child(groupID).child("members").child(currentUID).push();
    databaseReference.child("groups").child(groupID).child("members").child(currentUID).child("admin").setValue(false);
    databaseReference.child("groups").child(groupID).child("members").child(currentUID).push();
    String timestamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
    databaseReference.child("groups").child(groupID).child("members").child(currentUID).child("timestamp").setValue(timestamp);
    // aggiunto da riky
    databaseReference.child("groups").child(groupID).child("members").child(currentUID).push();
    databaseReference.child("groups").child(groupID).child("members").child(currentUID).child("deleted").setValue(false);
    // aggiungo l'invitante agli amici se non lo è già
    if (!userFriends.containsKey(inviterID)) {
        // todo aggiungere l'invitato tra gli amici dell'invitante
        addFriend(inviterID);
    }
    // Incremento il numero di partecipanti
    databaseReference.child("groups").child(groupID).child("numberMembers").runTransaction(new Transaction.Handler() {

        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            Integer numberMembers = mutableData.getValue(Integer.class);
            if (numberMembers == null) {
                return Transaction.success(mutableData);
            }
            // Set value and report transaction success
            mutableData.setValue(numberMembers + 1);
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
            // Transaction completed
            Log.d(TAG, "postTransaction:onComplete:" + databaseError);
        }
    });
}
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) SimpleDateFormat(java.text.SimpleDateFormat)

Example 33 with DataSnapshot

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

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

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

Aggregations

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