Coder Social home page Coder Social logo

miha-x64 / decouplex Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 0.0 290 KB

(deprecated) Decoupled executor — the easiest & type-safe way to run code in Android service

License: GNU Lesser General Public License v3.0

Java 100.00%
android-service config-change network

decouplex's Introduction

Deprecated: early StackOverflow discussions said you need a Service for network tasks. Actually, you need just a ViewModel/Presenter which outlives a View.

Decouplex

Decoupled executor — the easiest & type-safe way to run code in Android service

An alternative

In many cases you can use presenters which survive lifecycle changes, like in Moxy.

This library is abandoned and deprecated.

Include via Gradle

Just Decouplex

Download Methods count

compile 'net.aquadc.decouplex:decouplex:0.0.3'

DecouplexRetrofit — adapter for Retrofit

Download

compile 'net.aquadc.decouplex:decouplexRetrofit:0.0.3'

compile 'net.aquadc.decouplex:decouplexRetrofit:0.0.3:release@aar' — this strange thing will be fixed in next release

To do in next releases

  • Add something like @OnFinish with finally semantics;
  • Support DecouplexBatchRequest.retry & document DecouplexBatch;
  • Develop REMOTE DeliveryStrategy to use implementation that runs in another process;
  • Static factory with default parameter values to use in Kotlin without Builder;
  • Eager validation;
  • less ProGuard rules.

New in 0.0.3

  • Support nullable @OnResult and @OnError methods' arguments by means of @DcxNullable annotation;
  • Allow some methods to be called without result or error delivery by means of @DcxDelivery annotation;
  • If an exception has raised in @OnResult method, delivering InvocationTargetException to @OnError;
  • If an exception has raised in @OnError method, delivering two exception to fallback handler.

Now it's OK for @OnResult and @OnError methods to declare (throws) and throw exceptions — you can catch them all inside your fallback error handler and send them to Crashlytics.

Since 0.0.2

  • DeliveryStrategy: a way to transfer arguments from UI to Service. The only one is available for now — DeliveryStrategies.LOCAL. Method arguments and return value will now be transferred out of Bundle and there's no need for them to be Parcelable;
  • Adapters' API changed: they aren't dependent on Bundle any more;
  • A bug that caused a crash on SocketTimeoutException delivery was fixed (actually, any exception delivery would be failed if this exception provides no message but @OnError method requires it).

Since 0.0.1

  • Arguments and return values delivery;
  • Wildcard result/error handlers (e. g. @OnResult("list*") works with all methods which names start with "list");
  • @Debounce(delay).

Usage

You can write your code like this:

class SampleFragment extends DecouplexFragment {

    private GitHubService gitHubService;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (gitHubService == null) {
            // configure Retrofit
            GitHubService gitHubRetrofitService =
                    new Retrofit.Builder()
                            .baseUrl("https://api.github.com/")
                            .addConverterFactory(JacksonConverterFactory.create())
                            .build()
                            .create(GitHubService.class);

            // configure Decouplex
            gitHubService = DecouplexBuilder
                    .retrofit2(getActivity(),
                            GitHubService.class, gitHubRetrofitService, getClass());
        }
    }
    
    // OnClick
    public void myGitHub() {
        enableUi(false);
        gitHubService.listRepos("Miha-x64");
    }
    
    @OnResult("listRepos")
    protected void onReposListed(List<Repo> repos) {
        resultView.setText(formatRepos(repos));
        enableUi(true);
    }
    
    // some code
    // setting OnClickListeners
    // enableUi & formatRepos methods
    // and other presenter logic
    
}

Of course, GitHubService in this example is a Retrofit2-compatible interface:

public interface GitHubService {
    @GET("users/{user}/repos")
    Call<List<Repo>> listRepos(@Path("user") String user);
}

You have to add service in manifest:

<service android:name="net.aquadc.decouplex.DecouplexService" />

When your class does not extend DecouplexActivity or DecouplexFragment, you can register & unregister BroadcastReceiver wherever you need: in onCreate/onDestroy, onStart/onStop, or onResume/onPause.

class MyClass extends Fragment or Activity {

    private DecouplexReceiver decouplexReceiver;

    @Override
    protected void onStart() {
        super.onStart();
        if (decouplexReceiver == null)
            decouplexReceiver = new DecouplexReceiver(this);
        decouplexReceiver.register();
    }

    @Override
    protected void onStop() {
        decouplexReceiver.unregister();
        super.onStop();
    }
}

decouplex's People

Contributors

miha-x64 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.