Search in sources :

Example 1 with ChatMessageHelper

use of ingage.ingage.helpers.ChatMessageHelper 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.ingage.helpers.ChatMessageHelper) Iterator(java.util.Iterator) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 2 with ChatMessageHelper

use of ingage.ingage.helpers.ChatMessageHelper in project iNGAGE by davis123123.

the class ChatFragment method onDownvoteClick.

@Override
public void onDownvoteClick(int p) {
    // Log.d("vote" , "down : ");
    ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
    String chat_key = chatMessageHelper.getMessageID();
    DatabaseReference message_root = currPageData.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(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.ingage.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 3 with ChatMessageHelper

use of ingage.ingage.helpers.ChatMessageHelper in project iNGAGE by davis123123.

the class ChatArrayAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(final ChatArrayAdapter.ChatViewHolder holder, final int position) {
    final ChatMessageHelper chatMessageHelper = (ChatMessageHelper) this.getItem(position);
    holder.bind(position);
    String userVote = "";
    userVote = chatMessageHelper.getUserVote();
    // side = chatMessageHelper.getSide();
    if (userVote != null) {
        if (userVote.equals("up")) {
            toggleVote(holder.bUpvote, selectedColorAgree, false);
            toggleVote(holder.bDownvote, unselectedColor, true);
        } else if (userVote.equals("down")) {
            toggleVote(holder.bUpvote, unselectedColor, true);
            toggleVote(holder.bDownvote, selectedColorDisagree, false);
        }
    } else {
        toggleVote(holder.bUpvote, unselectedColor, true);
        toggleVote(holder.bDownvote, unselectedColor, true);
    }
    if (holder.avatar.getDrawable() == null) {
        Log.d("Upvote state: ", " no avatar");
        holder.downloadAvatar();
    }
    holder.bUpvote.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            itemClickCallback.onUpvoteClick(position);
            // MAKE boolean LOL
            String prev_voted = "false", vote = "up";
            // check if previously voted
            if (chatMessageHelper.getUserVote() != null) {
                if (chatMessageHelper.getUserVote().equals("down")) {
                    prev_voted = "true";
                    itemClickCallback.removeDownvote(position);
                }
            // remove previous downvote
            }
            // set user vote to up
            chatMessageHelper.setUserVote(vote);
            if (chatMessageHelper.getUserVote().equals(vote)) {
                toggleVote(holder.bUpvote, selectedColorAgree, false);
                toggleVote(holder.bDownvote, unselectedColor, true);
            } else {
                toggleVote(holder.bUpvote, unselectedColor, true);
                toggleVote(holder.bDownvote, unselectedColor, true);
            }
            // insert into user profile
            itemClickCallback.insertVote(position, prev_voted, vote);
        }
    });
    holder.bDownvote.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            itemClickCallback.onDownvoteClick(position);
            // MAKE boolean LOL
            String prev_voted = "false", vote = "down";
            // check if previously voted
            if (chatMessageHelper.getUserVote() != null) {
                if (chatMessageHelper.getUserVote().equals("up")) {
                    prev_voted = "true";
                    itemClickCallback.removeUpvote(position);
                }
            // remove previous downvote
            }
            chatMessageHelper.setUserVote(vote);
            if (chatMessageHelper.getUserVote().equals(vote)) {
                toggleVote(holder.bUpvote, unselectedColor, true);
                toggleVote(holder.bDownvote, selectedColorDisagree, false);
            } else {
                toggleVote(holder.bUpvote, unselectedColor, true);
                toggleVote(holder.bDownvote, unselectedColor, true);
            }
            // insert into user profile
            itemClickCallback.insertVote(position, prev_voted, vote);
        }
    });
    holder.avatar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            itemClickCallback.onAvatarClick(position);
        }
    });
}
Also used : ChatMessageHelper(ingage.ingage.helpers.ChatMessageHelper) ImageView(android.widget.ImageView) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View)

Example 4 with ChatMessageHelper

use of ingage.ingage.helpers.ChatMessageHelper in project iNGAGE by davis123123.

the class ChatFragment method removeDownvote.

@Override
public void removeDownvote(int p) {
    ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
    String chat_key = chatMessageHelper.getMessageID();
    DatabaseReference message_root = currPageData.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.ingage.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 5 with ChatMessageHelper

use of ingage.ingage.helpers.ChatMessageHelper 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.ingage.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)

Aggregations

ChatMessageHelper (ingage.ingage.helpers.ChatMessageHelper)11 DataSnapshot (com.google.firebase.database.DataSnapshot)6 DatabaseError (com.google.firebase.database.DatabaseError)4 DatabaseReference (com.google.firebase.database.DatabaseReference)4 MutableData (com.google.firebase.database.MutableData)4 Transaction (com.google.firebase.database.Transaction)4 ChatFeaturesHandler (ingage.ingage.handlers.ChatFeaturesHandler)2 Iterator (java.util.Iterator)2 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 SessionManager (ingage.ingage.managers.SessionManager)1 ExecutionException (java.util.concurrent.ExecutionException)1