I\'m looking into converting my android app to use Rxjava for network requests. I currently access a webservice similar to:
getUsersByKeyword(String query, int l
To be clear, I assume your questions is more how to apply RxJava in your Android application rather than on the back end (although it's possible to apply too but not as typical as the front end). And i'm not very sure if this example is a typical use case for using reactive functional programming (RFP) model except for the clean code constructs.
Each stream below is an Observable. Personally, I would like to think it as stream as it's easy to reason about the events. Pagination flow can be represented by 6 streams:
firstPageStream -f-------------------> // this is actually to produce very first event to obtain the first page result. The event can come from a touch in one of the screen that navigates to this list screen.
nextPageStream -----n-------n-------> // this is the source of events coming from Next button 'touch' actions
prevPageStream ---------p-------p---> // this is the source of events coming from Previous button 'touch' actions
requestStream -r---r---r---r---r---> // this is to consume the signal from 3 streams above and spawn events (r) which create a query and pagination details i.e.: offset and limit
responseStream -R---R---R---R---R---> // this is to take each r and invoke your web service getUsersByKeyword() then spawn the response (R)
The above streams can be represented in pseudo code (JS style) below (it would be fairly easy to translate to RxJava or other languages)
firstPageStream = Observable.just(0); // offset=0
nextPageStream = Observable.fromEvent(nextButton, 'touch')
.map(function() {
offset = offset + limit;
return offset;
});
prevPageStream = Observable.fromEvent(prevButton, 'touch')
.map(function() {
offset = offset - limit; // TODO some check here
return offset;
});
requestStream = Observable.merge(firstPageStream, nextPageStream, prevPageStream)
.map(function(offsetValue) {
return {offset : offsetValue, limit: limit};
});
responseStream = requestStream.flatMap(function(pagination) {
return webservice.getUsersByKeyword(query, pagination.offset, pagination.limit); // assume this is async response
});
responseStream.subscribe(function(result) {
// use result to render the display
});
PS1: I have not tested the above code. I'm learning RFP so I just try to think in reactive way by writing down. Welcome any suggestion.
PS2: I'm highly influenced by https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 for the way to explain reactive streams.