Favorite and unfavorite users for the anonymous user.

This commit is contained in:
Alex Ning 2021-09-24 19:16:35 +08:00
parent 20053881f1
commit cfab68ecd6

View File

@ -83,50 +83,60 @@ public class FavoriteThing {
RedditDataRoomDatabase redditDataRoomDatabase, RedditDataRoomDatabase redditDataRoomDatabase,
String accessToken, SubscribedUserData subscribedUserData, String accessToken, SubscribedUserData subscribedUserData,
FavoriteThingListener favoriteThingListener) { FavoriteThingListener favoriteThingListener) {
Map<String, String> params = new HashMap<>(); if (accessToken == null) {
params.put(APIUtils.SR_NAME_KEY, "u_" + subscribedUserData.getName()); InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase,
params.put(APIUtils.MAKE_FAVORITE_KEY, "true"); subscribedUserData, favoriteThingListener::success);
oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { } else {
@Override Map<String, String> params = new HashMap<>();
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { params.put(APIUtils.SR_NAME_KEY, "u_" + subscribedUserData.getName());
if (response.isSuccessful()) { params.put(APIUtils.MAKE_FAVORITE_KEY, "true");
InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() {
subscribedUserData, favoriteThingListener::success); @Override
} else { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase,
subscribedUserData, favoriteThingListener::success);
} else {
favoriteThingListener.failed();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
favoriteThingListener.failed(); favoriteThingListener.failed();
} }
} });
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
favoriteThingListener.failed();
}
});
} }
public static void unfavoriteUser(Executor executor, Handler handler, Retrofit oauthRetrofit, public static void unfavoriteUser(Executor executor, Handler handler, Retrofit oauthRetrofit,
RedditDataRoomDatabase redditDataRoomDatabase, RedditDataRoomDatabase redditDataRoomDatabase,
String accessToken, SubscribedUserData subscribedUserData, String accessToken, SubscribedUserData subscribedUserData,
FavoriteThingListener favoriteThingListener) { FavoriteThingListener favoriteThingListener) {
Map<String, String> params = new HashMap<>(); if (accessToken == null) {
params.put(APIUtils.SR_NAME_KEY, "u_" + subscribedUserData.getName()); InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, subscribedUserData,
params.put(APIUtils.MAKE_FAVORITE_KEY, "false"); favoriteThingListener::success);
oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { } else {
@Override Map<String, String> params = new HashMap<>();
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { params.put(APIUtils.SR_NAME_KEY, "u_" + subscribedUserData.getName());
if (response.isSuccessful()) { params.put(APIUtils.MAKE_FAVORITE_KEY, "false");
InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() {
subscribedUserData, favoriteThingListener::success); @Override
} else { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase,
subscribedUserData, favoriteThingListener::success);
} else {
favoriteThingListener.failed();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
favoriteThingListener.failed(); favoriteThingListener.failed();
} }
} });
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
favoriteThingListener.failed();
}
});
} }
public interface FavoriteThingListener { public interface FavoriteThingListener {