mirror of
https://github.com/Docile-Alligator/Infinity-For-Reddit.git
synced 2026-02-28 15:55:54 +00:00
Use Retrofit's enqueue() instead of execute() in some places.
This commit is contained in:
@ -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<String> 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<String> call, @NonNull Response<String> 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<String> call, @NonNull Throwable throwable) {
|
||||
fetchMyInfoListener.onFetchMyInfoFailed(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -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<String> 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<String> call, @NonNull Response<String> response) {
|
||||
if (response.isSuccessful()) {
|
||||
try {
|
||||
JSONObject jsonResponse = new JSONObject(response.body());
|
||||
JSONArray messageArray = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
||||
List<Message> 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<Message> 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<String> call, @NonNull Throwable throwable) {
|
||||
fetchMessagesListener.fetchFailed();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -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<String> flairsCall = api.getFlairs(APIUtils.getOAuthHeader(accessToken), subredditName);
|
||||
try {
|
||||
Response<String> response = flairsCall.execute();
|
||||
if (response.isSuccessful()) {
|
||||
List<Flair> 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<String> call, @NonNull Response<String> response) {
|
||||
if (response.isSuccessful()) {
|
||||
executor.execute(() -> {
|
||||
List<Flair> 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<String> call, @NonNull Throwable throwable) {
|
||||
fetchFlairsInSubredditListener.fetchFailed();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
|
||||
@ -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<String> userInfo;
|
||||
if (redditDataRoomDatabase == null) {
|
||||
userInfo = api.getUserData(userName);
|
||||
} else {
|
||||
userInfo = api.getUserDataOauth(APIUtils.getOAuthHeader(accessToken), userName);
|
||||
Call<String> 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<String> call, @NonNull Response<String> 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<String> 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<String> 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<String> userInfo = api.searchUsers(query, after, sortType, nsfw ? 1 : 0);
|
||||
String responseString = null;
|
||||
try {
|
||||
Response<String> response = userInfo.execute();
|
||||
responseString = response.body();
|
||||
Call<String> userInfo = api.searchUsers(query, after, sortType, nsfw ? 1 : 0);
|
||||
final String[] responseString = {null};
|
||||
userInfo.enqueue(new Callback<>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> 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<UserData> 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<UserData> 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<String> call, @NonNull Throwable throwable) {
|
||||
fetchUserListingDataListener.onFetchUserListingDataFailed();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -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<String> response = api.getUserFlairs(APIUtils.getOAuthHeader(accessToken), subredditName).execute();
|
||||
if (response.isSuccessful()) {
|
||||
ArrayList<UserFlair> 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<String> call, @NonNull Response<String> response) {
|
||||
if (response.isSuccessful()) {
|
||||
executor.execute(() -> {
|
||||
ArrayList<UserFlair> 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<String> call, @NonNull Throwable throwable) {
|
||||
fetchUserFlairsInSubredditListener.fetchFailed();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
|
||||
@ -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<String, String> 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<String> response = oauthRetrofit.create(RedditAPI.class).selectUserFlair(APIUtils.getOAuthHeader(accessToken), params, subredditName).execute();
|
||||
|
||||
Map<String, String> 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<String> call, @NonNull Response<String> 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<String> call, @NonNull Throwable throwable) {
|
||||
selectUserFlairListener.failed(throwable.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user