Skip to content

Commit dc66efb

Browse files
committed
add fluent api for RoboSpice request
1 parent 1c1501b commit dc66efb

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.anotherdev.android.robospice;
2+
3+
import com.octo.android.robospice.SpiceManager;
4+
import com.octo.android.robospice.request.SpiceRequest;
5+
import com.octo.android.robospice.request.listener.RequestListener;
6+
import com.octo.android.robospice.retry.RetryPolicy;
7+
8+
import javax.annotation.Nullable;
9+
10+
public class RequestCreator {
11+
12+
private final SpiceManager mSpiceManager;
13+
14+
private Optional<String> mCacheKey = Optional.absent();
15+
private Optional<Long> mCacheExpiry = Optional.absent();
16+
private Optional<Boolean> mAcceptDirtyCache = Optional.absent();
17+
private Optional<RetryPolicy> mRetryPolicy = Optional.absent();
18+
19+
20+
RequestCreator(SpiceManager manager) {
21+
if (manager == null) {
22+
throw new IllegalArgumentException("SpiceManager must not be null");
23+
}
24+
mSpiceManager = manager;
25+
}
26+
27+
public RequestCreator cache(@Nullable String key) {
28+
mCacheKey = Optional.from(key);
29+
return this;
30+
}
31+
32+
public RequestCreator expiry(long durationInMillis) {
33+
mCacheExpiry = Optional.from(durationInMillis);
34+
return this;
35+
}
36+
37+
public RequestCreator acceptDirtyCache(boolean enable) {
38+
mAcceptDirtyCache = Optional.from(enable);
39+
return this;
40+
}
41+
42+
public RequestCreator retry(RetryPolicy retryPolicy) {
43+
mRetryPolicy = Optional.from(retryPolicy);
44+
return this;
45+
}
46+
47+
public <T> RequestExecutor<T> notify(RequestListener<T> listener) {
48+
return new RequestExecutor<>(this, listener);
49+
}
50+
51+
public <T> void execute(SpiceRequest<T> request) {
52+
execute(request, null);
53+
}
54+
55+
<T> void execute(SpiceRequest<T> request, RequestListener<T> listener) {
56+
// TODO hand off to SpiceManager
57+
}
58+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.anotherdev.android.robospice;
2+
3+
import com.octo.android.robospice.request.SpiceRequest;
4+
import com.octo.android.robospice.request.listener.RequestListener;
5+
6+
import javax.annotation.Nullable;
7+
8+
public class RequestExecutor<T> {
9+
10+
private final RequestCreator mCreator;
11+
12+
private Optional<RequestListener<T>> mListener = Optional.absent();
13+
14+
15+
RequestExecutor(RequestCreator creator, @Nullable RequestListener<T> listener) {
16+
mCreator = creator;
17+
mListener = Optional.from(listener);
18+
}
19+
20+
public void execute(SpiceRequest<T> request) {
21+
mCreator.execute(request, mListener.orNull());
22+
}
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.anotherdev.android.robospice;
2+
3+
import com.octo.android.robospice.SpiceManager;
4+
5+
public class RoboSpice {
6+
7+
public static RequestCreator with(SpiceManager manager) {
8+
return new RequestCreator(manager);
9+
}
10+
11+
12+
private RoboSpice() {}
13+
}

0 commit comments

Comments
 (0)