Search in sources :

Example 11 with DatabaseReference

use of com.google.firebase.database.DatabaseReference 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 12 with DatabaseReference

use of com.google.firebase.database.DatabaseReference 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 13 with DatabaseReference

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

the class PostListFragment method onActivityCreated.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Set up Layout Manager, reverse layout
    mManager = new LinearLayoutManager(getActivity());
    mManager.setReverseLayout(true);
    mManager.setStackFromEnd(true);
    mRecycler.setLayoutManager(mManager);
    // Set up FirebaseRecyclerAdapter with the Query
    Query postsQuery = getQuery(mDatabase);
    mAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(Post.class, R.layout.item_post, PostViewHolder.class, postsQuery) {

        @Override
        protected void populateViewHolder(final PostViewHolder viewHolder, final Post model, final int position) {
            final DatabaseReference postRef = getRef(position);
            // Set click listener for the whole post view
            final String postKey = postRef.getKey();
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // Launch PostDetailActivity
                    Intent intent = new Intent(getActivity(), PostDetailActivity.class);
                    intent.putExtra(PostDetailActivity.EXTRA_POST_KEY, postKey);
                    startActivity(intent);
                }
            });
            // Determine if the current user has liked this post and set UI accordingly
            if (model.stars.containsKey(getUid())) {
                viewHolder.starView.setImageResource(R.drawable.ic_toggle_star_24);
            } else {
                viewHolder.starView.setImageResource(R.drawable.ic_toggle_star_outline_24);
            }
            // Bind Post to ViewHolder, setting OnClickListener for the star button
            viewHolder.bindToPost(model, new View.OnClickListener() {

                @Override
                public void onClick(View starView) {
                    // Need to write to both places the post is stored
                    DatabaseReference globalPostRef = mDatabase.child("posts").child(postRef.getKey());
                    DatabaseReference userPostRef = mDatabase.child("user-posts").child(model.uid).child(postRef.getKey());
                    // Run two transactions
                    onStarClicked(globalPostRef);
                    onStarClicked(userPostRef);
                }
            });
        }
    };
    mRecycler.setAdapter(mAdapter);
}
Also used : PostViewHolder(com.google.firebase.quickstart.database.viewholder.PostViewHolder) Query(com.google.firebase.database.Query) DatabaseReference(com.google.firebase.database.DatabaseReference) Post(com.google.firebase.quickstart.database.models.Post) Intent(android.content.Intent) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 14 with DatabaseReference

use of com.google.firebase.database.DatabaseReference 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 15 with DatabaseReference

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

the class UserHelper method transferData.

public void transferData(String prevUid) {
    if (TextUtils.isEmpty(prevUid))
        return;
    DatabaseReference prevTeamRef = FIREBASE_TEAM_INDICES.child(prevUid);
    new FirebaseCopier(prevTeamRef, TeamHelper.getIndicesRef()) {

        @Override
        public void onDataChange(DataSnapshot snapshot) {
            super.onDataChange(snapshot);
            prevTeamRef.removeValue();
        }
    }.performTransformation();
    DatabaseReference prevScoutTemplatesRef = getScoutTemplateIndicesRef(prevUid);
    new FirebaseCopier(prevScoutTemplatesRef, getScoutTemplateIndicesRef()) {

        @Override
        public void onDataChange(DataSnapshot snapshot) {
            super.onDataChange(snapshot);
            prevScoutTemplatesRef.removeValue();
        }
    }.performTransformation();
}
Also used : DatabaseReference(com.google.firebase.database.DatabaseReference) DataSnapshot(com.google.firebase.database.DataSnapshot)

Aggregations

DatabaseReference (com.google.firebase.database.DatabaseReference)25 DataSnapshot (com.google.firebase.database.DataSnapshot)13 DatabaseError (com.google.firebase.database.DatabaseError)13 RecyclerView (android.support.v7.widget.RecyclerView)6 View (android.view.View)6 MutableData (com.google.firebase.database.MutableData)6 Transaction (com.google.firebase.database.Transaction)6 ValueEventListener (com.google.firebase.database.ValueEventListener)6 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)5 Intent (android.content.Intent)4 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)4 Query (com.google.firebase.database.Query)4 ChatMessageHelper (ingage.ingage20.helpers.ChatMessageHelper)4 Bundle (android.os.Bundle)2 TextView (android.widget.TextView)2 Event (com.polito.mad17.madmax.entities.Event)2 User (com.polito.mad17.madmax.entities.User)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2