use of com.google.firebase.database.ValueEventListener in project MadMax by deviz92.
the class EventsViewAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(final EventViewHolder eventViewHolder, int position) {
final Event event = getItem(position).getValue();
User currentUser = MainActivity.getCurrentUser();
DatabaseReference databaseReference = FirebaseUtils.getDatabaseReference();
switch(event.getEventType()) {
case GROUP_ADD:
eventViewHolder.imageView.setImageResource(R.drawable.group_add);
event.setDescription(event.getSubject() + " " + context.getString(R.string.GROUP_ADD) + " \"" + event.getObject() + "\"");
break;
/*case GROUP_REMOVE:
eventViewHolder.imageView.setImageResource(R.drawable.group_remove);
event.setDescription("\"" + event.getSubject() + "\" " + context.getString(R.string.GROUP_REMOVE) + " \"" + event.getObject() + "\"");
break;*/
case GROUP_EDIT:
eventViewHolder.imageView.setImageResource(R.drawable.group_edit);
event.setDescription(event.getSubject() + " " + context.getString(R.string.GROUP_EDIT) + " \"" + event.getObject() + "\"");
break;
case GROUP_MEMBER_ADD:
eventViewHolder.imageView.setImageResource(R.drawable.user_add);
event.setDescription(event.getSubject() + " " + context.getString(R.string.GROUP_MEMBER_ADD) + " " + event.getObject());
break;
case GROUP_MEMBER_REMOVE:
eventViewHolder.imageView.setImageResource(R.drawable.user_remove);
event.setDescription(event.getSubject() + " " + context.getString(R.string.GROUP_MEMBER_REMOVE) + " " + event.getObject());
break;
case EXPENSE_ADD:
eventViewHolder.imageView.setImageResource(R.drawable.expense_add);
event.setDescription(event.getSubject() + " " + context.getString(R.string.EXPENSE_ADD) + " \"" + event.getObject() + "\"");
break;
case EXPENSE_REMOVE:
eventViewHolder.imageView.setImageResource(R.drawable.expense_remove);
event.setDescription(event.getSubject() + " " + context.getString(R.string.EXPENSE_REMOVE) + " \"" + event.getObject() + "\"");
break;
case EXPENSE_EDIT:
eventViewHolder.imageView.setImageResource(R.drawable.expense_edit);
event.setDescription(event.getSubject() + " " + context.getString(R.string.EXPENSE_EDIT) + " \"" + event.getObject() + "\"");
break;
case PENDING_EXPENSE_ADD:
eventViewHolder.imageView.setImageResource(R.drawable.expense_add);
event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_ADD) + " \"" + event.getObject() + "\"");
break;
case PENDING_EXPENSE_REMOVE:
eventViewHolder.imageView.setImageResource(R.drawable.expense_remove);
event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_REMOVE) + " \"" + event.getObject() + "\"");
break;
case PENDING_EXPENSE_EDIT:
eventViewHolder.imageView.setImageResource(R.drawable.expense_edit);
event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_EDIT) + " \"" + event.getObject() + "\"");
break;
case PENDING_EXPENSE_VOTE_UP:
eventViewHolder.imageView.setImageResource(R.drawable.thumb_up_black);
event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_VOTE) + " \"" + event.getObject() + "\"");
break;
case PENDING_EXPENSE_VOTE_DOWN:
eventViewHolder.imageView.setImageResource(R.drawable.thumb_down_black);
event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_VOTE) + " \"" + event.getObject() + "\"");
break;
case PENDING_EXPENSE_APPROVED:
eventViewHolder.imageView.setImageResource(R.drawable.expense_add);
event.setDescription(context.getString(R.string.PENDING_EXPENSE) + " \"" + event.getObject() + "\" " + context.getString(R.string.PENDING_EXPENSE_APPROVED));
break;
case PENDING_EXPENSE_NEGLECTED:
eventViewHolder.imageView.setImageResource(R.drawable.pending_expense_neglected);
event.setDescription(context.getString(R.string.PENDING_EXPENSE) + " \"" + event.getObject() + "\" " + context.getString(R.string.PENDING_EXPENSE_NEGLECTED));
break;
/*case FRIEND_INVITE:
eventViewHolder.imageView.setImageResource(R.drawable.user_add);
event.setDescription(context.getString(R.string.FRIEND_INVITE) + " \"" + event.getObject() + "\"");
break;*/
case FRIEND_GROUP_INVITE:
eventViewHolder.imageView.setImageResource(R.drawable.user_add);
event.setDescription(context.getString(R.string.FRIEND_GROUP_INVITE) + " " + event.getObject());
break;
case // instead of the username the subject must be the ID of the user
USER_PAY:
eventViewHolder.imageView.setImageResource(R.drawable.user_pay);
if (currentUser.getID().equals(event.getSubject())) {
event.setDescription(context.getString(R.string.ME_PAY) + " " + event.getObject());
} else {
databaseReference.child("users").child(event.getSubject()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
event.setDescription(dataSnapshot.child("name").getValue(String.class) + " " + dataSnapshot.child("surname").getValue(String.class) + " " + context.getString(R.string.FRIEND_PAY) + " " + event.getObject());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, databaseError.toException());
}
});
}
break;
case // instead of the username the subject must be the ID of the user
USER_COMMENT_ADD:
eventViewHolder.imageView.setImageResource(R.drawable.user_comment);
if (currentUser.getID().equals(event.getSubject())) {
event.setDescription(context.getString(R.string.ME_COMMENT_ADD) + " \"" + event.getObject() + "\"");
} else {
databaseReference.child("users").child(event.getSubject()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
event.setDescription(dataSnapshot.child("name").getValue(String.class) + " " + dataSnapshot.child("surname").getValue(String.class) + " " + context.getString(R.string.FRIEND_COMMENT_ADD) + " \"" + event.getObject() + "\"");
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, databaseError.toException());
}
});
}
break;
default:
eventViewHolder.imageView.setImageResource(R.drawable.event_default);
event.setDescription("\"" + event.getSubject() + "\" " + context.getString(R.string.did) + " \"" + event.getObject() + "\"");
}
eventViewHolder.descriptionTextView.setText(event.getDescription());
eventViewHolder.timestampTextView.setText(context.getString(R.string.day) + " " + event.getDate() + " " + context.getString(R.string.at) + " " + event.getTime());
}
use of com.google.firebase.database.ValueEventListener in project MadMax by deviz92.
the class GroupsFragment method getGroupAndBalance.
/* void getGroupAndBalance (final String userID, final String groupID)
{
//final HashMap <String, Double> totalBalance = new HashMap<>();
final HashMap <String, Expense> groupExpenses = new HashMap<>();
//totalBalance.put(userID,0d);
//totBalance = 0d;
final HashMap <String, Double> totBalances = new HashMap<>();
totBalances.clear();
groupListener = databaseReference.child("groups").child(groupID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot groupDataSnapshot) {
//totalBalance.put(userID,0d);
totBalances.clear();
if (!listenedGroups.contains(groupID))
listenedGroups.add(groupID);
final Boolean deleted = groupDataSnapshot.child("deleted").getValue(Boolean.class);
if (deleted != null)
{
for (DataSnapshot groupExpenseSnapshot: groupDataSnapshot.child("expenses").getChildren())
{
//Contribuiscono al bilancio solo le spese non eliminate dal gruppo
if (groupExpenseSnapshot.getValue(Boolean.class) == true)
{
//Adesso sono sicuro che la spesa non è stata eliminata
//Ascolto la singola spesa del gruppo
String expenseID = groupExpenseSnapshot.getKey();
databaseReference.child("expenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Boolean involved = false; //dice se user contribuisce o no a quella spesa
for (DataSnapshot participantSnapshot: dataSnapshot.child("participants").getChildren())
{
//todo poi gestire caso in cui utente viene tolto dai participant alla spesa
if (participantSnapshot.getKey().equals(userID))
involved = true;
}
//se user ha partecipato alla spesa
if (involved)
{
//alreadyPaid = soldi già messi dallo user per quella spesa
//dueImport = quota che user deve mettere per quella spesa
//balance = credito/debito dello user per quella spesa
for (DataSnapshot d : dataSnapshot.child("participants").getChildren())
{
Log.d (TAG, "PartCampo " + d.getKey() + ": " + d.getValue() );
}
Double alreadyPaid = dataSnapshot.child("participants").child(userID).child("alreadyPaid").getValue(Double.class);
Log.d (TAG, "Fraction: " + Double.parseDouble(String.valueOf(dataSnapshot.child("participants").child(userID).child("fraction").getValue())));
Double dueImport = Double.parseDouble(String.valueOf(dataSnapshot.child("participants").child(userID).child("fraction").getValue())) * dataSnapshot.child("amount").getValue(Double.class);
if (alreadyPaid != null && dueImport != null)
{
Double balance = alreadyPaid - dueImport;
String currency = dataSnapshot.child("currency").getValue(String.class);
//se user per quella spesa ha già pagato più soldi della sua quota, il balance è positivo
//current balance for that currency
Double temp = totBalances.get(currency);
//update balance for that currency
if (temp != null)
{
totBalances.put(currency, temp + balance);
// Log.d (TAG, "Actual debt for " + groupName + ": " + totBalances.get(currency) + " " + currency);
}
else
{
totBalances.put(currency, balance);
// Log.d (TAG, "Actual debt for " + groupName + ": " + totBalances.get(currency) + " " + currency);
}
//Double currentBalance = totalBalance.get(userID);
//totalBalance.put(userID, currentBalance+balance);
//totBalance += balance;
Expense expense = new Expense();
expense.setID(dataSnapshot.getKey());
expense.setCurrency(dataSnapshot.child("currency").getValue(String.class));
expense.setAmount(dataSnapshot.child("amount").getValue(Double.class));
groupExpenses.put(dataSnapshot.getKey(), expense);
Log.d(TAG, "added expense " + dataSnapshot.getKey());
}
else
{
Log.d (TAG, "Qui sarebbe crashato");
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
Group group = new Group();
group.setName(groupDataSnapshot.child("name").getValue(String.class));
group.setImage(groupDataSnapshot.child("image").getValue(String.class));
group.setDeleted(deleted);
group.setBalance(0d);
group.setCurrencyBalances(totBalances);
group.setExpenses(groupExpenses);
if (!deleted &&
groupDataSnapshot.child("members").hasChild(MainActivity.getCurrentUID()) &&
!groupDataSnapshot.child("members").child(MainActivity.getCurrentUID()).child("deleted").getValue(Boolean.class)) {
groups.put(groupID, group);
}
else
{
groups.remove(groupID);
}
// Group nullGroup = new Group();
// nullGroup.setID(groupID + 1);
// groups.put(nullGroup.getID(), nullGroup);
groupsViewAdapter.update(groups);
groupsViewAdapter.notifyDataSetChanged();
//totBalance = 0d;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return ;
}*/
// versione di ale
// /versione più complessa dell getGroup. Quest'ultima non verrà più usata.
// oltre al nome gruppo, prende anche il bilancio dello user col gruppo
void getGroupAndBalance(final String userID, final String groupID) {
// final HashMap <String, Double> totalBalance = new HashMap<>();
final HashMap<String, Expense> groupExpenses = new HashMap<>();
// totalBalance.put(userID,0d);
// totBalance = 0d;
final HashMap<String, Double> totBalances = new HashMap<>();
totBalances.clear();
groupListener = databaseReference.child("groups").child(groupID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot groupDataSnapshot) {
// totalBalance.put(userID,0d);
totBalances.clear();
if (!listenedGroups.contains(groupID))
listenedGroups.add(groupID);
final String groupName = groupDataSnapshot.child("name").getValue(String.class);
final Boolean deleted = groupDataSnapshot.child("deleted").getValue(Boolean.class);
if (deleted != null) {
for (DataSnapshot groupExpenseSnapshot : groupDataSnapshot.child("expenses").getChildren()) {
// Contribuiscono al bilancio solo le spese non eliminate dal gruppo
if (groupExpenseSnapshot.getValue(Boolean.class) == true) {
// Adesso sono sicuro che la spesa non è stata eliminata
// Ascolto la singola spesa del gruppo
String expenseID = groupExpenseSnapshot.getKey();
databaseReference.child("expenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// dice se user contribuisce o no a quella spesa
Boolean involved = false;
for (DataSnapshot participantSnapshot : dataSnapshot.child("participants").getChildren()) {
// todo poi gestire caso in cui utente viene tolto dai participant alla spesa
if (participantSnapshot.getKey().equals(userID))
involved = true;
}
// se user ha partecipato alla spesa
if (involved) {
// balance = credito/debito dello user per quella spesa
for (DataSnapshot d : dataSnapshot.child("participants").getChildren()) {
Log.d(TAG, "PartCampo " + d.getKey() + ": " + d.getValue());
}
Double alreadyPaid = dataSnapshot.child("participants").child(userID).child("alreadyPaid").getValue(Double.class);
// If has been made to avoid crash
if (dataSnapshot.child("participants").child(userID).hasChild("fraction") && dataSnapshot.hasChild("amount")) {
Log.d(TAG, "Fraction: " + Double.parseDouble(String.valueOf(dataSnapshot.child("participants").child(userID).child("fraction").getValue())));
Double dueImport = Double.parseDouble(String.valueOf(dataSnapshot.child("participants").child(userID).child("fraction").getValue())) * dataSnapshot.child("amount").getValue(Double.class);
if (alreadyPaid != null && dueImport != null) {
Double balance = alreadyPaid - dueImport;
String currency = dataSnapshot.child("currency").getValue(String.class);
// se user per quella spesa ha già pagato più soldi della sua quota, il balance è positivo
// current balance for that currency
Double temp = totBalances.get(currency);
// update balance for that currency
if (temp != null) {
totBalances.put(currency, temp + balance);
Log.d(TAG, "Actual debt for " + groupName + ": " + totBalances.get(currency) + " " + currency);
} else {
totBalances.put(currency, balance);
Log.d(TAG, "Actual debt for " + groupName + ": " + totBalances.get(currency) + " " + currency);
}
// Double currentBalance = totalBalance.get(userID);
// totalBalance.put(userID, currentBalance+balance);
// totBalance += balance;
Expense expense = new Expense();
expense.setID(dataSnapshot.getKey());
expense.setCurrency(dataSnapshot.child("currency").getValue(String.class));
expense.setAmount(dataSnapshot.child("amount").getValue(Double.class));
groupExpenses.put(dataSnapshot.getKey(), expense);
Log.d(TAG, "added expense " + dataSnapshot.getKey());
} else {
Log.d(TAG, "Qui sarebbe crashato");
}
}
}
Group g = new Group();
g.setName(groupName);
// g.setBalance(totalBalance.get(userID));
g.setCurrencyBalances(totBalances);
g.setExpenses(groupExpenses);
g.setDeleted(deleted);
g.setImage(groupDataSnapshot.child("image").getValue(String.class));
// se il gruppo non è deleted e io faccio ancora parte del gruppo
if (!deleted && groupDataSnapshot.child("members").hasChild(MainActivity.getCurrentUID()) && !groupDataSnapshot.child("members").child(MainActivity.getCurrentUID()).child("deleted").getValue(Boolean.class)) {
groups.put(groupID, g);
} else {
groups.remove(groupID);
}
groupsViewAdapter.update(groups);
groupsViewAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
// Per gestire il caso di quando ho appena abbandonato o eliminato il gruppo
// if (dataSnapshot.child("members").hasChild(userID))
// {
Group g = new Group();
g.setName(groupName);
g.setImage(groupDataSnapshot.child("image").getValue(String.class));
g.setBalance(0d);
g.setDeleted(deleted);
if (!deleted && groupDataSnapshot.child("members").hasChild(MainActivity.getCurrentUID()) && !groupDataSnapshot.child("members").child(MainActivity.getCurrentUID()).child("deleted").getValue(Boolean.class)) {
groups.put(groupID, g);
} else {
groups.remove(groupID);
}
// Group nullGroup = new Group();
// nullGroup.setID(groupID + 1);
// groups.put(nullGroup.getID(), nullGroup);
groupsViewAdapter.update(groups);
groupsViewAdapter.notifyDataSetChanged();
// totBalance = 0d;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return;
}
use of com.google.firebase.database.ValueEventListener in project MadMax by deviz92.
the class GroupsFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i(TAG, "onCreateView");
setInterface((OnItemClickInterface) getActivity(), (OnItemLongClickInterface) getActivity());
final View view = inflater.inflate(R.layout.skeleton_list, container, false);
databaseReference = FirebaseDatabase.getInstance().getReference();
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);
groups.clear();
groupsViewAdapter = new GroupsViewAdapter(this.getContext(), this, this, groups, GroupsFragment.TAG);
recyclerView.setAdapter(groupsViewAdapter);
// Ascolto i gruppi dello user
databaseReference.child("users").child(MainActivity.getCurrentUID()).child("groups").addValueEventListener(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
try {
Boolean trueGroup = (Boolean) groupSnapshot.getValue();
if (trueGroup)
getGroupAndBalance(MainActivity.getCurrentUID(), groupSnapshot.getKey());
else {
// tolgo il gruppo da quelli che verranno stampati, così lo vedo sparire realtime
groups.remove(groupSnapshot.getKey());
groupsViewAdapter.update(groups);
groupsViewAdapter.notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, groupSnapshot.getValue().toString());
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, databaseError.toException());
}
});
return view;
}
use of com.google.firebase.database.ValueEventListener in project SocialRec by Jkuras.
the class MainActivity method getPosts.
// sets listener at db/posts/deals/:all
// loads posts into mPosts, listener calls setUI
public void getPosts() {
mref = mDataBase.getReference("posts/deals");
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// initialize array of all posts and add them to mPosts
mPosts = new ArrayList<DealPost>();
for (DataSnapshot child : dataSnapshot.getChildren()) {
mPosts.add((DealPost) child.getValue(DealPost.class));
}
setUI();
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
};
mref.addListenerForSingleValueEvent(postListener);
}
Aggregations