aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java122
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java5
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java124
3 files changed, 157 insertions, 94 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java
index e076a293..15c0071d 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java
@@ -817,48 +817,86 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
subscribeSubredditChip.setOnClickListener(view -> {
if (mAccessToken == null) {
- Toast.makeText(ViewSubredditDetailActivity.this, R.string.login_first, Toast.LENGTH_SHORT).show();
- return;
- }
-
- if (subscriptionReady) {
- subscriptionReady = false;
- if (getResources().getString(R.string.subscribe).contentEquals(subscribeSubredditChip.getText())) {
- SubredditSubscription.subscribeToSubreddit(mOauthRetrofit, mRetrofit, mAccessToken,
- subredditName, mAccountName, mRedditDataRoomDatabase,
- new SubredditSubscription.SubredditSubscriptionListener() {
- @Override
- public void onSubredditSubscriptionSuccess() {
- subscribeSubredditChip.setText(R.string.unsubscribe);
- subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor));
- makeSnackbar(R.string.subscribed, false);
- subscriptionReady = true;
- }
-
- @Override
- public void onSubredditSubscriptionFail() {
- makeSnackbar(R.string.subscribe_failed, false);
- subscriptionReady = true;
- }
- });
- } else {
- SubredditSubscription.unsubscribeToSubreddit(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;
- }
- });
+ if (subscriptionReady) {
+ 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;
+ }
+
+ @Override
+ public void onSubredditSubscriptionFail() {
+ makeSnackbar(R.string.subscribe_failed, false);
+ subscriptionReady = true;
+ }
+ });
+ } else {
+ SubredditSubscription.anonymousUnsubscribeToSubreddit(mExecutor, new Handler(),
+ mRedditDataRoomDatabase, subredditName,
+ 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;
+ }
+ });
+ }
+ }
+ } else {
+ if (subscriptionReady) {
+ subscriptionReady = false;
+ if (getResources().getString(R.string.subscribe).contentEquals(subscribeSubredditChip.getText())) {
+ SubredditSubscription.subscribeToSubreddit(mExecutor, new Handler(), mOauthRetrofit,
+ mRetrofit, mAccessToken, subredditName, mAccountName, mRedditDataRoomDatabase,
+ new SubredditSubscription.SubredditSubscriptionListener() {
+ @Override
+ public void onSubredditSubscriptionSuccess() {
+ subscribeSubredditChip.setText(R.string.unsubscribe);
+ subscribeSubredditChip.setChipBackgroundColor(ColorStateList.valueOf(subscribedColor));
+ makeSnackbar(R.string.subscribed, false);
+ subscriptionReady = true;
+ }
+
+ @Override
+ public void onSubredditSubscriptionFail() {
+ makeSnackbar(R.string.subscribe_failed, false);
+ 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;
+ }
+ });
+ }
}
}
});
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java
index 69fd6091..9e515c8b 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java
@@ -155,8 +155,9 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
public void isNotSubscribed() {
((DataViewHolder) holder).subscribeButton.setVisibility(View.VISIBLE);
((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> {
- SubredditSubscription.subscribeToSubreddit(oauthRetrofit, retrofit,
- accessToken, subredditData.getName(), accountName, redditDataRoomDatabase,
+ SubredditSubscription.subscribeToSubreddit(executor, new Handler(),
+ oauthRetrofit, retrofit, accessToken, subredditData.getName(),
+ accountName, redditDataRoomDatabase,
new SubredditSubscription.SubredditSubscriptionListener() {
@Override
public void onSubredditSubscriptionSuccess() {
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 31615903..eab9957d 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java
@@ -1,39 +1,77 @@
package ml.docilealligator.infinityforreddit.subreddit;
-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.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
+import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import retrofit2.Call;
import retrofit2.Callback;
+import retrofit2.Response;
import retrofit2.Retrofit;
public class SubredditSubscription {
- public static void subscribeToSubreddit(Retrofit oauthRetrofit, Retrofit retrofit,
- String accessToken, String subredditName, String accountName,
- RedditDataRoomDatabase redditDataRoomDatabase,
+ public static void subscribeToSubreddit(Executor executor, Handler handler, Retrofit oauthRetrofit,
+ Retrofit retrofit, String accessToken, String subredditName,
+ String accountName, RedditDataRoomDatabase redditDataRoomDatabase,
SubredditSubscriptionListener subredditSubscriptionListener) {
- subredditSubscription(oauthRetrofit, retrofit, accessToken, subredditName, accountName, "sub",
- redditDataRoomDatabase, subredditSubscriptionListener);
+ subredditSubscription(executor, handler, oauthRetrofit, retrofit, accessToken, subredditName,
+ accountName, "sub", redditDataRoomDatabase, subredditSubscriptionListener);
}
- public static void unsubscribeToSubreddit(Retrofit oauthRetrofit, String accessToken,
- String subredditName, String accountName,
+ public static void anonymousSubscribeToSubreddit(Executor executor, Handler handler, Retrofit retrofit,
+ 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,
SubredditSubscriptionListener subredditSubscriptionListener) {
- subredditSubscription(oauthRetrofit, null, accessToken, subredditName, accountName, "unsub",
- redditDataRoomDatabase, subredditSubscriptionListener);
+ subredditSubscription(executor, handler, oauthRetrofit, null, accessToken, subredditName,
+ accountName, "unsub", redditDataRoomDatabase, subredditSubscriptionListener);
+ }
+
+ public static void anonymousUnsubscribeToSubreddit(Executor executor, Handler handler,
+ RedditDataRoomDatabase redditDataRoomDatabase,
+ String subredditName,
+ SubredditSubscriptionListener subredditSubscriptionListener) {
+ removeSubscription(executor, handler, redditDataRoomDatabase, subredditName, "-", subredditSubscriptionListener);
}
- private static void subredditSubscription(Retrofit oauthRetrofit, Retrofit retrofit, String accessToken,
- String subredditName, String accountName, String action,
+ private static void subredditSubscription(Executor executor, Handler handler, Retrofit oauthRetrofit,
+ Retrofit retrofit, String accessToken, String subredditName,
+ String accountName, String action,
RedditDataRoomDatabase redditDataRoomDatabase,
SubredditSubscriptionListener subredditSubscriptionListener) {
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
@@ -51,8 +89,8 @@ public class SubredditSubscription {
FetchSubredditData.fetchSubredditData(retrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() {
@Override
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {
- new UpdateSubscriptionAsyncTask(redditDataRoomDatabase,
- subredditData, accountName, true).execute();
+ insertSubscription(executor, handler, redditDataRoomDatabase,
+ subredditData, accountName, subredditSubscriptionListener);
}
@Override
@@ -61,9 +99,9 @@ public class SubredditSubscription {
}
});
} else {
- new UpdateSubscriptionAsyncTask(redditDataRoomDatabase, subredditName, accountName, false).execute();
+ removeSubscription(executor, handler, redditDataRoomDatabase, subredditName,
+ accountName, subredditSubscriptionListener);
}
- subredditSubscriptionListener.onSubredditSubscriptionSuccess();
} else {
subredditSubscriptionListener.onSubredditSubscriptionFail();
}
@@ -82,39 +120,25 @@ public class SubredditSubscription {
void onSubredditSubscriptionFail();
}
- private static class UpdateSubscriptionAsyncTask extends AsyncTask<Void, Void, Void> {
-
- private RedditDataRoomDatabase redditDataRoomDatabase;
- private String subredditName;
- private String accountName;
- private SubscribedSubredditData subscribedSubredditData;
- 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(),
+ private static void insertSubscription(Executor executor, Handler handler,
+ RedditDataRoomDatabase redditDataRoomDatabase,
+ SubredditData subredditData, String accountName,
+ SubredditSubscriptionListener subredditSubscriptionListener) {
+ executor.execute(() -> {
+ SubscribedSubredditData subscribedSubredditData = new SubscribedSubredditData(subredditData.getId(), subredditData.getName(),
subredditData.getIconUrl(), accountName, false);
- this.accountName = accountName;
- this.isSubscribing = isSubscribing;
- }
-
- @Override
- protected Void doInBackground(Void... voids) {
- if (isSubscribing) {
- redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData);
- } else {
- redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName);
- }
- return null;
- }
+ redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData);
+ handler.post(subredditSubscriptionListener::onSubredditSubscriptionSuccess);
+ });
+ }
+
+ private static void removeSubscription(Executor executor, Handler handler,
+ RedditDataRoomDatabase redditDataRoomDatabase,
+ String subredditName, String accountName,
+ SubredditSubscriptionListener subredditSubscriptionListener) {
+ executor.execute(() -> {
+ redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName);
+ handler.post(subredditSubscriptionListener::onSubredditSubscriptionSuccess);
+ });
}
}