Anonymous subreddit subscription in SubredditListingRecyclerViewAdapter.

This commit is contained in:
Alex Ning 2021-03-09 23:38:11 +08:00
parent 559dbf02d9
commit e43adf966c
4 changed files with 39 additions and 17 deletions

View File

@ -16,6 +16,9 @@ public interface AccountDao {
@Query("SELECT EXISTS (SELECT 1 FROM accounts WHERE username = '-')") @Query("SELECT EXISTS (SELECT 1 FROM accounts WHERE username = '-')")
boolean isAnonymousAccountInserted(); boolean isAnonymousAccountInserted();
@Query("INSERT INTO accounts(username, karma, is_current_user) VALUES('-', 0, 0)")
void insertAnonymousAccount();
@Query("SELECT * FROM accounts WHERE username != '-'") @Query("SELECT * FROM accounts WHERE username != '-'")
List<Account> getAllAccounts(); List<Account> getAllAccounts();

View File

@ -155,21 +155,38 @@ 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(executor, new Handler(), if (accessToken != null) {
oauthRetrofit, retrofit, accessToken, subredditData.getName(), SubredditSubscription.subscribeToSubreddit(executor, new Handler(),
accountName, redditDataRoomDatabase, oauthRetrofit, retrofit, accessToken, subredditData.getName(),
new SubredditSubscription.SubredditSubscriptionListener() { accountName, redditDataRoomDatabase,
@Override new SubredditSubscription.SubredditSubscriptionListener() {
public void onSubredditSubscriptionSuccess() { @Override
((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE); public void onSubredditSubscriptionSuccess() {
Toast.makeText(context, R.string.subscribed, Toast.LENGTH_SHORT).show(); ((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE);
} Toast.makeText(context, R.string.subscribed, Toast.LENGTH_SHORT).show();
}
@Override @Override
public void onSubredditSubscriptionFail() { public void onSubredditSubscriptionFail() {
Toast.makeText(context, R.string.subscribe_failed, Toast.LENGTH_SHORT).show(); 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();
}
});
}
}); });
} }
}); });

View File

@ -13,6 +13,9 @@ public class CheckIsSubscribedToSubreddit {
String subredditName, String accountName, String subredditName, String accountName,
CheckIsSubscribedToSubredditListener checkIsSubscribedToSubredditListener) { CheckIsSubscribedToSubredditListener checkIsSubscribedToSubredditListener) {
executor.execute(() -> { executor.execute(() -> {
if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) {
redditDataRoomDatabase.accountDao().insertAnonymousAccount();
}
SubscribedSubredditData subscribedSubredditData = redditDataRoomDatabase.subscribedSubredditDao().getSubscribedSubreddit(subredditName, accountName); SubscribedSubredditData subscribedSubredditData = redditDataRoomDatabase.subscribedSubredditDao().getSubscribedSubreddit(subredditName, accountName);
handler.post(() -> { handler.post(() -> {
if (subscribedSubredditData != null) { if (subscribedSubredditData != null) {

View File

@ -9,7 +9,6 @@ import java.util.Map;
import java.util.concurrent.Executor; 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.subscribedsubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData;
import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.APIUtils;
@ -130,7 +129,7 @@ public class SubredditSubscription {
subredditData.getIconUrl(), accountName, false); subredditData.getIconUrl(), accountName, false);
if (accountName.equals("-")) { if (accountName.equals("-")) {
if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) {
redditDataRoomDatabase.accountDao().insert(new Account("-", null, null, null, null, null, 0, false)); redditDataRoomDatabase.accountDao().insertAnonymousAccount();
} }
} }
redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData); redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData);
@ -145,7 +144,7 @@ public class SubredditSubscription {
executor.execute(() -> { executor.execute(() -> {
if (accountName.equals("-")) { if (accountName.equals("-")) {
if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) {
redditDataRoomDatabase.accountDao().insert(new Account("-", null, null, null, null, null, 0, false)); redditDataRoomDatabase.accountDao().insertAnonymousAccount();
} }
} }
redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName); redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName);