Skip to content

Commit

Permalink
Merge pull request #98 from shockbytes/improve-search
Browse files Browse the repository at this point in the history
Search for author and subTitle in search view too
  • Loading branch information
shockbytes authored Sep 29, 2020
2 parents ad4a584 + 38f86a1 commit 67707ae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SearchViewModel @Inject constructor(
}

private val searchState = MutableLiveData<SearchState>()
fun getSearchState(): LiveData<SearchState> = searchState

private val bookDownloadSubject = PublishSubject.create<BookSearchItem>()

Expand All @@ -35,25 +36,25 @@ class SearchViewModel @Inject constructor(
searchState.postValue(SearchState.InitialState)
}

fun getSearchState(): LiveData<SearchState> = searchState

fun showBooks(query: CharSequence, keepLocal: Boolean) {

if (query.isNotEmpty()) {

searchState.postValue(SearchState.LoadingState)

resolveSource(query.toString(), keepLocal)
.subscribe({
if (it.isNotEmpty()) {
searchState.postValue(SearchState.SuccessState(it))
.doOnSubscribe {
searchState.postValue(SearchState.LoadingState)
}
.map { bookSearchItems ->
if (bookSearchItems.isNotEmpty()) {
SearchState.SuccessState(bookSearchItems)
} else {
searchState.postValue(SearchState.EmptyState)
SearchState.EmptyState
}
}, {
Timber.e(it)
searchState.postValue(SearchState.ErrorState(it))
})
}
.doOnError { throwable ->
searchState.postValue(SearchState.ErrorState(throwable))
}
.subscribe(searchState::postValue, Timber::e)
.addTo(compositeDisposable)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ class RealmBookEntityDao(private val realm: RealmInstanceProvider) : BookEntityD
override fun search(query: String): Observable<List<BookEntity>> {
return realm.instance.where(bookClass)
.contains("title", query, Case.INSENSITIVE)
.or()
.contains("author", query, Case.INSENSITIVE)
.or()
.contains("subTitle", query, Case.INSENSITIVE)
.findAll()
.asFlowable()
.map { mapper.mapTo(it) }
Expand Down

0 comments on commit 67707ae

Please sign in to comment.