All Projects → k-kagurazaka → rx-property-android

k-kagurazaka / rx-property-android

Licence: MIT License
Bindable and observable property for Android Data Binding

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to rx-property-android

AndroidMVPArchitecture
Android MVP architecture sample project with or without RxJava and Dagger2 and Kotlin
Stars: ✭ 78 (+2.63%)
Mutual labels:  rxjava, data-binding
Alfonz
Mr. Alfonz is here to help you build your Android app, make the development process easier and avoid boilerplate code.
Stars: ✭ 90 (+18.42%)
Mutual labels:  rxjava, data-binding
RxProperty
RxJava binding APIs for observable fields and observable collections from the Data Binding Library
Stars: ✭ 17 (-77.63%)
Mutual labels:  rxjava, data-binding
form-binder-java
Java port of form-binder, a micro data binding and validating framework
Stars: ✭ 29 (-61.84%)
Mutual labels:  data-binding
WanAndroid
💪 WanAndroid应用,持续更新,不断打造成一款持续稳定, 功能完善的应用
Stars: ✭ 50 (-34.21%)
Mutual labels:  rxjava
rxify
Now: RxJava Playground, Previous: Demo for the talk at DroidconIN 2016, Droidcon Boston 2017 and Codelab for GDG January Meetup
Stars: ✭ 59 (-22.37%)
Mutual labels:  rxjava
Readhub
Readhub AndroidClient
Stars: ✭ 40 (-47.37%)
Mutual labels:  rxjava
ShareLoginPayUtil
原生三方(QQ、微信、微博、Instagram、Google、FaceBook)登陆,(QQ、微信、微博、系统)分享,(微信、支付宝)支付
Stars: ✭ 26 (-65.79%)
Mutual labels:  rxjava
BihuDaily
高仿知乎日报
Stars: ✭ 75 (-1.32%)
Mutual labels:  rxjava
RxWebView
RxJava2 binding APIs for Android's WebView
Stars: ✭ 22 (-71.05%)
Mutual labels:  rxjava
rxbus2
Listen and handle event ,based on RxJava.
Stars: ✭ 32 (-57.89%)
Mutual labels:  rxjava
Android-Learning-Resources
My curated list of resources for learning Android Development.
Stars: ✭ 24 (-68.42%)
Mutual labels:  rxjava
RxJava-Codelab
Codelab project for demonstration of RxJava features
Stars: ✭ 44 (-42.11%)
Mutual labels:  rxjava
reactive-wizard
Reactive non-blocking web applications made really easy with JAX-RS and RxJava.
Stars: ✭ 25 (-67.11%)
Mutual labels:  rxjava
DaMaiProject
大麦界面,实现多种方式网络访问、数据缓存
Stars: ✭ 24 (-68.42%)
Mutual labels:  rxjava
AndroidGo
Android、Flutter 开发者帮助 APP。包含事件分发、性能分析、Google Jetpack组件、OkHttp、RxJava、Retrofit、Volley、Canvas绘制以及优秀博文代码案例等内容,帮助开发者快速上手!
Stars: ✭ 30 (-60.53%)
Mutual labels:  rxjava
catchflicks
🎬 Kitchen sink project for learning android concepts 🎬
Stars: ✭ 12 (-84.21%)
Mutual labels:  rxjava
rxkotlin-jdbc
Fluent RxJava JDBC extension functions for Kotlin
Stars: ✭ 27 (-64.47%)
Mutual labels:  rxjava
RxRealm
Utilities for using RxJava with Realm
Stars: ✭ 23 (-69.74%)
Mutual labels:  rxjava
impex
a powerful web application engine
Stars: ✭ 74 (-2.63%)
Mutual labels:  data-binding

RxProperty Android

Bindable and observable property for Android Data Binding

This library is an Android port of ReactiveProperty.

Demo

Download

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

dependencies {
    compile 'com.github.k-kagurazaka.rx-property-android:rx-property:4.0.0'

    // If you want to use Kotlin syntax
    compile 'com.github.k-kagurazaka.rx-property-android:rx-property-kotlin:4.0.0'
}

Basics Usage

First, declare a view model class with RxProperty and RxCommand.

RxProperty represents a variable of a view state and RxCommand represents a command of "Command Pattern", which is abstraction of user manipulation.

public class ViewModel {
  public final RxProperty<String> input;
  public final ReadOnlyRxProperty<String> output;
  public final RxCommand<NoParameter> command;

  public JavaViewModel() {
    input = new RxProperty<>("")
        .setValidator(it -> TextUtils.isEmpty(it) ? "Text must not be empty!" : null);

    output = new ReadOnlyRxProperty<>(input.map(String::toUpperCase));

    command = new RxCommand<>(input.onHasErrorsChanged().map(it -> !it));
    command.subscribe(it -> { input.set("clicked!"); });
  }

Next, write a layout XML to bind the view model. Both one-way (by @{}) and two-way (by @={}) binding are supported.

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable name="viewModel" type="ViewModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:text="@={viewModel.input.value}" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{viewModel.output.value}" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Is not empty?"
            app:rxCommandOnClick="@{viewModel.command}" />
    </LinearLayout>
</layout>

Finally, execute data binding in your activity.

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    binding.setViewModel(new ViewModel());
  }

You are done!

Important Note: These snippets skips resource management/error handling for simplicity.

Validation

RxProperty provides validation. To use this feature, simply call setValidator method after RxProperty created.

public final RxProperty<String> input = new RxProperty<>("")
        .setValidator(it -> TextUtils.isEmpty(it) ? "Text must not be empty!" : null);

RxProperty also supports error notification with TextInputLayout.

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:error="@{viewModel.input.error}"
    app:errorEnabled="@{viewModel.input.hasError}">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:text="@={viewModel.input.value}"/>
</android.support.design.widget.TextInputLayout>

RxCommand with Trigger

By default, the library provides the only View#onClick binder for RxCommand. If you want to bind RxCommand to others view events, implement BindingAdapter for your desired events or use trigger binding.

RxCommand can be kicked by Observable by the bindTrigger method. RxBinding is match for the use.

// When the menu whose id is R.id.some_menu is selecetd, someMenuCommand executes
viewModel.someMenuCommand.bindTrigger(RxMenuItem.clicks(menu.findItem(R.id.some_menu)));

Create from android.databinding.Observable

If you already have a android.databinding.Observable based view model, you can use a converter from the view model into io.reactivex.Observable. The library provides a feature to convert from io.reactivex.Observable to RxProperty, so you easily create RxProperty from the view model.

class Person extends BaseObservable { ... }

Person person = new Person("John", "Smith");
Observable<String> firstNameChanged = Observe.propertyOf(person, BR.firstName, it -> it.getFirstName());
RxProperty<String> firstName = new RxProperty<>(firstNameChanged, person.getFirstName());

Kotlin Support

There are some useful extension methods in `rx-property-kotlin'.

class Person : BaseObservable { ... }

class ViewModel {
    val input = RxProperty("")
            .setValidator { if (TextUtils.isEmpty(it)) "Text must not be empty!" else null }

    val output = input.map(String::toUpperCase).toReadOnlyRxProperty()

    val command = input.onHasErrorsChanged()
            .map { !it }
            .toRxCommand<NoParameter>()

    val person = Person("John", "Smith")
    val firstName = person.observeProperty(BR.firstName) { it.firstName }
            .toRxProperty()

    init {
        command.subscribe { input.set("clicked!") }
    }
}

License

The MIT License (MIT)

Copyright (c) 2016 Keita Kagurazaka

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].