Skip to content

Commit

Permalink
add click listener
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Jul 23, 2024
1 parent e594a04 commit cd18bdf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
25 changes: 15 additions & 10 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 @@ -3,27 +3,23 @@ package org.ole.planet.myplanet.ui.chat
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import io.realm.RealmList
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.databinding.ItemAiResponseMessageBinding
import org.ole.planet.myplanet.databinding.ItemUserMessageBinding
import org.ole.planet.myplanet.model.Conversation

class ChatAdapter(private val chatList: ArrayList<String>, val context: Context, private val recyclerView: RecyclerView) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private lateinit var textUserMessageBinding: ItemUserMessageBinding
private lateinit var textAiMessageBinding: ItemAiResponseMessageBinding
var responseSource: Int = RESPONSE_SOURCE_UNKNOWN

private val viewTypeQuery = 1
private val viewTypeResponse = 2
companion object {
const val RESPONSE_SOURCE_SHARED_VIEW_MODEL = 1
const val RESPONSE_SOURCE_NETWORK = 2
const val RESPONSE_SOURCE_UNKNOWN = 0
}

class QueryViewHolder(private val textUserMessageBinding: ItemUserMessageBinding) : RecyclerView.ViewHolder(textUserMessageBinding.root) {
fun bind(query: String) {
textUserMessageBinding.textGchatMessageMe.text = query
Expand Down Expand Up @@ -64,14 +60,12 @@ class ChatAdapter(private val chatList: ArrayList<String>, val context: Context,
}

fun addQuery(query: String) {
Log.d("ChatAdapter", "addQuery: $query")
chatList.add(query)
notifyItemInserted(chatList.size - 1)
scrollToLastItem()
}

fun addResponse(response: String) {
Log.d("ChatAdapter", "addResponse: $response")
chatList.add(response)
notifyItemInserted(chatList.size - 1)
scrollToLastItem()
Expand Down Expand Up @@ -110,7 +104,6 @@ class ChatAdapter(private val chatList: ArrayList<String>, val context: Context,

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val chatItem = chatList[position]
Log.d("ChatAdapter", "chatList: $chatList")
when (holder.itemViewType) {
viewTypeQuery -> {
val queryViewHolder = holder as QueryViewHolder
Expand All @@ -127,4 +120,16 @@ class ChatAdapter(private val chatList: ArrayList<String>, val context: Context,
override fun getItemCount(): Int {
return chatList.size
}

companion object {
const val RESPONSE_SOURCE_SHARED_VIEW_MODEL = 1
const val RESPONSE_SOURCE_NETWORK = 2
const val RESPONSE_SOURCE_UNKNOWN = 0

private var chatHistoryItemClickListener: ChatHistoryListAdapter.ChatHistoryItemClickListener? = null

fun clickListener(conversations: String?, newsId: String?, newsRev: String?) {
chatHistoryItemClickListener?.onChatHistoryItemClicked(conversations as RealmList<Conversation>?, newsId ?: "", newsRev ?: "")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.ole.planet.myplanet.ui.news
import android.app.Activity
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.os.Build
import android.view.LayoutInflater
import android.view.MenuItem
Expand Down Expand Up @@ -36,6 +35,7 @@ import org.ole.planet.myplanet.model.RealmNews
import org.ole.planet.myplanet.model.RealmNews.Companion.createNews
import org.ole.planet.myplanet.model.RealmUserModel
import org.ole.planet.myplanet.ui.chat.ChatAdapter
import org.ole.planet.myplanet.ui.chat.ChatAdapter.Companion.clickListener
import org.ole.planet.myplanet.utilities.Constants
import org.ole.planet.myplanet.utilities.Constants.showBetaFeature
import org.ole.planet.myplanet.utilities.JsonUtils.getString
Expand Down Expand Up @@ -163,6 +163,10 @@ class AdapterNews(var context: Context, private val list: MutableList<RealmNews?
holder.rowNewsBinding.recyclerGchat.layoutManager = LinearLayoutManager(context)
holder.rowNewsBinding.recyclerGchat.visibility = View.VISIBLE
holder.rowNewsBinding.sharedChat.visibility = View.VISIBLE

holder.rowNewsBinding.linearLayout5.setOnClickListener {
clickListener(news.conversations, news.newsId, news.newsRev)
}
} else {
holder.rowNewsBinding.recyclerGchat.visibility = View.GONE
holder.rowNewsBinding.sharedChat.visibility = View.GONE
Expand Down

0 comments on commit cd18bdf

Please sign in to comment.