Search in sources :

Example 1 with RequestCancellationListener

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

the class RequestProcessor method addRequest.

// ============================================================================================
// PUBLIC
// ============================================================================================
public void addRequest(final CachedSpiceRequest<?> request, final Set<RequestListener<?>> listRequestListener) {
    if (isStopped) {
        Ln.d("Dropping request : " + request + " as processor is stopped.");
        return;
    }
    Ln.d("Adding request to queue " + hashCode() + ": " + request + " size is " + mapRequestToRequestListener.size());
    if (request.isCancelled()) {
        synchronized (mapRequestToRequestListener) {
            for (final CachedSpiceRequest<?> cachedSpiceRequest : mapRequestToRequestListener.keySet()) {
                if (request.equals(cachedSpiceRequest)) {
                    cachedSpiceRequest.cancel();
                    requestProgressManager.notifyListenersOfRequestCancellation(request);
                    return;
                }
            }
        }
    }
    boolean aggregated = false;
    Set<RequestListener<?>> listRequestListenerForThisRequest;
    synchronized (mapRequestToRequestListener) {
        listRequestListenerForThisRequest = mapRequestToRequestListener.get(request);
        if (listRequestListenerForThisRequest == null) {
            if (request.isProcessable()) {
                Ln.d("Adding entry for type %s and cacheKey %s.", request.getResultType(), request.getRequestCacheKey());
                listRequestListenerForThisRequest = Collections.synchronizedSet(new HashSet<RequestListener<?>>());
                this.mapRequestToRequestListener.put(request, listRequestListenerForThisRequest);
            }
        } else {
            Ln.d("Request for type %s and cacheKey %s already exists.", request.getResultType(), request.getRequestCacheKey());
            aggregated = true;
        }
    }
    if (listRequestListener != null && listRequestListenerForThisRequest != null) {
        listRequestListenerForThisRequest.addAll(listRequestListener);
    }
    if (aggregated) {
        requestProgressManager.notifyListenersOfRequestAggregated(request, listRequestListener);
        return;
    }
    if (request.isProcessable()) {
        requestProgressManager.notifyListenersOfRequestAdded(request, listRequestListener);
    } else {
        if (listRequestListenerForThisRequest == null) {
            requestProgressManager.notifyListenersOfRequestNotFound(request, listRequestListener);
        }
        requestProgressManager.notifyOfRequestProcessed(request, listRequestListener);
        // fix bug https://github.com/octo-online/robospice/issues/215
        return;
    }
    final RequestCancellationListener requestCancellationListener = new RequestCancellationListener() {

        @Override
        public void onRequestCancelled() {
            requestProgressManager.notifyListenersOfRequestCancellation(request);
            mapRequestToRequestListener.remove(request);
        }
    };
    request.setRequestCancellationListener(requestCancellationListener);
    if (request.isCancelled()) {
        requestProgressManager.notifyListenersOfRequestCancellation(request);
        mapRequestToRequestListener.remove(request);
        return;
    } else {
        requestRunner.executeRequest(request);
    }
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) RequestCancellationListener(com.octo.android.robospice.request.listener.RequestCancellationListener) HashSet(java.util.HashSet)

Aggregations

RequestCancellationListener (com.octo.android.robospice.request.listener.RequestCancellationListener)1 RequestListener (com.octo.android.robospice.request.listener.RequestListener)1 HashSet (java.util.HashSet)1