Skip to content

Commit

Permalink
Merge branch 'master' into 3787-update-teams-list-to-display-username…
Browse files Browse the repository at this point in the history
…-instead-of-first-and-last-name
  • Loading branch information
dogi authored Jul 9, 2024
2 parents 2e89ca9 + 020db62 commit 134010b
Show file tree
Hide file tree
Showing 22 changed files with 335 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ abstract class BaseRecyclerFragment<LI> : BaseRecyclerParentFragment<Any?>(), On
"submission" -> (v as TextView).setText(R.string.no_submissions)
"teams" -> (v as TextView).setText(R.string.no_teams)
"chatHistory" -> (v as TextView).setText(R.string.no_chats)
"feedback" -> (v as TextView).setText(R.string.no_feedback)
else -> (v as TextView).setText(R.string.no_data_available_please_check_and_try_again)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ open class RealmChatHistory : RealmObject() {
var aiProvider: String? = null
var title: String? = null
var createdDate: String? = null
var updatedDate: String? = null
var conversations: RealmList<Conversation>? = null
companion object {
@JvmStatic
Expand All @@ -33,6 +34,7 @@ open class RealmChatHistory : RealmObject() {
chatHistory._id = JsonUtils.getString("_id", act)
chatHistory.title = JsonUtils.getString("title", act)
chatHistory.createdDate = JsonUtils.getString("createdDate", act)
chatHistory.updatedDate = JsonUtils.getString("updatedDate", act)
chatHistory.user = JsonUtils.getString("user", act)
chatHistory.aiProvider = JsonUtils.getString("aiProvider", act)
chatHistory.conversations = parseConversations(mRealm, JsonUtils.getJsonArray("conversations", act))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import org.ole.planet.myplanet.datamanager.*
import org.ole.planet.myplanet.model.*
import org.ole.planet.myplanet.model.RealmChatHistory.Companion.addConversationToChatHistory
import org.ole.planet.myplanet.service.UserProfileDbHandler
import org.ole.planet.myplanet.ui.dashboard.DashboardActivity
import org.ole.planet.myplanet.utilities.Utilities
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.Date

class ChatDetailFragment : Fragment() {
lateinit var fragmentChatDetailBinding: FragmentChatDetailBinding
Expand All @@ -30,6 +32,7 @@ class ChatDetailFragment : Fragment() {
private var aiName: String = ""
private var aiModel: String = ""
private lateinit var mRealm: Realm
var user: RealmUserModel? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -43,7 +46,7 @@ class ChatDetailFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mRealm = DatabaseService(requireContext()).realmInstance
val user = UserProfileDbHandler(requireContext()).userModel
user = UserProfileDbHandler(requireContext()).userModel
mAdapter = ChatAdapter(ArrayList(), requireContext(), fragmentChatDetailBinding.recyclerGchat)
fragmentChatDetailBinding.recyclerGchat.adapter = mAdapter
val layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(requireContext())
Expand Down Expand Up @@ -268,19 +271,21 @@ class ChatDetailFragment : Fragment() {
override fun onResponse(call: Call<ChatModel>, response: Response<ChatModel>) {
val responseBody = response.body()
if (response.isSuccessful && responseBody != null) {
val jsonObject = JsonObject()
if (responseBody.status == "Success") {
val chatResponse = response.body()?.chat
if (chatResponse != null) {
mAdapter.responseSource = ChatAdapter.RESPONSE_SOURCE_NETWORK
mAdapter.addResponse(chatResponse)
_id = "${response.body()?.couchDBResponse?.id}"
_rev = "${response.body()?.couchDBResponse?.rev}"
val jsonObject = JsonObject()
jsonObject.addProperty("_rev", "${response.body()?.couchDBResponse?.rev}")
jsonObject.addProperty("_id", "${response.body()?.couchDBResponse?.id}")
jsonObject.addProperty("time", "")
jsonObject.addProperty("title", "")
jsonObject.addProperty("updatedTime", "")
jsonObject.addProperty("aiProvider", aiName)
jsonObject.addProperty("user", user?.name)
jsonObject.addProperty("title", query)
jsonObject.addProperty("createdTime", Date().time)
jsonObject.addProperty("updatedDate", "")

val conversationsArray = JsonArray()
val conversationObject = JsonObject()
Expand All @@ -289,16 +294,22 @@ class ChatDetailFragment : Fragment() {
conversationsArray.add(conversationObject)

jsonObject.add("conversations", conversationsArray)

requireActivity().runOnUiThread {
RealmChatHistory.insert(mRealm, jsonObject)
}
(requireActivity() as? DashboardActivity)?.refreshChatHistoryList()
}
} else {
fragmentChatDetailBinding.textGchatIndicator.visibility = View.VISIBLE
fragmentChatDetailBinding.textGchatIndicator.text = context?.getString(R.string.message_placeholder, responseBody.message)
val jsonObject = JsonObject()
jsonObject.addProperty("_rev", "")
jsonObject.addProperty("_id", "")
jsonObject.addProperty("time", "")
jsonObject.addProperty("title", "")
jsonObject.addProperty("updatedTime", "")
jsonObject.addProperty("aiProvider", aiName)
jsonObject.addProperty("user", user?.name)
jsonObject.addProperty("title", query)
jsonObject.addProperty("createdTime", Date().time)
jsonObject.addProperty("updatedDate", "")

val conversationsArray = JsonArray()
val conversationObject = JsonObject()
Expand All @@ -310,6 +321,7 @@ class ChatDetailFragment : Fragment() {
requireActivity().runOnUiThread {
RealmChatHistory.insert(mRealm, jsonObject)
}
(requireActivity() as? DashboardActivity)?.refreshChatHistoryList()
}
} else {
fragmentChatDetailBinding.textGchatIndicator.visibility = View.VISIBLE
Expand All @@ -318,23 +330,6 @@ class ChatDetailFragment : Fragment() {
} else {
response.message()
}
val jsonObject = JsonObject()
jsonObject.addProperty("_rev", "")
jsonObject.addProperty("_id", "")
jsonObject.addProperty("time", "")
jsonObject.addProperty("title", "")
jsonObject.addProperty("updatedTime", "")

val conversationsArray = JsonArray()
val conversationObject = JsonObject()
conversationObject.addProperty("query", query)
conversationObject.addProperty("response", "")
conversationsArray.add(conversationObject)

jsonObject.add("conversations", conversationsArray)
requireActivity().runOnUiThread {
RealmChatHistory.insert(mRealm, jsonObject)
}
}

fragmentChatDetailBinding.buttonGchatSend.isEnabled = true
Expand All @@ -343,23 +338,6 @@ class ChatDetailFragment : Fragment() {
}

override fun onFailure(call: Call<ChatModel>, t: Throwable) {
val jsonObject = JsonObject()
jsonObject.addProperty("_rev", "")
jsonObject.addProperty("_id", "")
jsonObject.addProperty("time", "")
jsonObject.addProperty("title", "")
jsonObject.addProperty("updatedTime", "")

val conversationsArray = JsonArray()
val conversationObject = JsonObject()
conversationObject.addProperty("query", query)
conversationObject.addProperty("response", "")
conversationsArray.add(conversationObject)

jsonObject.add("conversations", conversationsArray)
requireActivity().runOnUiThread {
RealmChatHistory.insert(mRealm, jsonObject)
}
fragmentChatDetailBinding.textGchatIndicator.visibility = View.VISIBLE
fragmentChatDetailBinding.textGchatIndicator.text = context?.getString(R.string.message_placeholder, t.message)
fragmentChatDetailBinding.buttonGchatSend.isEnabled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ class ChatHistoryListAdapter(var context: Context, private var chatHistory: List
if (chat.conversations != null && chat.conversations?.isNotEmpty() == true) {
chat.conversations?.get(0)?.query?.contains(query, ignoreCase = true) == true
} else {
chat.title?.contains(query, ignoreCase = true) == true
chat.title?.contains(query, ignoreCase = true) ==true
}
}
notifyItemRangeChanged(0, filteredChatHistory.size)
notifyDataSetChanged()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
Expand All @@ -77,23 +77,9 @@ class ChatHistoryListAdapter(var context: Context, private var chatHistory: List
}

fun updateChatHistory(newChatHistory: List<RealmChatHistory>) {
val oldListSize = chatHistory.size
val newListSize = newChatHistory.size
chatHistory = newChatHistory
filteredChatHistory = newChatHistory
if(oldListSize<newListSize){
notifyItemRangeInserted(oldListSize,newListSize-oldListSize)
}
else if(oldListSize>newListSize){
notifyItemRangeRemoved(newListSize,oldListSize-newListSize)
}
else{
for(i in 0 until newListSize){
if(chatHistory[i] != newChatHistory[i]){
notifyItemChanged(i)
}
}
}
notifyDataSetChanged()
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,42 +54,13 @@ class ChatHistoryListFragment : Fragment() {
}
}

val mRealm = DatabaseService(requireActivity()).realmInstance
val chats = mRealm.where(RealmChatHistory::class.java).findAll()

val list = mRealm.where(RealmChatHistory::class.java).equalTo("user", user?.name)
.sort("id", Sort.DESCENDING)
.findAll()

val filteredHistoryList = ArrayList<RealmChatHistory>()
for (chat in chats) {
val model = list.find { it.id == chat.id }
if (model != null && !filteredHistoryList.contains(model)) {
filteredHistoryList.add(model)
}
}
showNoData(fragmentChatHistoryListBinding.noChats, filteredHistoryList.size, "chatHistory")
if (filteredHistoryList.isEmpty()) {
fragmentChatHistoryListBinding.searchBar.visibility = View.GONE
fragmentChatHistoryListBinding.recyclerView.visibility = View.GONE
}
val adapter = ChatHistoryListAdapter(requireContext(), list, this)
adapter.setChatHistoryItemClickListener(object : ChatHistoryListAdapter.ChatHistoryItemClickListener {
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) }

fragmentChatHistoryListBinding.slidingPaneLayout.openPane()
}
})
fragmentChatHistoryListBinding.recyclerView.adapter = adapter
refreshChatHistoryList()

fragmentChatHistoryListBinding.searchBar.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
adapter.filter(s.toString())
(fragmentChatHistoryListBinding.recyclerView.adapter as? ChatHistoryListAdapter)?.filter(s.toString())
}

override fun afterTextChanged(s: Editable?) {}
Expand All @@ -102,8 +73,30 @@ class ChatHistoryListFragment : Fragment() {
.sort("id", Sort.DESCENDING)
.findAll()

val adapter = fragmentChatHistoryListBinding.recyclerView.adapter as ChatHistoryListAdapter
adapter.updateChatHistory(list)
val adapter = fragmentChatHistoryListBinding.recyclerView.adapter as? ChatHistoryListAdapter
if (adapter == null) {
val newAdapter = ChatHistoryListAdapter(requireContext(), list, this)
newAdapter.setChatHistoryItemClickListener(object : ChatHistoryListAdapter.ChatHistoryItemClickListener {
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) }

fragmentChatHistoryListBinding.slidingPaneLayout.openPane()
}
})
fragmentChatHistoryListBinding.recyclerView.adapter = newAdapter
} else {
adapter.updateChatHistory(list)
fragmentChatHistoryListBinding.searchBar.visibility = View.VISIBLE
fragmentChatHistoryListBinding.recyclerView.visibility = View.VISIBLE
}

showNoData(fragmentChatHistoryListBinding.noChats, list.size, "chatHistory")
if (list.isEmpty()) {
fragmentChatHistoryListBinding.searchBar.visibility = View.GONE
fragmentChatHistoryListBinding.recyclerView.visibility = View.GONE
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.databinding.RowJoinedUserBinding
import org.ole.planet.myplanet.model.RealmUserModel

class AdapterLeader(var context: Context, var leaders: List<RealmUserModel>) :
class AdapterLeader(var context: Context, private var leaders: List<RealmUserModel>) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private lateinit var rowJoinedUserBinding: RowJoinedUserBinding
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
Expand All @@ -23,15 +24,15 @@ class AdapterLeader(var context: Context, var leaders: List<RealmUserModel>) :
if (leaders[position].firstName == null) {
holder.title.text = leaders[position].name
} else {
holder.title.text = "${leaders[position]}"
holder.title.text = context.getString(R.string.message_placeholder, leaders[position])
}
holder.tv_description.text = leaders[position].email
holder.tvDescription.text = leaders[position].email
}
}

internal inner class ViewHolderLeader(private val rowJoinedUserBinding: RowJoinedUserBinding) : RecyclerView.ViewHolder(rowJoinedUserBinding.root){
internal inner class ViewHolderLeader(rowJoinedUserBinding: RowJoinedUserBinding) : RecyclerView.ViewHolder(rowJoinedUserBinding.root){
var title= rowJoinedUserBinding.tvTitle
var tv_description= rowJoinedUserBinding.tvDescription
var tvDescription= rowJoinedUserBinding.tvDescription
var icon= rowJoinedUserBinding.icMore
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayoutMediator
import org.ole.planet.myplanet.databinding.FragmentTeamDetailBinding
import org.ole.planet.myplanet.service.UserProfileDbHandler
import org.ole.planet.myplanet.ui.sync.SyncActivity
import org.ole.planet.myplanet.utilities.Constants.PREFS_NAME
import org.ole.planet.myplanet.utilities.TimeUtils
import java.util.Date
Expand Down
Loading

0 comments on commit 134010b

Please sign in to comment.