var searchResponseLiveData : MutableLiveData = MutableLiveData() var searchProgressBarLiveData : MutableLiveData = MutableLiveData() /** * Method to observe the text changes and filter out the appropriate stream to make the api call. */ fun processSearchTextChanges(changedText: InitialValueObservable) { compositeDisposable.add( changedText .skipInitialValue() .debounce(500, TimeUnit.MILLISECONDS) .doOnNext { if (it.trim().toString().length >= 3) searchProgressBarLiveData.postValue(true) } .filter { it.trim().toString().length >= 3 } .flatMapSingle { fetchSearchResults(it.trim().toString()) } .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe({ // success searchProgressBarLiveData.postValue(false) searchResponseLiveData.postValue(it) }, { // error searchProgressBarLiveData.postValue(false) searchResponseLiveData.postValue(Resource.error()) }) ) } /** * Method to make the Auto Suggestion Search Api call. */ private fun fetchSearchResults(searchTerm: String?): Single { return searchRepository.getAutoSuggestionResults(searchTerm!!) }