Skip to content
This repository has been archived by the owner on Jan 14, 2018. It is now read-only.

added PendingRequestListener::onRequestAttached #383

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.octo.android.robospice.stub;

import com.octo.android.robospice.request.listener.PendingRequestListener;
import com.octo.android.robospice.request.CachedSpiceRequest;

public class PendingRequestListenerWithProgressStub<T> extends RequestListenerWithProgressStub<T> implements PendingRequestListener<T> {

private boolean isRequestNotFound = false;

private CachedSpiceRequest<?> attachedRequest;


@Override
public void onRequestNotFound() {
isRequestNotFound = true;
Expand All @@ -14,4 +18,13 @@ public void onRequestNotFound() {
public boolean isRequestNotFound() {
return isRequestNotFound;
}

@Override
public void onRequestAttached(CachedSpiceRequest<?> request) {
attachedRequest = request;
}

public CachedSpiceRequest<?> getAttachedRequest() {
return attachedRequest;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.octo.android.robospice.request.listener;

import com.octo.android.robospice.request.CachedSpiceRequest;

/**
* Listens to a SpiceRequest that may be pending, or not. It will be notified of
* request's result if such a request is pending, otherwise it will notified
Expand All @@ -8,4 +10,6 @@

public interface PendingRequestListener<RESULT> extends RequestListener<RESULT> {
void onRequestNotFound();

void onRequestAttached(CachedSpiceRequest<?> request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public <T> void notifyListenersOfRequestAdded(final CachedSpiceRequest<T> reques

@Override
public <T> void notifyListenersOfRequestAggregated(final CachedSpiceRequest<T> request, Set<RequestListener<?>> listeners) {
// does nothing for now

post(new PendingAttachedRunnable<T>(listeners, request), request.getRequestCacheKey());
}

@Override
Expand Down Expand Up @@ -111,7 +112,37 @@ public void run() {
}
}
}


private static class PendingAttachedRunnable<T> implements Runnable {
private final Set<RequestListener<?>> listeners;
private final CachedSpiceRequest<T> request;

public PendingAttachedRunnable(final Set<RequestListener<?>> listeners, final CachedSpiceRequest<T> request) {
this.listeners = listeners;
this.request = request;
}

@Override
public void run() {

if (listeners == null) {
return;
}

Ln.v("Notifying " + listeners.size() + " listeners of pending request attached");
synchronized (listeners) {
for (final RequestListener<?> listener : listeners) {
if (listener != null && listener instanceof PendingRequestListener) {
@SuppressWarnings("unchecked")
final PendingRequestListener<T> listenerOfT = (PendingRequestListener<T>) listener;
Ln.v("Notifying %s", listener.getClass().getSimpleName());
listenerOfT.onRequestAttached(request);
}
}
}
}
}

private static class ProgressRunnable implements Runnable {
private final RequestProgress progress;
private final Set<RequestListener<?>> listeners;
Expand Down