<commit_before>package com.thangiee.LoLWithFriends.fragments

import java.util.Date

import android.app.Fragment
import android.os.{Bundle, SystemClock}
import android.view.View.OnClickListener
import android.view.{LayoutInflater, View, ViewGroup}
import android.widget.EditText
import com.dd.CircularProgressButton
import com.ruenzuo.messageslistview.adapters.MessageAdapter
import com.ruenzuo.messageslistview.models
import com.ruenzuo.messageslistview.models.MessageType._
import com.ruenzuo.messageslistview.widget.MessagesListView
import com.thangiee.LoLWithFriends.api.{LoLChat, Summoner}
import com.thangiee.LoLWithFriends.utils.DataBaseHandler
import com.thangiee.LoLWithFriends.utils.Events.ReceivedMessage
import com.thangiee.LoLWithFriends.{MyApp, R}
import de.greenrobot.event.EventBus
import de.keyboardsurfer.android.widget.crouton.{Crouton, Style}
import org.scaloid.common._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class ChatPaneFragment private extends Fragment with TagUtil {
  private var view: View = _
  private lazy val sendButton = view.findViewById(R.id.btn_send_msg).asInstanceOf[CircularProgressButton]
  private lazy val msgField = view.findViewById(R.id.et_msg_field).asInstanceOf[EditText]
  private lazy val friendName = getArguments.getString("name-key")
  private lazy val messageAdapter = new MessageAdapter(getActivity, 0)

  override def onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle): View = {
    super.onCreateView(inflater, container, savedInstanceState)
    EventBus.getDefault.register(this)
    view = inflater.inflate(R.layout.chat_pane, container, false)

    sendButton.setOnClickListener(new OnClickListener {
      override def onClick(v: View): Unit = sendMessage()
    })
    sendButton.setIndeterminateProgressMode(true)
    msgField.setHint("send to " + friendName)

    val messageLog = DataBaseHandler.getMessageLog(MyApp.currentUser, friendName)
    messageAdapter.addAll(messageLog) // add all messages
    setMessagesRead()
    val messageListView = view.findViewById(R.id.lsv_chat).asInstanceOf[MessagesListView]
    messageListView.setAdapter(messageAdapter)

    messageListView.setSelection(messageAdapter.getCount - 1) // scroll to the bottom (newer messages)

    view
  }

  override def onDestroy(): Unit = {
    EventBus.getDefault.unregister(this, classOf[ReceivedMessage])
    super.onDestroy()
  }

  def setMessagesRead() {
    for (i <- messageAdapter.getCount-1 to 0 by -1) {
      val msg = messageAdapter.getItem(i)
      if (msg.isRead) return else msg.setIsRead(true).save()
    }
  }

  private def sendMessage() {
    if (msgField.getText.length() == 0) return // don't send if blank
    sendButton.setProgress(50)
    sendButton.setEnabled(false)
    Future {
      SystemClock.sleep(750)
      if (LoLChat.sendMessage(LoLChat.getFriendByName(friendName).get, msgField.getText.toString)) {
        // if message sent, then save that message to DB
        val msg = new models.Message.MessageBuilder(MESSAGE_TYPE_SENT).text(msgField.getText.toString)
          .date(new Date()).otherPerson(friendName).thisPerson(MyApp.currentUser).isRead(true).build()
        msg.save() // save to DB

        runOnUiThread(messageAdapter.add(msg)) // add to adapter to show the message on the chat
        runOnUiThread(msgField.setText("")) // clear the message field
        runOnUiThread(sendButton.setProgress(100)) // success state
        SystemClock.sleep(150)
        runOnUiThread(sendButton.setProgress(0)) // normal state
        runOnUiThread(sendButton.setEnabled(true))
      } else {  // message failed to send
        runOnUiThread(sendButton.setProgress(-1)) // error state
        SystemClock.sleep(150)
        runOnUiThread(Crouton.makeText(getActivity, "Fail to send message", Style.ALERT).show()) // alert the user
        runOnUiThread(sendButton.setProgress(0)) // normal state
        runOnUiThread(sendButton.setEnabled(true))
      }
    }
  }

  def onEventMainThread(event: ReceivedMessage): Unit = {
    info("[*]onEvent: received message from "+event.summoner.name)
    messageAdapter.add(event.msg) // add received message to adapter to show the message on the chat
  }

  private def runOnUiThread(f: => Unit) {
    getActivity.runOnUiThread(new Runnable {
      override def run(): Unit = f
    })
  }
}

object ChatPaneFragment {
  def newInstance(summoner: Summoner): ChatPaneFragment = {
    val bundle = new Bundle()
    bundle.putString("name-key", summoner.name)
    val frag = new ChatPaneFragment
    frag.setArguments(bundle)
    frag
  }
}
<commit_msg>add Crouton to inform user that they cant send empty message<commit_after>package com.thangiee.LoLWithFriends.fragments

import java.util.Date

