Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvise search in open conversations #4559

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/src/main/java/com/nextcloud/talk/api/NcApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ Observable<GenericOverall> setChatReadMarker(@Header("Authorization") String aut
Server URL is: baseUrl + ocsApiVersion + spreedApiVersion + /listed-room
*/
@GET
Observable<RoomsOverall> getOpenConversations(@Header("Authorization") String authorization, @Url String url);
Observable<RoomsOverall> getOpenConversations(@Header("Authorization") String authorization, @Url String url,
@Query("searchTerm") String searchTerm);

@GET
Observable<StatusOverall> status(@Header("Authorization") String authorization, @Url String url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.nextcloud.talk.models.json.conversations.Conversation
import com.nextcloud.talk.openconversations.adapters.OpenConversationsAdapter
import com.nextcloud.talk.openconversations.viewmodels.OpenConversationsViewModel
import com.nextcloud.talk.utils.bundle.BundleKeys
import com.vanniktech.ui.hideKeyboardAndFocus
import com.vanniktech.ui.showKeyboardAndFocus
import javax.inject.Inject

Expand Down Expand Up @@ -69,9 +70,11 @@ class ListOpenConversationsActivity : BaseActivity() {
handleSearchUI(searching)
}
binding.editText.doOnTextChanged { text, _, _, count ->
adapter.filter(text.toString())
if (!text.isNullOrBlank()) {
openConversationsViewModel.updateSearchTerm(text.toString())
openConversationsViewModel.fetchConversations()
}
}

initObservers()
}

Expand All @@ -83,6 +86,7 @@ class ListOpenConversationsActivity : BaseActivity() {
} else {
binding.searchOpenConversations.visibility = View.VISIBLE
binding.textInputLayout.visibility = View.GONE
binding.editText.hideKeyboardAndFocus()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class OpenConversationsAdapter(
private val onClick: (Conversation) -> Unit
) :
ListAdapter<Conversation, OpenConversationsAdapter.OpenConversationsViewHolder>(ConversationsCallback) {
private var originalList: List<Conversation> = emptyList()
private var isFiltering = false

inner class OpenConversationsViewHolder(val itemBinding: RvItemOpenConversationBinding) :
RecyclerView.ViewHolder(itemBinding.root) {
Expand Down Expand Up @@ -78,33 +76,6 @@ class OpenConversationsAdapter(
val conversation = getItem(position)
holder.bindItem(conversation)
}

fun filter(text: String) {
if (text == "") {
submitList(originalList)
isFiltering = false
return
}

isFiltering = true
val newList = mutableListOf<Conversation>()
for (conversation in originalList) {
if (conversation.displayName.contains(text, true) || conversation.description!!.contains(text, true)) {
newList.add(conversation)
}
}

if (newList.isNotEmpty()) {
submitList(newList)
}
}

override fun onCurrentListChanged(previousList: MutableList<Conversation>, currentList: MutableList<Conversation>) {
if (!isFiltering) {
originalList = currentList
}
super.onCurrentListChanged(previousList, currentList)
}
}

object ConversationsCallback : DiffUtil.ItemCallback<Conversation>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ import io.reactivex.Observable

interface OpenConversationsRepository {

fun fetchConversations(): Observable<List<Conversation>>
fun fetchConversations(searchTerm: String): Observable<List<Conversation>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ class OpenConversationsRepositoryImpl(private val ncApi: NcApi, currentUserProvi

val apiVersion = ApiUtils.getConversationApiVersion(currentUser, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V3, 1))

override fun fetchConversations(): Observable<List<Conversation>> {
override fun fetchConversations(searchTerm: String): Observable<List<Conversation>> {
val roomOverall = ncApi.getOpenConversations(
credentials,
ApiUtils.getUrlForOpenConversations(apiVersion, currentUser.baseUrl!!)
ApiUtils.getUrlForOpenConversations(apiVersion, currentUser.baseUrl!!),
searchTerm
)
return roomOverall.map { it.ocs?.data!! }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,22 @@ class OpenConversationsViewModel @Inject constructor(private val repository: Ope
val viewState: LiveData<ViewState>
get() = _viewState

private val _searchTerm: MutableLiveData<String> = MutableLiveData("")
val searchTerm: LiveData<String>
get() = _searchTerm

fun fetchConversations() {
_viewState.value = FetchConversationsStartState
repository.fetchConversations()
repository.fetchConversations(_searchTerm.value ?: "")
.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(FetchConversationsObserver())
}

fun updateSearchTerm(newTerm: String) {
_searchTerm.value = newTerm
}

inner class FetchConversationsObserver : Observer<List<Conversation>> {
override fun onSubscribe(d: Disposable) {
// unused atm
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_open_conversations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
android:layout_height="wrap_content"
android:hint="@string/nc_search"
android:textColorHint="@color/low_emphasis_text"
android:singleLine="true"
android:textColor = "@color/high_emphasis_text"/>
</com.google.android.material.textfield.TextInputLayout>
</com.google.android.material.appbar.MaterialToolbar>
Expand Down
Loading