aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/ParseMultiReddit.java
blob: a737fd1003f9786582e28297c55b503f9433390d (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.multireddit;

import android.os.AsyncTask;

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

import java.util.ArrayList;

import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.utils.JSONUtils;

public class ParseMultiReddit {
    interface ParseMultiRedditsListListener {
        void success(ArrayList<MultiReddit> multiReddits);
        void failed();
    }

    interface ParseMultiRedditListener {
        void success();
        void failed();
    }

    public static void parseMultiRedditsList(String response, ParseMultiRedditsListListener parseMultiRedditsListListener) {
        new ParseMultiRedditsListAsyncTask(response, parseMultiRedditsListListener).execute();
    }

    public static void parseAndSaveMultiReddit(String response, RedditDataRoomDatabase redditDataRoomDatabase,
                                               ParseMultiRedditListener parseMultiRedditListener) {
        new ParseAndSaveMultiRedditAsyncTask(response, redditDataRoomDatabase, parseMultiRedditListener).execute();
    }

    private static MultiReddit parseMultiReddit(JSONObject singleMultiRedditJSON) throws JSONException {
        String displayName = singleMultiRedditJSON.getString(JSONUtils.DISPLAY_NAME_KEY);
        String name = singleMultiRedditJSON.getString(JSONUtils.NAME_KEY);
        String description = singleMultiRedditJSON.getString(JSONUtils.DESCRIPTION_MD_KEY);
        int nSubscribers = singleMultiRedditJSON.getInt(JSONUtils.NUM_SUBSCRIBERS_KEY);
        String copiedFrom = singleMultiRedditJSON.getString(JSONUtils.COPIED_FROM_KEY);
        String iconUrl = singleMultiRedditJSON.getString(JSONUtils.ICON_URL_KEY);
        long createdUTC = singleMultiRedditJSON.getLong(JSONUtils.CREATED_UTC_KEY);
        String visibility = singleMultiRedditJSON.getString(JSONUtils.VISIBILITY_KEY);
        boolean over18 = singleMultiRedditJSON.getBoolean(JSONUtils.OVER_18_KEY);
        String path = singleMultiRedditJSON.getString(JSONUtils.PATH_KEY);
        String owner = singleMultiRedditJSON.getString(JSONUtils.OWNER_KEY);
        boolean isSubscriber = singleMultiRedditJSON.getBoolean(JSONUtils.IS_SUBSCRIBER_KEY);
        boolean isFavorited = singleMultiRedditJSON.getBoolean(JSONUtils.IS_FAVORITED_KEY);

        JSONArray subredditsArray = singleMultiRedditJSON.getJSONArray(JSONUtils.SUBREDDITS_KEY);
        ArrayList<String> subreddits = new ArrayList<>();
        for (int j = 0; j < subredditsArray.length(); j++) {
            subreddits.add(subredditsArray.getJSONObject(j).getString(JSONUtils.NAME_KEY));
        }

        return new MultiReddit(path, displayName, name, description, copiedFrom,
                iconUrl, visibility, owner, nSubscribers, createdUTC, over18, isSubscriber,
                isFavorited, subreddits);
    }

    private static class ParseMultiRedditsListAsyncTask extends AsyncTask<Void, Void, Void> {
        private JSONArray arrayResponse;
        private boolean parseFailed;
        private ArrayList<MultiReddit> multiReddits;
        private final ParseMultiRedditsListListener parseMultiRedditsListListener;

        ParseMultiRedditsListAsyncTask(String response,
                                       ParseMultiRedditsListListener parseMultiRedditsListListener) {
            this.parseMultiRedditsListListener = parseMultiRedditsListListener;
            try {
                arrayResponse = new JSONArray(response);
                multiReddits = new ArrayList<>();
                parseFailed = false;
            } catch (JSONException e) {
                e.printStackTrace();
                parseFailed = true;
            }
        }

        @Override
        protected Void doInBackground(Void... voids) {
            if (!parseFailed) {
                for (int i = 0; i < arrayResponse.length(); i++) {
                    try {
                        multiReddits.add(parseMultiReddit(arrayResponse.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY)));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            if (!parseFailed) {
                parseMultiRedditsListListener.success(multiReddits);
            } else {
                parseMultiRedditsListListener.failed();
            }
        }
    }

    private static class ParseAndSaveMultiRedditAsyncTask extends AsyncTask<Void, Void, Void> {
        private final String response;
        private final RedditDataRoomDatabase redditDataRoomDatabase;
        private MultiReddit multiReddit;
        private final ParseMultiRedditListener parseMultiRedditListener;

        ParseAndSaveMultiRedditAsyncTask(String response, RedditDataRoomDatabase redditDataRoomDatabase,
                                         ParseMultiRedditListener parseMultiRedditListener) {
            this.redditDataRoomDatabase = redditDataRoomDatabase;
            this.parseMultiRedditListener = parseMultiRedditListener;
            this.response = response;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            try {
                multiReddit = parseMultiReddit(new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY));
                redditDataRoomDatabase.multiRedditDao().insert(multiReddit);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            if (multiReddit != null) {
                parseMultiRedditListener.success();
            } else {
                parseMultiRedditListener.failed();
            }
        }
    }
}