Skip to content

Commit

Permalink
chat: mark shared (fixes #3734) (#3735)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Jul 1, 2024
1 parent 3286e7a commit 45a922b
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 83 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1637
versionName "0.16.37"
versionCode 1638
versionName "0.16.38"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import io.realm.Case
import io.realm.Realm
import io.realm.RealmList
import io.realm.RealmResults
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.databinding.AddNoteDialogBinding
import org.ole.planet.myplanet.databinding.ChatShareDialogBinding
Expand All @@ -18,6 +20,7 @@ import org.ole.planet.myplanet.datamanager.DatabaseService
import org.ole.planet.myplanet.model.Conversation
import org.ole.planet.myplanet.model.RealmChatHistory
import org.ole.planet.myplanet.model.RealmMyTeam
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.service.UserProfileDbHandler
Expand All @@ -27,49 +30,59 @@ import org.ole.planet.myplanet.ui.team.BaseTeamFragment.Companion.settings
import org.ole.planet.myplanet.utilities.Constants.PREFS_NAME
import java.util.Date

class ChatHistoryListAdapter(var context: Context, private var chatHistory: List<RealmChatHistory>) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private lateinit var rowChatHistoryBinding: RowChatHistoryBinding
private var chatHistoryItemClickListener: ChatHistoryItemClickListener? = null
private var filteredChatHistory: List<RealmChatHistory> = chatHistory
private var chatTitle: String? = ""
private lateinit var expandableListAdapter: ExpandableListAdapter
private lateinit var expandableTitleList: List<String>
private lateinit var expandableDetailList: HashMap<String, List<String>>
private lateinit var mRealm: Realm
var user: RealmUserModel? = null

interface ChatHistoryItemClickListener {
fun onChatHistoryItemClicked(conversations: RealmList<Conversation>?, _id: String, _rev: String?)
}
class ChatHistoryListAdapter(var context: Context, private var chatHistory: List<RealmChatHistory>, private val fragment: ChatHistoryListFragment) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private lateinit var rowChatHistoryBinding: RowChatHistoryBinding
private var chatHistoryItemClickListener: ChatHistoryItemClickListener? = null
private var filteredChatHistory: List<RealmChatHistory> = chatHistory
private var chatTitle: String? = ""
private lateinit var expandableListAdapter: ExpandableListAdapter
private lateinit var expandableTitleList: List<String>
private lateinit var expandableDetailList: HashMap<String, List<String>>
private lateinit var mRealm: Realm
var user: RealmUserModel? = null
private var newsList: RealmResults<RealmNews>? = null

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

fun setChatHistoryItemClickListener(listener: ChatHistoryItemClickListener) {
chatHistoryItemClickListener = listener
}
fun setChatHistoryItemClickListener(listener: ChatHistoryItemClickListener) {
chatHistoryItemClickListener = listener
}

fun filter(query: String) {
filteredChatHistory = chatHistory.filter { chat ->
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
}
fun filter(query: String) {
filteredChatHistory = chatHistory.filter { chat ->
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
}
notifyDataSetChanged()
}
notifyDataSetChanged()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
rowChatHistoryBinding = RowChatHistoryBinding.inflate(LayoutInflater.from(parent.context), parent, false)
mRealm = DatabaseService(context).realmInstance
user = UserProfileDbHandler(context).userModel
newsList = mRealm.where(RealmNews::class.java)
.equalTo("docType", "message", Case.INSENSITIVE)
.equalTo("createdOn", user?.planetCode, Case.INSENSITIVE)
.findAll()
return ViewHolderChat(rowChatHistoryBinding)
}

override fun getItemCount(): Int {
return filteredChatHistory.size
}
override fun getItemCount(): Int {
return filteredChatHistory.size
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
fun updateChatHistory(newChatHistory: List<RealmChatHistory>) {
chatHistory = newChatHistory
filteredChatHistory = newChatHistory
notifyDataSetChanged()
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val viewHolderChat = holder as ViewHolderChat
if (filteredChatHistory[position].conversations != null && filteredChatHistory[position].conversations?.isNotEmpty() == true) {
viewHolderChat.rowChatHistoryBinding.chatTitle.text = filteredChatHistory[position].conversations?.get(0)?.query
Expand All @@ -90,53 +103,62 @@ class ChatHistoryListAdapter(var context: Context, private var chatHistory: List
)
}

viewHolderChat.rowChatHistoryBinding.shareChat.setOnClickListener {
val chatShareDialogBinding = ChatShareDialogBinding.inflate(LayoutInflater.from(context))
var dialog: AlertDialog? = null

expandableDetailList = getData() as HashMap<String, List<String>>
expandableTitleList = ArrayList<String>(expandableDetailList.keys)
expandableListAdapter = ExpandableListAdapter(context, expandableTitleList, expandableDetailList)
chatShareDialogBinding.listView.setAdapter(expandableListAdapter)

chatShareDialogBinding.listView.setOnChildClickListener { _, _, groupPosition, childPosition, id ->
if (expandableTitleList[groupPosition] == "share with team/enterprise") {
val teamList = mRealm.where(RealmMyTeam::class.java)
.isEmpty("teamId").notEqualTo("status", "archived")
.equalTo("type", "team").findAll()

val enterpriseList = mRealm.where(RealmMyTeam::class.java)
.isEmpty("teamId").notEqualTo("status", "archived")
.equalTo("type", "enterprise").findAll()

if (expandableDetailList[expandableTitleList[groupPosition]]?.get(childPosition) == "teams") {
showGrandChildRecyclerView(teamList, "teams", filteredChatHistory[position])
} else {
showGrandChildRecyclerView(enterpriseList, "enterprises", filteredChatHistory[position])
}
} else {
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
val sParentcode = settings?.getString("parentCode", "")
val communityName = settings?.getString("communityName", "")
val teamId = "$communityName@$sParentcode"
val community = mRealm.where(RealmMyTeam::class.java).equalTo("_id", teamId).findFirst()
showEditTextAndShareButton(community, "community", filteredChatHistory[position])
val isInNewsList = newsList?.any { newsItem ->
newsItem.newsId == filteredChatHistory[position]._id
} ?: false

if (isInNewsList) {
viewHolderChat.rowChatHistoryBinding.shareChat.setImageResource(R.drawable.baseline_check_24)
} else {
viewHolderChat.rowChatHistoryBinding.shareChat.setImageResource(R.drawable.baseline_share_24)
viewHolderChat.rowChatHistoryBinding.shareChat.setOnClickListener {
val chatShareDialogBinding = ChatShareDialogBinding.inflate(LayoutInflater.from(context))
var dialog: AlertDialog? = null

expandableDetailList = getData() as HashMap<String, List<String>>
expandableTitleList = ArrayList<String>(expandableDetailList.keys)
expandableListAdapter = ExpandableListAdapter(context, expandableTitleList, expandableDetailList)
chatShareDialogBinding.listView.setAdapter(expandableListAdapter)

chatShareDialogBinding.listView.setOnChildClickListener { _, _, groupPosition, childPosition, _ ->
if (expandableTitleList[groupPosition] == "share with team/enterprise") {
val teamList = mRealm.where(RealmMyTeam::class.java)
.isEmpty("teamId").notEqualTo("status", "archived")
.equalTo("type", "team").findAll()

val enterpriseList = mRealm.where(RealmMyTeam::class.java)
.isEmpty("teamId").notEqualTo("status", "archived")
.equalTo("type", "enterprise").findAll()

if (expandableDetailList[expandableTitleList[groupPosition]]?.get(childPosition) == "teams") {
showGrandChildRecyclerView(teamList, "teams", filteredChatHistory[position])
} else {
showGrandChildRecyclerView(enterpriseList, "enterprises", filteredChatHistory[position])
}
} else {
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
val sParentcode = settings?.getString("parentCode", "")
val communityName = settings?.getString("communityName", "")
val teamId = "$communityName@$sParentcode"
val community = mRealm.where(RealmMyTeam::class.java).equalTo("_id", teamId).findFirst()
showEditTextAndShareButton(community, "community", filteredChatHistory[position])
}
dialog?.dismiss()
false
}
dialog?.dismiss()
false
}

val builder = AlertDialog.Builder(context)
builder.setView(chatShareDialogBinding.root)
builder.setPositiveButton(context.getString(R.string.close)) { _, _ ->
dialog?.dismiss()
val builder = AlertDialog.Builder(context)
builder.setView(chatShareDialogBinding.root)
builder.setPositiveButton(context.getString(R.string.close)) { _, _ ->
dialog?.dismiss()
}
dialog = builder.create()
dialog.show()
}
dialog = builder.create()
dialog.show()
}
}

private fun showGrandChildRecyclerView(items: List<RealmMyTeam>, section: String, realmChatHistory: RealmChatHistory) {
private fun showGrandChildRecyclerView(items: List<RealmMyTeam>, section: String, realmChatHistory: RealmChatHistory) {
val grandChildDialogBinding = GrandChildRecyclerviewDialogBinding.inflate(LayoutInflater.from(context))
var dialog: AlertDialog? = null

Expand All @@ -161,7 +183,7 @@ class ChatHistoryListAdapter(var context: Context, private var chatHistory: List
dialog.show()
}

private fun showEditTextAndShareButton(team: RealmMyTeam? = null, section: String, chatHistory: RealmChatHistory) {
private fun showEditTextAndShareButton(team: RealmMyTeam? = null, section: String, chatHistory: RealmChatHistory) {
val addNoteDialogBinding = AddNoteDialogBinding.inflate(LayoutInflater.from(context))
val builder = AlertDialog.Builder(context)
builder.setView(addNoteDialogBinding.root)
Expand All @@ -187,6 +209,7 @@ class ChatHistoryListAdapter(var context: Context, private var chatHistory: List
map["news"] = Gson().toJson(serializedMap)

createNews(map, mRealm, user, null)
fragment.refreshChatHistoryList()
dialog.dismiss()
}
builder.setNegativeButton(context.getString(R.string.cancel)) { dialog, _ ->
Expand All @@ -196,14 +219,14 @@ class ChatHistoryListAdapter(var context: Context, private var chatHistory: List
dialog.show()
}

private fun serializeConversation(conversation: Conversation): HashMap<String?, String> {
private fun serializeConversation(conversation: Conversation): HashMap<String?, String> {
val conversationMap = HashMap<String?, String>()
conversationMap["query"] = conversation.query ?: ""
conversationMap["response"] = conversation.response ?: ""
return conversationMap
}

private fun getData(): Map<String, List<String>> {
private fun getData(): Map<String, List<String>> {
val expandableListDetail: MutableMap<String, List<String>> = HashMap()
val community: MutableList<String> = ArrayList()
community.add("community")
Expand All @@ -214,9 +237,8 @@ class ChatHistoryListAdapter(var context: Context, private var chatHistory: List

expandableListDetail["share with community"] = community
expandableListDetail["share with team/enterprise"] = teams

return expandableListDetail
}

class ViewHolderChat(val rowChatHistoryBinding: RowChatHistoryBinding) : RecyclerView.ViewHolder(rowChatHistoryBinding.root)
}
class ViewHolderChat(val rowChatHistoryBinding: RowChatHistoryBinding) : RecyclerView.ViewHolder(rowChatHistoryBinding.root)
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ChatHistoryListFragment : Fragment() {
filteredHistoryList.add(model)
}
}
val adapter = ChatHistoryListAdapter(requireContext(), list)
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) }
Expand All @@ -89,6 +89,16 @@ class ChatHistoryListFragment : Fragment() {
override fun afterTextChanged(s: Editable?) {}
})
}

fun refreshChatHistoryList() {
val mRealm = DatabaseService(requireActivity()).realmInstance
val list = mRealm.where(RealmChatHistory::class.java).equalTo("user", user?.name)
.sort("id", Sort.DESCENDING)
.findAll()

val adapter = fragmentChatHistoryListBinding.recyclerView.adapter as ChatHistoryListAdapter
adapter.updateChatHistory(list)
}
}

class ChatHistoryListOnBackPressedCallback(private val slidingPaneLayout: SlidingPaneLayout) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class NewsFragment : BaseNewsFragment() {
return@setOnClickListener
}
fragmentNewsBinding.etMessage.setText("")
val map = HashMap<String?, String>() // Changed to String, String
val map = HashMap<String?, String>()
map["message"] = message
map["viewInId"] = "${user?.planetCode ?: ""}@${user?.parentCode ?: ""}"
map["viewInSection"] = "community"
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/baseline_check_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorPrimary"
android:viewportWidth="24"
android:viewportHeight="24">

<path
android:fillColor="@android:color/white"
android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z" />

</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/row_chat_history.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="@dimen/_4dp"
android:padding="10dp"
android:background="?attr/selectableItemBackground"
android:contentDescription="@string/share_chat"
android:src="@drawable/baseline_share_24"
Expand Down

0 comments on commit 45a922b

Please sign in to comment.