Skip to content

Commit

Permalink
chat: solve text warnings (fixes #3766)
Browse files Browse the repository at this point in the history
  • Loading branch information
ewhang5 committed Jul 2, 2024
1 parent bfc79e0 commit 952a723
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 33 deletions.
31 changes: 16 additions & 15 deletions app/src/main/java/org/ole/planet/myplanet/ui/chat/ChatAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.databinding.ItemAiResponseMessageBinding
import org.ole.planet.myplanet.databinding.ItemUserMessageBinding

Expand All @@ -16,8 +17,8 @@ class ChatAdapter(private val chatList: ArrayList<String>, val context: Context,
private lateinit var textAiMessageBinding: ItemAiResponseMessageBinding
var responseSource: Int = RESPONSE_SOURCE_UNKNOWN

private val VIEW_TYPE_QUERY = 1
private val VIEW_TYPE_RESPONSE = 2
private val viewTypeQuery = 1
private val viewTypeResponse = 2
companion object {
const val RESPONSE_SOURCE_SHARED_VIEW_MODEL = 1
const val RESPONSE_SOURCE_NETWORK = 2
Expand All @@ -29,36 +30,36 @@ class ChatAdapter(private val chatList: ArrayList<String>, val context: Context,
}
}

class ResponseViewHolder(private val textAiMessageBinding: ItemAiResponseMessageBinding) : RecyclerView.ViewHolder(textAiMessageBinding.root) {
class ResponseViewHolder(private val textAiMessageBinding: ItemAiResponseMessageBinding, val context: Context) : RecyclerView.ViewHolder(textAiMessageBinding.root) {
fun bind(response: String, responseSource: Int) {
if(responseSource == RESPONSE_SOURCE_NETWORK){
val typingDelayMillis = 10L
val typingAnimationDurationMillis = response.length * typingDelayMillis
textAiMessageBinding.textGchatMessageOther.text = ""
textAiMessageBinding.textGchatMessageOther.text = context.getString(R.string.empty_text)
Handler(Looper.getMainLooper()).postDelayed({
animateTyping(response, typingDelayMillis)
animateTyping(response)
}, typingAnimationDurationMillis)
} else if(responseSource == RESPONSE_SOURCE_SHARED_VIEW_MODEL){
if (response != "") {
if (response.isNotEmpty()) {
textAiMessageBinding.textGchatMessageOther.text = response
} else{
textAiMessageBinding.textGchatMessageOther.visibility = View.GONE
}
}
}

private fun animateTyping(response: String, typingDelayMillis: Long) {
private fun animateTyping(response: String) {
var currentIndex = 0
val typingRunnable = object : Runnable {
override fun run() {
if (currentIndex < response.length) {
textAiMessageBinding.textGchatMessageOther.text = response.substring(0, currentIndex + 1)
currentIndex++
Handler(Looper.getMainLooper()).postDelayed(this, typingDelayMillis)
Handler(Looper.getMainLooper()).postDelayed(this, 10L)
}
}
}
Handler(Looper.getMainLooper()).postDelayed(typingRunnable, typingDelayMillis)
Handler(Looper.getMainLooper()).postDelayed(typingRunnable, 10L)
}
}

Expand Down Expand Up @@ -90,18 +91,18 @@ class ChatAdapter(private val chatList: ArrayList<String>, val context: Context,
}

override fun getItemViewType(position: Int): Int {
return if (position % 2 == 0) VIEW_TYPE_QUERY else VIEW_TYPE_RESPONSE
return if (position % 2 == 0) viewTypeQuery else viewTypeResponse
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
VIEW_TYPE_QUERY -> {
viewTypeQuery -> {
textUserMessageBinding = ItemUserMessageBinding.inflate(LayoutInflater.from(context), parent, false)
QueryViewHolder(textUserMessageBinding)
}
VIEW_TYPE_RESPONSE -> {
viewTypeResponse -> {
textAiMessageBinding = ItemAiResponseMessageBinding.inflate(LayoutInflater.from(context), parent, false)
ResponseViewHolder(textAiMessageBinding)
ResponseViewHolder(textAiMessageBinding, context)
}
else -> throw IllegalArgumentException("Invalid view type")
}
Expand All @@ -111,11 +112,11 @@ class ChatAdapter(private val chatList: ArrayList<String>, val context: Context,
val chatItem = chatList[position]
Log.d("ChatAdapter", "chatList: $chatList")
when (holder.itemViewType) {
VIEW_TYPE_QUERY -> {
viewTypeQuery -> {
val queryViewHolder = holder as QueryViewHolder
queryViewHolder.bind(chatItem)
}
VIEW_TYPE_RESPONSE -> {
viewTypeResponse -> {
val responseViewHolder = holder as ResponseViewHolder
responseViewHolder.bind(chatItem, responseSource)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ class ChatDetailFragment : Fragment() {
}
}

sharedViewModel.getSelected_id().observe(viewLifecycleOwner) { selected_id ->
_id = selected_id
sharedViewModel.getSelected_id().observe(viewLifecycleOwner) { selectedId ->
_id = selectedId
}

sharedViewModel.getSelected_rev().observe(viewLifecycleOwner) { selected_rev ->
_rev = selected_rev
sharedViewModel.getSelected_rev().observe(viewLifecycleOwner) { selectedRev ->
_rev = selectedRev
}
view.post {
clearChatDetail()
Expand Down Expand Up @@ -292,7 +292,7 @@ class ChatDetailFragment : Fragment() {
}
} else {
fragmentChatDetailBinding.textGchatIndicator.visibility = View.VISIBLE
fragmentChatDetailBinding.textGchatIndicator.text = "${responseBody.message}"
fragmentChatDetailBinding.textGchatIndicator.text = context?.getString(R.string.message_placeholder, responseBody.message)
val jsonObject = JsonObject()
jsonObject.addProperty("_rev", "")
jsonObject.addProperty("_id", "")
Expand Down Expand Up @@ -361,15 +361,15 @@ class ChatDetailFragment : Fragment() {
RealmChatHistory.insert(mRealm, jsonObject)
}
fragmentChatDetailBinding.textGchatIndicator.visibility = View.VISIBLE
fragmentChatDetailBinding.textGchatIndicator.text = "${t.message}"
fragmentChatDetailBinding.textGchatIndicator.text = context?.getString(R.string.message_placeholder, t.message)
fragmentChatDetailBinding.buttonGchatSend.isEnabled = true
fragmentChatDetailBinding.editGchatMessage.isEnabled = true
fragmentChatDetailBinding.imageGchatLoading.visibility = View.INVISIBLE
}
})
}

private fun continueChatRequest(content: RequestBody, _id: String, query: String) {
private fun continueChatRequest(content: RequestBody, id: String, query: String) {
fragmentChatDetailBinding.buttonGchatSend.isEnabled = false
fragmentChatDetailBinding.editGchatMessage.isEnabled = false
fragmentChatDetailBinding.imageGchatLoading.visibility = View.VISIBLE
Expand All @@ -385,16 +385,16 @@ class ChatDetailFragment : Fragment() {
mAdapter.responseSource = ChatAdapter.RESPONSE_SOURCE_NETWORK
mAdapter.addResponse(chatResponse)
_rev = "${response.body()?.couchDBResponse?.rev}"
continueConversationRealm(_id, query, chatResponse)
continueConversationRealm(id, query, chatResponse)
}
} else {
fragmentChatDetailBinding.textGchatIndicator.visibility = View.VISIBLE
fragmentChatDetailBinding.textGchatIndicator.text = "${responseBody.message}"
fragmentChatDetailBinding.textGchatIndicator.text = context?.getString(R.string.message_placeholder, responseBody.message)
}
} else {
fragmentChatDetailBinding.textGchatIndicator.visibility = View.VISIBLE
fragmentChatDetailBinding.textGchatIndicator.text = getString(R.string.request_failed_please_retry)
continueConversationRealm(_id, query, "")
continueConversationRealm(id, query, "")
}

fragmentChatDetailBinding.buttonGchatSend.isEnabled = true
Expand All @@ -403,19 +403,19 @@ class ChatDetailFragment : Fragment() {
}

override fun onFailure(call: Call<ChatModel>, t: Throwable) {
continueConversationRealm(_id, query, "")
continueConversationRealm(id, query, "")
fragmentChatDetailBinding.textGchatIndicator.visibility = View.VISIBLE
fragmentChatDetailBinding.textGchatIndicator.text = "${t.message}"
fragmentChatDetailBinding.textGchatIndicator.text = context?.getString(R.string.message_placeholder, t.message)
fragmentChatDetailBinding.buttonGchatSend.isEnabled = true
fragmentChatDetailBinding.editGchatMessage.isEnabled = true
fragmentChatDetailBinding.imageGchatLoading.visibility = View.INVISIBLE
}
})
}

private fun continueConversationRealm(_id:String, query:String, chatResponse:String) {
private fun continueConversationRealm(id:String, query:String, chatResponse:String) {
try {
addConversationToChatHistory(mRealm, _id, query, chatResponse)
addConversationToChatHistory(mRealm, id, query, chatResponse)
mRealm.commitTransaction()
} catch (e: Exception) {
e.printStackTrace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ChatHistoryListAdapter(var context: Context, private var chatHistory: List
private var newsList: RealmResults<RealmNews>? = null

interface ChatHistoryItemClickListener {
fun onChatHistoryItemClicked(conversations: RealmList<Conversation>?, _id: String, _rev: String?)
fun onChatHistoryItemClicked(conversations: RealmList<Conversation>?, id: String, rev: String?)
}

fun setChatHistoryItemClickListener(listener: ChatHistoryItemClickListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ class ChatHistoryListFragment : Fragment() {
}
val adapter = ChatHistoryListAdapter(requireContext(), list, this)
adapter.setChatHistoryItemClickListener(object : ChatHistoryListAdapter.ChatHistoryItemClickListener {
override fun onChatHistoryItemClicked(conversations: RealmList<Conversation>?, _id: String, _rev:String?) {
override fun onChatHistoryItemClicked(conversations: RealmList<Conversation>?, id: String, rev:String?) {
conversations?.let { sharedViewModel.setSelectedChatHistory(it) }
sharedViewModel.setSelected_id(_id)
_rev?.let { sharedViewModel.setSelected_rev(it) }
sharedViewModel.setSelected_id(id)
rev?.let { sharedViewModel.setSelected_rev(it) }

fragmentChatHistoryListBinding.slidingPaneLayout.openPane()
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1049,5 +1049,7 @@
<string name="planet_name">كوكب %s</string>
<string name="no_submissions">التقديمات غير متاحة</string>
<string name="no_chats">لا توجد محادثات سابقة</string>
<string name="empty_text"></string>
<string name="message_placeholder">%s</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1049,5 +1049,7 @@
<string name="planet_name">%s Planeta</string>
<string name="no_submissions">envíos no disponibles</string>
<string name="no_chats">no hay chats anteriores</string>
<string name="empty_text"></string>
<string name="message_placeholder">%s</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1049,5 +1049,7 @@
<string name="planet_name">%s planète</string>
<string name="no_submissions">soumissions non disponibles</string>
<string name="no_chats">aucune discussion précédente</string>
<string name="empty_text"></string>
<string name="message_placeholder">%s</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-ne/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1049,5 +1049,7 @@
<string name="planet_name">%s ग्रह</string>
<string name="no_submissions">पेशाहरू उपलब्ध छैनन्</string>
<string name="no_chats">अघिल्ला कुराकानीहरू छैनन्</string>
<string name="empty_text"></string>
<string name="message_placeholder">%s</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-so/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1049,5 +1049,7 @@
<string name="planet_name">%s Meerah</string>
<string name="no_submissions">soo gudbin lama heli karo</string>
<string name="no_chats">ma jiraan wada sheekaysi hore</string>
<string name="empty_text"></string>
<string name="message_placeholder">%s</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1049,5 +1049,7 @@
<string name="planet_name">%s\'s Planet</string>
<string name="no_submissions">submissions not available</string>
<string name="no_chats">no previous chats</string>
<string name="empty_text"></string>
<string name="message_placeholder">%s</string>

</resources>

0 comments on commit 952a723

Please sign in to comment.