diff options
Diffstat (limited to 'app')
3 files changed, 111 insertions, 53 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index d11fb1c6..5d748bef 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -413,47 +413,79 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele if (userData.isCanBeFollowed()) { subscribeUserChip.setVisibility(View.VISIBLE); subscribeUserChip.setOnClickListener(view -> { - if (mAccessToken == null) { - Toast.makeText(ViewUserDetailActivity.this, R.string.login_first, Toast.LENGTH_SHORT).show(); - return; - } - if (subscriptionReady) { subscriptionReady = false; if (resources.getString(R.string.follow).contentEquals(subscribeUserChip.getText())) { - UserFollowing.followUser(mOauthRetrofit, mRetrofit, mAccessToken, - username, mAccountName, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { - @Override - public void onUserFollowingSuccess() { - subscribeUserChip.setText(R.string.unfollow); - subscribeUserChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor)); - showMessage(R.string.followed, false); - subscriptionReady = true; - } - - @Override - public void onUserFollowingFail() { - showMessage(R.string.follow_failed, false); - subscriptionReady = true; - } - }); + if (mAccessToken == null) { + UserFollowing.anonymousFollowUser(mExecutor, new Handler(), mRetrofit, + username, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { + @Override + public void onUserFollowingSuccess() { + subscribeUserChip.setText(R.string.unfollow); + subscribeUserChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor)); + showMessage(R.string.followed, false); + subscriptionReady = true; + } + + @Override + public void onUserFollowingFail() { + showMessage(R.string.follow_failed, false); + subscriptionReady = true; + } + }); + } else { + UserFollowing.followUser(mOauthRetrofit, mRetrofit, mAccessToken, + username, mAccountName, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { + @Override + public void onUserFollowingSuccess() { + subscribeUserChip.setText(R.string.unfollow); + subscribeUserChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor)); + showMessage(R.string.followed, false); + subscriptionReady = true; + } + + @Override + public void onUserFollowingFail() { + showMessage(R.string.follow_failed, false); + subscriptionReady = true; + } + }); + } } else { - UserFollowing.unfollowUser(mOauthRetrofit, mRetrofit, mAccessToken, - username, mAccountName, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { - @Override - public void onUserFollowingSuccess() { - subscribeUserChip.setText(R.string.follow); - subscribeUserChip.setChipBackgroundColor(ColorStateList.valueOf(unsubscribedColor)); - showMessage(R.string.unfollowed, false); - subscriptionReady = true; - } - - @Override - public void onUserFollowingFail() { - showMessage(R.string.unfollow_failed, false); - subscriptionReady = true; - } - }); + if (mAccessToken == null) { + UserFollowing.anonymousUnfollowUser(mExecutor, new Handler(), username, + mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { + @Override + public void onUserFollowingSuccess() { + subscribeUserChip.setText(R.string.follow); + subscribeUserChip.setChipBackgroundColor(ColorStateList.valueOf(unsubscribedColor)); + showMessage(R.string.unfollowed, false); + subscriptionReady = true; + } + + @Override + public void onUserFollowingFail() { + //Will not be called + } + }); + } else { + UserFollowing.unfollowUser(mOauthRetrofit, mRetrofit, mAccessToken, + username, mAccountName, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { + @Override + public void onUserFollowingSuccess() { + subscribeUserChip.setText(R.string.follow); + subscribeUserChip.setChipBackgroundColor(ColorStateList.valueOf(unsubscribedColor)); + showMessage(R.string.unfollowed, false); + subscriptionReady = true; + } + + @Override + public void onUserFollowingFail() { + showMessage(R.string.unfollow_failed, false); + subscriptionReady = true; + } + }); + } } } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java index 242edf18..1b4f366e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java @@ -15,7 +15,6 @@ import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubred import ml.docilealligator.infinityforreddit.utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; -import retrofit2.Response; import retrofit2.Retrofit; public class SubredditSubscription { @@ -31,25 +30,15 @@ public class SubredditSubscription { RedditDataRoomDatabase redditDataRoomDatabase, String subredditName, SubredditSubscriptionListener subredditSubscriptionListener) { - retrofit.create(RedditAPI.class).getSubredditData(subredditName).enqueue(new Callback<String>() { + FetchSubredditData.fetchSubredditData(retrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() { @Override - public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { - FetchSubredditData.fetchSubredditData(retrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() { - @Override - public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { - insertSubscription(executor, handler, redditDataRoomDatabase, - subredditData, "-", subredditSubscriptionListener); - } - - @Override - public void onFetchSubredditDataFail(boolean isQuarantined) { - - } - }); + public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { + insertSubscription(executor, handler, redditDataRoomDatabase, + subredditData, "-", subredditSubscriptionListener); } @Override - public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + public void onFetchSubredditDataFail(boolean isQuarantined) { subredditSubscriptionListener.onSubredditSubscriptionFail(); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java index 0dd5157a..1bde2aa3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java @@ -1,13 +1,16 @@ package ml.docilealligator.infinityforreddit.user; import android.os.AsyncTask; +import android.os.Handler; import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.subscribeduser.SubscribedUserDao; import ml.docilealligator.infinityforreddit.subscribeduser.SubscribedUserData; @@ -25,6 +28,30 @@ public class UserFollowing { redditDataRoomDatabase.subscribedUserDao(), userFollowingListener); } + public static void anonymousFollowUser(Executor executor, Handler handler, Retrofit retrofit, String username, + RedditDataRoomDatabase redditDataRoomDatabase, + UserFollowingListener userFollowingListener) { + FetchUserData.fetchUserData(retrofit, username, new FetchUserData.FetchUserDataListener() { + @Override + public void onFetchUserDataSuccess(UserData userData, int inboxCount) { + executor.execute(() -> { + if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { + redditDataRoomDatabase.accountDao().insert(Account.getAnonymousAccount()); + } + redditDataRoomDatabase.subscribedUserDao().insert(new SubscribedUserData(userData.getName(), userData.getIconUrl(), + "-", false)); + + handler.post(userFollowingListener::onUserFollowingSuccess); + }); + } + + @Override + public void onFetchUserDataFailed() { + userFollowingListener.onUserFollowingFail(); + } + }); + } + public static void unfollowUser(Retrofit oauthRetrofit, Retrofit retrofit, String accessToken, String username, String accountName, RedditDataRoomDatabase redditDataRoomDatabase, @@ -33,6 +60,16 @@ public class UserFollowing { redditDataRoomDatabase.subscribedUserDao(), userFollowingListener); } + public static void anonymousUnfollowUser(Executor executor, Handler handler, String username, + RedditDataRoomDatabase redditDataRoomDatabase, + UserFollowingListener userFollowingListener) { + executor.execute(() -> { + redditDataRoomDatabase.subscribedUserDao().deleteSubscribedUser(username, "-"); + + handler.post(userFollowingListener::onUserFollowingSuccess); + }); + } + private static void userFollowing(Retrofit oauthRetrofit, Retrofit retrofit, String accessToken, String username, String accountName, String action, SubscribedUserDao subscribedUserDao, UserFollowingListener userFollowingListener) { |