<commit_before>package com.greencode.enticement_android.LayoutControllers;

import android.app.Activity;
import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.greencode.enticement_android.Activities.ChatRoomActivity;
import com.greencode.enticement_android.Interfaces.MessageClickListener;
import com.greencode.enticement_android.Models.Message;
import com.greencode.enticement_android.R;

import org.w3c.dom.Text;

import java.text.SimpleDateFormat;
import java.util.Locale;

import vc908.stickerfactory.StickersManager;

/**
 * Created by jasonnguyen on 11/28/16.
 */

public class MessagesAdapter extends FirebaseRecyclerAdapter<Message,MessagesAdapter.MessageViewHolder> {

    private static final int RIGHT_MSG = 0;
    private static final int LEFT_MSG = 1;
    private static final int RIGHT_MSG_IMG = 2;
    private static final int LEFT_MSG_IMG = 3;

    private final SimpleDateFormat MESSAGE_TIME_FORMAT = new SimpleDateFormat("M/dd, k:mm", Locale.US);

    private Context mContext;

    public MessagesAdapter(DatabaseReference ref, Context context) {
        super(Message.class, R.layout.message_from_me, MessagesAdapter.MessageViewHolder.class, ref);
        this.mContext = context;
    }

    @Override
    public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == RIGHT_MSG){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_from_me,parent,false);
            return new MessageViewHolder(view);
        }else if (viewType == LEFT_MSG){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_from_friend,parent,false);
            return new MessageViewHolder(view);
        }else if (viewType == RIGHT_MSG_IMG){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sticker_from_me,parent,false);
            return new MessageViewHolder(view);
        }else{
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sticker_from_friend,parent,false);
            return new MessageViewHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        Message model = getItem(position);
        if (model.getType() == Message.MessageType.MESSAGE_IN){
            return LEFT_MSG;
        } else if (model.getType() == Message.MessageType.MESSAGE_OUT) {
            return RIGHT_MSG;
        } else if (model.getType() == Message.MessageType.STICKER_IN) {
            return LEFT_MSG_IMG;
        } else {
            return RIGHT_MSG_IMG;
        }
    }

    @Override
    protected void populateViewHolder(MessageViewHolder viewHolder, Message model, int position) {
        if (model.getType() == Message.MessageType.STICKER_IN
                || model.getType() == Message.MessageType.STICKER_OUT) {
            viewHolder.loadSticker(model.getMessage());
            viewHolder.setTimestamp(String.valueOf(model.getTime()));
        } else {
            viewHolder.setTxtMessage(model.getMessage());
            viewHolder.setTimestamp(String.valueOf(model.getTime()));
        }

        if (position == 0) {
            viewHolder.updateTime(position, getItem(position).getTime(),
                    getItem(position).getType(), -1);
            viewHolder.updateAvatarVisibility(position, -1);
        } else {
            viewHolder.updateTime(position, getItem(position).getTime(),
                    getItem(position).getType(), getItemViewType(position-1));
            viewHolder.updateAvatarVisibility(position, getItemViewType(position-1));
        }
    }

    class MessageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView mTimeStamp;
        TextView mContent;
        TextView mStatus;
        ImageView mUserProfile;
        ImageView mSticker;
        View mInfoView;

        MessageViewHolder(View itemView) {
            super(itemView);
            mTimeStamp = (TextView)itemView.findViewById(R.id.message_timestamp);
            mContent = (TextView)itemView.findViewById(R.id.message_content);
            mStatus = (TextView) itemView.findViewById(R.id.message_status);
            mSticker = (ImageView) itemView.findViewById(R.id.message_sticker);
            mUserProfile = (ImageView)itemView.findViewById(R.id.message_userimg);
            mInfoView = itemView.findViewById(R.id.message_infoview);
        }

        void loadSticker(String message) {
            StickersManager.with(mContext)
                    .loadSticker(message)
                    .into(mSticker);
        }

        void setTime(long time) {
            mTimeStamp.setText(MESSAGE_TIME_FORMAT.format(time));
        }

        void hideTime() {
            mInfoView.setVisibility(View.GONE);
        }

        @Override
        public void onClick(View view) {
            int position = getAdapterPosition();
            Message model = getItem(position);
//            if (model.getMapModel() != null){
//                mClickListenerChatFirebase.clickImageMapChat(view,position,model.getMapModel().getLatitude(),model.getMapModel().getLongitude());
//            }else{
//                mClickListenerChatFirebase.clickImageChat(view,position,model.getUserModel().getName(),model.getUserModel().getPhoto_profile(),model.getFile().getUrl_file());
//            }
        }

        void updateAvatarVisibility(int position,int viewType) {
            if (mUserProfile != null) {
                if (position != 0) {
                    Message.MessageType prevItemType = Message.MessageType.values()[viewType];
                    if (prevItemType == Message.MessageType.MESSAGE_IN
                            || prevItemType == Message.MessageType.STICKER_IN) {
                        mUserProfile.setVisibility(View.GONE);
                    } else {
                        mUserProfile.setVisibility(View.VISIBLE);
                    }
                } else {
                    mUserProfile.setVisibility(View.VISIBLE);
                }
            }
        }

        void updateTime(int position, long time, Message.MessageType currentItemType, int preViewType) {
            if (preViewType == -1){
                setTime(time);
                return;
            }

            if (position != getItemCount() - 1) {
                Message.MessageType nextItemType = Message.MessageType.values()[preViewType];
                switch (nextItemType) {
                    case STICKER_OUT:
                    case MESSAGE_OUT:
                        if (currentItemType == Message.MessageType.MESSAGE_OUT
                                || currentItemType == Message.MessageType.STICKER_OUT) {
                            hideTime();
                        } else {
                            setTime(time);
                        }
                        break;
                    case STICKER_IN:
                    case MESSAGE_IN:<commit_msg>changed time view for each message<commit_after>package com.greencode.enticement_android.LayoutControllers;

import android.app.Activity;
import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.greencode.enticement_android.Activities.ChatRoomActivity;
import com.greencode.enticement_android.Interfaces.MessageClickListener;
import com.greencode.enticement_android.Models.Message;
import com.greencode.enticement_android.R;

import org.w3c.dom.Text;

import java.text.SimpleDateFormat;
import java.util.Locale;

import vc908.stickerfactory.StickersManager;

/**
 * Created by jasonnguyen on 11/28/16.
 */

public class MessagesAdapter extends FirebaseRecyclerAdapter<Message,MessagesAdapter.MessageViewHolder> {

    private static final int RIGHT_MSG = 0;
    private static final int LEFT_MSG = 1;
    private static final int RIGHT_MSG_IMG = 2;
    private static final int LEFT_MSG_IMG = 3;

    private final SimpleDateFormat MESSAGE_TIME_FORMAT = new SimpleDateFormat("M/dd, k:mm", Locale.US);

    private Context mContext;

    public MessagesAdapter(DatabaseReference ref, Context context) {
        super(Message.class, R.layout.message_from_me, MessagesAdapter.MessageViewHolder.class, ref);
        this.mContext = context;
    }

    @Override
    public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == RIGHT_MSG){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_from_me,parent,false);
            return new MessageViewHolder(view);
        }else if (viewType == LEFT_MSG){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_from_friend,parent,false);
            return new MessageViewHolder(view);
        }else if (viewType == RIGHT_MSG_IMG){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sticker_from_me,parent,false);
            return new MessageViewHolder(view);
        }else{
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sticker_from_friend,parent,false);
            return new MessageViewHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        Message model = getItem(position);
        if (model.getType() == Message.MessageType.MESSAGE_IN){
            return LEFT_MSG;
        } else if (model.getType() == Message.MessageType.MESSAGE_OUT) {
            return RIGHT_MSG;
        } else if (model.getType() == Message.MessageType.STICKER_IN) {
            return LEFT_MSG_IMG;
        } else {
            return RIGHT_MSG_IMG;
        }
    }

    @Override
    protected void populateViewHolder(MessageViewHolder viewHolder, Message model, int position) {
        if (model.getType() == Message.MessageType.STICKER_IN
                || model.getType() == Message.MessageType.STICKER_OUT) {
            viewHolder.loadSticker(model.getMessage());
            viewHolder.setTimestamp(String.valueOf(model.getTime()));
        } else {
            viewHolder.setTxtMessage(model.getMessage());
            viewHolder.setTimestamp(String.valueOf(model.getTime()));
        }

        if (position == 0) {
            viewHolder.updateTime(position, getItem(position).getTime(),
                    getItem(position).getType(), -1);
            viewHolder.updateAvatarVisibility(position, -1);
        } else {
            viewHolder.updateTime(position, getItem(position).getTime(),
                    getItem(position).getType(), getItemViewType(position - 1));
            viewHolder.updateAvatarVisibility(position, getItemViewType(position - 1));
        }
    }

    class MessageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView mTimeStamp;
        TextView mContent;
        TextView mStatus;
        ImageView mUserProfile;
        ImageView mSticker;
        View mInfoView;

        MessageViewHolder(View itemView) {
            super(itemView);
            mTimeStamp = (TextView)itemView.findViewById(R.id.message_timestamp);
            mContent = (TextView)itemView.findViewById(R.id.message_content);
            mStatus = (TextView) itemView.findViewById(R.id.message_status);
            mSticker = (ImageView) itemView.findViewById(R.id.message_sticker);
            mUserProfile = (ImageView)itemView.findViewById(R.id.message_userimg);
            mInfoView = itemView.findViewById(R.id.message_infoview);
        }

        void loadSticker(String message) {
            StickersManager.with(mContext)
                    .loadSticker(message)
                    .into(mSticker);
        }

        void setTime(long time) {
            mTimeStamp.setText(MESSAGE_TIME_FORMAT.format(time));
        }

        void hideTime() {
            mInfoView.setVisibility(View.GONE);
        }

        @Override
        public void onClick(View view) {
            int position = getAdapterPosition();
            Message model = getItem(position);
//            if (model.getMapModel() != null){
//                mClickListenerChatFirebase.clickImageMapChat(view,position,model.getMapModel().getLatitude(),model.getMapModel().getLongitude());
//            }else{
//                mClickListenerChatFirebase.clickImageChat(view,position,model.getUserModel().getName(),model.getUserModel().getPhoto_profile(),model.getFile().getUrl_file());
//            }
        }

        void updateAvatarVisibility(int position,int viewType) {
            if (mUserProfile != null) {
                if (position != 0) {
                    Message.MessageType prevItemType = Message.MessageType.values()[viewType];
                    if (prevItemType == Message.MessageType.MESSAGE_IN
                            || prevItemType == Message.MessageType.STICKER_IN) {
                        mUserProfile.setVisibility(View.GONE);
                    } else {
                        mUserProfile.setVisibility(View.VISIBLE);
                    }
                } else {
                    mUserProfile.setVisibility(View.VISIBLE);
                }
            }
        }

        void updateTime(int position, long time, Message.MessageType currentItemType, int preViewType) {
            if (preViewType == -1){
                setTime(time);
                return;
            }

            if (position != getItemCount() - 1) {
                Message.MessageType prevItemType = Message.MessageType.values()[preViewType];
                switch (prevItemType) {
                    case STICKER_OUT:
                    case MESSAGE_OUT:
                        if (currentItemType == Message.MessageType.MESSAGE_OUT
                                || currentItemType == Message.MessageType.STICKER_OUT) {
                            hideTime();
                        } else {
                            setTime(time);
                        }
                        break;
                    case STICKER_IN:
                    case MESSAGE_IN:
{'fraction_non_alphanumeric': 0.0965928334349387, 'fraction_numerical': 0.003115897852739958, 'mean_word_length': 2.322232223222322, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2503634', 'n_tokens_mistral': 4034, 'n_tokens_neox': 3845, 'n_words': 670}