ReactiveAndroid


Source link: https://github.com/kittinunf/ReactiveAndroid

ReactiveAndroid

Reactive events and properties with RxJava for Android

Installation

Dependency

Gradle

repositories {

  jcenter() 
}
  dependencies {

  compile 'com.github.kittinunf.reactiveandroid:reactiveandroid-ui:<latest-version>' //for base UI
  compile 'com.github.kittinunf.reactiveandroid:reactiveandroid-appcompat-v7:<latest-version>' //for appcompat-v7 module
  compile 'com.github.kittinunf.reactiveandroid:reactiveandroid-support-v4:<latest-version>' //for support-v4 module
  compile 'com.github.kittinunf.reactiveandroid:reactiveandroid-design:<latest-version>' //for design support module 
}

Why you might need this?

Have you ever wish Android SDK had supported RxJava by default? Well, you have that wish with ReactiveAndroid.

Let's say, you need to observe view being clicked. Yes, you definitely can do it with View.OnClickListener. However, with ReactiveAndroid, you can simply do this:

button.rx.click().subscribe {

  //do something when button is clicked 
}

Or, let's assume that you have Observable<String> that wants to be outputted with TextView. You can just do this:

val o = Observable.just("Hello") o.map {
 "$it Bob" 
}
.subcribe(textView.rx.text)

From above example, whenever Observable emits next, it will bind with setText(value) of TextView.

In this way, you could construct your logic as Observable in Presenter or ViewModel, then bind it with your view to make it updated.

This makes ReactiveAndroid a powerful tool to perform data binding in your MV* architecture (MVP, MVVM).

Another example is registration form. You probably want to know whether input email & password are valid or not. So that, you could disable/enable button as user types.

val emails = emailEditText.rx.text().map {
 it.toString() 
}
 // becomes Observable<String> for email val passwords = passwordEditText.rx.text().map {
 it.toString() 
}
 // becomes Observable<String> for password  val emailValids = emails.map {
 Pattern.matches(EMAIL_PATTERN, it) 
}
 val passwordValids = passwords.map {
 it.length > PASSWORD_LENGTH 
}
  val emailAndPasswordValids = Observables.combineLatest(emailValids, passwordValids) {
 user, pass -> user and pass 
}
 // becomes Observable<Boolean> for validity  emailAndPasswordValids.bindTo(signInButton.rx.enabled) // bind validity value with button

Sample

There are couple of sample code that take advantage power of ReactiveAndroid. You can checkout in sample folder.

Terminology

We want to be familiar as much as possible to the Android SDK. So that Event and Property from Android elements follow naming convention of Android SDK whenever it can.

Events

Think about listener from Android SDK of your choice, then remove the word "on" and also the word "listener". Then, append that "name" after rx_.

For example,

  • View.OnClickListener => view.rx.click()
  • RatingBar.OnRatingBarChangeListener => ratingBar.rx_ratingBarChange()
  • MenuItem.OnMenuItemClickListener => menuItem.menuItemClick() etc.

Properties

Think about name of property of UI element from Android SDK. Remove { set|get|is|has } . Then, append after rx_ in the similar fashion as Events.

For example,

  • View.setEnabled/isEnabled => view.rx.enabled
  • DatePicker.setMinDate/getMinDate => datePicker.rx_minDate
  • RecyclerView.setHasFixedSize/hasFixedSize => recyclerView.rx_hasFixedSize etc.

Credits

ReactiveAndroid is brought to you by contributors.

Licenses

ReactiveAndroid is released under the MIT license.

Resources

This is a simple start-template to save you a little time.

This is a useful log tool for Android.

An enum based library to replace fragments, because #perfmatters.

View in circle shape that can be used as button with progress on long click. Also as static view to show progress or to simulate progress animation.

A pretty image picker for Android.

Monad on Android like Scala.

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