Android Netflix like Search using Rxjava (Epoxy Recyclerview, Retrofit)

Android RxJava search using Epoxy recyclerview

In this article I have implemented Android search using RxJava for instant search results. Various libraries such as Airbnb’s Epoxy recyclerview, Retrofit for network call is implemented for fetching results from the TMDB api.

I would be discussing only the important things in this article that deals with RXJava , Network calls and the Epoxy recyclerview implementation.

Usages:

  1. RxJava
  2. Epoxy library
  3. Retrofit
  4. TMDB API

RxJava

For the basic implementation of a search view in RxJava I have used four operators namely debounce, filter, distinctUntilChanged and switchMap.

I will discuss the usages of the above mentioned operators .

Debounce : Returns an Observable that mirrors the source ObservableSource, except that it drops items emitted by the source ObservableSource that are followed by newer items before a timeout value expires. The timer resets on each emission.

In Other words using the debounce will prevent the search request or letters being sent to the server immediately, meaning debounce(300,TimeUnit.MILLISECONDS) will wait for 300 milliseconds in sending every request to the server. If your search query is “armageddon” and for each the my character tying speed is 100 milliseconds per letter then the request query sent to the server would be arm, armage, armageddo since it debounce wait for 300 ms of every request.

If no debounce is used the request to the server be like a, ar, arm, arma…. 4 network call is being made for 4 characters typed which is a costly one.

Filter: Filters items emitted by an ObservableSource by only emitting those that satisfy a specified predicate.

Filter operator are used in the search feature to filter out needless queries such as empty requests or if I need to send network request for queries more than 2 characters filter operator can be used. Suppose if, filter{it >2} is used and typing a search query the network request is sent only after tying arm because it allows network calls to be made only the queries greater than 2 characters.

DistinctUntilChanged: Returns an Observable that emits all items emitted by the source ObservableSource that are distinct from their
immediate predecessors.

distinctUntilChanged operator is used to avoid unnecessary or identical network calls. This operator stops request to sent to the server if has already fetched, eg: if the my current query is “Dunkr” and I cancelling out the typed query by a character say “Dunk” since a network request has already been sent for “Dunk” the distinctUntilChanged operator wont allow a new network call instead it will retrieve from the previous result.

SwitchMap: Returns a new ObservableSource by applying a function that you supply to each item emitted by the source
ObservableSource that returns an ObservableSource, and then emitting the items emitted by the most recently emitted of these ObservableSources.

switchMap is as powerful operator very much like other operators. Suppose if the search query is “Dunk “ and the network call is made and the observer is subscribed for this result and in the meantime another query is made which is for “Dunkr” and a network call is again made, what the switchMap operator does is that it helps the observer to be subscribed to the latest query which is “Dunkr” since the network response for the query “Dunk” is no longer required.

Epoxy Implementation:

build.gradle file

Epoxy is an Android library for building complex screens in a RecyclerView.

SearchController.kt

SearchController.kt acts basically as an adapter as in a recyclerview. It gets all the response data and feeds to the layout.

EpoxyView.kt

EpoxyView.kt has all the layout for using the Epoxy library, HomeView is a class that is mapped to the layout file and updates the data coming in from the SearchController.kt.

SearchActivty.kt

Above code snippet shows how the Activity is implemented with the Epoxy recyclerview.

SearchViewModel.kt

SearchViewModel.kt makes the network call request using Retrofit.

RestApi.kt

Thanks for going through my blog post and send me feedback so that I could update myself for future posts.

!!!! Cheers !!!

Android developer and a learner. https://github.com/thulasiram-sarathy