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 = '-')")
boolean isAnonymousAccountInserted();
@Query("INSERT INTO accounts(username, karma, is_current_user) VALUES('-', 0, 0)")
void insertAnonymousAccount();
@Query("SELECT * FROM accounts WHERE username != '-'")
List<Account> getAllAccounts();

View File

@ -155,21 +155,38 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
public void isNotSubscribed() {
((DataViewHolder) holder).subscribeButton.setVisibility(View.VISIBLE);
((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> {
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();
}
});
}
});
}
});

View File

@ -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) {

View File

@ -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);