Other improvements

One can notice pretty fast that the rate the Twitter status updates are received at is really fast. To make the UI a little less overcrowded, we can add a simple .sample() method to reduce its rate:

observeTwitterStream(configuration, filterQuery)
.sample(700, TimeUnit.MILLISECONDS)
.map(StockUpdate::create)

With the following line, we make the TwitterStream Observable output an entry every 700 ms instead of flushing everything it receives:

.sample(700, TimeUnit.MILLISECONDS)

Obviously, this way we are losing some data, but for the sake of the exercise, it is a useful approach to cope with the data that keeps overflowing the UI.

Now, another problem is that the stream quickly leaves the user in the current scroll position, and it looks like it's stuck. That happens because we were viewing entry at position 0 initially, but there are new entries being entered above it constantly, so the current entry becomes second, third and so on pretty rapidly. In the end, we see stale data.

To make the stream always scroll to the top, whenever the new entry is received, we can update the .subscribe() block to include the following:

recyclerView.smoothScrollToPosition(0);

So, it becomes this:

.subscribe(stockUpdate -> {
Log.d("APP", "New update " + stockUpdate.getStockSymbol());
noDataAvailableView.setVisibility(View.GONE);
stockDataAdapter.add(stockUpdate);
recyclerView.smoothScrollToPosition(0);
}

This will cause the RecyclerView to always go to the very first element whenever a new entry is inserted.

After these few simple changes, we have made the app a bit more pleasant to use.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.27.178