All Projects → mohamad-amin → RxActivityResults

mohamad-amin / RxActivityResults

Licence: Apache-2.0 license
Android onActivityResult wrapper powered by RxJava

Programming Languages

java
68154 projects - #9 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to RxActivityResults

Thirtyinch
a MVP library for Android favoring a stateful Presenter
Stars: ✭ 1,052 (+7414.29%)
Mutual labels:  rxjava2, activity
Protein
💊 Protein is an IntelliJ Plugin to generate Kotlin code for Retrofit 2 and RxJava 2 based on a Swagger definition
Stars: ✭ 273 (+1850%)
Mutual labels:  reactivex, rxjava2
Rxbus
Android reactive event bus that simplifies communication between Presenters, Activities, Fragments, Threads, Services, etc.
Stars: ✭ 79 (+464.29%)
Mutual labels:  rxjava2, activity
Rxdownloader
- Reactive Extension Library for Android to download files
Stars: ✭ 40 (+185.71%)
Mutual labels:  reactivex, rxjava2
Rxlifecycle
Rx binding of stock Android Activities & Fragment Lifecycle, avoiding memory leak
Stars: ✭ 131 (+835.71%)
Mutual labels:  rxjava2, activity
RxPager
RxPager is an Android library that helps handling paginated results in a reactive way
Stars: ✭ 56 (+300%)
Mutual labels:  reactivex, rxjava2
navigator
Annotation processor that eliminates navigation and Bundle boilerplate
Stars: ✭ 13 (-7.14%)
Mutual labels:  activity
Android-Code-Demos
📦 Android learning code demos.
Stars: ✭ 41 (+192.86%)
Mutual labels:  rxjava2
Muvi
Very simple project to show a collection of Movie from MovieDb with a minimalist design
Stars: ✭ 46 (+228.57%)
Mutual labels:  rxjava2
rx-scheduler-transformer
rxjava scheduler transformer tools for android
Stars: ✭ 15 (+7.14%)
Mutual labels:  rxjava2
purescript-outwatch
A functional and reactive UI framework based on Rx and VirtualDom
Stars: ✭ 33 (+135.71%)
Mutual labels:  reactivex
Reaktor
👾 A Framework for reactive and unidirectional Kotlin application archtitecture with RxJava2.
Stars: ✭ 16 (+14.29%)
Mutual labels:  rxjava2
rx
Reactive Extensions for D Programming Language
Stars: ✭ 52 (+271.43%)
Mutual labels:  reactivex
Clean-MVVM-NewsApp
Android News app developed using Clean + MVVM architecture
Stars: ✭ 52 (+271.43%)
Mutual labels:  rxjava2
MVPSamples
🚀(Java 版)快速搭建 MVP + RxJava + Retrofit + EventBus 的框架,方便快速开发新项目、减少开发成本。
Stars: ✭ 113 (+707.14%)
Mutual labels:  rxjava2
RxSocketClient
🚀Reactive Socket APIs for Android, Java and Kotlin, powered by RxJava2
Stars: ✭ 46 (+228.57%)
Mutual labels:  rxjava2
LifecycleAwareRx
Make your RxJava2 streams life-cycle aware with Android Architecture Components.
Stars: ✭ 33 (+135.71%)
Mutual labels:  rxjava2
NoWordsChat
No Words Chat,Just For Fun! Use MVVM,DataBinding,Fresco......
Stars: ✭ 46 (+228.57%)
Mutual labels:  rxjava2
Sunset-hadith
Islamic app written with Kotlin, using KTOR + coroutines + flow + MVVM + Android Jetpack + Navigation component. Old version using RxJava + Retrofit + OKHttp
Stars: ✭ 26 (+85.71%)
Mutual labels:  rxjava2
AndroidSamples
Android例子----View、指纹、Canvas、RecyclerView、BottomSheet、PopupWindow、Broadcast、Service、Rxjava、Retrofit、Handler等
Stars: ✭ 107 (+664.29%)
Mutual labels:  rxjava2

Travis-ci CodeCov Bintray Min API License Methods Count

RxActivityResults

This library uses the power of RxJava to wrap an Observable android Activity#onActivityResult() method so you can easily request something from other activities and have the result right there in your observabale's subscribe() method.

Main Benifits

  • Don't Break The Chain: Prevents you to split your code between the permission request and the result handling. Currently without this library you have to request in one place and handle the result in Activity#onActivityResult().
  • Reactive: All what RX provides about transformation, filter, chaining...

If you have any issues or need more features, you can submit an issue in the issue tracker or make a pull request.

Demo

Importing

Add this line to your module's build.gradle file:

dependencies {
    compile 'com.mohamadamin.rxactivityresults:rxactivityresults:0.1'
}

Usage

First you need to create an instance of RxActivityResults in your Activity:

RxActivityResults rxActivityResults;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    rxActivityResults = new RxActivityResults(this);
    ...
}

Then you can use this instance to start an activity for result and have it's result in your observer like this:

Intent intent = new Intent(this, LoginActivity.class);
rxActivityResults.start(loginIntent)
                .subscribe(activityResult -> {
                    // Checking if the result's resultCode is Activity.RESULT_OK
                    if (activityResult.isOk()) {
                        Intent responseData = activityResult.getData();
                        // Do some other things with the response data
                   }
                });
      

Reactive Results

You can use RxActivityResults#composer(Intent intent) method to have an ObservableTransformer to chain your observables together and avoid from breaking the reactive chain:

(Note: we used RxBinding library to turn view clicks into an observable)

Disposable disposable = RxView.clicks(loginButton)
        // Fire an intent when the user clicks on the button
        .compose(rxActivityResults.composer(getLoginIntent()))
        // Filter the activity results which their resultCode is Activity.RESULT_OK
        .filter(activityResult -> activityResult.isOk())
        // Extract the data intent of the activity result
        .map(ActivityResult::getData)
        .subscribe(intent -> {

            String firstName = intent.getStringExtra(LoginActivity.FIRST_NAME);
            String lastName = intent.getStringExtra(LoginActivity.LAST_NAME);
            String emailAddress = intent.getStringExtra(LoginActivity.EMAIL_ADDRESS);

            String result = getString(R.string.first_name) + ": " + firstName + "\n"
                    + getString(R.string.last_name) + ": " + lastName + "\n"
                    + getString(R.string.email_address) + ": " + emailAddress + "\n";

            loginInfoText.setText(result);

        });

disposables.add(disposable);

Chain Transformation

You can use RxActivityResults#ensureOkResult(Intent intent) method to have an ObservableTransformer to chain your observables together and avoid from breaking the reactive chain:

Disposable disposable = someAnotherChainOfObservables
        // Fire an intent when the user clicks on the button
        .compose(rxActivityResults.ensureOkResult(getIntent()))
        .switchMap(okResult -> {
            if (okResult) {
                return someAnotherObservable();
            } else {
                return Observable.error(new RuntimeException("Intent didn't return RESULT_OK"));
            }
        })
        .flatMap(...)
        ...

disposables.add(disposable);

Demo

You can see a full demo of the library in the sample module.

Credits

This library was inspired by RxPermissions, so great thanks to @tbruyelle for his great contribution.

Licence

Copyright 2017 Mohamad Amin Mohamadi

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.
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].