Anonymous subreddit subscription is available.

This commit is contained in:
Alex Ning 2021-03-04 22:45:41 +08:00
parent e8cb0d329c
commit 2ef72bc798
4 changed files with 166 additions and 104 deletions

View File

@ -817,48 +817,86 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
subscribeSubredditChip.setOnClickListener(view -> { subscribeSubredditChip.setOnClickListener(view -> {
if (mAccessToken == null) { if (mAccessToken == null) {
Toast.makeText(ViewSubredditDetailActivity.this, R.string.login_first, Toast.LENGTH_SHORT).show(); if (subscriptionReady) {
return; subscriptionReady = false;
} if (getResources().getString(R.string.subscribe).contentEquals(subscribeSubredditChip.getText())) {
SubredditSubscription.anonymousSubscribeToSubreddit(mExecutor, new Handler(),
mRetrofit, mRedditDataRoomDatabase, subredditName,
new SubredditSubscription.SubredditSubscriptionListener() {
@Override
public void onSubredditSubscriptionSuccess() {
subscribeSubredditChip.setText(R.string.unsubscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor));
makeSnackbar(R.string.subscribed, false);
subscriptionReady = true;
}
if (subscriptionReady) { @Override
subscriptionReady = false; public void onSubredditSubscriptionFail() {
if (getResources().getString(R.string.subscribe).contentEquals(subscribeSubredditChip.getText())) { makeSnackbar(R.string.subscribe_failed, false);
SubredditSubscription.subscribeToSubreddit(mOauthRetrofit, mRetrofit, mAccessToken, subscriptionReady = true;
subredditName, mAccountName, mRedditDataRoomDatabase, }
new SubredditSubscription.SubredditSubscriptionListener() { });
@Override } else {
public void onSubredditSubscriptionSuccess() { SubredditSubscription.anonymousUnsubscribeToSubreddit(mExecutor, new Handler(),
subscribeSubredditChip.setText(R.string.unsubscribe); mRedditDataRoomDatabase, subredditName,
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor)); new SubredditSubscription.SubredditSubscriptionListener() {
makeSnackbar(R.string.subscribed, false); @Override
subscriptionReady = true; public void onSubredditSubscriptionSuccess() {
} subscribeSubredditChip.setText(R.string.subscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(unsubscribedColor));
makeSnackbar(R.string.unsubscribed, false);
subscriptionReady = true;
}
@Override @Override
public void onSubredditSubscriptionFail() { public void onSubredditSubscriptionFail() {
makeSnackbar(R.string.subscribe_failed, false); makeSnackbar(R.string.unsubscribe_failed, false);
subscriptionReady = true; subscriptionReady = true;
} }
}); });
} else { }
SubredditSubscription.unsubscribeToSubreddit(mOauthRetrofit, mAccessToken, }
subredditName, mAccountName, mRedditDataRoomDatabase, } else {
new SubredditSubscription.SubredditSubscriptionListener() { if (subscriptionReady) {
@Override subscriptionReady = false;
public void onSubredditSubscriptionSuccess() { if (getResources().getString(R.string.subscribe).contentEquals(subscribeSubredditChip.getText())) {
subscribeSubredditChip.setText(R.string.subscribe); SubredditSubscription.subscribeToSubreddit(mExecutor, new Handler(), mOauthRetrofit,
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(unsubscribedColor)); mRetrofit, mAccessToken, subredditName, mAccountName, mRedditDataRoomDatabase,
makeSnackbar(R.string.unsubscribed, false); new SubredditSubscription.SubredditSubscriptionListener() {
subscriptionReady = true; @Override
} public void onSubredditSubscriptionSuccess() {
subscribeSubredditChip.setText(R.string.unsubscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor));
makeSnackbar(R.string.subscribed, false);
subscriptionReady = true;
}
@Override @Override
public void onSubredditSubscriptionFail() { public void onSubredditSubscriptionFail() {
makeSnackbar(R.string.unsubscribe_failed, false); makeSnackbar(R.string.subscribe_failed, false);
subscriptionReady = true; subscriptionReady = true;
} }
}); });
} else {
SubredditSubscription.unsubscribeToSubreddit(mExecutor, new Handler(), mOauthRetrofit,
mAccessToken, subredditName, mAccountName, mRedditDataRoomDatabase,
new SubredditSubscription.SubredditSubscriptionListener() {
@Override
public void onSubredditSubscriptionSuccess() {
subscribeSubredditChip.setText(R.string.subscribe);
subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(unsubscribedColor));
makeSnackbar(R.string.unsubscribed, false);
subscriptionReady = true;
}
@Override
public void onSubredditSubscriptionFail() {
makeSnackbar(R.string.unsubscribe_failed, false);
subscriptionReady = true;
}
});
}
} }
} }
}); });

