RxComprehensions


Source link: https://github.com/pakoito/RxComprehensions

RxComprehensions

RxComprehensions is a library to reduce boilerplate and simplify your Observable chains.

Rationale

As your code starts getting more and more functional, you find that you have to chain multiple Observable by means of flatMap(), concatMap(), or switchMap(). This causes indentation levels to go quite high, and would often require that you split the code in several methods just to keep it readable.

Observable<String> getUserFriends =
  profileClicks()

 .flatMap {
 position ->

  getUserFromProfile(position)

 .flatMap {
 user ->

  requestFriendListForUser(position, user.id)

 .flatMap {
 friends ->

  storeUserAndFriends(user, friends)

 .flatMap {
 result ->

  toUserDisplayString(position, user, friends, result)

 
}

 
}

 
}

 
}

Comprehensions are a language feature that allow you to define such a chain in a way where every observable is a function at topmost indentation, yet still contains all the parameters received in the previous functions.

Usage

Map comprehensions

RxComprehensions contains static methods doFlatMap() for flatMap(), doConcatMap() for concatMap(), doSwitchMap() for switchMap(). Each takes from 1 to 9 FuncN each with an increasing number of parameters, and returns an Observable of the type of the return of the last function.

Observable<String> getUserFriends =
  // chained with flatMap()
  RxComprehensions.doFlatMap(

() -> profileClicks(),

position -> getUserFromProfile(position),

position, user -> requestFriendListForUser(position, user.id),

position, user, friends -> storeUserAndFriends(user, friends),

position, user, friends, result -> toUserDisplayString(position, user, friends, result)
  );

Compose comprehensions

RxComprehensions contains static methods doCompose() for compose(). Each takes from 1 to 9 Transformer<T, U> (RxJava 1.X) or ObservableTransformer<T, U> (RxJava 2.X), and returns an Observable of the type of the return of the last one.

Observable<List<Siblings>> getRelatives =
  // chained with compose()
  RxComprehensions.doCompose(

() -> requestRelative("12345"),

validate(),

assureThreads(Schedulers.io(), AndroidSchedulers.main()),

respectLifecycle(activity),

toUILayerModel(),

groupSiblings()
  );
  Observable<RelativeDto> requestRelative(String id) {
 /* ... */ 
}
  ObservableTransformer<RelativeDto, RelativeDto> validate() {
 /* ... */ 
}
  ObservableTransformer<RelativeDto, RelativeDto> assureThreads(Scheduler in, Scheduler out) {
 /* ... */ 
}
  ObservableTransformer<RelativeDto, RelativeDto> respectLifecycle(Activity activity) {
 /* ... */ 
}
  ObservableTransformer<RelativeDto, Relative> toUILayerModel() {
 /* ... */ 
}
  ObservableTransformer<Relative, List<Siblings>> groupSiblings() {
 /* ... */ 
}

Distribution

RxJava 1.X

Add as a dependency to your build.gradle

repositories {

  ...
  maven {
 url "https://jitpack.io" 
}

  ... 
}

dependencies {

  ...
  compile 'com.github.pakoito.RxComprehensions:rxcomprehensions:1.3.0'
  ... 
}

or to your pom.xml

<repositories>
  <repository>

<id>jitpack.io</id>

<url>https://jitpack.io</url>
  </repository> </repositories>  <dependency>
  <groupId>com.github.pakoito.RxComprehensions</groupId>
  <artifactId>rxcomprehensions</artifactId>
  <version>1.3.0</version> </dependency>

RxJava 2.X

Add as a dependency to your build.gradle

repositories {

  ...
  maven {
 url "https://jitpack.io" 
}

  ... 
}

dependencies {

  ...
  compile 'com.github.pakoito.RxComprehensions:rxcomprehensions2:1.3.0'
  ... 
}

or to your pom.xml

<repositories>
  <repository>

<id>jitpack.io</id>

<url>https://jitpack.io</url>
  </repository> </repositories>  <dependency>
  <groupId>com.github.pakoito.RxComprehensions</groupId>
  <artifactId>rxcomprehensions2</artifactId>
  <version>1.3.0</version> </dependency>

License

Copyright (c) pakoito 2016

The Apache Software License, Version 2.0

See LICENSE.md

Resources

An android widget to input passcode.

GenericRenderers is an Android library created to avoid all the boilerplate needed to use a RecyclerView with adapters.

Cursor binding which uses annotation processing to generate boilerplate code for you.

A very simple library to animate the Support Library FAB when the user scrolls.

XIntro allows to build introduction activity for android application.

Android CatLoadingView.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes