Skip to content

Instantly share code, notes, and snippets.

@digitalbuddha
Last active September 8, 2022 10:20
Show Gist options
  • Save digitalbuddha/da66781bb6be5e5e921c to your computer and use it in GitHub Desktop.
Save digitalbuddha/da66781bb6be5e5e921c to your computer and use it in GitHub Desktop.
public class BaseModel<R, T> implements Serializable {
public T payload;
public HashMap<Integer, BaseModel<R, T>> partialPayload = new HashMap<>();
public long lastUpdated;
public ResultCode resultCode;
}
public interface Github {
@GET("/users/{userName}/repos")
ArrayList<Repo> repos(@Path("userName") String user)
}
public class GitHubStore extends ObservableStore<ArrayList<Repo>, String> {
@Inject
protected Github github
@Inject
GitHubStore() {
}
@Override
public ArrayList<Repo> load(String user) throws Exception {
github.repos(user)
}
}
public
abstract class ObservableStore<T extends Serializable, V> extends AbstractStore<T, V> implements ObservableStoreInterface<T, V> {
public ObservableStore() {
objectName = this.toString()
initialize()
}
public all(final V request) {
fresh(request).startWith(getCachedValue(request))
}
public Observable<BaseModel<V, T>> cached(final V request) {
observabler(getCachedValue(request))
}
public Observable<BaseModel<V, T>> fresh(final V request) {
if (!isInFlightNetwork(request)) {
response(request).subscribeOn(Schedulers.io())
} else {
inFlightResponse(request)
}
}
public Observable<BaseModel<V, T>> get(final V request) {
observabler(getCachedValue(request))
.flatMap({
cachedValue == null ? fresh(request) : observabler(cachedValue);
}).subscribeOn(Schedulers.io())
}
}
public class GithubActivity extends DemoBaseActivity {
@Inject
protected GitHubStore gitHubStore
@Inject
@ForActivity
protected Context context
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate savedInstanceState
contentView = activity_post
initButton()
}
private initButton() {
clicks(view(button))
.flatMap({ gitHubStore.fresh("digitalbuddha") })
.observeOn(mainThread())
.subscribe({ makeToast it.payload },//onNext
{ makeText(context, it.getCause().toString(), LENGTH_SHORT) show() })//onError
}
private void makeToast(ArrayList<Repo> it) {
def url = it.get(0).clone_url
makeText(context, url, LENGTH_SHORT).show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment