Giter Club home page Giter Club logo

thinr's Introduction

Thinr

Download Android Arsenal Build Status License Coverage Status

About

Thinr is a replacement for AsyncTask. It is a tiny library that makes things like the following possible in a leak free and configuration change aware way:

Thinr.task(getContext(), "getQOTD", MainActivity.class, Void.class)
                        .onMain((target, param) -> {
                            target.textView.setText(target.getApplicationContext().getString(R.string.loading_message));
                            return null;
                        })
                        .inBackground(
                                (appCtx, param, flowControl) -> {
                                    OkHttpClient client = new OkHttpClient();

                                    Request request = new Request.Builder()
                                            .url("http://api.icndb.com/jokes/random")
                                            .build();

                                    try {
                                        Response response = client.newCall(request).execute();
                                        JSONObject json = new JSONObject(response.body().string());
                                        return json.getJSONObject("value").getString("joke");
                                    } catch (Exception e) {
                                        return null;
                                    }
                                }
                        )
                        .endsOnMain((target, qotd) -> {
                            if (qotd != null) {
                                target.textView.setText(qotd);
                            } else {
                                target.textView.setText(target.getApplicationContext().getString(R.string.error_message));
                            }
                        })
                        .execute(null, "ComponentID");

While the above example is not very exciting you might be able to estimate it's value considering it's

  • life cycle aware
  • leak free

Anything in "inBackground" will be executing on a background thread.

Anything in "onMain" will be executing when - and only when - the component is in a state that is fine for accessing the component. Imagine that "inBackground" can be any time consuming blocking operation accessing the network and hitting the filesystem.

It's a beautiful replacement for AsyncTask (which it internally uses) when using Java 8.

You can also use it with Java 7 but Java 8 will make your experience much more exciting.

Currently the official support for Java 8 is limited to using Jack (see http://developer.android.com/preview/j8-jack.html ) but for sure the complete toolchain will support Java 8 sooner or later.

Additionally RetroLambda is supported starting with version 0.1.0.

If you still don't know what this will do for you have a look at other approaches making AsyncTask work (right) for you.

Don't get me wrong there is nothing wrong with AsyncTask - it just needs a lot of care to implement it in a leak and error free way.

Unfortunately those implementations add a lot of boiler plate code which made a lot of developers to come up with alternative solutions (which are also not easy to understand and do it right - and most of the time the code will look just ugly, hard to understand and hard to maintain).

If you ever did some non-trivial Android app you should know the pain I*m talking about.

Objectives

The objectives of this tiny library are:

  • easy usage
  • easy to understand
  • no boilerplate code involved (when using Java 8)
  • easy to integrate
  • no need to rewrite your whole code base to make use of it / can be introduced step-by-step where it is needed while leaving all the other code untouched
  • non invasive (i.e. don't forces you to use a special style of programming)
  • beautiful code (when using Java 8)

Additionally while the API is quite easy it comes with a lot of flexibility.

Get started

The easiest way to get started is to look at the sample app. It's kept small to focus on the usage of Thinr.

Integrate

This library is available on JCenter Download

Add the dependency to your app and make sure to use the jcenter repository:

    compile 'de.mobilej:thinr:0.2.0'

Version History

Version Description
0.0.0 initial public release
0.1.0 supports RetroLambda, possible to disable runtime checks
0.1.1 runtime checks now allow RetroLambda and Jacoco at the same time
0.2.0 fixed problem with InstantRun, added a Lint check, added more JavaDoc

What does the name mean?

Thinr means THis Is Not Reactive .... it's name came to my mind because in the past there were developers telling us the answer to every single challenge in Android development is reactive programming.

While it might be a good way for you depending on your needs I really doubt it's a good idea to switch paradigms just to workaround some very special issues. Additionally "thinner" means smaller and leaner - which pretty much matches what I am expecting from this.

License

Copyright 2016 Björn Quentin

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.

thinr's People

Contributors

bjoernq avatar

Watchers

James Cloos avatar dev.with 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.