View File

@ -155,8 +155,9 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
public void isNotSubscribed() { public void isNotSubscribed() {
((DataViewHolder) holder).subscribeButton.setVisibility(View.VISIBLE); ((DataViewHolder) holder).subscribeButton.setVisibility(View.VISIBLE);
((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> { ((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> {
SubredditSubscription.subscribeToSubreddit(oauthRetrofit, retrofit, SubredditSubscription.subscribeToSubreddit(executor, new Handler(),
accessToken, subredditData.getName(), accountName, redditDataRoomDatabase, oauthRetrofit, retrofit, accessToken, subredditData.getName(),
accountName, redditDataRoomDatabase,
new SubredditSubscription.SubredditSubscriptionListener() { new SubredditSubscription.SubredditSubscriptionListener() {
@Override @Override
public void onSubredditSubscriptionSuccess() { public void onSubredditSubscriptionSuccess() {

View File

@ -1,39 +1,77 @@
package ml.docilealligator.infinityforreddit.subreddit; package ml.docilealligator.infinityforreddit.subreddit;
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.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData;
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 {
public static void subscribeToSubreddit(Retrofit oauthRetrofit, Retrofit retrofit, public static void subscribeToSubreddit(Executor executor, Handler handler, Retrofit oauthRetrofit,
String accessToken, String subredditName, String accountName, Retrofit retrofit, String accessToken, String subredditName,
RedditDataRoomDatabase redditDataRoomDatabase, String accountName, RedditDataRoomDatabase redditDataRoomDatabase,
SubredditSubscriptionListener subredditSubscriptionListener) { SubredditSubscriptionListener subredditSubscriptionListener) {
subredditSubscription(oauthRetrofit, retrofit, accessToken, subredditName, accountName, "sub", subredditSubscription(executor, handler, oauthRetrofit, retrofit, accessToken, subredditName,
redditDataRoomDatabase, subredditSubscriptionListener); accountName, "sub", redditDataRoomDatabase, subredditSubscriptionListener);
} }
public static void unsubscribeToSubreddit(Retrofit oauthRetrofit, String accessToken, public static void anonymousSubscribeToSubreddit(Executor executor, Handler handler, Retrofit retrofit,
String subredditName, String accountName, RedditDataRoomDatabase redditDataRoomDatabase,
String subredditName,
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() {
@Override
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {
insertSubscription(executor, handler, redditDataRoomDatabase,
subredditData, "-", subredditSubscriptionListener);
}
@Override
public void onFetchSubredditDataFail(boolean isQuarantined) {
}
});
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
subredditSubscriptionListener.onSubredditSubscriptionFail();
}
});
}
public static void unsubscribeToSubreddit(Executor executor, Handler handler, Retrofit oauthRetrofit,
String accessToken, String subredditName, String accountName,
RedditDataRoomDatabase redditDataRoomDatabase, RedditDataRoomDatabase redditDataRoomDatabase,
SubredditSubscriptionListener subredditSubscriptionListener) { SubredditSubscriptionListener subredditSubscriptionListener) {
subredditSubscription(oauthRetrofit, null, accessToken, subredditName, accountName, "unsub", subredditSubscription(executor, handler, oauthRetrofit, null, accessToken, subredditName,
redditDataRoomDatabase, subredditSubscriptionListener); accountName, "unsub", redditDataRoomDatabase, subredditSubscriptionListener);
} }
private static void subredditSubscription(Retrofit oauthRetrofit, Retrofit retrofit, String accessToken, public static void anonymousUnsubscribeToSubreddit(Executor executor, Handler handler,
String subredditName, String accountName, String action, RedditDataRoomDatabase redditDataRoomDatabase,
String subredditName,
SubredditSubscriptionListener subredditSubscriptionListener) {
removeSubscription(executor, handler, redditDataRoomDatabase, subredditName, "-", subredditSubscriptionListener);
}
private static void subredditSubscription(Executor executor, Handler handler, Retrofit oauthRetrofit,
Retrofit retrofit, String accessToken, String subredditName,
String accountName, String action,
RedditDataRoomDatabase redditDataRoomDatabase, RedditDataRoomDatabase redditDataRoomDatabase,
SubredditSubscriptionListener subredditSubscriptionListener) { SubredditSubscriptionListener subredditSubscriptionListener) {
RedditAPI api = oauthRetrofit.create(RedditAPI.class); RedditAPI api = oauthRetrofit.create(RedditAPI.class);
@ -51,8 +89,8 @@ public class SubredditSubscription {
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) {
new UpdateSubscriptionAsyncTask(redditDataRoomDatabase, insertSubscription(executor, handler, redditDataRoomDatabase,
subredditData, accountName, true).execute(); subredditData, accountName, subredditSubscriptionListener);
} }
@Override @Override
@ -61,9 +99,9 @@ public class SubredditSubscription {
} }
}); });
} else { } else {
new UpdateSubscriptionAsyncTask(redditDataRoomDatabase, subredditName, accountName, false).execute(); removeSubscription(executor, handler, redditDataRoomDatabase, subredditName,
accountName, subredditSubscriptionListener);
} }
subredditSubscriptionListener.onSubredditSubscriptionSuccess();
} else { } else {
subredditSubscriptionListener.onSubredditSubscriptionFail(); subredditSubscriptionListener.onSubredditSubscriptionFail();
} }
@ -82,39 +120,25 @@ public class SubredditSubscription {
void onSubredditSubscriptionFail(); void onSubredditSubscriptionFail();
} }
private static class UpdateSubscriptionAsyncTask extends AsyncTask<Void, Void, Void> { private static void insertSubscription(Executor executor, Handler handler,
RedditDataRoomDatabase redditDataRoomDatabase,
private RedditDataRoomDatabase redditDataRoomDatabase; SubredditData subredditData, String accountName,
private String subredditName; SubredditSubscriptionListener subredditSubscriptionListener) {
private String accountName; executor.execute(() -> {
private SubscribedSubredditData subscribedSubredditData; SubscribedSubredditData subscribedSubredditData = new SubscribedSubredditData(subredditData.getId(), subredditData.getName(),
private boolean isSubscribing;
UpdateSubscriptionAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String subredditName,
String accountName, boolean isSubscribing) {
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.subredditName = subredditName;
this.accountName = accountName;
this.isSubscribing = isSubscribing;
}
UpdateSubscriptionAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, SubredditData subredditData,
String accountName, boolean isSubscribing) {
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.subscribedSubredditData = new SubscribedSubredditData(subredditData.getId(), subredditData.getName(),
subredditData.getIconUrl(), accountName, false); subredditData.getIconUrl(), accountName, false);
this.accountName = accountName; redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData);
this.isSubscribing = isSubscribing; handler.post(subredditSubscriptionListener::onSubredditSubscriptionSuccess);
} });
}
@Override private static void removeSubscription(Executor executor, Handler handler,
protected Void doInBackground(Void... voids) { RedditDataRoomDatabase redditDataRoomDatabase,
if (isSubscribing) { String subredditName, String accountName,
redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData); SubredditSubscriptionListener subredditSubscriptionListener) {
} else { executor.execute(() -> {
redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName); redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName);
} handler.post(subredditSubscriptionListener::onSubredditSubscriptionSuccess);
return null; });
}
} }
} }

View File

@ -1,23 +1,9 @@
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:application="ml.docilealligator.infinityforreddit.fragments.PostFragment"> tools:application="ml.docilealligator.infinityforreddit.fragments.PostFragment">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout_post_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ml.docilealligator.infinityforreddit.customviews.CustomToroContainer
android:id="@+id/recycler_view_post_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<LinearLayout <LinearLayout
android:id="@+id/fetch_post_info_linear_layout_post_fragment" android:id="@+id/fetch_post_info_linear_layout_post_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -44,4 +30,17 @@
</LinearLayout> </LinearLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout_post_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ml.docilealligator.infinityforreddit.customviews.CustomToroContainer
android:id="@+id/recycler_view_post_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>