Search in sources :

Example 21 with DataSnapshot

use of com.google.firebase.database.DataSnapshot 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);
        }
    });
}
Also used : DatabaseReference(com.google.firebase.database.DatabaseReference) DataSnapshot(com.google.firebase.database.DataSnapshot) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.google.firebase.database.Transaction) MutableData(com.google.firebase.database.MutableData) SimpleDateFormat(java.text.SimpleDateFormat)

Example 22 with DataSnapshot

use of com.google.firebase.database.DataSnapshot in project iNGAGE by davis123123.

the class ChatActivity method removeDownvote.

@Override
public void removeDownvote(int p) {
    ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
    String chat_key = chatMessageHelper.getMessageID();
    DatabaseReference message_root = root.child(chat_key);
    //get upvote data
    DatabaseReference downvote_count = message_root.child("downvotes");
    downvote_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) {
        }
    });
}
Also used : ChatMessageHelper(ingage.ingage20.helpers.ChatMessageHelper) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.google.firebase.database.Transaction) DatabaseReference(com.google.firebase.database.DatabaseReference) MutableData(com.google.firebase.database.MutableData) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 23 with DataSnapshot

use of com.google.firebase.database.DataSnapshot in project quickstart-android by firebase.

the class NewPostActivity method submitPost.

private void submitPost() {
    final String title = mTitleField.getText().toString();
    final String body = mBodyField.getText().toString();
    // Title is required
    if (TextUtils.isEmpty(title)) {
        mTitleField.setError(REQUIRED);
        return;
    }
    // Body is required
    if (TextUtils.isEmpty(body)) {
        mBodyField.setError(REQUIRED);
        return;
    }
    // Disable button so there are no multi-posts
    setEditingEnabled(false);
    Toast.makeText(this, "Posting...", Toast.LENGTH_SHORT).show();
    // [START single_value_read]
    final String userId = getUid();
    mDatabase.child("users").child(userId).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get user value
            User user = dataSnapshot.getValue(User.class);
            // [START_EXCLUDE]
            if (user == null) {
                // User is null, error out
                Log.e(TAG, "User " + userId + " is unexpectedly null");
                Toast.makeText(NewPostActivity.this, "Error: could not fetch user.", Toast.LENGTH_SHORT).show();
            } else {
                // Write new post
                writeNewPost(userId, user.username, title, body);
            }
            // Finish this Activity, back to the stream
            setEditingEnabled(true);
            finish();
        // [END_EXCLUDE]
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, "getUser:onCancelled", databaseError.toException());
            // [START_EXCLUDE]
            setEditingEnabled(true);
        // [END_EXCLUDE]
        }
    });
// [END single_value_read]
}
Also used : User(com.google.firebase.quickstart.database.models.User) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 24 with DataSnapshot

use of com.google.firebase.database.DataSnapshot in project quickstart-android by firebase.

the class PostDetailActivity method onStart.

@Override
public void onStart() {
    super.onStart();
    // Add value event listener to the post
    // [START post_value_event_listener]
    ValueEventListener postListener = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get Post object and use the values to update the UI
            Post post = dataSnapshot.getValue(Post.class);
            // [START_EXCLUDE]
            mAuthorView.setText(post.author);
            mTitleView.setText(post.title);
            mBodyView.setText(post.body);
        // [END_EXCLUDE]
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Getting Post failed, log a message
            Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
            // [START_EXCLUDE]
            Toast.makeText(PostDetailActivity.this, "Failed to load post.", Toast.LENGTH_SHORT).show();
        // [END_EXCLUDE]
        }
    };
    mPostReference.addValueEventListener(postListener);
    // [END post_value_event_listener]
    // Keep copy of post listener so we can remove it when app stops
    mPostListener = postListener;
    // Listen for comments
    mAdapter = new CommentAdapter(this, mCommentsReference);
    mCommentsRecycler.setAdapter(mAdapter);
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Post(com.google.firebase.quickstart.database.models.Post) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 25 with DataSnapshot

use of com.google.firebase.database.DataSnapshot 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());
            }
        }
    });
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.google.firebase.database.Transaction) DatabaseReference(com.google.firebase.database.DatabaseReference) List(java.util.List) LinkedList(java.util.LinkedList) MutableData(com.google.firebase.database.MutableData) DataSnapshot(com.google.firebase.database.DataSnapshot)

Aggregations

DataSnapshot (com.google.firebase.database.DataSnapshot)47 DatabaseError (com.google.firebase.database.DatabaseError)40 ValueEventListener (com.google.firebase.database.ValueEventListener)34 DatabaseReference (com.google.firebase.database.DatabaseReference)14 View (android.view.View)13 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)11 RecyclerView (android.support.v7.widget.RecyclerView)11 Intent (android.content.Intent)10 Bundle (android.os.Bundle)10 User (com.polito.mad17.madmax.entities.User)10 MutableData (com.google.firebase.database.MutableData)6 Transaction (com.google.firebase.database.Transaction)6 SimpleDateFormat (java.text.SimpleDateFormat)6 Event (com.polito.mad17.madmax.entities.Event)5 ChatMessageHelper (ingage.ingage20.helpers.ChatMessageHelper)5 HashMap (java.util.HashMap)5 TextView (android.widget.TextView)4 ArrayList (java.util.ArrayList)4 ActionBar (android.support.v7.app.ActionBar)3 MenuItem (android.view.MenuItem)3