Search in sources :

Example 16 with ChatMessageHelper

use of ingage.ingage20.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.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 17 with ChatMessageHelper

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

Example 18 with ChatMessageHelper

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

the class ChatFragment method appendChatConversation.

private void appendChatConversation(DataSnapshot dataSnapshot) {
    Iterator i = dataSnapshot.getChildren().iterator();
    Log.d("STATE", "pageData2 : " + dataSnapshot);
    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();
        String chat_userVote;
        chat_userVote = userVotes.get(chat_id);
        Log.d("CHATVOTE", "result : " + chat_userVote);
        ChatMessageHelper msg = new ChatMessageHelper(chat_id, chat_side, chat_msg, chat_username, chat_upvote, chat_downvote, chat_timestamp, chat_userVote);
        chatAdapter.add(msg);
    }
    chatAdapter.notifyDataSetChanged();
    recyclerView.scrollToPosition(chatAdapter.getItemCount() - 1);
}
Also used : ChatMessageHelper(ingage.ingage20.helpers.ChatMessageHelper) Iterator(java.util.Iterator) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 19 with ChatMessageHelper

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

the class ChatFragment method removeUpvote.

@Override
public void removeUpvote(int p) {
    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(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)

Aggregations

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