11package com .anotherdev .android .robospice ;
22
3+ import com .anotherdev .android .robospice .request .Cacheable ;
34import com .octo .android .robospice .SpiceManager ;
45import com .octo .android .robospice .request .SpiceRequest ;
56import com .octo .android .robospice .request .listener .RequestListener ;
910
1011public 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