use of com.google.firebase.database.ValueEventListener in project MadMax by deviz92.
the class NewExpenseActivity method onOptionsItemSelected.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
DatabaseReference groupRef;
int itemThatWasClickedId = item.getItemId();
if (itemThatWasClickedId == R.id.action_save) {
if (!validateForm())
return true;
//display message if text field is empty
Toast.makeText(getBaseContext(), "Saved expense", Toast.LENGTH_SHORT).show();
final Expense newExpense = new Expense();
newExpense.setDescription(description.getText().toString());
newExpense.setAmount(Double.valueOf(amount.getText().toString()));
newExpense.setCurrency(currency.getSelectedItem().toString());
newExpense.setGroupID(groupID);
newExpense.setCreatorID(userID);
newExpense.setEquallyDivided(true);
newExpense.setDeleted(false);
Log.d(TAG, "Before first access to firebase");
groupRef = databaseReference.child("groups");
groupRef.child(groupID).child("members").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot membersSnapshot) {
int participantsCount = 0;
//Attenzione! Non contare i membri eliminati tra i partecipanti alla spesa
for (DataSnapshot memberSnap : membersSnapshot.getChildren()) {
if (!memberSnap.child("deleted").getValue(Boolean.class)) {
participantsCount++;
}
}
Double amountPerMember = 1 / (double) participantsCount;
for (DataSnapshot member : membersSnapshot.getChildren()) {
//Aggiungo alla spesa solo i membri non eliminati dal gruppo
if (!member.child("deleted").getValue(Boolean.class)) {
newExpense.getParticipants().put(member.getKey(), amountPerMember);
}
}
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
newExpense.setTimestamp(timeStamp);
//Aggiungo una pending expense
if (callingActivity.equals("ChooseGroupActivity")) {
newExpense.setGroupName(groupName);
if (groupImage != null)
newExpense.setGroupImage(groupImage);
FirebaseUtils.getInstance().addPendingExpenseFirebase(newExpense, expensePhoto, billPhoto);
//todo qui
Intent myIntent = new Intent(NewExpenseActivity.this, MainActivity.class);
myIntent.putExtra("UID", MainActivity.getCurrentUser().getID());
myIntent.putExtra("currentFragment", 2);
startActivity(myIntent);
// add event for PENDING_EXPENSE_ADD
User currentUser = MainActivity.getCurrentUser();
Event event = new Event(groupID, Event.EventType.PENDING_EXPENSE_ADD, 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);
} else //Aggiungo una spesa normale
{
FirebaseUtils.getInstance().addExpenseFirebase(newExpense, expensePhoto, billPhoto);
// add event for EXPENSE_ADD
User currentUser = MainActivity.getCurrentUser();
Event event = new Event(newExpense.getGroupID(), Event.EventType.EXPENSE_ADD, 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);
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
}
this.finish();
return super.onOptionsItemSelected(item);
}
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;
}
use of com.google.firebase.database.ValueEventListener 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);
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);
payExpenseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Clicked payButton");
if (expenseBalance >= 0) {
Toast.makeText(ExpenseDetailActivity.this, "You have no debts to pay for this expense", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(ExpenseDetailActivity.this, PayExpenseActivity.class);
intent.putExtra("groupID", groupID);
intent.putExtra("userID", userID);
intent.putExtra("debt", expenseBalance);
intent.putExtra("expenseID", expenseID);
intent.putExtra("expenseName", expenseName);
intent.putExtra("expenseCurrency", currency);
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);
expenseNameTextView.setText(expenseName);
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) {
}
});
}
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.getCurrentUser().getID()).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());
}
});
}
use of com.google.firebase.database.ValueEventListener 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;
}
Aggregations