From e43adf966c44f436b35f1445fd75ca6f15523a33 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 9 Mar 2021 23:38:11 +0800 Subject: Anonymous subreddit subscription in SubredditListingRecyclerViewAdapter. --- .../infinityforreddit/account/AccountDao.java | 3 ++ .../SubredditListingRecyclerViewAdapter.java | 45 +++++++++++++++------- .../asynctasks/CheckIsSubscribedToSubreddit.java | 3 ++ .../subreddit/SubredditSubscription.java | 5 +-- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java index fcd824a1..721dea6f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java @@ -16,6 +16,9 @@ public interface AccountDao { @Query("SELECT EXISTS (SELECT 1 FROM accounts WHERE username = '-')") boolean isAnonymousAccountInserted(); + @Query("INSERT INTO accounts(username, karma, is_current_user) VALUES('-', 0, 0)") + void insertAnonymousAccount(); + @Query("SELECT * FROM accounts WHERE username != '-'") List getAllAccounts(); 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 9e515c8b..f7891958 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java @@ -155,21 +155,38 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter { - SubredditSubscription.subscribeToSubreddit(executor, new Handler(), - oauthRetrofit, retrofit, accessToken, subredditData.getName(), - accountName, redditDataRoomDatabase, - new SubredditSubscription.SubredditSubscriptionListener() { - @Override - public void onSubredditSubscriptionSuccess() { - ((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE); - Toast.makeText(context, R.string.subscribed, Toast.LENGTH_SHORT).show(); - } + if (accessToken != null) { + SubredditSubscription.subscribeToSubreddit(executor, new Handler(), + oauthRetrofit, retrofit, accessToken, subredditData.getName(), + accountName, redditDataRoomDatabase, + new SubredditSubscription.SubredditSubscriptionListener() { + @Override + public void onSubredditSubscriptionSuccess() { + ((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE); + Toast.makeText(context, R.string.subscribed, Toast.LENGTH_SHORT).show(); + } - @Override - public void onSubredditSubscriptionFail() { - Toast.makeText(context, R.string.subscribe_failed, Toast.LENGTH_SHORT).show(); - } - }); + @Override + public void onSubredditSubscriptionFail() { + Toast.makeText(context, R.string.subscribe_failed, Toast.LENGTH_SHORT).show(); + } + }); + } else { + SubredditSubscription.anonymousSubscribeToSubreddit(executor, new Handler(), + retrofit, redditDataRoomDatabase, subredditData.getName(), + new SubredditSubscription.SubredditSubscriptionListener() { + @Override + public void onSubredditSubscriptionSuccess() { + ((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE); + Toast.makeText(context, R.string.subscribed, Toast.LENGTH_SHORT).show(); + } + + @Override + public void onSubredditSubscriptionFail() { + Toast.makeText(context, R.string.subscribe_failed, Toast.LENGTH_SHORT).show(); + } + }); + } }); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/CheckIsSubscribedToSubreddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/CheckIsSubscribedToSubreddit.java index fed411cc..d977f159 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/CheckIsSubscribedToSubreddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/CheckIsSubscribedToSubreddit.java @@ -13,6 +13,9 @@ public class CheckIsSubscribedToSubreddit { String subredditName, String accountName, CheckIsSubscribedToSubredditListener checkIsSubscribedToSubredditListener) { executor.execute(() -> { + if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { + redditDataRoomDatabase.accountDao().insertAnonymousAccount(); + } SubscribedSubredditData subscribedSubredditData = redditDataRoomDatabase.subscribedSubredditDao().getSubscribedSubreddit(subredditName, accountName); handler.post(() -> { if (subscribedSubredditData != null) { 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 296e319b..7302dd6a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java @@ -9,7 +9,6 @@ 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.subscribedsubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.utils.APIUtils; @@ -130,7 +129,7 @@ public class SubredditSubscription { subredditData.getIconUrl(), accountName, false); if (accountName.equals("-")) { if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { - redditDataRoomDatabase.accountDao().insert(new Account("-", null, null, null, null, null, 0, false)); + redditDataRoomDatabase.accountDao().insertAnonymousAccount(); } } redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData); @@ -145,7 +144,7 @@ public class SubredditSubscription { executor.execute(() -> { if (accountName.equals("-")) { if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { - redditDataRoomDatabase.accountDao().insert(new Account("-", null, null, null, null, null, 0, false)); + redditDataRoomDatabase.accountDao().insertAnonymousAccount(); } } redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName); -- cgit v1.2.3