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

ResponseCallback.java « api « remote « 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: 1167fb7e2e5c964cbf4ba54431df561aa9e3f0be (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
73
74
75
76
77
78
79
80
package it.niedermann.nextcloud.deck.remote.api;

import android.annotation.SuppressLint;

import androidx.annotation.NonNull;

import java.util.Collection;
import java.util.List;

import it.niedermann.nextcloud.deck.model.Account;
import it.niedermann.nextcloud.deck.model.interfaces.AbstractRemoteEntity;

/**
 * Abstract implementation of {@link IResponseCallback} which is aware of an {@link Account}.
 *
 * @param <T> payload type of the {@link #onResponse(T)} method
 */
public abstract class ResponseCallback<T> implements IResponseCallback<T> {
    @NonNull
    protected Account account;

    public ResponseCallback(@NonNull Account account) {
        this.account = account;
    }

    public void fillAccountIDs(T response) {
        if (response != null) {
            if (isListOfRemoteEntity(response)) {
                fillAccountIDs((Collection<AbstractRemoteEntity>) response);
            } else if (isRemoteEntity(response)) {
                fillAccountIDs((AbstractRemoteEntity) response);
            }
        }
    }

    private void fillAccountIDs(AbstractRemoteEntity response) {
        response.setAccountId(this.account.getId());
    }

    private void fillAccountIDs(Collection<AbstractRemoteEntity> response) {
        for (AbstractRemoteEntity entity : response) {
            entity.setAccountId(this.account.getId());
        }
    }

    private boolean isRemoteEntity(T response) {
        return response instanceof AbstractRemoteEntity;
    }

    private boolean isListOfRemoteEntity(T response) {
        if (response instanceof List) {
            final var collection = (List<?>) response;
            return collection.size() > 0 && collection.get(0) instanceof AbstractRemoteEntity;
        }
        return false;
    }

    @NonNull
    public Account getAccount() {
        return account;
    }

    /**
     * Forwards responses and errors to the given {@param callback}
     */
    public static <T> ResponseCallback<T> from(@NonNull Account account, IResponseCallback<T> callback) {
        return new ResponseCallback<>(account) {
            @Override
            public void onResponse(T response) {
                callback.onResponse(response);
            }

            @SuppressLint("MissingSuperCall")
            @Override
            public void onError(Throwable throwable) {
                callback.onError(throwable);
            }
        };
    }
}