mirror of
https://github.com/nextcloud/talk-android
synced 2025-01-18 21:18:15 +00:00
Merge pull request #4559 from nextcloud/search_open_conversations
Improvise search in open conversations
This commit is contained in:
commit
1f908f8bae
@ -494,7 +494,8 @@ public interface NcApi {
|
||||
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);
|
||||
|
@ -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
|
||||
|
||||
@ -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()
|
||||
}
|
||||
|
||||
@ -83,6 +86,7 @@ class ListOpenConversationsActivity : BaseActivity() {
|
||||
} else {
|
||||
binding.searchOpenConversations.visibility = View.VISIBLE
|
||||
binding.textInputLayout.visibility = View.GONE
|
||||
binding.editText.hideKeyboardAndFocus()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
@ -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>() {
|
||||
|
@ -11,5 +11,5 @@ import io.reactivex.Observable
|
||||
|
||||
interface OpenConversationsRepository {
|
||||
|
||||
fun fetchConversations(): Observable<List<Conversation>>
|
||||
fun fetchConversations(searchTerm: String): Observable<List<Conversation>>
|
||||
}
|
||||
|
@ -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!! }
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user