Search in sources :

Example 46 with DataSnapshot

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

the class GroupDetailActivity method itemLongClicked.

//Apro popup menu quando ho tenuto premuto un friend o gruppo per 1 secondo
@Override
public void itemLongClicked(String fragmentName, final String itemID, View v) {
    Log.i(TAG, "fragmentName " + fragmentName + " itemID " + itemID);
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Bundle bundle = new Bundle();
    Intent intent = null;
    switch(fragmentName) {
        case "FriendsFragment":
            break;
        case "GroupsFragment":
            popup = new PopupMenu(GroupDetailActivity.this, v, Gravity.RIGHT);
            popup.getMenuInflater().inflate(R.menu.longclick_popup_menu, popup.getMenu());
            one = popup.getMenu().findItem(R.id.one);
            one.setTitle("Remove friend");
            popup.getMenu().findItem(R.id.two).setVisible(false);
            popup.getMenu().findItem(R.id.three).setVisible(false);
            //registering popup with OnMenuItemClickListener
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                public boolean onMenuItemClick(MenuItem item) {
                    Toast.makeText(GroupDetailActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                    return true;
                }
            });
            //showing popup menu
            popup.show();
        case "ExpensesFragment":
            popup = new PopupMenu(GroupDetailActivity.this, v, Gravity.RIGHT);
            popup.getMenuInflater().inflate(R.menu.longclick_popup_menu, popup.getMenu());
            one = popup.getMenu().findItem(R.id.one);
            one.setTitle("Remove expense");
            popup.getMenu().findItem(R.id.two).setVisible(false);
            popup.getMenu().findItem(R.id.three).setVisible(false);
            //registering popup with OnMenuItemClickListener
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                public boolean onMenuItemClick(MenuItem item) {
                    //Toast.makeText(GroupDetailActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
                    FirebaseUtils.getInstance().removeExpenseFirebase(itemID, getApplicationContext());
                    // add event for EXPENSE_REMOVE
                    databaseReference.child("expenses").child(itemID).addListenerForSingleValueEvent(new ValueEventListener() {

                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            User currentUser = MainActivity.getCurrentUser();
                            Event event = new Event(groupID, 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());
                        }
                    });
                    return true;
                }
            });
            //showing popup menu
            popup.show();
            break;
    }
}
Also used : User(com.polito.mad17.madmax.entities.User) Bundle(android.os.Bundle) Intent(android.content.Intent) MenuItem(android.view.MenuItem) DataSnapshot(com.google.firebase.database.DataSnapshot) FragmentManager(android.support.v4.app.FragmentManager) FragmentTransaction(android.support.v4.app.FragmentTransaction) DatabaseError(com.google.firebase.database.DatabaseError) Event(com.polito.mad17.madmax.entities.Event) ValueEventListener(com.google.firebase.database.ValueEventListener) SimpleDateFormat(java.text.SimpleDateFormat) PopupMenu(android.support.v7.widget.PopupMenu)

Example 47 with DataSnapshot

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

the class PendingExpensesFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /*getSupportFragmentManager().beginTransaction()
                .add(detailFragment, "detail")
                // Add this transaction to the back stack
                .addToBackStack()
                .commit();*/
    Log.d(TAG, "onCreateView");
    setInterface((OnItemClickInterface) getActivity(), (OnItemLongClickInterface) getActivity());
    View view = inflater.inflate(R.layout.skeleton_list, container, false);
    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);
    pendingExpenseViewAdapter = new PendingExpenseViewAdapter(this.getContext(), this, this, pendingExpensesMap);
    recyclerView.setAdapter(pendingExpenseViewAdapter);
    //Ascolto le pending expenses dello user
    databaseReference.child("users").child(MainActivity.getCurrentUser().getID()).child("proposedExpenses").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //Per ogni pending expense dello user
            for (DataSnapshot pendingExpenseSnap : dataSnapshot.getChildren()) {
                //Se la pending expense non è stata eliminata (NELLO USER)
                if (pendingExpenseSnap.getValue(Boolean.class)) {
                    FirebaseUtils.getInstance().getPendingExpense(pendingExpenseSnap.getKey(), pendingExpensesMap, pendingExpenseViewAdapter);
                    pendingExpenseViewAdapter.update(pendingExpensesMap);
                    pendingExpenseViewAdapter.notifyDataSetChanged();
                } else {
                    //tolgo la spesa da quelle che verranno stampate, così la vedo sparire realtime
                    pendingExpensesMap.remove(pendingExpenseSnap.getKey());
                    pendingExpenseViewAdapter.update(pendingExpensesMap);
                    pendingExpenseViewAdapter.notifyDataSetChanged();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return view;
}
Also used : 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) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Aggregations

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