use of com.google.firebase.database.Transaction in project MadMax by deviz92.
the class User method joinGroup.
public void joinGroup(String groupID, String inviterUID) {
final DatabaseReference databaseReference = FirebaseUtils.getDatabaseReference();
final String currentUID = this.getID();
//Aggiungo gruppo alla lista gruppi dello user
databaseReference.child("users").child(currentUID).child("groups").push();
databaseReference.child("users").child(currentUID).child("groups").child(groupID).setValue("true");
//Aggiungo user (con sottocampi admin e timestamp) alla lista membri del gruppo
databaseReference.child("groups").child(groupID).child("members").push();
databaseReference.child("groups").child(groupID).child("members").child(currentUID).push();
databaseReference.child("groups").child(groupID).child("members").child(currentUID).child("admin").setValue(false);
databaseReference.child("groups").child(groupID).child("members").child(currentUID).push();
String timestamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
databaseReference.child("groups").child(groupID).child("members").child(currentUID).child("timestamp").setValue(timestamp);
// aggiunto da riky
databaseReference.child("groups").child(groupID).child("members").child(currentUID).push();
databaseReference.child("groups").child(groupID).child("members").child(currentUID).child("deleted").setValue(false);
// aggiungo l'invitante agli amici se non lo è già
if (!userFriends.containsKey(inviterUID)) {
//todo aggiungere l'invitato tra gli amici dell'invitante
addFriend(inviterUID);
}
//Incremento il numero di partecipanti
databaseReference.child("groups").child(groupID).child("numberMembers").runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer numberMembers = mutableData.getValue(Integer.class);
if (numberMembers == null) {
return Transaction.success(mutableData);
}
// Set value and report transaction success
mutableData.setValue(numberMembers + 1);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
// Transaction completed
Log.d(TAG, "postTransaction:onComplete:" + databaseError);
}
});
}
use of com.google.firebase.database.Transaction in project pratilipi by Pratilipi.
the class FirebaseApi method updateUserNotificationData.
public static void updateUserNotificationData(Long userId, final List<Long> notifIdListToAdd, final List<Long> notifIdListToRemove, final Async async) {
initialiseFirebase();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child(DATABASE_NOTIFICATION_TABLE).child(userId.toString());
databaseReference.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
// Current list of notificationIds with Firebase
List<Long> notifIdList = new LinkedList<>();
if (mutableData.getValue() != null) {
NotificationDB notifDB = mutableData.getValue(NotificationDB.class);
if (notifDB.getNewNotificationCount() > 0)
notifIdList = notifDB.getNotificationIdList();
}
// Add/Remove notificationIds
// Remove ids first to avoid duplicates
notifIdList.removeAll(notifIdListToAdd);
notifIdList.removeAll(notifIdListToRemove);
notifIdList.addAll(notifIdListToAdd);
// Updating Firebase
mutableData.setValue(new NotificationDB(notifIdList));
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean committed, DataSnapshot dataSnapshot) {
if (committed) {
// Transaction successful
async.exec();
// } else if( databaseError == null ) { // Transaction aborted
} else {
// Transaction failed
logger.log(Level.SEVERE, "Transaction failed with error code : " + databaseError.getCode());
}
}
});
}
Aggregations