use of com.google.firebase.database.ValueEventListener 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;
}
}
});
}
use of com.google.firebase.database.ValueEventListener in project MadMax by deviz92.
the class PendingExpenseDetailFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
Log.i(TAG, "onCreateView");
// 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);
// todo mettere a posto
votersViewAdapter = new VotersViewAdapter(voters, getContext());
recyclerView.setAdapter(votersViewAdapter);
// Retrieve data of this pending expense
databaseReference.child("proposedExpenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Show list of voters for this pending expense
for (DataSnapshot voterSnap : dataSnapshot.child("participants").getChildren()) {
String vote = voterSnap.child("vote").getValue(String.class);
FirebaseUtils.getInstance().getVoter(voterSnap.getKey(), vote, voters, votersViewAdapter);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return view;
}
use of com.google.firebase.database.ValueEventListener in project MadMax by deviz92.
the class ProposedExpenseCommentsFragment 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("proposedExpenses");
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 SplitPolicyActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_split_policy);
// somma quote già splittate
totalTextView = (TextView) findViewById(R.id.total);
// costo della spesa
amountTextView = (TextView) findViewById(R.id.tv_amount);
currencyTextView = (TextView) findViewById(R.id.currency);
currencyAmountTextView = (TextView) findViewById(R.id.currency_amount);
Intent intent = getIntent();
amount = intent.getDoubleExtra("amount", 0);
currency = intent.getStringExtra("currency");
groupID = intent.getStringExtra("groupID");
amountsList = (HashMap<String, Double>) intent.getSerializableExtra("participants");
Log.d(TAG, "I just entered SplitPolicyActivity. amountsList contains: ");
for (Map.Entry<String, Double> entry : amountsList.entrySet()) {
Log.d(TAG, entry.getKey() + " " + entry.getValue());
}
totalSplit = intent.getDoubleExtra("totalSplit", 0d);
totalTextView.setText(df.format(totalSplit));
amountTextView.setText(df.format(amount));
currencyTextView.setText(currency);
currencyAmountTextView.setText(currency);
participants.clear();
// Retrieve info about members for this expense
for (final Map.Entry<String, Double> entry : amountsList.entrySet()) {
databaseReference.child("users").child(entry.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User u = new User();
u.setName(dataSnapshot.child("name").getValue(String.class));
u.setSurname(dataSnapshot.child("surname").getValue(String.class));
u.setProfileImage(dataSnapshot.child("image").getValue(String.class));
u.setSplitPart(entry.getValue());
u.setExpenseCurrency(currency);
participants.put(entry.getKey(), u);
splittersViewAdapter.update(participants);
splittersViewAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
RecyclerView.ItemDecoration divider = new InsetDivider.Builder(SplitPolicyActivity.this).orientation(InsetDivider.VERTICAL_LIST).dividerHeight(getResources().getDimensionPixelSize(R.dimen.divider_height)).color(ContextCompat.getColor(SplitPolicyActivity.this, 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);
splittersViewAdapter = new SplittersViewAdapter(participants, SplitPolicyActivity.this, this);
recyclerView.setAdapter(splittersViewAdapter);
}
use of com.google.firebase.database.ValueEventListener in project MadMax by deviz92.
the class GroupEdit method onCreate.
@Override
@TargetApi(23)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_group);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
groupNameView = (EditText) this.findViewById(R.id.group_name);
groupDescriptionView = (EditText) this.findViewById(R.id.group_description);
groupImageView = (ImageView) this.findViewById(R.id.group_image);
saveButton = (Button) this.findViewById(R.id.btn_save);
final Intent intent = getIntent();
final String groupID = intent.getStringExtra("groupID");
Log.d("DAVIDE", "da GroupEdit: " + groupID);
databaseReference.child("groups").child(groupID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
group = new Group();
group.setID(dataSnapshot.getKey());
group.setName(dataSnapshot.child("name").getValue(String.class));
group.setDescription(dataSnapshot.child("description").getValue(String.class));
group.setImage(dataSnapshot.child("image").getValue(String.class));
groupNameView.setText(group.getName());
groupDescriptionView.setText(group.getDescription());
// progressDialog = new ProgressDialog(ProfileEdit.this);
String groupImage = group.getImage();
if (groupImage != null && !groupImage.equals("noImage")) {
// Loading image
Glide.with(getApplicationContext()).load(groupImage).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).into(groupImageView);
} else {
// Loading image
Glide.with(getApplicationContext()).load(R.drawable.group_default).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).into(groupImageView);
}
groupImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "image clicked");
if (MainActivity.shouldAskPermission()) {
String[] perms = { "android.permission.READ_EXTERNAL_STORAGE" };
int permsRequestCode = 200;
requestPermissions(perms, permsRequestCode);
}
// allow to the user the choose image
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select picture"), PICK_IMAGE_REQUEST);
// now see onActivityResult
}
});
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "save clicked");
if (updateGroup(group)) {
Toast.makeText(GroupEdit.this, getString(R.string.saved), Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), GroupDetailActivity.class);
i.putExtra("groupID", groupID);
i.putExtra("userID", MainActivity.getCurrentUID());
startActivity(i);
finish();
}
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, databaseError.getMessage());
}
});
}
Aggregations