use of com.google.firebase.database.DatabaseError in project MadMax by deviz92.
the class User method addFriend.
public void addFriend(final String friendID) {
final DatabaseReference databaseReference = FirebaseUtils.getDatabaseReference();
final DatabaseReference usersRef = databaseReference.child("users");
final DatabaseReference groupsRef = databaseReference.child("groups");
final String currentUID = this.getID();
// getting friend data from db
DatabaseReference friendRef = usersRef.child(friendID);
User friend = new User(friendID, friendRef.child("username").toString(), friendRef.child("name").toString(), friendRef.child("surname").toString(), friendRef.child("email").toString(), friendRef.child("profileImage").toString());
// Add friendID to friend list of currentUID
// add friend to current user local HashMap
this.userFriends.put(friendID, friend);
usersRef.child(currentUID).child("friends").push();
usersRef.child(currentUID).child("friends").child(friendID).child("deleted").setValue(false);
// Add currentUID to friend list of friendID
usersRef.child(friendID).child("friends").push();
usersRef.child(friendID).child("friends").child(currentUID).child("deleted").setValue(false);
// Read groups currentUID belongs to
Query query = databaseReference.child("users").child(currentUID).child("groups");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final ArrayList<String> u1Groups = new ArrayList<String>();
for (DataSnapshot groupSnapshot : dataSnapshot.getChildren()) {
Log.d(TAG, groupSnapshot.getKey() + " : " + groupSnapshot.getValue());
if (groupSnapshot.getValue(Boolean.class))
u1Groups.add(groupSnapshot.getKey());
}
Query query = databaseReference.child("users").child(friendID).child("groups");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<String> sharedGroups = new ArrayList<String>();
for (DataSnapshot groupSnapshot : dataSnapshot.getChildren()) {
if (u1Groups.contains(groupSnapshot.getKey()) && groupSnapshot.getValue(Boolean.class))
sharedGroups.add(groupSnapshot.getKey());
}
final HashMap<String, Group> groups = new HashMap<>();
// ora in sharedGroups ci sono solo i gruppi di cui fanno parte entrambi gli utenti
for (String groupID : sharedGroups) {
// getting group data from db
DatabaseReference groupRef = groupsRef.child(groupID);
Group group = new Group(groupID, groupRef.child("name").toString(), groupRef.child("image").toString(), groupRef.child("description").toString(), Integer.parseInt(groupRef.child("numberMembers").toString()));
groups.put(friendID, group);
usersRef.child(currentUID).child("friends").child(friendID).child(groupID).setValue(true);
usersRef.child(friendID).child("friends").child(currentUID).child(groupID).setValue(true);
}
// add shared groups to local sharedGroupPerFriend HashMap
sharedGroupPerFriend.put(friendID, groups);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w("addFriendFirebase", databaseError.getMessage());
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w("addFriendFirebase", databaseError.getMessage());
}
});
}
use of com.google.firebase.database.DatabaseError in project iNGAGE by davis123123.
the class ChatActivity method onUpvoteClick.
//modify unblock functions here
@Override
public void onUpvoteClick(int p) {
//Log.d("vote" , "up : ");
//get correct chat msg with ith key from chatmessage helper
ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
String chat_key = chatMessageHelper.getMessageID();
DatabaseReference message_root = root.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(1);
} 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.DatabaseError in project iNGAGE by davis123123.
the class ChatFragment method onUpvoteClick.
@Override
public void onUpvoteClick(int p) {
// Log.d("vote" , "up : ");
// get correct chat msg with ith key from chatmessage helper
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(1);
} 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.DatabaseError 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.DatabaseError 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;
}
Aggregations