Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RequestHelper.java « api « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b632b4501e3a6c671d49c92a286098f1a93c8036 (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
70
71
72
package it.niedermann.nextcloud.deck.api;

import android.app.Activity;

import androidx.annotation.Nullable;

import com.nextcloud.android.sso.api.NextcloudAPI;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class RequestHelper {

    public static <T> void request(@Nullable final Activity sourceActivity, 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(sourceActivity, call.getObservableFromCall(), callback);
    }

    private static <T> void runRequest(@Nullable final Activity sourceActivity, final Observable<T> request, final IResponseCallback<T> callback){
        ResponseConsumer<T> cb = new ResponseConsumer<>(sourceActivity, callback);
        request.subscribeOn(Schedulers.newThread())
                .subscribe(cb, cb.getExceptionConsumer());
    }


    public interface ObservableProvider <T> {
        Observable<T> getObservableFromCall();
    }

    public static class ResponseConsumer<T> implements Consumer<T> {

        @Nullable private Activity sourceActivity;
        private IResponseCallback<T> callback;
        private Consumer<Throwable> exceptionConsumer = new Consumer<Throwable>() {
            @Override
            public void accept(final Throwable throwable) {
                if(sourceActivity == null) {
                    callback.onError(throwable);
                } else {
                    sourceActivity.runOnUiThread(() -> callback.onError(throwable));
                }
            }
        };

        public ResponseConsumer(@Nullable Activity sourceActivity, IResponseCallback<T> callback) {
            this.sourceActivity = sourceActivity;
            this.callback = callback;
        }

        @Override
        public void accept(final T t) {
            callback.fillAccountIDs(t);
            callback.onResponse(t);
//            sourceActivity.runOnUiThread(() -> callback.onResponse(t) );
        }

        public Consumer<Throwable> getExceptionConsumer() {
            return exceptionConsumer;
        }
    }
}