Search in sources :

Example 56 with DataSnapshot

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

the class NewMemberActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate di NewMemeberAcitivity");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_member);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    buttonInvite = (Button) findViewById(R.id.btn_new_friend);
    Intent intent = getIntent();
    groupID = intent.getStringExtra("groupID");
    groupName = intent.getStringExtra("groupName");
    Log.d(TAG, groupID);
    friendsListView = (ListView) findViewById(R.id.lv_friends);
    friendsAdapter = new HashMapFriendsAdapter(friends);
    addedFriendsListView = (ListView) findViewById(R.id.lv_added_members);
    addedAdapter = new HashMapFriendsAdapter(alreadySelected);
    addedFriendsListView.setAdapter(addedAdapter);
    databaseReference.child("users").child(MainActivity.getCurrentUID()).child("friends").addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Boolean alreadyAdded = false;
            for (DataSnapshot friendSnapshot : dataSnapshot.getChildren()) {
                if (friendSnapshot.hasChild("sharedGroups")) {
                    if (friendSnapshot.child("sharedGroups").hasChild(groupID)) {
                        alreadyAdded = friendSnapshot.child("sharedGroups").child(groupID).getValue(Boolean.class);
                    }
                }
                // se sono già nel gruppo => vengono inseriti nell'addedAdapter
                if (alreadyAdded) {
                    FirebaseUtils.getInstance().getFriendInviteToGroup(friendSnapshot.getKey(), groupID, alreadySelected, addedAdapter);
                } else // altrimenti vengono inseriti nella lista friendsAdapter degli amici disponibili
                {
                    FirebaseUtils.getInstance().getFriendInviteToGroup(friendSnapshot.getKey(), "", friends, friendsAdapter);
                }
            }
            friendsListView.setAdapter(friendsAdapter);
            addedFriendsListView.setAdapter(addedAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, databaseError.getMessage());
        }
    });
    // When i click on one friend of the list
    friendsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            User item = friendsAdapter.getItem(position).getValue();
            friends.remove(item.getID());
            friendsAdapter.update(friends);
            friendsAdapter.notifyDataSetChanged();
            alreadySelected.put(item.getID(), item);
            addedAdapter.update(alreadySelected);
            addedAdapter.notifyDataSetChanged();
        }
    });
    // When i click on one added friend of the list
    addedFriendsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            User item = addedAdapter.getItem(position).getValue();
            alreadySelected.remove(item.getID());
            addedAdapter.update(alreadySelected);
            addedAdapter.notifyDataSetChanged();
            friends.put(item.getID(), item);
            friendsAdapter.update(friends);
            friendsAdapter.notifyDataSetChanged();
        }
    });
    buttonInvite.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d(TAG, "button clicked");
            Log.d(TAG, "invite a member to join the group");
            // String deepLink = getString(R.string.invitation_deep_link) + "?groupToBeAddedID=" + groupID+ "?inviterToGroupUID=" + MainActivity.getCurrentUID();
            Uri.Builder builder = Uri.parse(getString(R.string.invitation_deep_link)).buildUpon().appendQueryParameter("groupToBeAddedID", groupID).appendQueryParameter("inviterID", MainActivity.getCurrentUID());
            Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title)).setDeepLink(builder.build()).setMessage(// .setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
            getString(R.string.invitationToGroup_message)).setCallToActionText(// todo vedere perchè non mostra questo link
            getString(R.string.invitationToGroup)).build();
            startActivityForResult(intent, MainActivity.REQUEST_INVITE_GROUP);
        }
    });
}
Also used : User(com.polito.mad17.madmax.entities.User) AppInviteInvitation(com.google.android.gms.appinvite.AppInviteInvitation) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) DatabaseError(com.google.firebase.database.DatabaseError) AdapterView(android.widget.AdapterView) ValueEventListener(com.google.firebase.database.ValueEventListener) Toolbar(android.support.v7.widget.Toolbar)

Example 57 with DataSnapshot

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

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

the class ChatActivity method onUpvoteClick.

//modify unblock functions here
@Override
public void onUpvoteClick(int p) {
    //Log.d("vote" , "up : ");
    //get correct chat msg with ith key from chatmessage helper
    ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
    String chat_key = chatMessageHelper.getMessageID();
    DatabaseReference message_root = root.child(chat_key);
    //get upvote data
    DatabaseReference upvote_count = message_root.child("upvotes");
    upvote_count.runTransaction(new Transaction.Handler() {

        @Override
        public Transaction.Result doTransaction(MutableData currentData) {
            Log.d("Data", String.valueOf(currentData));
            if (currentData.getValue() == null) {
                currentData.setValue(1);
            } 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 59 with DataSnapshot

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

the class ChatFragment method onUpvoteClick.

@Override
public void onUpvoteClick(int p) {
    // Log.d("vote" , "up : ");
    // get correct chat msg with ith key from chatmessage helper
    ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
    String chat_key = chatMessageHelper.getMessageID();
    DatabaseReference message_root = currPageData.child(chat_key);
    // get upvote data
    DatabaseReference upvote_count = message_root.child("upvotes");
    upvote_count.runTransaction(new Transaction.Handler() {

        @Override
        public Transaction.Result doTransaction(MutableData currentData) {
            Log.d("Data", String.valueOf(currentData));
            if (currentData.getValue() == null) {
                currentData.setValue(1);
            } 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 60 with DataSnapshot

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

the class ChatFragment method updateChatConversation.

private void updateChatConversation(DataSnapshot dataSnapshot) {
    Iterator i = dataSnapshot.getChildren().iterator();
    while (i.hasNext()) {
        chat_id = dataSnapshot.getKey();
        Log.d("STATE", "result : " + chat_id);
        chat_msg = (String) ((DataSnapshot) i.next()).getValue();
        chat_side = (String) ((DataSnapshot) i.next()).getValue();
        chat_timestamp = (String) ((DataSnapshot) i.next()).getValue();
        chat_username = (String) ((DataSnapshot) i.next()).getValue();
        chat_downvote = (Long) ((DataSnapshot) i.next()).getValue();
        chat_upvote = (Long) ((DataSnapshot) i.next()).getValue();
        // gets previous msg of user's vote status
        ChatMessageHelper msgId = (ChatMessageHelper) chatAdapter.getItemFromID(chat_id);
        String chat_userVote = msgId.getUserVote();
        Log.d("USERVOTE", "result : " + chat_userVote);
        ChatMessageHelper msg = new ChatMessageHelper(chat_id, chat_side, chat_msg, chat_username, chat_upvote, chat_downvote, chat_timestamp, chat_userVote);
        chatAdapter.update(msg, chat_id, true);
    // chatAdapter.notifyDataSetChanged();
    }
}
Also used : ChatMessageHelper(ingage.ingage20.helpers.ChatMessageHelper) Iterator(java.util.Iterator) 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