Search in sources :

Example 1 with ExpenseDetailPagerAdapter

use of com.polito.mad17.madmax.activities.ExpenseDetailPagerAdapter in project MadMax by deviz92.

the class PendingExpenseDetailActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "OnCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pending_expense_detail);
    Intent intent = getIntent();
    expenseID = intent.getStringExtra("expenseID");
    // intent.getStringExtra("userID");
    userID = MainActivity.getCurrentUID();
    fab = (FloatingActionButton) findViewById(R.id.fab);
    updateFab(0);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setBackgroundColor(0x0000FF00);
    // Get a support ActionBar corresponding to this toolbar
    ActionBar ab = getSupportActionBar();
    // Enable the Up button
    ab.setDisplayHomeAsUpEnabled(true);
    // insert tabs and current fragment in the main layout
    // mainView.addView(getLayoutInflater().inflate(R.layout.activity_expense_detail, null));
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText(R.string.expense_detail));
    tabLayout.addTab(tabLayout.newTab().setText(R.string.comments));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    viewPager = (ViewPager) findViewById(R.id.expense_detail_view_pager);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            Log.d(TAG, String.valueOf(tab.getPosition()));
            updateFab(tab.getPosition());
            Log.d(TAG, "tab.getPosition() " + tab.getPosition());
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
    ExpenseDetailPagerAdapter adapter = new ExpenseDetailPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount(), expenseID, TAG);
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(0);
    imageView = (ImageView) findViewById(R.id.img_photo);
    amountTextView = (TextView) findViewById(R.id.tv_amount);
    creatorNameTextView = (TextView) findViewById(R.id.tv_creator_name);
    groupTextView = (TextView) findViewById(R.id.tv_group_name);
    expenseNameTextView = (TextView) findViewById(R.id.tv_pending_name);
    moveExpenseButton = (Button) findViewById(R.id.btn_move_expense);
    // Retrieve data of this pending expense
    databaseReference.child("proposedExpenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            expenseName = dataSnapshot.child("description").getValue(String.class);
            String groupName = dataSnapshot.child("groupName").getValue(String.class);
            Double amount = dataSnapshot.child("amount").getValue(Double.class);
            expenseNameTextView.setText(expenseName);
            groupTextView.setText(groupName);
            amountTextView.setText(amount.toString());
            creatorID = dataSnapshot.child("creatorID").getValue(String.class);
            groupID = dataSnapshot.child("groupID").getValue(String.class);
            // .load(dataSnapshot.child("image").getValue(String.class))
            Glide.with(getApplicationContext()).load(dataSnapshot.child("expensePhoto").getValue(String.class)).placeholder(R.color.colorPrimary).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
            databaseReference.child("users").child(creatorID).addListenerForSingleValueEvent(new ValueEventListener() {

                @Override
                public void onDataChange(DataSnapshot creatorSnapshot) {
                    creatorNameTextView.setText(creatorSnapshot.child("name").getValue(String.class) + " " + creatorSnapshot.child("surname").getValue(String.class));
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
            DecimalFormat df = new DecimalFormat("#.##");
            amountTextView.setText(df.format(amount) + " €");
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    // Click on button to move from pending to real expense
    moveExpenseButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Only the creator of pending expense can move it
            if (creatorID.equals(userID)) {
                databaseReference.child("proposedExpenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        ArrayList<String> participants = new ArrayList<String>();
                        expenseName = dataSnapshot.child("description").getValue(String.class);
                        groupID = dataSnapshot.child("groupID").getValue(String.class);
                        Double amount = dataSnapshot.child("amount").getValue(Double.class);
                        String currency = dataSnapshot.child("currency").getValue(String.class);
                        String creatorID = dataSnapshot.child("creatorID").getValue(String.class);
                        String expensePhoto = dataSnapshot.child("expensePhoto").getValue(String.class);
                        for (DataSnapshot participantSnap : dataSnapshot.child("participants").getChildren()) participants.add(participantSnap.getKey());
                        Expense newExpense = new Expense();
                        newExpense.setDescription(expenseName);
                        newExpense.setAmount(amount);
                        newExpense.setCurrency(currency);
                        newExpense.setGroupID(groupID);
                        newExpense.setCreatorID(creatorID);
                        newExpense.setEquallyDivided(true);
                        newExpense.setDeleted(false);
                        newExpense.setExpensePhoto(expensePhoto);
                        Double amountPerMember = 1 / (double) participants.size();
                        for (String participant : participants) {
                            newExpense.getParticipants().put(participant, amountPerMember);
                            // Delete expense from his proposed expenses
                            databaseReference.child("users").child(participant).child("proposedExpenses").child(expenseID).setValue(false);
                        }
                        String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
                        newExpense.setTimestamp(timeStamp);
                        // Add expense to db
                        FirebaseUtils.getInstance().addExpenseFirebase(newExpense, null, null, getApplicationContext());
                        // Delete pending expense from proposed expenses list
                        databaseReference.child("proposedExpenses").child(expenseID).child("deleted").setValue(true);
                        // Delete pending expense from group
                        databaseReference.child("groups").child(groupID).child("proposedExpenses").child(expenseID).setValue(false);
                        // add event for PENDING_EXPENSE_APPROVED
                        User currentUser = MainActivity.getCurrentUser();
                        String userID = currentUser.getID();
                        Event event = new Event(groupID, Event.EventType.PENDING_EXPENSE_APPROVED, currentUser.getName() + " " + currentUser.getSurname(), newExpense.getDescription(), newExpense.getAmount());
                        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);
                        Intent myIntent = new Intent(PendingExpenseDetailActivity.this, GroupDetailActivity.class);
                        myIntent.putExtra("groupID", groupID);
                        myIntent.putExtra("userID", userID);
                        finish();
                        startActivity(myIntent);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
            } else {
                Toast.makeText(PendingExpenseDetailActivity.this, getString(R.string.only_creator), Toast.LENGTH_SHORT).show();
                return;
            }
        }
    });
}
Also used : User(com.polito.mad17.madmax.entities.User) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) DataSnapshot(com.google.firebase.database.DataSnapshot) Expense(com.polito.mad17.madmax.entities.Expense) TabLayout(android.support.design.widget.TabLayout) ValueEventListener(com.google.firebase.database.ValueEventListener) ActionBar(android.support.v7.app.ActionBar) ExpenseDetailPagerAdapter(com.polito.mad17.madmax.activities.ExpenseDetailPagerAdapter) Intent(android.content.Intent) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) DatabaseError(com.google.firebase.database.DatabaseError) Event(com.polito.mad17.madmax.entities.Event) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with ExpenseDetailPagerAdapter

