Search in sources :

Example 21 with DatabaseReference

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

the class ChatActivity method sendMsg.

private void sendMsg() {
    timer(180000);
    String messageText = textField.getText().toString();
    HashMap<String, String> user = session.getUserDetails();
    String messageBy = user.get(SessionManager.KEY_NAME);
    //firebase area to send msg
    Map<String, Object> map = new HashMap<String, Object>();
    temp_key = root.push().getKey();
    root.updateChildren(map);
    String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
    DatabaseReference message_root = root.child(temp_key);
    Map<String, Object> map_message = new HashMap<String, Object>();
    map_message.put("Username", messageBy);
    map_message.put("Msg", messageText);
    map_message.put("Side", user_side);
    map_message.put("upvotes", 0);
    map_message.put("downvotes", 0);
    map_message.put("TimeStamp", currentDateTimeString);
    message_root.updateChildren(map_message);
    //send token
    if (tagged) {
        tagged = false;
        sendCoin();
    }
    textField.setText("");
    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
    int pos = chatAdapter.getItemCount() - 1;
    manager.scrollToPosition(pos);
}
Also used : HashMap(java.util.HashMap) DatabaseReference(com.google.firebase.database.DatabaseReference) RecyclerView(android.support.v7.widget.RecyclerView) Date(java.util.Date)

Example 22 with DatabaseReference

use of com.google.firebase.database.DatabaseReference 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());
        }
    });
}
Also used : Query(com.google.firebase.database.Query) DatabaseReference(com.google.firebase.database.DatabaseReference) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataSnapshot(com.google.firebase.database.DataSnapshot) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener)

Example 23 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project MadMax by deviz92.

