From c74ceda81630e2e82c61d50de9fc5998ea6d3f2b Mon Sep 17 00:00:00 2001 From: Nick Migel Date: Tue, 25 Nov 2014 18:18:10 +0100 Subject: [PATCH] added PendingRequestListener::onRequestAttached --- ...endingRequestListenerWithProgressStub.java | 13 +++++++ .../listener/PendingRequestListener.java | 4 +++ .../DefaultRequestListenerNotifier.java | 35 +++++++++++++++++-- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/robospice-core-parent/robospice-core-test/src/main/java/com/octo/android/robospice/stub/PendingRequestListenerWithProgressStub.java b/robospice-core-parent/robospice-core-test/src/main/java/com/octo/android/robospice/stub/PendingRequestListenerWithProgressStub.java index 501b1c0a6..1bde7d860 100644 --- a/robospice-core-parent/robospice-core-test/src/main/java/com/octo/android/robospice/stub/PendingRequestListenerWithProgressStub.java +++ b/robospice-core-parent/robospice-core-test/src/main/java/com/octo/android/robospice/stub/PendingRequestListenerWithProgressStub.java @@ -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 extends RequestListenerWithProgressStub implements PendingRequestListener { private boolean isRequestNotFound = false; + private CachedSpiceRequest attachedRequest; + + @Override public void onRequestNotFound() { isRequestNotFound = true; @@ -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; + } } diff --git a/robospice-core-parent/robospice/src/main/java/com/octo/android/robospice/request/listener/PendingRequestListener.java b/robospice-core-parent/robospice/src/main/java/com/octo/android/robospice/request/listener/PendingRequestListener.java index 3bf185ac1..5481848e8 100644 --- a/robospice-core-parent/robospice/src/main/java/com/octo/android/robospice/request/listener/PendingRequestListener.java +++ b/robospice-core-parent/robospice/src/main/java/com/octo/android/robospice/request/listener/PendingRequestListener.java @@ -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 @@ -8,4 +10,6 @@ public interface PendingRequestListener extends RequestListener { void onRequestNotFound(); + + void onRequestAttached(CachedSpiceRequest request); } diff --git a/robospice-core-parent/robospice/src/main/java/com/octo/android/robospice/request/notifier/DefaultRequestListenerNotifier.java b/robospice-core-parent/robospice/src/main/java/com/octo/android/robospice/request/notifier/DefaultRequestListenerNotifier.java index 6ed360b57..87572b358 100644 --- a/robospice-core-parent/robospice/src/main/java/com/octo/android/robospice/request/notifier/DefaultRequestListenerNotifier.java +++ b/robospice-core-parent/robospice/src/main/java/com/octo/android/robospice/request/notifier/DefaultRequestListenerNotifier.java @@ -49,7 +49,8 @@ public void notifyListenersOfRequestAdded(final CachedSpiceRequest reques @Override public void notifyListenersOfRequestAggregated(final CachedSpiceRequest request, Set> listeners) { - // does nothing for now + + post(new PendingAttachedRunnable(listeners, request), request.getRequestCacheKey()); } @Override @@ -111,7 +112,37 @@ public void run() { } } } - + + private static class PendingAttachedRunnable implements Runnable { + private final Set> listeners; + private final CachedSpiceRequest request; + + public PendingAttachedRunnable(final Set> listeners, final CachedSpiceRequest 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 listenerOfT = (PendingRequestListener) listener; + Ln.v("Notifying %s", listener.getClass().getSimpleName()); + listenerOfT.onRequestAttached(request); + } + } + } + } + } + private static class ProgressRunnable implements Runnable { private final RequestProgress progress; private final Set> listeners;