Search in sources :

Example 91 with ValueEventListener

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

the class ChooseGroupActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choose_group);
    RecyclerView.ItemDecoration divider = new InsetDivider.Builder(this).orientation(InsetDivider.VERTICAL_LIST).dividerHeight(getResources().getDimensionPixelSize(R.dimen.divider_height)).color(getResources().getColor(R.color.colorDivider)).insets(getResources().getDimensionPixelSize(R.dimen.divider_inset), 0).overlay(true).build();
    recyclerView = (RecyclerView) findViewById(R.id.rv_skeleton);
    layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.addItemDecoration(divider);
    groupsViewAdapter = new GroupsViewAdapter(getBaseContext(), this, groups, ChooseGroupActivity.TAG);
    recyclerView.setAdapter(groupsViewAdapter);
    // Ascolto i gruppi dello user
    databaseReference.child("users").child(MainActivity.getCurrentUID()).child("groups").addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Per ogni gruppo dello user
            for (DataSnapshot groupSnapshot : dataSnapshot.getChildren()) {
                // Se il gruppo è true, ossia è ancora tra quelli dello user
                if (groupSnapshot.getValue(Boolean.class))
                    FirebaseUtils.getInstance().getGroup(groupSnapshot.getKey(), groups, groupsViewAdapter);
                else {
                    // tolgo il gruppo da quelli che verranno stampati, così lo vedo sparire realtime
                    groups.remove(groupSnapshot.getKey());
                    groupsViewAdapter.update(groups);
                    groupsViewAdapter.notifyDataSetChanged();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, databaseError.toException());
        }
    });
}
Also used : GroupsViewAdapter(com.polito.mad17.madmax.activities.groups.GroupsViewAdapter) DatabaseError(com.google.firebase.database.DatabaseError) RecyclerView(android.support.v7.widget.RecyclerView) ValueEventListener(com.google.firebase.database.ValueEventListener) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 92 with ValueEventListener

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

the class ExpenseCommentsFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    String expenseID;
    View view = inflater.inflate(R.layout.skeleton_list, container, false);
    Bundle fragmentArguments = getArguments();
    expenseID = fragmentArguments.getString("expenseID");
    RecyclerView.ItemDecoration divider = new InsetDivider.Builder(getContext()).orientation(InsetDivider.VERTICAL_LIST).dividerHeight(getResources().getDimensionPixelSize(R.dimen.divider_height)).color(getResources().getColor(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);
    expenseCommentsViewAdapter = new ExpenseCommentsViewAdapter(this.getContext(), this, commentsMap, getFragmentManager());
    recyclerView.setAdapter(expenseCommentsViewAdapter);
    DatabaseReference expenseRef = databaseReference.child("expenses");
    expenseRef.child(expenseID).child("comments").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot commentSnapshot) {
            for (DataSnapshot comment : commentSnapshot.getChildren()) {
                FirebaseUtils.getInstance().getComment(comment.getKey(), commentsMap, expenseCommentsViewAdapter);
                Log.d(TAG, comment.getKey());
            }
            expenseCommentsViewAdapter.update(commentsMap);
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w(TAG, "Failed to read value.", error.toException());
        }
    });
    return view;
}
Also used : DatabaseReference(com.google.firebase.database.DatabaseReference) Bundle(android.os.Bundle) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DataSnapshot(com.google.firebase.database.DataSnapshot) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) DatabaseError(com.google.firebase.database.DatabaseError) RecyclerView(android.support.v7.widget.RecyclerView) ValueEventListener(com.google.firebase.database.ValueEventListener)

Example 93 with ValueEventListener

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

the class EventsFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    String groupID;
    View view = inflater.inflate(R.layout.skeleton_list, container, false);
    Bundle fragmentArguments = getArguments();
    groupID = fragmentArguments.getString("groupID");
    recyclerView = (RecyclerView) view.findViewById(R.id.rv_skeleton);
    recyclerView.setHasFixedSize(true);
    RecyclerView.ItemDecoration divider = new InsetDivider.Builder(getContext()).orientation(InsetDivider.VERTICAL_LIST).dividerHeight(getResources().getDimensionPixelSize(R.dimen.divider_height)).color(getResources().getColor(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);
    eventsViewAdapter = new EventsViewAdapter(this.getContext(), eventMap);
    recyclerView.setAdapter(eventsViewAdapter);
    DatabaseReference groupRef = databaseReference.child("groups");
    Log.d(TAG, "groupID: " + groupID);
    // retrieving events for current group
    groupRef.child(groupID).child("events").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot eventSnapshot) {
            for (DataSnapshot event : eventSnapshot.getChildren()) {
                FirebaseUtils.getInstance().getEvent(event.getKey(), eventMap, eventsViewAdapter);
                Log.d(TAG, event.getKey());
            }
            eventsViewAdapter.update(eventMap);
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w(TAG, "Failed to read value.", error.toException());
        }
    });
    return view;
}
Also used : DatabaseReference(com.google.firebase.database.DatabaseReference) Bundle(android.os.Bundle) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DataSnapshot(com.google.firebase.database.DataSnapshot) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) DatabaseError(com.google.firebase.database.DatabaseError) RecyclerView(android.support.v7.widget.RecyclerView) ValueEventListener(com.google.firebase.database.ValueEventListener)

Example 94 with ValueEventListener

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

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

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