From 0b2ed14e6911e9ca9d615e7eae65ab1eb80fbf21 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Mon, 14 Apr 2025 10:50:30 -0400 Subject: [PATCH] Use Retrofit's enqueue() instead of execute() in some places. --- .../account/FetchMyInfo.java | 41 ++++-- .../message/FetchMessage.java | 42 +++--- .../subreddit/FetchFlairs.java | 55 ++++---- .../infinityforreddit/user/FetchUserData.java | 132 ++++++++++-------- .../user/FetchUserFlairs.java | 50 ++++--- .../user/SelectUserFlair.java | 73 ++++++---- 6 files changed, 223 insertions(+), 170 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/account/FetchMyInfo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/account/FetchMyInfo.java index c50e5bc9..930cb911 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/account/FetchMyInfo.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/account/FetchMyInfo.java @@ -3,16 +3,19 @@ package ml.docilealligator.infinityforreddit.account; import android.os.Handler; import android.text.Html; +import androidx.annotation.NonNull; + import org.json.JSONException; import org.json.JSONObject; -import java.io.IOException; import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; 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; @@ -21,25 +24,33 @@ public class FetchMyInfo { public static void fetchAccountInfo(final Executor executor, final Handler handler, final Retrofit retrofit, final RedditDataRoomDatabase redditDataRoomDatabase, final String accessToken, final FetchMyInfoListener fetchMyInfoListener) { - executor.execute(() -> { - try { - Response response = retrofit.create(RedditAPI.class).getMyInfo(APIUtils.getOAuthHeader(accessToken)).execute(); + retrofit.create(RedditAPI.class).getMyInfo(APIUtils.getOAuthHeader(accessToken)).enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { - JSONObject jsonResponse = new JSONObject(response.body()); - String name = jsonResponse.getString(JSONUtils.NAME_KEY); - String profileImageUrl = Html.fromHtml(jsonResponse.getString(JSONUtils.ICON_IMG_KEY)).toString(); - String bannerImageUrl = !jsonResponse.isNull(JSONUtils.SUBREDDIT_KEY) ? Html.fromHtml(jsonResponse.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.BANNER_IMG_KEY)).toString() : null; - int karma = jsonResponse.getInt(JSONUtils.TOTAL_KARMA_KEY); + executor.execute(() -> { + try { + JSONObject jsonResponse = new JSONObject(response.body()); + String name = jsonResponse.getString(JSONUtils.NAME_KEY); + String profileImageUrl = Html.fromHtml(jsonResponse.getString(JSONUtils.ICON_IMG_KEY)).toString(); + String bannerImageUrl = !jsonResponse.isNull(JSONUtils.SUBREDDIT_KEY) ? Html.fromHtml(jsonResponse.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.BANNER_IMG_KEY)).toString() : null; + int karma = jsonResponse.getInt(JSONUtils.TOTAL_KARMA_KEY); - redditDataRoomDatabase.accountDao().updateAccountInfo(name, profileImageUrl, bannerImageUrl, karma); + redditDataRoomDatabase.accountDao().updateAccountInfo(name, profileImageUrl, bannerImageUrl, karma); - handler.post(() -> fetchMyInfoListener.onFetchMyInfoSuccess(name, profileImageUrl, bannerImageUrl, karma)); + handler.post(() -> fetchMyInfoListener.onFetchMyInfoSuccess(name, profileImageUrl, bannerImageUrl, karma)); + } catch (JSONException e) { + handler.post(() -> fetchMyInfoListener.onFetchMyInfoFailed(true)); + } + }); } else { - handler.post(() -> fetchMyInfoListener.onFetchMyInfoFailed(false)); + fetchMyInfoListener.onFetchMyInfoFailed(false); } - } catch (IOException | JSONException e) { - e.printStackTrace(); - handler.post(() -> fetchMyInfoListener.onFetchMyInfoFailed(e instanceof JSONException)); + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable throwable) { + fetchMyInfoListener.onFetchMyInfoFailed(false); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/message/FetchMessage.java b/app/src/main/java/ml/docilealligator/infinityforreddit/message/FetchMessage.java index 423ac573..60bc376d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/message/FetchMessage.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/message/FetchMessage.java @@ -2,13 +2,13 @@ package ml.docilealligator.infinityforreddit.message; 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.io.IOException; import java.util.List; import java.util.Locale; import java.util.concurrent.Executor; @@ -16,6 +16,8 @@ import java.util.concurrent.Executor; 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; @@ -33,26 +35,30 @@ public class FetchMessage { static void fetchInbox(Executor executor, Handler handler, Retrofit oauthRetrofit, Locale locale, String accessToken, String where, String after, int messageType, FetchMessagesListener fetchMessagesListener) { - executor.execute(() -> { - try { - Response response = oauthRetrofit.create(RedditAPI.class).getMessages(APIUtils.getOAuthHeader(accessToken), where, after).execute(); + oauthRetrofit.create(RedditAPI.class).getMessages(APIUtils.getOAuthHeader(accessToken), where, after).enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { - try { - JSONObject jsonResponse = new JSONObject(response.body()); - JSONArray messageArray = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - List messages = ParseMessage.parseMessages(messageArray, locale, messageType); - String newAfter = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); - handler.post(() -> fetchMessagesListener.fetchSuccess(messages, newAfter)); - } catch (JSONException e) { - e.printStackTrace(); - handler.post(fetchMessagesListener::fetchFailed); - } + executor.execute(() -> { + try { + JSONObject jsonResponse = new JSONObject(response.body()); + JSONArray messageArray = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); + List messages = ParseMessage.parseMessages(messageArray, locale, messageType); + String newAfter = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); + handler.post(() -> fetchMessagesListener.fetchSuccess(messages, newAfter)); + } catch (JSONException e) { + e.printStackTrace(); + handler.post(fetchMessagesListener::fetchFailed); + } + }); } else { - handler.post(fetchMessagesListener::fetchFailed); + fetchMessagesListener.fetchFailed(); } - } catch (IOException e) { - e.printStackTrace(); - handler.post(fetchMessagesListener::fetchFailed); + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable throwable) { + fetchMessagesListener.fetchFailed(); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchFlairs.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchFlairs.java index 86643c34..f8c52f29 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchFlairs.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchFlairs.java @@ -2,13 +2,13 @@ package ml.docilealligator.infinityforreddit.subreddit; import android.os.Handler; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.WorkerThread; import org.json.JSONArray; import org.json.JSONException; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor; @@ -17,35 +17,40 @@ 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(Executor executor, Handler handler, Retrofit oauthRetrofit, String accessToken, String subredditName, FetchFlairsInSubredditListener fetchFlairsInSubredditListener) { - executor.execute(() -> { - RedditAPI api = oauthRetrofit.create(RedditAPI.class); - - Call flairsCall = api.getFlairs(APIUtils.getOAuthHeader(accessToken), subredditName); - try { - Response response = flairsCall.execute(); - if (response.isSuccessful()) { - List flairs = parseFlairs(response.body()); - if (flairs != null) { - handler.post(() -> fetchFlairsInSubredditListener.fetchSuccessful(flairs)); - } else { - handler.post(fetchFlairsInSubredditListener::fetchFailed); + public static void fetchFlairsInSubreddit(Executor executor, Handler handler, Retrofit oauthRetrofit, + String accessToken, String subredditName, + FetchFlairsInSubredditListener fetchFlairsInSubredditListener) { + oauthRetrofit.create(RedditAPI.class).getFlairs(APIUtils.getOAuthHeader(accessToken), subredditName) + .enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + executor.execute(() -> { + List flairs = parseFlairs(response.body()); + if (flairs != null) { + handler.post(() -> fetchFlairsInSubredditListener.fetchSuccessful(flairs)); + } else { + handler.post(fetchFlairsInSubredditListener::fetchFailed); + } + }); + } else if (response.code() == 403) { + //No flairs + fetchFlairsInSubredditListener.fetchSuccessful(null); + } else { + fetchFlairsInSubredditListener.fetchFailed(); + } } - } else if (response.code() == 403) { - //No flairs - handler.post(() -> fetchFlairsInSubredditListener.fetchSuccessful(null)); - } else { - handler.post(fetchFlairsInSubredditListener::fetchFailed); - } - } catch (IOException e) { - e.printStackTrace(); - handler.post(fetchFlairsInSubredditListener::fetchFailed); - } - }); + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable throwable) { + fetchFlairsInSubredditListener.fetchFailed(); + } + }); } @WorkerThread diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java index f898c1d6..6c1ddb2c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java @@ -2,13 +2,13 @@ package ml.docilealligator.infinityforreddit.user; import android.os.Handler; +import androidx.annotation.NonNull; import androidx.annotation.WorkerThread; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor; @@ -19,6 +19,7 @@ import ml.docilealligator.infinityforreddit.thing.SortType; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.JSONUtils; import retrofit2.Call; +import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; @@ -29,80 +30,93 @@ public class FetchUserData { public static void fetchUserData(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase, Retrofit retrofit, String accessToken, String userName, FetchUserDataListener fetchUserDataListener) { - executor.execute(() -> { - RedditAPI api = retrofit.create(RedditAPI.class); + RedditAPI api = retrofit.create(RedditAPI.class); - Call userInfo; - if (redditDataRoomDatabase == null) { - userInfo = api.getUserData(userName); - } else { - userInfo = api.getUserDataOauth(APIUtils.getOAuthHeader(accessToken), userName); + Call userInfo; + if (redditDataRoomDatabase == null) { + userInfo = api.getUserData(userName); + } else { + userInfo = api.getUserDataOauth(APIUtils.getOAuthHeader(accessToken), userName); + } + + userInfo.enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + executor.execute(() -> { + try { + JSONObject jsonResponse = new JSONObject(response.body()); + UserData userData = parseUserDataBase(jsonResponse, true); + if (redditDataRoomDatabase != null) { + redditDataRoomDatabase.accountDao().updateAccountInfo(userData.getName(), userData.getIconUrl(), userData.getBanner(), userData.getTotalKarma()); + } + if (jsonResponse.getJSONObject(JSONUtils.DATA_KEY).has(JSONUtils.INBOX_COUNT_KEY)) { + int inboxCount = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getInt(JSONUtils.INBOX_COUNT_KEY); + handler.post(() -> fetchUserDataListener.onFetchUserDataSuccess(userData, inboxCount)); + } else { + handler.post(() -> fetchUserDataListener.onFetchUserDataSuccess(userData, -1)); + } + } catch (JSONException e) { + e.printStackTrace(); + handler.post(fetchUserDataListener::onFetchUserDataFailed); + } + }); + } else { + fetchUserDataListener.onFetchUserDataFailed(); + } } - try { - Response response = userInfo.execute(); - if (response.isSuccessful()) { - JSONObject jsonResponse = new JSONObject(response.body()); - UserData userData = parseUserDataBase(jsonResponse, true); - if (redditDataRoomDatabase != null) { - redditDataRoomDatabase.accountDao().updateAccountInfo(userData.getName(), userData.getIconUrl(), userData.getBanner(), userData.getTotalKarma()); - } - if (jsonResponse.getJSONObject(JSONUtils.DATA_KEY).has(JSONUtils.INBOX_COUNT_KEY)) { - int inboxCount = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getInt(JSONUtils.INBOX_COUNT_KEY); - handler.post(() -> fetchUserDataListener.onFetchUserDataSuccess(userData, inboxCount)); - } else { - handler.post(() -> fetchUserDataListener.onFetchUserDataSuccess(userData, -1)); - } - } else { - handler.post(fetchUserDataListener::onFetchUserDataFailed); - } - } catch (IOException | JSONException e) { - e.printStackTrace(); - handler.post(fetchUserDataListener::onFetchUserDataFailed); + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable throwable) { + fetchUserDataListener.onFetchUserDataFailed(); } }); } public static void fetchUserListingData(Executor executor, Handler handler, Retrofit retrofit, String query, String after, SortType.Type sortType, boolean nsfw, FetchUserListingDataListener fetchUserListingDataListener) { - executor.execute(() -> { - RedditAPI api = retrofit.create(RedditAPI.class); + RedditAPI api = retrofit.create(RedditAPI.class); - Call userInfo = api.searchUsers(query, after, sortType, nsfw ? 1 : 0); - String responseString = null; - try { - Response response = userInfo.execute(); - responseString = response.body(); + Call userInfo = api.searchUsers(query, after, sortType, nsfw ? 1 : 0); + final String[] responseString = {null}; + userInfo.enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { - JSONObject jsonResponse = new JSONObject(responseString); - String newAfter = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); - JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - List userDataList = new ArrayList<>(); - for (int i = 0; i < children.length(); i++) { + executor.execute(() -> { try { - UserData userData = parseUserDataBase(children.getJSONObject(i), false); - userDataList.add(userData); + responseString[0] = response.body(); + JSONObject jsonResponse = new JSONObject(responseString[0]); + String newAfter = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); + JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); + List userDataList = new ArrayList<>(); + for (int i = 0; i < children.length(); i++) { + try { + UserData userData = parseUserDataBase(children.getJSONObject(i), false); + userDataList.add(userData); + } catch (JSONException e) { + e.printStackTrace(); + } + } + handler.post(() -> fetchUserListingDataListener.onFetchUserListingDataSuccess(userDataList, newAfter)); } catch (JSONException e) { - e.printStackTrace(); + handler.post(() -> { + if (responseString[0] != null && responseString[0].equals("\"{}\"")) { + fetchUserListingDataListener.onFetchUserListingDataSuccess(new ArrayList<>(), null); + } else { + fetchUserListingDataListener.onFetchUserListingDataFailed(); + } + }); } - } - handler.post(() -> fetchUserListingDataListener.onFetchUserListingDataSuccess(userDataList, newAfter)); + }); } else { - handler.post(fetchUserListingDataListener::onFetchUserListingDataFailed); + fetchUserListingDataListener.onFetchUserListingDataFailed(); } - } catch (JSONException e) { - e.printStackTrace(); - String finalResponseString = responseString; - handler.post(() -> { - if (finalResponseString != null && finalResponseString.equals("\"{}\"")) { - fetchUserListingDataListener.onFetchUserListingDataSuccess(new ArrayList<>(), null); - } else { - fetchUserListingDataListener.onFetchUserListingDataFailed(); - } - }); - } catch (IOException e) { - e.printStackTrace(); - handler.post(fetchUserListingDataListener::onFetchUserListingDataFailed); + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable throwable) { + fetchUserListingDataListener.onFetchUserListingDataFailed(); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserFlairs.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserFlairs.java index 25776379..257a7243 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserFlairs.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserFlairs.java @@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit.user; import android.os.Handler; import android.text.Html; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.WorkerThread; @@ -10,40 +11,45 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import java.io.IOException; import java.util.ArrayList; import java.util.concurrent.Executor; 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 FetchUserFlairs { public static void fetchUserFlairsInSubreddit(Executor executor, Handler handler, Retrofit oauthRetrofit, String accessToken, String subredditName, FetchUserFlairsInSubredditListener fetchUserFlairsInSubredditListener) { - executor.execute(() -> { - RedditAPI api = oauthRetrofit.create(RedditAPI.class); - - try { - Response response = api.getUserFlairs(APIUtils.getOAuthHeader(accessToken), subredditName).execute(); - if (response.isSuccessful()) { - ArrayList userFlairs = parseUserFlairs(response.body()); - if (userFlairs == null) { - handler.post(fetchUserFlairsInSubredditListener::fetchFailed); - } else { - handler.post(() -> fetchUserFlairsInSubredditListener.fetchSuccessful(userFlairs)); + oauthRetrofit.create(RedditAPI.class).getUserFlairs(APIUtils.getOAuthHeader(accessToken), subredditName) + .enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + executor.execute(() -> { + ArrayList userFlairs = parseUserFlairs(response.body()); + if (userFlairs == null) { + handler.post(fetchUserFlairsInSubredditListener::fetchFailed); + } else { + handler.post(() -> fetchUserFlairsInSubredditListener.fetchSuccessful(userFlairs)); + } + }); + } else if (response.code() == 403) { + //No flairs + fetchUserFlairsInSubredditListener.fetchSuccessful(null); + } else { + fetchUserFlairsInSubredditListener.fetchFailed(); + } } - } else if (response.code() == 403) { - //No flairs - handler.post(() -> fetchUserFlairsInSubredditListener.fetchSuccessful(null)); - } else { - handler.post(fetchUserFlairsInSubredditListener::fetchFailed); - } - } catch (IOException e) { - handler.post(fetchUserFlairsInSubredditListener::fetchFailed); - } - }); + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable throwable) { + fetchUserFlairsInSubredditListener.fetchFailed(); + } + }); } @WorkerThread diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/SelectUserFlair.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/SelectUserFlair.java index 6e511256..bbc1be2c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/SelectUserFlair.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/SelectUserFlair.java @@ -9,7 +9,6 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executor; @@ -17,6 +16,8 @@ import java.util.concurrent.Executor; 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; @@ -28,43 +29,53 @@ public class SelectUserFlair { public static void selectUserFlair(Executor executor, Handler handler, Retrofit oauthRetrofit, String accessToken, @Nullable UserFlair userFlair, String subredditName, @NonNull String accountName, SelectUserFlairListener selectUserFlairListener) { - executor.execute(() -> { - Map params = new HashMap<>(); - params.put(APIUtils.API_TYPE_KEY, APIUtils.API_TYPE_JSON); - if (userFlair != null) { - params.put(APIUtils.FLAIR_TEMPLATE_ID_KEY, userFlair.getId()); - params.put(APIUtils.TEXT_KEY, userFlair.getText()); - } - params.put(APIUtils.NAME_KEY, accountName); - try { - Response response = oauthRetrofit.create(RedditAPI.class).selectUserFlair(APIUtils.getOAuthHeader(accessToken), params, subredditName).execute(); + + Map params = new HashMap<>(); + params.put(APIUtils.API_TYPE_KEY, APIUtils.API_TYPE_JSON); + if (userFlair != null) { + params.put(APIUtils.FLAIR_TEMPLATE_ID_KEY, userFlair.getId()); + params.put(APIUtils.TEXT_KEY, userFlair.getText()); + } + params.put(APIUtils.NAME_KEY, accountName); + + oauthRetrofit.create(RedditAPI.class).selectUserFlair(APIUtils.getOAuthHeader(accessToken), params, subredditName).enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { - JSONObject responseObject = new JSONObject(response.body()).getJSONObject(JSONUtils.JSON_KEY); + executor.execute(() -> { + try { + JSONObject responseObject = new JSONObject(response.body()).getJSONObject(JSONUtils.JSON_KEY); - if (responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) { - JSONArray error = responseObject.getJSONArray(JSONUtils.ERRORS_KEY) - .getJSONArray(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() - 1); - if (error.length() != 0) { - String errorString; - if (error.length() >= 2) { - errorString = error.getString(1); + if (responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) { + JSONArray error = responseObject.getJSONArray(JSONUtils.ERRORS_KEY) + .getJSONArray(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() - 1); + if (error.length() != 0) { + String errorString; + if (error.length() >= 2) { + errorString = error.getString(1); + } else { + errorString = error.getString(0); + } + + handler.post(() -> selectUserFlairListener.failed(errorString.substring(0, 1).toUpperCase() + errorString.substring(1))); + } else { + handler.post(selectUserFlairListener::success); + } } else { - errorString = error.getString(0); + handler.post(selectUserFlairListener::success); } - - handler.post(() -> selectUserFlairListener.failed(errorString.substring(0, 1).toUpperCase() + errorString.substring(1))); - } else { - handler.post(selectUserFlairListener::success); + } catch (JSONException e) { + e.printStackTrace(); } - } else { - handler.post(selectUserFlairListener::success); - } + }); } else { - handler.post(() -> selectUserFlairListener.failed(response.message())); + selectUserFlairListener.failed(response.message()); } - } catch (IOException | JSONException e) { - e.printStackTrace(); - handler.post(() -> selectUserFlairListener.failed(e.getMessage())); + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable throwable) { + selectUserFlairListener.failed(throwable.getMessage()); } }); }