import android.app.Fragment
import android.os.{Bundle, SystemClock}
import android.view.View.OnClickListener
import android.view.{LayoutInflater, View, ViewGroup}
import android.widget.EditText
import com.dd.CircularProgressButton
import com.ruenzuo.messageslistview.adapters.MessageAdapter
import com.ruenzuo.messageslistview.models
import com.ruenzuo.messageslistview.models.MessageType._
import com.ruenzuo.messageslistview.widget.MessagesListView
import com.thangiee.LoLWithFriends.api.{LoLChat, Summoner}
import com.thangiee.LoLWithFriends.utils.DataBaseHandler
import com.thangiee.LoLWithFriends.utils.Events.ReceivedMessage
import com.thangiee.LoLWithFriends.{MyApp, R}
import de.greenrobot.event.EventBus
import de.keyboardsurfer.android.widget.crouton.{Crouton, Style}
import org.scaloid.common._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class ChatPaneFragment private extends Fragment with TagUtil {
  private var view: View = _
  private lazy val sendButton = view.findViewById(R.id.btn_send_msg).asInstanceOf[CircularProgressButton]
  private lazy val msgField = view.findViewById(R.id.et_msg_field).asInstanceOf[EditText]
  private lazy val friendName = getArguments.getString("name-key")
  private lazy val messageAdapter = new MessageAdapter(getActivity, 0)

  override def onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle): View = {
    super.onCreateView(inflater, container, savedInstanceState)
    EventBus.getDefault.register(this)
    view = inflater.inflate(R.layout.chat_pane, container, false)

    sendButton.setOnClickListener(new OnClickListener {
      override def onClick(v: View): Unit = sendMessage()
    })
    sendButton.setIndeterminateProgressMode(true)
    msgField.setHint("send to " + friendName)

    val messageLog = DataBaseHandler.getMessageLog(MyApp.currentUser, friendName)
    messageAdapter.addAll(messageLog) // add all messages
    setMessagesRead()
    val messageListView = view.findViewById(R.id.lsv_chat).asInstanceOf[MessagesListView]
    messageListView.setAdapter(messageAdapter)

    messageListView.setSelection(messageAdapter.getCount - 1) // scroll to the bottom (newer messages)

    view
  }

  override def onDestroy(): Unit = {
    EventBus.getDefault.unregister(this, classOf[ReceivedMessage])
    super.onDestroy()
  }

  def setMessagesRead() {
    for (i <- messageAdapter.getCount-1 to 0 by -1) {
      val msg = messageAdapter.getItem(i)
      if (msg.isRead) return else msg.setIsRead(true).save()
    }
  }

  private def sendMessage() {
    // don't send if blank
    if (msgField.getText.length() == 0) {
      Crouton.makeText(getActivity, "Can't send empty message", Style.INFO).show()
      return
    }

    sendButton.setProgress(50)
    sendButton.setEnabled(false)
    Future {
      SystemClock.sleep(750)
      if (LoLChat.sendMessage(LoLChat.getFriendByName(friendName).get, msgField.getText.toString)) {
        // if message sent, then save that message to DB
        val msg = new models.Message.MessageBuilder(MESSAGE_TYPE_SENT).text(msgField.getText.toString)
          .date(new Date()).otherPerson(friendName).thisPerson(MyApp.currentUser).isRead(true).build()
        msg.save() // save to DB

        runOnUiThread(messageAdapter.add(msg)) // add to adapter to show the message on the chat
        runOnUiThread(msgField.setText("")) // clear the message field
        runOnUiThread(sendButton.setProgress(100)) // success state
        SystemClock.sleep(150)
        runOnUiThread(sendButton.setProgress(0)) // normal state
        runOnUiThread(sendButton.setEnabled(true))
      } else {  // message failed to send
        runOnUiThread(sendButton.setProgress(-1)) // error state
        SystemClock.sleep(150)
        runOnUiThread(Crouton.makeText(getActivity, "Fail to send message", Style.ALERT).show()) // alert the user
        runOnUiThread(sendButton.setProgress(0)) // normal state
        runOnUiThread(sendButton.setEnabled(true))
      }
    }
  }

  def onEventMainThread(event: ReceivedMessage): Unit = {
    info("[*]onEvent: received message from "+event.summoner.name)
    messageAdapter.add(event.msg) // add received message to adapter to show the message on the chat
  }

  private def runOnUiThread(f: => Unit) {
    getActivity.runOnUiThread(new Runnable {
      override def run(): Unit = f
    })
  }
}

object ChatPaneFragment {
  def newInstance(summoner: Summoner): ChatPaneFragment = {
    val bundle = new Bundle()
    bundle.putString("name-key", summoner.name)
    val frag = new ChatPaneFragment
    frag.setArguments(bundle)
    frag
  }
}

{'fraction_non_alphanumeric': 0.09962982548915918, 'fraction_numerical': 0.004865150713907985, 'mean_word_length': 4.1700382722799345, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3108850', 'n_tokens_mistral': 2868, 'n_tokens_neox': 2781, 'n_words': 596}