mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-30 03:24:44 +01:00
Anonymous user subscription.
This commit is contained in:
parent
643d9395a4
commit
20053881f1
@ -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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
});
|
||||
@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;
|
||||
}
|
||||
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() {
|
||||
showMessage(R.string.unfollow_failed, 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -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();
|
||||
}
|
||||
});
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user