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;
}
}
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;
}
Aggregations