Search in sources :

Example 1 with RequestProgressListener

use of com.octo.android.robospice.request.listener.RequestProgressListener in project robospice by stephanenicolas.

the class DefaultRequestRunner method processRequest.

protected <T> void processRequest(final CachedSpiceRequest<T> request) {
    final long startTime = System.currentTimeMillis();
    Ln.d("Processing request : " + request);
    T result = null;
    // add a progress listener to the request to be notified of
    // progress during load data from network
    final RequestProgressListener requestProgressListener = requestProgressManager.createProgressListener(request);
    request.setRequestProgressListener(requestProgressListener);
    if (request.getRequestCacheKey() != null && request.getCacheDuration() != DurationInMillis.ALWAYS_EXPIRED) {
        // First, search data in cache
        try {
            Ln.d("Loading request from cache : " + request);
            request.setStatus(RequestStatus.READING_FROM_CACHE);
            result = loadDataFromCache(request.getResultType(), request.getRequestCacheKey(), request.getCacheDuration());
            // request
            if (result != null) {
                Ln.d("Request loaded from cache : " + request + " result=" + result);
                requestProgressManager.notifyListenersOfRequestSuccess(request, result);
                printRequestProcessingDuration(startTime, request);
                return;
            } else if (request.isAcceptingDirtyCache()) {
                // as a fallback, some request may accept whatever is in the
                // cache but still
                // want an update from network.
                result = loadDataFromCache(request.getResultType(), request.getRequestCacheKey(), DurationInMillis.ALWAYS_RETURNED);
                if (result != null) {
                    requestProgressManager.notifyListenersOfRequestSuccessButDontCompleteRequest(request, result);
                }
            }
        } catch (final SpiceException e) {
            Ln.d(e, "Cache file could not be read.");
            if (failOnCacheError) {
                handleRetry(request, e);
                printRequestProcessingDuration(startTime, request);
                return;
            }
            cacheManager.removeDataFromCache(request.getResultType(), request.getRequestCacheKey());
            Ln.d(e, "Cache file deleted.");
        }
    }
    // if result is not in cache, load data from network
    Ln.d("Cache content not available or expired or disabled");
    if (!networkStateChecker.isNetworkAvailable(applicationContext) && !request.isOffline()) {
        Ln.e("Network is down.");
        if (!request.isCancelled()) {
            // don't retry when there is no network
            requestProgressManager.notifyListenersOfRequestFailure(request, new NoNetworkException());
        }
        printRequestProcessingDuration(startTime, request);
        return;
    }
    // network is ok, load data from network
    try {
        if (request.isCancelled()) {
            printRequestProcessingDuration(startTime, request);
            return;
        }
        Ln.d("Calling netwok request.");
        request.setStatus(RequestStatus.LOADING_FROM_NETWORK);
        result = request.loadDataFromNetwork();
        Ln.d("Network request call ended.");
    } catch (final Exception e) {
        if (!request.isCancelled()) {
            Ln.e(e, "An exception occurred during request network execution :" + e.getMessage());
            handleRetry(request, new NetworkException("Exception occurred during invocation of web service.", e));
        } else {
            Ln.e("An exception occurred during request network execution but request was cancelled, so listeners are not called.");
        }
        printRequestProcessingDuration(startTime, request);
        return;
    }
    if (result != null && request.getRequestCacheKey() != null) {
        // it to cache
        try {
            if (request.isCancelled()) {
                printRequestProcessingDuration(startTime, request);
                return;
            }
            Ln.d("Start caching content...");
            request.setStatus(RequestStatus.WRITING_TO_CACHE);
            result = saveDataToCacheAndReturnData(result, request.getRequestCacheKey());
            if (request.isCancelled()) {
                printRequestProcessingDuration(startTime, request);
                return;
            }
            requestProgressManager.notifyListenersOfRequestSuccess(request, result);
            printRequestProcessingDuration(startTime, request);
            return;
        } catch (final SpiceException e) {
            Ln.d(e, "An exception occurred during service execution :" + e.getMessage());
            if (failOnCacheError) {
                handleRetry(request, e);
                printRequestProcessingDuration(startTime, request);
                return;
            } else {
                if (request.isCancelled()) {
                    printRequestProcessingDuration(startTime, request);
                    return;
                }
                // result can't be saved to
                // cache but we reached that
                // point after a success of load
                // data from
                // network
                requestProgressManager.notifyListenersOfRequestSuccess(request, result);
            }
            cacheManager.removeDataFromCache(request.getResultType(), request.getRequestCacheKey());
            Ln.d(e, "Cache file deleted.");
        }
    } else {
        // result can't be saved to cache but we reached
        // that point after a success of load data from
        // network
        requestProgressManager.notifyListenersOfRequestSuccess(request, result);
        printRequestProcessingDuration(startTime, request);
        return;
    }
}
Also used : RequestProgressListener(com.octo.android.robospice.request.listener.RequestProgressListener) NoNetworkException(com.octo.android.robospice.exception.NoNetworkException) SpiceException(com.octo.android.robospice.persistence.exception.SpiceException) NetworkException(com.octo.android.robospice.exception.NetworkException) NoNetworkException(com.octo.android.robospice.exception.NoNetworkException) CacheSavingException(com.octo.android.robospice.persistence.exception.CacheSavingException) CacheCreationException(com.octo.android.robospice.persistence.exception.CacheCreationException) SpiceException(com.octo.android.robospice.persistence.exception.SpiceException) NetworkException(com.octo.android.robospice.exception.NetworkException) NoNetworkException(com.octo.android.robospice.exception.NoNetworkException) CacheLoadingException(com.octo.android.robospice.persistence.exception.CacheLoadingException)

Example 2 with RequestProgressListener

use of com.octo.android.robospice.request.listener.RequestProgressListener in project robospice by stephanenicolas.

the class RequestProgressManager method createProgressListener.

public <T> RequestProgressListener createProgressListener(final CachedSpiceRequest<T> request) {
    // add a progress listener to the request to be notified of
    // progress during load data from network
    final RequestProgressListener requestProgressListener = new RequestProgressListener() {

        @Override
        public void onRequestProgressUpdate(final RequestProgress progress) {
            final Set<RequestListener<?>> listeners = mapRequestToRequestListener.get(request);
            notifyListenersOfRequestProgress(request, listeners, progress);
        }
    };
    return requestProgressListener;
}
Also used : RequestProgressListener(com.octo.android.robospice.request.listener.RequestProgressListener) RequestListener(com.octo.android.robospice.request.listener.RequestListener) RequestProgress(com.octo.android.robospice.request.listener.RequestProgress)

Aggregations

RequestProgressListener (com.octo.android.robospice.request.listener.RequestProgressListener)2 NetworkException (com.octo.android.robospice.exception.NetworkException)1 NoNetworkException (com.octo.android.robospice.exception.NoNetworkException)1 CacheCreationException (com.octo.android.robospice.persistence.exception.CacheCreationException)1 CacheLoadingException (com.octo.android.robospice.persistence.exception.CacheLoadingException)1 CacheSavingException (com.octo.android.robospice.persistence.exception.CacheSavingException)1 SpiceException (com.octo.android.robospice.persistence.exception.SpiceException)1 RequestListener (com.octo.android.robospice.request.listener.RequestListener)1 RequestProgress (com.octo.android.robospice.request.listener.RequestProgress)1