blob: 6bdfbbee198ca63116ccdf072e29dc74a54f97b0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package it.niedermann.nextcloud.deck.api;
import com.nextcloud.android.sso.api.NextcloudAPI;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.Schedulers;
import it.niedermann.nextcloud.deck.DeckLog;
public class RequestHelper {
static {
RxJavaPlugins.setErrorHandler(DeckLog::logError);
}
public static <T> void request(final ApiProvider provider, final ObservableProvider<T> call, final IResponseCallback<T> callback) {
if (provider.getDeckAPI() == null) {
provider.initSsoApi(new NextcloudAPI.ApiConnectedListener() {
@Override
public void onConnected() { /* great, nothing to do. */}
@Override
public void onError(Exception e) {
callback.onError(e);
}
});
}
runRequest(call.getObservableFromCall(), callback);
}
private static <T> void runRequest(final Observable<T> request, final IResponseCallback<T> callback) {
ResponseConsumer<T> cb = new ResponseConsumer<>(callback);
request.subscribeOn(Schedulers.newThread())
.subscribe(cb, cb.getExceptionConsumer());
}
public interface ObservableProvider<T> {
Observable<T> getObservableFromCall();
}
public static class ResponseConsumer<T> implements Consumer<T> {
private IResponseCallback<T> callback;
private Consumer<Throwable> exceptionConsumer = new Consumer<Throwable>() {
@Override
public void accept(final Throwable throwable) {
callback.onError(throwable);
}
};
private ResponseConsumer(IResponseCallback<T> callback) {
this.callback = callback;
}
@Override
public void accept(final T t) {
callback.fillAccountIDs(t);
callback.onResponse(t);
}
private Consumer<Throwable> getExceptionConsumer() {
return exceptionConsumer;
}
}
}
|