Search in sources :

Example 41 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)

Example 42 with DataSnapshot

use of com.google.firebase.database.DataSnapshot in project Robot-Scouter by SUPERCILEX.

the class ScoutPagerAdapter method onDataChange.

@Override
public void onDataChange(DataSnapshot snapshot) {
    removeNameListeners();
    boolean hadScouts = !mKeys.isEmpty();
    mKeys.clear();
    for (DataSnapshot scoutIndex : snapshot.getChildren()) {
        String key = scoutIndex.getKey();
        mKeys.add(0, key);
        getTabNameRef(key).addValueEventListener(mTabNameListener);
    }
    if (hadScouts && mKeys.isEmpty() && !isOffline(mFragment.getContext()) && mFragment.isResumed()) {
        ShouldDeleteTeamDialog.Companion.show(mFragment.getChildFragmentManager(), mTeamHelper);
    }
    mFragment.getView().findViewById(R.id.no_content_hint).setVisibility(mKeys.isEmpty() ? View.VISIBLE : View.GONE);
    mAppBarViewHolder.setDeleteScoutMenuItemVisible(!mKeys.isEmpty());
    mTabLayout.removeOnTabSelectedListener(this);
    notifyDataSetChanged();
    mTabLayout.addOnTabSelectedListener(this);
    if (!mKeys.isEmpty()) {
        if (TextUtils.isEmpty(mCurrentScoutKey)) {
            selectTab(0);
            mCurrentScoutKey = mKeys.get(0);
        } else {
            selectTab(mKeys.indexOf(mCurrentScoutKey));
        }
    }
}
Also used : DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 43 with DataSnapshot

use of com.google.firebase.database.DataSnapshot in project priend by TakoJ.

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 = mAuth.getCurrentUser().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.example.management.models.User) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 44 with DataSnapshot

use of com.google.firebase.database.DataSnapshot in project priend by TakoJ.

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.example.management.models.Post) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 45 with DataSnapshot

use of com.google.firebase.database.DataSnapshot in project priend by TakoJ.

the class PostDetailActivity method postComment.

private void postComment() {
    final String uid = mAuth.getCurrentUser().getUid();
    FirebaseDatabase.getInstance().getReference().child("users").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get user information
            User user = dataSnapshot.getValue(User.class);
            String authorName = user.username;
            // Create new comment object
            String commentText = mCommentField.getText().toString();
            Comment comment = new Comment(uid, authorName, commentText);
            // Push the comment, it will appear in the list
            mCommentsReference.push().setValue(comment);
            // Clear the field
            mCommentField.setText(null);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Also used : Comment(com.example.management.models.Comment) User(com.example.management.models.User) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Aggregations

DataSnapshot (com.google.firebase.database.DataSnapshot)69 DatabaseError (com.google.firebase.database.DatabaseError)59 ValueEventListener (com.google.firebase.database.ValueEventListener)44 DatabaseReference (com.google.firebase.database.DatabaseReference)22 View (android.view.View)20 Intent (android.content.Intent)15 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)12 RecyclerView (android.support.v7.widget.RecyclerView)12 User (com.polito.mad17.madmax.entities.User)12 ChatMessageHelper (ingage.ingage20.helpers.ChatMessageHelper)12 MutableData (com.google.firebase.database.MutableData)11 Transaction (com.google.firebase.database.Transaction)11 Bundle (android.os.Bundle)10 TextView (android.widget.TextView)8 HashMap (java.util.HashMap)7 SimpleDateFormat (java.text.SimpleDateFormat)6 Map (java.util.Map)6 ImageView (android.widget.ImageView)5 ChildEventListener (com.google.firebase.database.ChildEventListener)5 Event (com.polito.mad17.madmax.entities.Event)5