Search in sources :

Example 1 with LoaderEmitter

use of com.koushikdutta.ion.Loader.LoaderEmitter in project ion by koush.

the class IonRequestBuilder method execute.

<T> ResponseFuture<T> execute(final AsyncParser<T> parser, Runnable cancel) {
    assert parser != null;
    final Uri uri = prepareURI();
    AsyncHttpRequest request = null;
    if (uri != null) {
        request = prepareRequest(uri);
        Type type = parser.getType();
        for (Loader loader : ion.loaders) {
            ResponseFuture<T> quickLoad = loader.load(ion, request, type);
            if (quickLoad != null)
                return quickLoad;
        }
    }
    EmitterTransform<T> ret = new EmitterTransform<T>(cancel) {

        EmitterTransform<T> self = this;

        @Override
        protected void transform(LoaderEmitter emitter) throws Exception {
            super.transform(emitter);
            parser.parse(this.emitter).setCallback(new FutureCallback<T>() {

                @Override
                public void onCompleted(Exception e, T result) {
                    postExecute(self, e, result);
                }
            });
        }
    };
    if (uri == null) {
        ret.setComplete(new Exception("Invalid URI"));
        return ret;
    }
    ret.initialRequest = request;
    getLoaderEmitter(ret);
    return ret;
}
Also used : Type(java.lang.reflect.Type) Uri(android.net.Uri) LoaderEmitter(com.koushikdutta.ion.Loader.LoaderEmitter) AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest)

Example 2 with LoaderEmitter

use of com.koushikdutta.ion.Loader.LoaderEmitter in project ion by koush.

the class IonRequestBuilder method execute.

<T> EmitterTransform<T> execute(final DataSink sink, final boolean close, final T result, final Runnable cancel) {
    EmitterTransform<T> ret = new EmitterTransform<T>(cancel) {

        @Override
        protected void cleanup() {
            super.cleanup();
            if (close)
                sink.end();
        }

        EmitterTransform<T> self = this;

        @Override
        protected void transform(LoaderEmitter emitter) throws Exception {
            super.transform(emitter);
            Util.pump(this.emitter, sink, new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    postExecute(self, ex, result);
                }
            });
        }
    };
    getLoaderEmitter(ret);
    return ret;
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) LoaderEmitter(com.koushikdutta.ion.Loader.LoaderEmitter)

Aggregations

LoaderEmitter (com.koushikdutta.ion.Loader.LoaderEmitter)2 Uri (android.net.Uri)1 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 AsyncHttpRequest (com.koushikdutta.async.http.AsyncHttpRequest)1 Type (java.lang.reflect.Type)1