Reptar


Source link: https://github.com/Commit451/Reptar

Reptar

Roaring RxJava. A collection of useful RxJava 2.X classes.

Gradle Dependency

Add the jitpack url to the project:

allprojects {

  repositories {

...

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

  
}
 
}

then, in your app build.gradle

dependencies {

  compile "com.github.Commit451.Reptar:reptar:latest.version.here"
  //for Retrofit support
  compile "com.github.Commit451.Reptar:reptar-retrofit:latest.version.here"
  //for Kotlin support
  compile "com.github.Commit451.Reptar:reptar-kotlin:latest.version.here" 
}

Usage

Usage can be found in the sample project.

Observers

For instances where you only want to implement certain callbacks, use:

  • AdapterObserver
  • AdapterSingleObserver

Composable Observers

Often times, you want to subscribe to Observables and have certain rules surrounding the observers. For example, you may want to use a SingleObserver, but ignore any CancellationExceptions since your Activity or Fragment is probably destroyed when this exception is thrown. You could potentially override the Observer and do the check in the subclass. But, this can optional in tons of subclasses that all perform simple boolean checks.

ComposableSingleObserver and ComposableObserver make it simple to add checks on the success and failure of observers. For example:

someObservable
  .subscribe(new ComposableSingleObserver<Boolean>() {

@Override

public void success(Boolean aBoolean) {

 //do some success thing

}

 @Override

public void error(Throwable t) {

 //error block will never get CancellationExceptions

 onHandleError(t);

}

  
}
.add(new CancellationFailureChecker()));

CancellationFailureChecker:

public class CancellationFailureChecker implements FailureChecker {

@Override
  public boolean check(Throwable t) {

return t instanceof CancellationException;
  
}
 
}

You could also just create an abstract Observer that you use throughout the app that contains these rules so that you do not have to repeat them:

public abstract class CustomSingleObserver<T> extends ComposableSingleObserver<T> {

public CustomSingleObserver() {

add(new CancellationFailureChecker());

//add other success and failure checks as desired
  
}
  
}

See SuccessChecker and FailureChecker for more.

Avoiding Null

RxJava 2.x does not allow propagating null. Read more here. null is still something we may not want to have fall through into the onError block though. For instance, if we want to check if a value exists, we could say that null means no, and a valid value means yes.

As a replacement, we can use Optional. For example:

Optional<String> optional; if (random.nextInt() % 2 == 0) {

  optional = new Optional<>("hi");
 
}
 else {

  optional = Optional.empty();
 
}
 Single.just(optional)

.subscribe(new ComposableSingleObserver<Optional<String>>() {

 @Override

 public void success(Optional<String> optional) {

  if (optional.hasValue()) {

Snackbar.make(root, "Has a optional", Snackbar.LENGTH_SHORT)

  .show();

  
}
 else {

Snackbar.make(root, "No optional", Snackbar.LENGTH_SHORT)

  .show();

  
}

 
}

  @Override

 public void error(Throwable e) {

  //Note that an empty optional would not be an error

 
}

}
);

This is similar to an Optional in Guava

Retrofit Usage

For Retrofit, many times, you need to get the raw response from Retrofit, but you also want all non 2XX error codes to fall through to the onError(). For this, ResponseSingleObservable is perfect:

gitHub.contributors("square", "okhttp")
  .subscribe(new ResponseSingleObserver<List<Contributor>>() {

@Override

protected void responseSuccess(List<Contributor> contributors) {

 int responseCode = response().code();

 //do what you need to with the response

}

 @Override

public void error(Throwable e) {

 if (e instanceof HttpException) {

  //check the response code, do what you need to

 
}
 else {

  //handle any other error

 
}

}

  
}
);

Similarly, you can use ResponseFunction in replacement of Function to easily flatMap a optional without needing to worry about checking .isSuccessful() on the optional.

Kotlin Usage

Kotlin extensions allow for easy composition of Single and Observable for Android:

gitHub.contributors("jetbrains", "kotlin")
  .fromIoToMainThread()
  .subscribe(object : CustomSingleObserver<List<Contributor>>() {

override fun success(t: List<Contributor>) {

 Snackbar.make(root, "It worked!", Snackbar.LENGTH_SHORT)

.show()

}

 override fun error(t: Throwable) {

 onHandleError(t)

}

  
}
)

License

Copyright 2017 Commit 451  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

Add multiple TextWatcher on multiple Editext and receive callback at one place.

Gradle plugin to generate config classes for your Android projects.

A reactive layer on top of android palette (easy and hassle-free) using RxJava 2.

Remove boilerplate code for using SharePreference to persist app's config data.

Celebrate more with this lightweight confetti particle system 🎊 Create realistic confetti by implementing this easy to use library.

A library for draggable boardview.

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