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,47 +413,79 @@ 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())) {
UserFollowing.followUser(mOauthRetrofit, mRetrofit, mAccessToken, if (mAccessToken == null) {
username, mAccountName, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { UserFollowing.anonymousFollowUser(mExecutor, new Handler(), mRetrofit,
@Override username, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() {
public void onUserFollowingSuccess() { @Override
subscribeUserChip.setText(R.string.unfollow); public void onUserFollowingSuccess() {
subscribeUserChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor)); subscribeUserChip.setText(R.string.unfollow);
showMessage(R.string.followed, false); subscribeUserChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor));
subscriptionReady = true; showMessage(R.string.followed, false);
} subscriptionReady = true;
}
@Override @Override
public void onUserFollowingFail() { public void onUserFollowingFail() {
showMessage(R.string.follow_failed, false); showMessage(R.string.follow_failed, false);
subscriptionReady = true; 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 { } else {
UserFollowing.unfollowUser(mOauthRetrofit, mRetrofit, mAccessToken, if (mAccessToken == null) {
username, mAccountName, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { UserFollowing.anonymousUnfollowUser(mExecutor, new Handler(), username,
@Override mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() {
public void onUserFollowingSuccess() { @Override
subscribeUserChip.setText(R.string.follow); public void onUserFollowingSuccess() {
subscribeUserChip.setChipBackgroundColor(ColorStateList.valueOf(unsubscribedColor)); subscribeUserChip.setText(R.string.follow);
showMessage(R.string.unfollowed, false); subscribeUserChip.setChipBackgroundColor(ColorStateList.valueOf(unsubscribedColor));
subscriptionReady = true; showMessage(R.string.unfollowed, false);
} subscriptionReady = true;
}
@Override @Override
public void onUserFollowingFail() { public void onUserFollowingFail() {
showMessage(R.string.unfollow_failed, false); //Will not be called
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;
}
});
}
} }
} }
}); });

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,25 +30,15 @@ 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>() { FetchSubredditData.fetchSubredditData(retrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {
FetchSubredditData.fetchSubredditData(retrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() { insertSubscription(executor, handler, redditDataRoomDatabase,
@Override subredditData, "-", subredditSubscriptionListener);
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {
insertSubscription(executor, handler, redditDataRoomDatabase,
subredditData, "-", subredditSubscriptionListener);
}
@Override
public void onFetchSubredditDataFail(boolean isQuarantined) {
}
});
} }
@Override @Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { public void onFetchSubredditDataFail(boolean isQuarantined) {
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) {