From cfab68ecd664649dec39e34116fc0a25c088b307 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 24 Sep 2021 19:16:35 +0800 Subject: Favorite and unfavorite users for the anonymous user. --- .../infinityforreddit/FavoriteThing.java | 78 ++++++++++++---------- 1 file changed, 44 insertions(+), 34 deletions(-) (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java index 29aace70..81ae4fa3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java @@ -83,50 +83,60 @@ public class FavoriteThing { RedditDataRoomDatabase redditDataRoomDatabase, String accessToken, SubscribedUserData subscribedUserData, FavoriteThingListener favoriteThingListener) { - Map params = new HashMap<>(); - params.put(APIUtils.SR_NAME_KEY, "u_" + subscribedUserData.getName()); - params.put(APIUtils.MAKE_FAVORITE_KEY, "true"); - oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, - subscribedUserData, favoriteThingListener::success); - } else { - favoriteThingListener.failed(); + if (accessToken == null) { + InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, + subscribedUserData, favoriteThingListener::success); + } else { + Map params = new HashMap<>(); + params.put(APIUtils.SR_NAME_KEY, "u_" + subscribedUserData.getName()); + params.put(APIUtils.MAKE_FAVORITE_KEY, "true"); + oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, + subscribedUserData, favoriteThingListener::success); + } else { + favoriteThingListener.failed(); + } } - } - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - favoriteThingListener.failed(); - } - }); + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + favoriteThingListener.failed(); + } + }); + } } public static void unfavoriteUser(Executor executor, Handler handler, Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase, String accessToken, SubscribedUserData subscribedUserData, FavoriteThingListener favoriteThingListener) { - Map params = new HashMap<>(); - params.put(APIUtils.SR_NAME_KEY, "u_" + subscribedUserData.getName()); - params.put(APIUtils.MAKE_FAVORITE_KEY, "false"); - oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, - subscribedUserData, favoriteThingListener::success); - } else { - favoriteThingListener.failed(); + if (accessToken == null) { + InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, subscribedUserData, + favoriteThingListener::success); + } else { + Map params = new HashMap<>(); + params.put(APIUtils.SR_NAME_KEY, "u_" + subscribedUserData.getName()); + params.put(APIUtils.MAKE_FAVORITE_KEY, "false"); + oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, + subscribedUserData, favoriteThingListener::success); + } else { + favoriteThingListener.failed(); + } } - } - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - favoriteThingListener.failed(); - } - }); + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + favoriteThingListener.failed(); + } + }); + } } public interface FavoriteThingListener { -- cgit v1.2.3