<commit_before>package me.shreyasr.chatse.chat.adapters

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.support.v4.content.ContextCompat
import android.support.v7.app.AlertDialog
import android.support.v7.widget.RecyclerView
import android.text.Html
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.koushikdutta.ion.Ion
import com.orhanobut.dialogplus.DialogPlus
import com.orhanobut.dialogplus.ListHolder
import com.squareup.okhttp.FormEncodingBuilder
import com.squareup.okhttp.Request
import kotlinx.android.synthetic.main.list_item_message.view.*
import me.shreyasr.chatse.R
import me.shreyasr.chatse.chat.ChatRoom
import me.shreyasr.chatse.event.EventList
import me.shreyasr.chatse.event.presenter.message.MessageEvent
import me.shreyasr.chatse.network.Client
import me.shreyasr.chatse.network.ClientManager
import org.jetbrains.anko.defaultSharedPreferences
import org.jetbrains.anko.doAsync
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList

/**
 * The beautiful adapter that handles all new messages in the chat
 */
class MessageAdapter(val mContext: Context, val events: EventList, val chatFkey: String?, val room: ChatRoom?, var messages: ArrayList<MessageEvent> = ArrayList()) : RecyclerView.Adapter<MessageAdapter.MessageViewHolder>() {

    override fun onBindViewHolder(viewHolder: MessageViewHolder?, pos: Int) {
        val message = messages[pos]
        val holder = viewHolder as MessageViewHolder
        holder.bindMessage(message)
    }

    /**
     * Called when we want to update the messages (there's a new message)
     */
    fun update() {
        messages.clear()
        messages.addAll(events.messagePresenter.getEventsList())
        this.notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MessageViewHolder {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.list_item_message, parent, false)
        return MessageViewHolder(mContext, view, chatFkey, room)
    }

    override fun getItemCount() = messages.size

    /**
     * ViewHolder that handles setting all content in itemView
     */
    class MessageViewHolder(val mContext: Context, itemView: View, val chatFkey: String?, val room: ChatRoom?) : RecyclerView.ViewHolder(itemView) {
        val timestampFormat = SimpleDateFormat("hh:mm aa", Locale.getDefault())
        val messageView = itemView.findViewById(R.id.message_content) as TextView
        val userNameView = itemView.findViewById(R.id.message_user_name) as TextView
        val messageTimestamp = itemView.findViewById(R.id.message_timestamp) as TextView
        val editIndicator = itemView.findViewById(R.id.message_edit_indicator) as ImageView
        val starIndicator = itemView.findViewById(R.id.message_star_indicator) as ImageView
        val starCount = itemView.findViewById(R.id.message_star_count) as TextView
        val oneboxImage = itemView.findViewById(R.id.message_image) as ImageView

        fun bindMessage(message: MessageEvent) {
            //Make sure the image is gone unless needed later
            oneboxImage.visibility = View.GONE
            //Same with the indicator for starred messages
            starIndicator.visibility = View.GONE

            //If the message is starred, show the indicator and set the count text to the star count
            if (message.message_starred) {
                starIndicator.visibility = View.VISIBLE
                starCount.visibility = View.VISIBLE
                starCount.text = message.message_stars.toString()
            }

            //If the message is deleted, show the text "removed" and make it gray
            if (message.isDeleted) {
                messageView.setTextColor(ContextCompat.getColor(itemView.context, R.color.deleted))
                messageView.text = itemView.context.getString(R.string.removed)
            } else {
                //If it's just a plain message, then set the text from HTML
                if (!message.onebox) {
                    messageView.setTextColor(ContextCompat.getColor(itemView.context, R.color.primary_text))
                    messageView.text = message.content
                    //If Android version is 24 and above use the updated version, otherwise use the deprecated version
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        messageView.text = Html.fromHtml(message.content, Html.FROM_HTML_MODE_LEGACY)
                    } else {
                        @Suppress("DEPRECATION")
                        messageView.text = Html.fromHtml(message.content)
                    }
                } else {
                    //if it's a onebox, then display it specially
                    when (message.onebox_type) {
                        "image" -> {
                            //For images, load the image into the ImageView, making sure it's visible
                            oneboxImage.visibility = View.VISIBLE

                            Ion.with(itemView.context)
                                    .load(message.onebox_content)
                                    .intoImageView(itemView.message_image)

                            //Set the text to nothing just in case
                            messageView.text = ""
                        }
                        //For Youtube videos, display the image and some text, linking the view to the video on Youtube
                        "youtube" -> {
                            itemView.setOnClickListener {
                                mContext.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(message.onebox_extra)))
                            }
                            oneboxImage.visibility = View.VISIBLE

                            Ion.with(itemView.context)
                                    .load(message.onebox_content)
                                    .intoImageView(itemView.message_image)
                            messageView.text = message.content
                        }
                        //Other oneboxed items just display the HTML until we implement them all
                        else -> {
                            Log.d("Onebox", "Type: ${message.onebox_type}")
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                messageView.text = Html.fromHtml(message.content, Html.FROM_HTML_MODE_LEGACY)
                            } else {
                                @Suppress("DEPRECATION")
                                messageView.text = Html.fromHtml(message.content)
                            }
                        }
                    }
                }
            }<commit_msg>Don't render HTML if not onebox<commit_after>package me.shreyasr.chatse.chat.adapters

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.support.v4.content.ContextCompat
import android.support.v7.app.AlertDialog
import android.support.v7.widget.RecyclerView
import android.text.Html
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.koushikdutta.ion.Ion
import com.orhanobut.dialogplus.DialogPlus
import com.orhanobut.dialogplus.ListHolder
import com.squareup.okhttp.FormEncodingBuilder
import com.squareup.okhttp.Request
import kotlinx.android.synthetic.main.list_item_message.view.*
import me.shreyasr.chatse.R
import me.shreyasr.chatse.chat.ChatRoom
import me.shreyasr.chatse.event.EventList
import me.shreyasr.chatse.event.presenter.message.MessageEvent
import me.shreyasr.chatse.network.Client
import me.shreyasr.chatse.network.ClientManager
import org.jetbrains.anko.defaultSharedPreferences
import org.jetbrains.anko.doAsync
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList

