aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2021-09-23 12:54:54 +0000
committerAlex Ning <chineseperson5@gmail.com>2021-09-23 12:54:54 +0000
commit20053881f1ebde6ac5910a39b61f807fd8099058 (patch)
tree9712fca330f58d8fe7f17046f4251e62fe73a0b0 /app/src/main/java
parent643d9395a48145011bedc041c6a7af18429bad9e (diff)
downloadinfinity-for-reddit-20053881f1ebde6ac5910a39b61f807fd8099058.tar
infinity-for-reddit-20053881f1ebde6ac5910a39b61f807fd8099058.tar.gz
infinity-for-reddit-20053881f1ebde6ac5910a39b61f807fd8099058.tar.bz2
infinity-for-reddit-20053881f1ebde6ac5910a39b61f807fd8099058.tar.lz
infinity-for-reddit-20053881f1ebde6ac5910a39b61f807fd8099058.tar.xz
infinity-for-reddit-20053881f1ebde6ac5910a39b61f807fd8099058.tar.zst
infinity-for-reddit-20053881f1ebde6ac5910a39b61f807fd8099058.zip
Anonymous user subscription.
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java106
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java21
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java37
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) {