aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/post/FetchRules.java
blob: 6a1efca3906ab6d49cf74ed1319a4b8c5059469e (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
86
87
package ml.docilealligator.infinityforreddit.post;

import android.os.Handler;

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

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.concurrent.Executor;

import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.subreddit.Rule;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import ml.docilealligator.infinityforreddit.utils.Utils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class FetchRules {
    public interface FetchRulesListener {
        void success(ArrayList<Rule> rules);
        void failed();
    }

    public static void fetchRules(Executor executor, Handler handler, Retrofit retrofit, @Nullable String accessToken,
                                  @NonNull String accountName, String subredditName,
                                  FetchRulesListener fetchRulesListener) {
        RedditAPI api = retrofit.create(RedditAPI.class);
        Call<String> rulesCall = accountName.equals(Account.ANONYMOUS_ACCOUNT)
                ? api.getRules(subredditName)
                : api.getRulesOauth(APIUtils.getOAuthHeader(accessToken), subredditName);
        rulesCall.enqueue(new Callback<>() {
            @Override
            public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
                if (response.isSuccessful()) {
                    parseRules(executor, handler, response.body(), new FetchRulesListener() {
                        @Override
                        public void success(ArrayList<Rule> rules) {
                            fetchRulesListener.success(rules);
                        }

                        @Override
                        public void failed() {
                            fetchRulesListener.failed();
                        }
                    });
                } else {
                    fetchRulesListener.failed();
                }
            }

            @Override
            public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
                fetchRulesListener.failed();
            }
        });
    }

    private static void parseRules(Executor executor, Handler handler, String response,
                                   FetchRulesListener fetchRulesListener) {
        executor.execute(() -> {
            try {
                JSONArray rulesArray = new JSONObject(response).getJSONArray(JSONUtils.RULES_KEY);
                ArrayList<Rule> rules = new ArrayList<>();
                for (int i = 0; i < rulesArray.length(); i++) {
                    String shortName = rulesArray.getJSONObject(i).getString(JSONUtils.SHORT_NAME_KEY);
                    String description = null;
                    if (rulesArray.getJSONObject(i).has(JSONUtils.DESCRIPTION_KEY)) {
                        description = Utils.modifyMarkdown(rulesArray.getJSONObject(i).getString(JSONUtils.DESCRIPTION_KEY));
                    }
                    rules.add(new Rule(shortName, description));
                }
                handler.post(() -> fetchRulesListener.success(rules));
            } catch (JSONException e) {
                e.printStackTrace();
                handler.post(fetchRulesListener::failed);
            }
        });
    }
}