Skip to content

Commit

Permalink
chat: smoother searching (fixes #5296) (#5345)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
pavi38 and dogi authored Feb 26, 2025
1 parent b153596 commit 5828d72
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 26
targetSdkVersion 34
versionCode 2331
versionName "0.23.31"
versionCode 2332
versionName "0.23.32"
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 @@ -76,7 +76,49 @@ class ChatHistoryListAdapter(var context: Context, private var chatHistory: List
.replace(Regex("\\p{InCombiningDiacriticalMarks}+"), "")
}

fun search(s: String) {
fun search(s: String, isFullSearch: Boolean, isQuestion: Boolean){
if(isFullSearch){
FullConvoSearch(s, isQuestion)
} else {
searchByTitle(s)
}
notifyDataSetChanged()
}

private fun FullConvoSearch(s: String, isQuestion: Boolean){
var conversation: String?
val queryParts = s.split(" ").filterNot { it.isEmpty() }
val normalizedQuery = normalizeText(s)
val inTitleStartQuery = mutableListOf<RealmChatHistory>()
val inTitleContainsQuery = mutableListOf<RealmChatHistory>()
val startsWithQuery = mutableListOf<RealmChatHistory>()
val containsQuery = mutableListOf<RealmChatHistory>()

for (chat in chatHistory) {
if(chat.conversations != null && chat.conversations?.isNotEmpty() == true) {
for (i in 0 until chat.conversations!!.size) {
conversation = if(isQuestion){
chat.conversations?.get(i)?.query?.let { normalizeText(it) }
} else{
chat.conversations?.get(i)?.response?.let { normalizeText(it) }
}
if(conversation == null) continue
if (conversation.startsWith(normalizedQuery, ignoreCase = true)) {
if(i == 0) inTitleStartQuery.add(chat)
else startsWithQuery.add(chat)
break
} else if (queryParts.all { conversation.contains(normalizeText(it), ignoreCase = true) }) {
if(i == 0) inTitleContainsQuery.add(chat)
else containsQuery.add(chat)
break
}
}
}
}
filteredChatHistory = inTitleStartQuery+ inTitleContainsQuery + startsWithQuery + containsQuery
}

private fun searchByTitle(s: String) {
var title: String?
val queryParts = s.split(" ").filterNot { it.isEmpty() }
val normalizedQuery = normalizeText(s)
Expand All @@ -97,7 +139,6 @@ class ChatHistoryListAdapter(var context: Context, private var chatHistory: List
}
}
filteredChatHistory = startsWithQuery + containsQuery
notifyDataSetChanged()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.ole.planet.myplanet.ui.chat

import android.content.res.ColorStateList
import android.content.res.Resources
import android.os.Bundle
import android.text.*
import android.view.*
Expand All @@ -19,6 +21,8 @@ class ChatHistoryListFragment : Fragment() {
private lateinit var fragmentChatHistoryListBinding: FragmentChatHistoryListBinding
private lateinit var sharedViewModel: ChatViewModel
var user: RealmUserModel? = null
private var isFullSearch: Boolean = false
private var isQuestion: Boolean = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -37,6 +41,7 @@ class ChatHistoryListFragment : Fragment() {
slidingPaneLayout.lockMode = SlidingPaneLayout.LOCK_MODE_LOCKED
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, ChatHistoryListOnBackPressedCallback(slidingPaneLayout))

fragmentChatHistoryListBinding.toggleGroup.visibility = View.GONE
fragmentChatHistoryListBinding.newChat.setOnClickListener {
if (resources.getBoolean(R.bool.isLargeScreen)) {
val chatHistoryListFragment = ChatHistoryListFragment()
Expand All @@ -61,11 +66,49 @@ class ChatHistoryListFragment : Fragment() {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

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

override fun afterTextChanged(s: Editable?) {}
})

fragmentChatHistoryListBinding.fullSearch.setOnCheckedChangeListener { _, isChecked ->
val density = Resources.getSystem().displayMetrics.density
val params = fragmentChatHistoryListBinding.fullSearch.layoutParams as ViewGroup.MarginLayoutParams
if (isChecked) {
isFullSearch = true
fragmentChatHistoryListBinding.toggleGroup.visibility = View.VISIBLE
params.topMargin = (0 * density).toInt()
} else {
isFullSearch = false
fragmentChatHistoryListBinding.toggleGroup.visibility = View.GONE
params.topMargin = (20 * density).toInt()
}
fragmentChatHistoryListBinding.fullSearch.layoutParams = params
}

fragmentChatHistoryListBinding.toggleGroup.addOnButtonCheckedListener { _, checkedId, isChecked ->
if(isChecked){
when (checkedId) {
R.id.btnQuestions -> {
isQuestion = true
fragmentChatHistoryListBinding.btnQuestions.strokeColor = ColorStateList.valueOf(resources.getColor(R.color.mainColor))
fragmentChatHistoryListBinding.btnResponses.strokeColor = ColorStateList.valueOf(resources.getColor(R.color.hint_color))

fragmentChatHistoryListBinding.btnQuestions.setTextColor(resources.getColor(R.color.mainColor))
fragmentChatHistoryListBinding.btnResponses.setTextColor(resources.getColor(R.color.hint_color))
}
R.id.btnResponses -> {
isQuestion = false
fragmentChatHistoryListBinding.btnResponses.strokeColor = ColorStateList.valueOf(resources.getColor(R.color.mainColor))
fragmentChatHistoryListBinding.btnQuestions.strokeColor = ColorStateList.valueOf(resources.getColor(R.color.hint_color))

fragmentChatHistoryListBinding.btnResponses.setTextColor(resources.getColor(R.color.mainColor))
fragmentChatHistoryListBinding.btnQuestions.setTextColor(resources.getColor(R.color.hint_color))
}
}
}
}
}

fun refreshChatHistoryList() {
Expand Down
75 changes: 63 additions & 12 deletions app/src/main/res/layout/fragment_chat_history_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,71 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/searchBar"
<LinearLayout
android:id="@+id/search_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:hint="@string/search_dots"
android:textColor="@color/daynight_textColor"
android:textColorHint="@color/hint_color"
android:backgroundTint="@color/hint_color"
app:layout_constraintEnd_toEndOf="parent"
android:orientation="horizontal"
android:background="@color/secondary_bg"
android:layout_marginTop="@dimen/_10dp"
app:layout_constraintTop_toBottomOf="@+id/newChat"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/newChat" />
app:layout_constraintEnd_toEndOf="parent">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation = "vertical"
android:layout_marginStart="15dp">

<CheckBox
android:id="@+id/full_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/hint_color"
android:layout_marginTop="20dp"
android:text="Full conversation search"/>
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:singleSelection="true">

<com.google.android.material.button.MaterialButton
android:id="@+id/btnQuestions"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Question"
android:textSize="10sp"
android:textColor="@color/hint_color"
app:strokeColor="@color/hint_color"
style="@style/Widget.MaterialComponents.Button.OutlinedButton" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnResponses"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Response"
android:textSize="10sp"
android:textColor="@color/hint_color"
app:strokeColor="@color/hint_color"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"/>
</com.google.android.material.button.MaterialButtonToggleGroup>
</LinearLayout>

<EditText
android:id="@+id/searchBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:hint="@string/search_dots"
android:textColor="@color/daynight_textColor"
android:textColorHint="@color/hint_color"
android:backgroundTint="@color/hint_color" />
</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
Expand All @@ -54,7 +105,7 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchBar" />
app:layout_constraintTop_toBottomOf="@+id/search_layout" />
<TextView
android:visibility="gone"
android:id="@+id/noChats"
Expand Down

0 comments on commit 5828d72

Please sign in to comment.