use of com.google.firebase.database.DataSnapshot in project iNGAGE by davis123123.
the class ChatFragment method appendChatConversation.
private void appendChatConversation(DataSnapshot dataSnapshot) {
Iterator i = dataSnapshot.getChildren().iterator();
Log.d("STATE", "pageData2 : " + dataSnapshot);
while (i.hasNext()) {
chat_id = dataSnapshot.getKey();
Log.d("STATE", "result : " + chat_id);
chat_msg = (String) ((DataSnapshot) i.next()).getValue();
chat_side = (String) ((DataSnapshot) i.next()).getValue();
chat_timestamp = (String) ((DataSnapshot) i.next()).getValue();
chat_username = (String) ((DataSnapshot) i.next()).getValue();
chat_downvote = (Long) ((DataSnapshot) i.next()).getValue();
chat_upvote = (Long) ((DataSnapshot) i.next()).getValue();
String chat_userVote;
chat_userVote = userVotes.get(chat_id);
Log.d("CHATVOTE", "result : " + chat_userVote);
ChatMessageHelper msg = new ChatMessageHelper(chat_id, chat_side, chat_msg, chat_username, chat_upvote, chat_downvote, chat_timestamp, chat_userVote);
chatAdapter.add(msg);
}
chatAdapter.notifyDataSetChanged();
recyclerView.scrollToPosition(chatAdapter.getItemCount() - 1);
}
use of com.google.firebase.database.DataSnapshot in project iNGAGE by davis123123.
the class ChatFragment method removeUpvote.
@Override
public void removeUpvote(int p) {
ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
String chat_key = chatMessageHelper.getMessageID();
DatabaseReference message_root = currPageData.child(chat_key);
// get upvote data
DatabaseReference upvote_count = message_root.child("upvotes");
upvote_count.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData currentData) {
Log.d("Data", String.valueOf(currentData));
if (currentData.getValue() == null) {
currentData.setValue(0);
} else {
currentData.setValue((Long) currentData.getValue() - 1);
}
// we can also abort by calling Transaction.abort()
return Transaction.success(currentData);
}
// TODO:Error handle here
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot 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) {
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.getCurrentUID()).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;
}
use of com.google.firebase.database.DataSnapshot 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.DataSnapshot 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;
}
Aggregations