the class EventsViewAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(final EventViewHolder eventViewHolder, int position) {
    final Event event = getItem(position).getValue();
    User currentUser = MainActivity.getCurrentUser();
    DatabaseReference databaseReference = FirebaseUtils.getDatabaseReference();
    switch(event.getEventType()) {
        case GROUP_ADD:
            eventViewHolder.imageView.setImageResource(R.drawable.group_add);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.GROUP_ADD) + " \"" + event.getObject() + "\"");
            break;
        /*case GROUP_REMOVE:
                eventViewHolder.imageView.setImageResource(R.drawable.group_remove);
                event.setDescription("\"" + event.getSubject() + "\" " + context.getString(R.string.GROUP_REMOVE) + " \"" + event.getObject() + "\"");
                break;*/
        case GROUP_EDIT:
            eventViewHolder.imageView.setImageResource(R.drawable.group_edit);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.GROUP_EDIT) + " \"" + event.getObject() + "\"");
            break;
        case GROUP_MEMBER_ADD:
            eventViewHolder.imageView.setImageResource(R.drawable.user_add);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.GROUP_MEMBER_ADD) + " " + event.getObject());
            break;
        case GROUP_MEMBER_REMOVE:
            eventViewHolder.imageView.setImageResource(R.drawable.user_remove);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.GROUP_MEMBER_REMOVE) + " " + event.getObject());
            break;
        case EXPENSE_ADD:
            eventViewHolder.imageView.setImageResource(R.drawable.expense_add);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.EXPENSE_ADD) + " \"" + event.getObject() + "\"");
            break;
        case EXPENSE_REMOVE:
            eventViewHolder.imageView.setImageResource(R.drawable.expense_remove);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.EXPENSE_REMOVE) + " \"" + event.getObject() + "\"");
            break;
        case EXPENSE_EDIT:
            eventViewHolder.imageView.setImageResource(R.drawable.expense_edit);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.EXPENSE_EDIT) + " \"" + event.getObject() + "\"");
            break;
        case PENDING_EXPENSE_ADD:
            eventViewHolder.imageView.setImageResource(R.drawable.expense_add);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_ADD) + " \"" + event.getObject() + "\"");
            break;
        case PENDING_EXPENSE_REMOVE:
            eventViewHolder.imageView.setImageResource(R.drawable.expense_remove);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_REMOVE) + " \"" + event.getObject() + "\"");
            break;
        case PENDING_EXPENSE_EDIT:
            eventViewHolder.imageView.setImageResource(R.drawable.expense_edit);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_EDIT) + " \"" + event.getObject() + "\"");
            break;
        case PENDING_EXPENSE_VOTE_UP:
            eventViewHolder.imageView.setImageResource(R.drawable.thumb_up_black);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_VOTE) + " \"" + event.getObject() + "\"");
            break;
        case PENDING_EXPENSE_VOTE_DOWN:
            eventViewHolder.imageView.setImageResource(R.drawable.thumb_down_black);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_VOTE) + " \"" + event.getObject() + "\"");
            break;
        case PENDING_EXPENSE_APPROVED:
            eventViewHolder.imageView.setImageResource(R.drawable.expense_add);
            event.setDescription(context.getString(R.string.PENDING_EXPENSE) + " \"" + event.getObject() + "\" " + context.getString(R.string.PENDING_EXPENSE_APPROVED));
            break;
        case PENDING_EXPENSE_NEGLECTED:
            eventViewHolder.imageView.setImageResource(R.drawable.pending_expense_neglected);
            event.setDescription(context.getString(R.string.PENDING_EXPENSE) + " \"" + event.getObject() + "\" " + context.getString(R.string.PENDING_EXPENSE_NEGLECTED));
            break;
        /*case FRIEND_INVITE:
                eventViewHolder.imageView.setImageResource(R.drawable.user_add);
                event.setDescription(context.getString(R.string.FRIEND_INVITE) + " \"" + event.getObject() + "\"");
                break;*/
        case FRIEND_GROUP_INVITE:
            eventViewHolder.imageView.setImageResource(R.drawable.user_add);
            event.setDescription(context.getString(R.string.FRIEND_GROUP_INVITE) + " " + event.getObject());
            break;
        case // instead of the username the subject must be the ID of the user
        USER_PAY:
            // todo da mettere currency
            eventViewHolder.imageView.setImageResource(R.drawable.user_pay);
            if (currentUser.getID().equals(event.getSubject())) {
                event.setDescription(context.getString(R.string.ME_PAY) + " " + event.getAmount());
            } else {
                databaseReference.child("users").child(event.getSubject()).addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        event.setDescription(dataSnapshot.child("name").getValue(String.class) + " " + dataSnapshot.child("surname").getValue(String.class) + " " + context.getString(R.string.FRIEND_PAY) + " " + event.getAmount());
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Log.w(TAG, databaseError.toException());
                    }
                });
            }
            break;
        case // instead of the username the subject must be the ID of the user
        USER_COMMENT_ADD:
            eventViewHolder.imageView.setImageResource(R.drawable.user_comment);
            if (currentUser.getID().equals(event.getSubject())) {
                event.setDescription(context.getString(R.string.ME_COMMENT_ADD) + " \"" + event.getObject() + "\"");
            } else {
                databaseReference.child("users").child(event.getSubject()).addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        event.setDescription(dataSnapshot.child("name").getValue(String.class) + " " + dataSnapshot.child("surname").getValue(String.class) + " " + context.getString(R.string.FRIEND_COMMENT_ADD) + " \"" + event.getObject() + "\"");
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Log.w(TAG, databaseError.toException());
                    }
                });
            }
            break;
        default:
            eventViewHolder.imageView.setImageResource(R.drawable.event_default);
            event.setDescription("\"" + event.getSubject() + "\" " + context.getString(R.string.did) + " \"" + event.getObject() + "\"");
    }
    eventViewHolder.descriptionTextView.setText(event.getDescription());
    eventViewHolder.timestampTextView.setText(context.getString(R.string.day) + " " + event.getDate() + " " + context.getString(R.string.at) + " " + event.getTime());
}
Also used : User(com.polito.mad17.madmax.entities.User) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) Event(com.polito.mad17.madmax.entities.Event) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 24 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project MadMax by deviz92.

the class FirebaseUtils method addComment.

/*
        END EVENT
     */
/*
        COMMENT
     */
public String addComment(Comment comment) {
    Log.d(TAG, "addComment");
    final String ID = databaseReference.child("comments").push().getKey();
    DatabaseReference commentReference = databaseReference.child("comments").child(ID);
    DatabaseReference expenseReference = databaseReference.child("expenses").child(comment.getExpenseID());
    commentReference.setValue(comment);
    expenseReference.child("comments").child(ID).setValue(true);
    return ID;
}
Also used : DatabaseReference(com.google.firebase.database.DatabaseReference)

Example 25 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project MadMax by deviz92.

the class FirebaseUtils method addEvent.

/*
        EVENT
     */
public String addEvent(Event event) {
    Log.d(TAG, "addEvent");
    final String ID = databaseReference.child("events").push().getKey();
    DatabaseReference eventReference = databaseReference.child("events").child(ID);
    DatabaseReference groupReference = databaseReference.child("groups").child(event.getGroupID());
    eventReference.setValue(event);
    groupReference.child("events").child(ID).setValue(true);
    return ID;
}
Also used : DatabaseReference(com.google.firebase.database.DatabaseReference)

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