Anonymous user subscription.

This commit is contained in:
Alex Ning 2021-09-23 20:54:54 +08:00
parent 643d9395a4
commit 20053881f1
3 changed files with 109 additions and 51 deletions

View File

@ -413,14 +413,27 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
if (userData.isCanBeFollowed()) { if (userData.isCanBeFollowed()) {
subscribeUserChip.setVisibility(View.VISIBLE); subscribeUserChip.setVisibility(View.VISIBLE);
subscribeUserChip.setOnClickListener(view -> { subscribeUserChip.setOnClickListener(view -> {
if (mAccessToken == null) {
Toast.makeText(ViewUserDetailActivity.this, R.string.login_first, Toast.LENGTH_SHORT).show();
return;
}
if (subscriptionReady) { if (subscriptionReady) {
subscriptionReady = false; subscriptionReady = false;
if (resources.getString(R.string.follow).contentEquals(subscribeUserChip.getText())) { if (resources.getString(R.string.follow).contentEquals(subscribeUserChip.getText())) {
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, UserFollowing.followUser(mOauthRetrofit, mRetrofit, mAccessToken,
username, mAccountName, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { username, mAccountName, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() {
@Override @Override
@ -437,6 +450,24 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
subscriptionReady = true; subscriptionReady = true;
} }
}); });
}
} else {
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 { } else {
UserFollowing.unfollowUser(mOauthRetrofit, mRetrofit, mAccessToken, UserFollowing.unfollowUser(mOauthRetrofit, mRetrofit, mAccessToken,
username, mAccountName, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { username, mAccountName, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() {
@ -456,6 +487,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
}); });
} }
} }
}
}); });
CheckIsFollowingUser.checkIsFollowingUser(mExecutor, new Handler(), mRedditDataRoomDatabase, CheckIsFollowingUser.checkIsFollowingUser(mExecutor, new Handler(), mRedditDataRoomDatabase,

View File

@ -15,7 +15,6 @@ import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubred
import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.APIUtils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit; import retrofit2.Retrofit;
public class SubredditSubscription { public class SubredditSubscription {
@ -31,9 +30,6 @@ public class SubredditSubscription {
RedditDataRoomDatabase redditDataRoomDatabase, RedditDataRoomDatabase redditDataRoomDatabase,
String subredditName, String subredditName,
SubredditSubscriptionListener subredditSubscriptionListener) { SubredditSubscriptionListener subredditSubscriptionListener) {
retrofit.create(RedditAPI.class).getSubredditData(subredditName).enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
FetchSubredditData.fetchSubredditData(retrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() { FetchSubredditData.fetchSubredditData(retrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() {
@Override @Override
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {
@ -43,13 +39,6 @@ public class SubredditSubscription {
@Override @Override
public void onFetchSubredditDataFail(boolean isQuarantined) { public void onFetchSubredditDataFail(boolean isQuarantined) {
}
});
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
subredditSubscriptionListener.onSubredditSubscriptionFail(); subredditSubscriptionListener.onSubredditSubscriptionFail();
} }
}); });

View File

@ -1,13 +1,16 @@
package ml.docilealligator.infinityforreddit.user; package ml.docilealligator.infinityforreddit.user;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Handler;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.subscribeduser.SubscribedUserDao; import ml.docilealligator.infinityforreddit.subscribeduser.SubscribedUserDao;
import ml.docilealligator.infinityforreddit.subscribeduser.SubscribedUserData; import ml.docilealligator.infinityforreddit.subscribeduser.SubscribedUserData;
@ -25,6 +28,30 @@ public class UserFollowing {
redditDataRoomDatabase.subscribedUserDao(), userFollowingListener); 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, public static void unfollowUser(Retrofit oauthRetrofit, Retrofit retrofit,
String accessToken, String username, String accountName, String accessToken, String username, String accountName,
RedditDataRoomDatabase redditDataRoomDatabase, RedditDataRoomDatabase redditDataRoomDatabase,
@ -33,6 +60,16 @@ public class UserFollowing {
redditDataRoomDatabase.subscribedUserDao(), userFollowingListener); 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, private static void userFollowing(Retrofit oauthRetrofit, Retrofit retrofit, String accessToken,
String username, String accountName, String action, SubscribedUserDao subscribedUserDao, String username, String accountName, String action, SubscribedUserDao subscribedUserDao,
UserFollowingListener userFollowingListener) { UserFollowingListener userFollowingListener) {