/**
 * The beautiful adapter that handles all new messages in the chat
 */
class MessageAdapter(val mContext: Context, val events: EventList, val chatFkey: String?, val room: ChatRoom?, var messages: ArrayList<MessageEvent> = ArrayList()) : RecyclerView.Adapter<MessageAdapter.MessageViewHolder>() {

    override fun onBindViewHolder(viewHolder: MessageViewHolder?, pos: Int) {
        val message = messages[pos]
        val holder = viewHolder as MessageViewHolder
        holder.bindMessage(message)
    }

    /**
     * Called when we want to update the messages (there's a new message)
     */
    fun update() {
        messages.clear()
        messages.addAll(events.messagePresenter.getEventsList())
        this.notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MessageViewHolder {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.list_item_message, parent, false)
        return MessageViewHolder(mContext, view, chatFkey, room)
    }

    override fun getItemCount() = messages.size

    /**
     * ViewHolder that handles setting all content in itemView
     */
    class MessageViewHolder(val mContext: Context, itemView: View, val chatFkey: String?, val room: ChatRoom?) : RecyclerView.ViewHolder(itemView) {
        val timestampFormat = SimpleDateFormat("hh:mm aa", Locale.getDefault())
        val messageView = itemView.findViewById(R.id.message_content) as TextView
        val userNameView = itemView.findViewById(R.id.message_user_name) as TextView
        val messageTimestamp = itemView.findViewById(R.id.message_timestamp) as TextView
        val editIndicator = itemView.findViewById(R.id.message_edit_indicator) as ImageView
        val starIndicator = itemView.findViewById(R.id.message_star_indicator) as ImageView
        val starCount = itemView.findViewById(R.id.message_star_count) as TextView
        val oneboxImage = itemView.findViewById(R.id.message_image) as ImageView

        fun bindMessage(message: MessageEvent) {
            //Make sure the image is gone unless needed later
            oneboxImage.visibility = View.GONE
            //Same with the indicator for starred messages
            starIndicator.visibility = View.GONE

            //If the message is starred, show the indicator and set the count text to the star count
            if (message.message_starred) {
                starIndicator.visibility = View.VISIBLE
                starCount.visibility = View.VISIBLE
                starCount.text = message.message_stars.toString()
            }

            //If the message is deleted, show the text "removed" and make it gray
            if (message.isDeleted) {
                messageView.setTextColor(ContextCompat.getColor(itemView.context, R.color.deleted))
                messageView.text = itemView.context.getString(R.string.removed)
            } else {
                //If it's just a plain message, then set the text from HTML
                if (!message.onebox) {
                    messageView.setTextColor(ContextCompat.getColor(itemView.context, R.color.primary_text))
                    messageView.text = message.content
                } else {
                    //if it's a onebox, then display it specially
                    when (message.onebox_type) {
                        "image" -> {
                            //For images, load the image into the ImageView, making sure it's visible
                            oneboxImage.visibility = View.VISIBLE

                            Ion.with(itemView.context)
                                    .load(message.onebox_content)
                                    .intoImageView(itemView.message_image)

                            //Set the text to nothing just in case
                            messageView.text = ""
                        }
                    //For Youtube videos, display the image and some text, linking the view to the video on Youtube
                        "youtube" -> {
                            itemView.setOnClickListener {
                                mContext.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(message.onebox_extra)))
                            }
                            oneboxImage.visibility = View.VISIBLE

                            Ion.with(itemView.context)
                                    .load(message.onebox_content)
                                    .intoImageView(itemView.message_image)
                            messageView.text = message.content
                        }
                    //Other oneboxed items just display the HTML until we implement them all
                        else -> {
                            Log.d("Onebox", "Type: ${message.onebox_type}")
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                messageView.text = Html.fromHtml(message.content, Html.FROM_HTML_MODE_LEGACY)
                            } else {
                                @Suppress("DEPRECATION")
                                messageView.text = Html.fromHtml(message.content)
                            }
                        }
                    }
                }
            }
{'fraction_non_alphanumeric': 0.07692307692307693, 'fraction_numerical': 0.0005866392901664589, 'mean_word_length': 2.0401248328131967, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1373550', 'n_tokens_mistral': 3556, 'n_tokens_neox': 3381, 'n_words': 813}