Building balances layout in eMoney Android (RxJava + Retrofit)

Merab Tato Kutalia
eMoney Engineering
Published in
2 min readMar 2, 2018

There are 2 interesting cases in the eMoney Android app, which is solved nicely using RxJava and Retrofit. Here is a video what the balances layout looks like, 2 separate parts.

balances layout

// sensitive details are hidden.

The first item is a layout for user’s balances from eMoney. The one which loads slower is for the bank accounts and linked cards.

The first layout shows total available balance and after expanding detailed balances are shown.

Second layout fetches all the bank accounts and linked cards , which are separate network requests and combines them as a single list.

Tasks

  • Fetch currency exchange rates from the network, merge with balances network request and display total available balance and dropdown list same time.
  • Fetch user’s bank accounts and linked cards, both of them are separate network calls, merge them, display with the specific type appearances. + if one of requests failed just ignore and display available result.

Solution

After a quite bit of thinking and searching of the correct operator for RxJava I came up with this solution.

Actually using Retrofit + RxJava Adapter (this is a standard already, nowadays, I guess)

Problem can be solved with the different approaches but let’s use sweet streams. I will focus only on abstract layer without a very detailed implementation parts such as a updating adapter and setting click listeners.

So, let’s start with the task #1.

this is a clean solution without any deep implementation details. Code explains itself

“Every time you write a comment, you should grimace and feel the failure of your ability of expression.”

Robert C. Martin

zipWith operator appeared very handy and easy to use. First parameter is another observable which should be zipped with and second parameter is a zip function itself, how those 2 observable contents will be merged and its completely on you.

Real devs know all the parameters too

// yes, at eMoney we still use RxJava 1.X

eMoney is a mix of old and new technologies, everyday we strive to achieve stability and improve codebase. Migration has already started and nowadays eMoney Android app contains Java and Kotlin, Apache HTTP legacy and Retrofit, etc. Let’s discuss later about it.

Now, review the task #2.

create UserAccount abstract class which will be implemented by bank accounts and linked card accounts so that adapter could get 1 ArrayList with different types of data.

Here we use flatMap. First parameter is a collection selector and second parameter is a result collector, which accepts 2 parameters from both observables.

Knowing and understanding how the RxJava operators work and their different versions, helped to achieve the desired result in faster and more elegant way.

That’s all for now folks, see more in the video from Droidcon 2017

droidcon NYC 2017 — Advanced Networking with RxJava + Retrofit

👉 Follow me on Twitter and check my other articles.

--

--