Skip to content

Commit a003369

Browse files
committed
add hand off to SpiceManager
1 parent 6460994 commit a003369

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

library/src/main/java/com/anotherdev/android/robospice/RequestCreator.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.anotherdev.android.robospice;
22

3+
import com.anotherdev.android.robospice.request.Cacheable;
34
import com.octo.android.robospice.SpiceManager;
45
import com.octo.android.robospice.request.SpiceRequest;
56
import com.octo.android.robospice.request.listener.RequestListener;
@@ -9,7 +10,7 @@
910

1011
public class RequestCreator {
1112

12-
private final SpiceManager mSpiceManager;
13+
private final SpiceManager mManager;
1314

1415
private Optional<String> mCacheKey = Optional.absent();
1516
private Optional<Long> mCacheExpiry = Optional.absent();
@@ -21,7 +22,7 @@ public class RequestCreator {
2122
if (manager == null) {
2223
throw new IllegalArgumentException("SpiceManager must not be null");
2324
}
24-
mSpiceManager = manager;
25+
mManager = manager;
2526
}
2627

2728
public RequestCreator cache(@Nullable String key) {
@@ -53,6 +54,21 @@ public <T> void execute(SpiceRequest<T> request) {
5354
}
5455

5556
<T> void execute(SpiceRequest<T> request, RequestListener<T> listener) {
56-
// TODO hand off to SpiceManager
57+
if (request instanceof Cacheable) {
58+
Cacheable cacheable = (Cacheable) request;
59+
Optional<String> cacheKey = Optional.from(cacheable.getCacheKey());
60+
61+
final String key = mCacheKey.or(cacheKey).orNull();
62+
final long expiry = mCacheExpiry.or(cacheable.getCacheDurationInMillis());
63+
final boolean acceptDirtyCache = mAcceptDirtyCache.or(cacheable.isAcceptingDirtyCache());
64+
65+
if (acceptDirtyCache) {
66+
mManager.getFromCacheAndLoadFromNetworkIfExpired(request, key, expiry, listener);
67+
} else {
68+
mManager.execute(request, key, expiry, listener);
69+
}
70+
} else {
71+
mManager.execute(request, listener);
72+
}
5773
}
5874
}

0 commit comments

Comments
 (0)