package ml.docilealligator.infinityforreddit.subreddit; import android.os.AsyncTask; import androidx.annotation.NonNull; import org.json.JSONArray; import org.json.JSONException; import java.util.ArrayList; import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.JSONUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; public class FetchFlairs { public static void fetchFlairsInSubreddit(Retrofit oauthRetrofit, String accessToken, String subredditName, FetchFlairsInSubredditListener fetchFlairsInSubredditListener) { RedditAPI api = oauthRetrofit.create(RedditAPI.class); Call flairsCall = api.getFlairs(APIUtils.getOAuthHeader(accessToken), subredditName); flairsCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { new ParseFlairsAsyncTask(response.body(), new ParseFlairsAsyncTask.ParseFlairsAsyncTaskListener() { @Override public void parseSuccessful(ArrayList flairs) { fetchFlairsInSubredditListener.fetchSuccessful(flairs); } @Override public void parseFailed() { fetchFlairsInSubredditListener.fetchFailed(); } }).execute(); } else if (response.code() == 403) { //No flairs fetchFlairsInSubredditListener.fetchSuccessful(null); } else { fetchFlairsInSubredditListener.fetchFailed(); } } @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { fetchFlairsInSubredditListener.fetchFailed(); } }); } public interface FetchFlairsInSubredditListener { void fetchSuccessful(ArrayList flairs); void fetchFailed(); } private static class ParseFlairsAsyncTask extends AsyncTask, ArrayList> { private final String response; private final ParseFlairsAsyncTaskListener parseFlairsAsyncTaskListener; ParseFlairsAsyncTask(String response, ParseFlairsAsyncTaskListener parseFlairsAsyncTaskListener) { this.response = response; this.parseFlairsAsyncTaskListener = parseFlairsAsyncTaskListener; } @Override protected ArrayList doInBackground(Void... voids) { try { JSONArray jsonArray = new JSONArray(response); ArrayList flairs = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { String id = jsonArray.getJSONObject(i).getString(JSONUtils.ID_KEY); String text = jsonArray.getJSONObject(i).getString(JSONUtils.TEXT_KEY); boolean editable = jsonArray.getJSONObject(i).getBoolean(JSONUtils.TEXT_EDITABLE_KEY); flairs.add(new Flair(id, text, editable)); } return flairs; } catch (JSONException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(ArrayList strings) { if (strings != null) { parseFlairsAsyncTaskListener.parseSuccessful(strings); } else { parseFlairsAsyncTaskListener.parseFailed(); } } interface ParseFlairsAsyncTaskListener { void parseSuccessful(ArrayList flairs); void parseFailed(); } } }