Skip to content

Instantly share code, notes, and snippets.

@txomon
Last active December 14, 2015 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save txomon/645efdf6b030b4bcd30e to your computer and use it in GitHub Desktop.
Save txomon/645efdf6b030b4bcd30e to your computer and use it in GitHub Desktop.
Reactive Extension for Java design doubts

Rx doubt

When creating and Android app, and probably any other kind of app that has UI, I find myself with all the business code inside the UI code (Activity, Fragments, etc.). When using RX, this means I have giantic subscribe() unsubscribe() functions that basically setUp and tearDown all the Rx pipelines.

With the objective of dividing UI from Business logic, after watching https://realm.io/news/gotocph-mattias-kappler-reactive-architecture-android/ I want to replicate the idea of having all Activities, Fragments, etc., all android stuff as Observers and Observables. Without having any kind (really any kind) of Business logic.

The UI will directly connect to the BS (BusinessSingleton).

Therefore, following the same pattern, I want to create something that would have a use like the following:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...

        bs = getBs();

        Observable<String> navigationObservable bs.getNavigationObservable();
        navigationObservable
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                        
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(String name) {
                        changeScreenTo(name);
                    }
                });
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_explore) {
            bs.emitNavigation("explore", null);
        } else if (id == R.id.nav_custom) {
            bs.emitNavigation("custom", null);
        }

        // ...
        return true;
    }

    public void changeScreenTo(String name) {
        // ...
    }

So bs would have emitNavigation(String, Object) method that would handle all the navigation inputs, and would decide to emit or not a navigationObservable item.

What is left here is a change to make this sample code be registered as an observable in BS, and all the BS code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment