RxCommand

by listenzz

listenzz / RxCommand

A UI-related rxjava component for android mvvm architecture

131 Stars 14 Forks Last release: Not found MIT License 31 Commits 3 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

RxCommand

A command is an Observable triggered in response to some action, typicallyUI-related.

It manage the extra states, such as loading, enabled, errors for you, when using RxJava2 implement the functions of your ViewModel.

博客

Android 生命周期架构组件与 RxJava 完美协作

Code like this

ViewModel

public class MyViewModel extends ViewModel {

public final RxCommand<list>&gt; usersCommand;

public MyViewModel(final UserRepository userRepository) {

    usersCommand = RxCommand.create(o -&gt; {
            return userRepository.getUsers();
        });
}

}

Activity

public class MyActivity extends AppCompatActivity {
    public void onCreate(Bundle savedInstanceState) {
        MyViewModel viewModel = ViewModelProviders.of(this).get(MyViewModel.class);

    viewModel.usersCommand
            .switchToLatest()
            .observeOn(AndroidSchedulers.mainThread())
            .compose(Live.bindLifecycle(this))
            .subscribe(users -&gt; {
                // update UI
            });

    viewModel.usersCommand
            .executing()
            .compose(Live.bindLifecycle(this))
            .subscribe(executing -&gt; {
                // show or hide loading
            })

    viewModel.usersCommand
            .errors()
            .compose(Live.bindLifecycle(this))
            .subscribe(throwable -&gt; {
                // show error message
            });
}

}

Usage

buildscript {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
    }
}
dependencies {

//  using Support Library 26.1+
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:support-v4:26.1.0'
compile 'com.android.support:design:26.1.0'

// RxJava
compile 'io.reactivex.rxjava2:rxjava:2.1.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

// Live
compile 'com.shundaojia:live:1.0.2'

// RxCommand
compile 'com.shundaojia:rxcommand:1.2.2'
compile 'android.arch.lifecycle:extensions:1.0.0' // for ViewModel

}

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.