aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java
blob: a1740f4dc1ebd724021b6f123a356f5a8cb0accd (plain) (blame)
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
81
82
83
84
85
package ml.docilealligator.infinityforreddit.subreddit;

import android.os.Handler;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;

import ml.docilealligator.infinityforreddit.thing.SortType;
import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;

public class FetchSubredditData {
    public static void fetchSubredditData(Executor executor, Handler handler, Retrofit oauthRetrofit, Retrofit retrofit,
                                          String subredditName, String accessToken,
                                          final FetchSubredditDataListener fetchSubredditDataListener) {
        executor.execute(() -> {
            RedditAPI api = retrofit.create(RedditAPI.class);

            Call<String> subredditData;
            if (oauthRetrofit == null) {
                subredditData = api.getSubredditData(subredditName);
            } else {
                RedditAPI oauthApi = oauthRetrofit.create(RedditAPI.class);
                subredditData = oauthApi.getSubredditDataOauth(subredditName, APIUtils.getOAuthHeader(accessToken));
            }
            try {
                Response<String> response = subredditData.execute();
                if (response.isSuccessful()) {
                    ParseSubredditData.parseSubredditDataSync(handler, response.body(), fetchSubredditDataListener);
                } else {
                    handler.post(() -> fetchSubredditDataListener.onFetchSubredditDataFail(response.code() == 403));
                }
            } catch (IOException e) {
                handler.post(() -> fetchSubredditDataListener.onFetchSubredditDataFail(false));
            }
        });
    }

    static void fetchSubredditListingData(Executor executor, Handler handler, Retrofit retrofit, String query,
                                          String after, SortType.Type sortType, @Nullable String accessToken,
                                          @NonNull String accountName, boolean nsfw,
                                          final FetchSubredditListingDataListener fetchSubredditListingDataListener) {
        executor.execute(() -> {
            RedditAPI api = retrofit.create(RedditAPI.class);

            Map<String, String> map = new HashMap<>();
            Map<String, String> headers = accountName.equals(Account.ANONYMOUS_ACCOUNT) ? map : APIUtils.getOAuthHeader(accessToken);
            Call<String> subredditDataCall = api.searchSubreddits(query, after, sortType, nsfw ? 1 : 0, headers);
            try {
                Response<String> response = subredditDataCall.execute();
                if (response.isSuccessful()) {
                    ParseSubredditData.parseSubredditListingDataSync(handler, response.body(), nsfw,
                            fetchSubredditListingDataListener);
                } else {
                    handler.post(fetchSubredditListingDataListener::onFetchSubredditListingDataFail);
                }
            } catch (IOException e) {
                e.printStackTrace();
                handler.post(fetchSubredditListingDataListener::onFetchSubredditListingDataFail);
            }
        });
    }

    public interface FetchSubredditDataListener {
        void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers);

        void onFetchSubredditDataFail(boolean isQuarantined);
    }

    public interface FetchSubredditListingDataListener {
        void onFetchSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after);

        void onFetchSubredditListingDataFail();
    }
}