aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/thing/ParseSubscribedThing.java
blob: ef803aff628611bb96d6230030a343bac8208089 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package ml.docilealligator.infinityforreddit.thing;

import android.os.AsyncTask;

import androidx.annotation.NonNull;

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

import java.util.ArrayList;

import ml.docilealligator.infinityforreddit.subreddit.SubredditData;
import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData;
import ml.docilealligator.infinityforreddit.subscribeduser.SubscribedUserData;
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import ml.docilealligator.infinityforreddit.utils.Utils;

class ParseSubscribedThing {
    static void parseSubscribedSubreddits(String response, @NonNull String accountName,
                                          ArrayList<SubscribedSubredditData> subscribedSubredditData,
                                          ArrayList<SubscribedUserData> subscribedUserData,
                                          ArrayList<SubredditData> subredditData,
                                          ParseSubscribedSubredditsListener parseSubscribedSubredditsListener) {
        new ParseSubscribedSubredditsAsyncTask(response, accountName, subscribedSubredditData, subscribedUserData, subredditData,
                parseSubscribedSubredditsListener).execute();
    }

    interface ParseSubscribedSubredditsListener {
        void onParseSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
                                                ArrayList<SubscribedUserData> subscribedUserData,
                                                ArrayList<SubredditData> subredditData,
                                                String lastItem);

        void onParseSubscribedSubredditsFail();
    }

    private static class ParseSubscribedSubredditsAsyncTask extends AsyncTask<Void, Void, Void> {
        private JSONObject jsonResponse;
        private String accountName;
        private boolean parseFailed;
        private String lastItem;
        private ArrayList<SubscribedSubredditData> subscribedSubredditData;
        private ArrayList<SubscribedUserData> subscribedUserData;
        private ArrayList<SubredditData> subredditData;
        private ArrayList<SubscribedSubredditData> newSubscribedSubredditData;
        private ArrayList<SubscribedUserData> newSubscribedUserData;
        private ArrayList<SubredditData> newSubredditData;
        private final ParseSubscribedSubredditsListener parseSubscribedSubredditsListener;

        ParseSubscribedSubredditsAsyncTask(String response, @NonNull String accountName, ArrayList<SubscribedSubredditData> subscribedSubredditData,
                                           ArrayList<SubscribedUserData> subscribedUserData,
                                           ArrayList<SubredditData> subredditData,
                                           ParseSubscribedSubredditsListener parseSubscribedSubredditsListener) {
            this.parseSubscribedSubredditsListener = parseSubscribedSubredditsListener;
            try {
                jsonResponse = new JSONObject(response);
                this.accountName = accountName;
                parseFailed = false;
                this.subscribedSubredditData = subscribedSubredditData;
                this.subscribedUserData = subscribedUserData;
                this.subredditData = subredditData;
                newSubscribedSubredditData = new ArrayList<>();
                newSubscribedUserData = new ArrayList<>();
                newSubredditData = new ArrayList<>();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected Void doInBackground(Void... voids) {
            try {
                if (jsonResponse == null) {
                    parseFailed = true;
                    return null;
                }
                JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
                for (int i = 0; i < children.length(); i++) {
                    JSONObject data = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
                    String name = data.getString(JSONUtils.DISPLAY_NAME_KEY);
                    String bannerImageUrl = data.getString(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY);
                    if (bannerImageUrl.equals("") || bannerImageUrl.equals("null")) {
                        bannerImageUrl = data.getString(JSONUtils.BANNER_IMG_KEY);
                        if (bannerImageUrl.equals("null")) {
                            bannerImageUrl = "";
                        }
                    }
                    String iconUrl = data.getString(JSONUtils.COMMUNITY_ICON_KEY);
                    if (iconUrl.equals("") || iconUrl.equals("null")) {
                        iconUrl = data.getString(JSONUtils.ICON_IMG_KEY);
                        if (iconUrl.equals("null")) {
                            iconUrl = "";
                        }
                    }
                    String id = data.getString(JSONUtils.NAME_KEY);
                    boolean isFavorite = data.getBoolean(JSONUtils.USER_HAS_FAVORITED_KEY);

                    if (data.getString(JSONUtils.SUBREDDIT_TYPE_KEY)
                            .equals(JSONUtils.SUBREDDIT_TYPE_VALUE_USER)) {
                        //It's a user
                        newSubscribedUserData.add(new SubscribedUserData(name.substring(2), iconUrl, accountName, isFavorite));
                    } else {
                        String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME_KEY);
                        String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim();
                        String sidebarDescription = Utils.modifyMarkdown(data.getString(JSONUtils.DESCRIPTION_KEY).trim());
                        int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY);
                        long createdUTC = data.getLong(JSONUtils.CREATED_UTC_KEY) * 1000;
                        String suggestedCommentSort = data.getString(JSONUtils.SUGGESTED_COMMENT_SORT_KEY);
                        boolean isNSFW = data.getBoolean(JSONUtils.OVER18_KEY);
                        newSubscribedSubredditData.add(new SubscribedSubredditData(id, name, iconUrl, accountName, isFavorite));
                        newSubredditData.add(new SubredditData(id, subredditFullName, iconUrl,
                                bannerImageUrl, description, sidebarDescription, nSubscribers, createdUTC,
                                suggestedCommentSort, isNSFW));
                    }
                }
                lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
            } catch (JSONException e) {
                parseFailed = true;
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if (!parseFailed) {
                subscribedSubredditData.addAll(newSubscribedSubredditData);
                subscribedUserData.addAll(newSubscribedUserData);
                subredditData.addAll(newSubredditData);
                parseSubscribedSubredditsListener.onParseSubscribedSubredditsSuccess(subscribedSubredditData,
                        subscribedUserData, subredditData, lastItem);
            } else {
                parseSubscribedSubredditsListener.onParseSubscribedSubredditsFail();
            }
        }
    }
}