use of com.polito.mad17.madmax.activities.ExpenseDetailPagerAdapter in project MadMax by deviz92.

the class ExpenseDetailActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_expense_detail);
    Intent intent = getIntent();
    groupID = intent.getStringExtra("groupID");
    userID = intent.getStringExtra("userID");
    expenseID = intent.getStringExtra("expenseID");
    fab = (FloatingActionButton) findViewById(R.id.fab);
    updateFab(0);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setBackgroundColor(0x0000FF00);
    // Get a support ActionBar corresponding to this toolbar
    ActionBar ab = getSupportActionBar();
    // Enable the Up button
    ab.setDisplayHomeAsUpEnabled(true);
    // insert tabs and current fragment in the main layout
    // mainView.addView(getLayoutInflater().inflate(R.layout.activity_expense_detail, null));
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText(R.string.expense_detail));
    tabLayout.addTab(tabLayout.newTab().setText(R.string.comments));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    viewPager = (ViewPager) findViewById(R.id.expense_detail_view_pager);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            Log.d(TAG, String.valueOf(tab.getPosition()));
            updateFab(tab.getPosition());
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
    ExpenseDetailPagerAdapter adapter = new ExpenseDetailPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount(), expenseID, TAG);
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(0);
    // Set data of upper part of Activity
    imageView = (ImageView) findViewById(R.id.img_photo);
    amountTextView = (TextView) findViewById(R.id.tv_amount);
    creatorNameTextView = (TextView) findViewById(R.id.tv_creator_name);
    expenseNameTextView = (TextView) findViewById(R.id.tv_pending_name);
    balanceTextTextView = (TextView) findViewById(R.id.tv_balance_text);
    balanceTextView = (TextView) findViewById(R.id.tv_balance);
    payExpenseButton = (Button) findViewById(R.id.btn_pay_debt);
    userImage = MainActivity.getCurrentUser().getProfileImage();
    payExpenseButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d(TAG, "Clicked payButton");
            if (expenseBalance >= 0) {
                Toast.makeText(ExpenseDetailActivity.this, getString(R.string.no_debts_to_pay_for_expense), Toast.LENGTH_SHORT).show();
            } else {
                Intent intent = new Intent(ExpenseDetailActivity.this, PayExpenseActivity.class);
                intent.putExtra("groupID", groupID);
                intent.putExtra("userID", userID);
                intent.putExtra("userImage", userImage);
                intent.putExtra("debt", expenseBalance);
                intent.putExtra("expenseID", expenseID);
                intent.putExtra("expenseName", expenseName);
                intent.putExtra("expenseCurrency", currency);
                intent.putExtra("expenseImage", expensePhoto);
                startActivity(intent);
                finish();
            }
        }
    });
    // Retrieve data of this expense
    databaseReference.child("expenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            expenseName = dataSnapshot.child("description").getValue(String.class);
            Double amount = dataSnapshot.child("amount").getValue(Double.class);
            currency = dataSnapshot.child("currency").getValue(String.class);
            expensePhoto = dataSnapshot.child("expensePhoto").getValue(String.class);
            expenseNameTextView.setText(expenseName);
            // .load(dataSnapshot.child("image").getValue(String.class))
            Glide.with(getApplicationContext()).load(dataSnapshot.child("expensePhoto").getValue(String.class)).placeholder(R.color.colorPrimary).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
            DecimalFormat df = new DecimalFormat("#.##");
            amountTextView.setText(df.format(amount) + " " + currency);
            // Retrieve my balance for this expense
            Double dueImport = Double.parseDouble(String.valueOf(dataSnapshot.child("participants").child(userID).child("fraction").getValue())) * dataSnapshot.child("amount").getValue(Double.class);
            Double alreadyPaid = dataSnapshot.child("participants").child(userID).child("alreadyPaid").getValue(Double.class);
            expenseBalance = alreadyPaid - dueImport;
            expenseBalance = Math.floor(expenseBalance * 100) / 100;
            if (expenseBalance > 0) {
                balanceTextTextView.setText("For this expense you should receive");
                balanceTextView.setText(expenseBalance.toString() + " " + currency);
            } else if (expenseBalance < 0) {
                balanceTextTextView.setText("For this expense you should pay");
                Double absBalance = abs(expenseBalance);
                balanceTextView.setText(absBalance.toString() + " " + currency);
            } else if (expenseBalance == 0) {
                balanceTextTextView.setText("For this expense you have no debts");
                balanceTextView.setText("0" + " " + currency);
            }
            // Retrieve name and surname of creator
            String creatorID = dataSnapshot.child("creatorID").getValue(String.class);
            databaseReference.child("users").child(creatorID).addListenerForSingleValueEvent(new ValueEventListener() {

                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    String name = dataSnapshot.child("name").getValue(String.class);
                    String surname = dataSnapshot.child("surname").getValue(String.class);
                    creatorNameTextView.setText(name + " " + surname);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Also used : DecimalFormat(java.text.DecimalFormat) ExpenseDetailPagerAdapter(com.polito.mad17.madmax.activities.ExpenseDetailPagerAdapter) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) DatabaseError(com.google.firebase.database.DatabaseError) TabLayout(android.support.design.widget.TabLayout) ValueEventListener(com.google.firebase.database.ValueEventListener) ActionBar(android.support.v7.app.ActionBar)

Example 3 with ExpenseDetailPagerAdapter

use of com.polito.mad17.madmax.activities.ExpenseDetailPagerAdapter in project MadMax by deviz92.

the class ExpenseDetailFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    Log.i(TAG, "onCreateView");
    setInterface((OnItemClickInterface) getActivity());
    // Read expenseID from ExpenseDetailPagerAdapter
    Bundle b = this.getArguments();
    expenseID = b.getString("expenseID");
    final 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);
    participantsViewAdapter = new ParticipantsViewAdapter(this.getContext(), this, participants);
    recyclerView.setAdapter(participantsViewAdapter);
    // Ascolto i participants alla spesa
    databaseReference.child("expenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Per ogni participant
            for (DataSnapshot participantSnap : dataSnapshot.child("participants").getChildren()) {
                Double alreadyPaid = participantSnap.child("alreadyPaid").getValue(Double.class);
                Double dueImport = alreadyPaid - participantSnap.child("fraction").getValue(Double.class) * dataSnapshot.child("amount").getValue(Double.class);
                String currency = dataSnapshot.child("currency").getValue(String.class);
                User u = new User();
                u.setAlreadyPaid(alreadyPaid);
                u.setDueImport(dueImport);
                u.setExpenseCurrency(currency);
                String participantID = participantSnap.getKey();
                FirebaseUtils.getInstance().getParticipantName(participantID, participants, participantsViewAdapter, u);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, databaseError.toException());
        }
    });
    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)

Aggregations

View (android.view.View)3 DataSnapshot (com.google.firebase.database.DataSnapshot)3 DatabaseError (com.google.firebase.database.DatabaseError)3 ValueEventListener (com.google.firebase.database.ValueEventListener)3 Intent (android.content.Intent)2 TabLayout (android.support.design.widget.TabLayout)2 ActionBar (android.support.v7.app.ActionBar)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 ExpenseDetailPagerAdapter (com.polito.mad17.madmax.activities.ExpenseDetailPagerAdapter)2 User (com.polito.mad17.madmax.entities.User)2 DecimalFormat (java.text.DecimalFormat)2 Bundle (android.os.Bundle)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 Event (com.polito.mad17.madmax.entities.Event)1 Expense (com.polito.mad17.madmax.entities.Expense)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1