Fix crash when subscribing in anonymous mode.

This commit is contained in:
Alex Ning 2021-03-09 23:20:06 +08:00
parent 6bdf38b996
commit 1ecdb93139
2 changed files with 14 additions and 0 deletions

View File

@ -13,6 +13,9 @@ public interface AccountDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Account account); void insert(Account account);
@Query("SELECT EXISTS (SELECT 1 FROM accounts WHERE username = '-')")
boolean isAnonymousAccountInserted();
@Query("SELECT * FROM accounts WHERE username != '-'") @Query("SELECT * FROM accounts WHERE username != '-'")
List<Account> getAllAccounts(); List<Account> getAllAccounts();

View File

@ -9,6 +9,7 @@ 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;
@ -127,6 +128,11 @@ public class SubredditSubscription {
executor.execute(() -> { executor.execute(() -> {
SubscribedSubredditData subscribedSubredditData = new SubscribedSubredditData(subredditData.getId(), subredditData.getName(), SubscribedSubredditData subscribedSubredditData = new SubscribedSubredditData(subredditData.getId(), subredditData.getName(),
subredditData.getIconUrl(), accountName, false); subredditData.getIconUrl(), accountName, false);
if (accountName.equals("-")) {
if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) {
redditDataRoomDatabase.accountDao().insert(new Account("-", null, null, null, null, null, 0, false));
}
}
redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData); redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData);
handler.post(subredditSubscriptionListener::onSubredditSubscriptionSuccess); handler.post(subredditSubscriptionListener::onSubredditSubscriptionSuccess);
}); });
@ -137,6 +143,11 @@ public class SubredditSubscription {
String subredditName, String accountName, String subredditName, String accountName,
SubredditSubscriptionListener subredditSubscriptionListener) { SubredditSubscriptionListener subredditSubscriptionListener) {
executor.execute(() -> { executor.execute(() -> {
if (accountName.equals("-")) {
if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) {
redditDataRoomDatabase.accountDao().insert(new Account("-", null, null, null, null, null, 0, false));
}
}
redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName); redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName);
handler.post(subredditSubscriptionListener::onSubredditSubscriptionSuccess); handler.post(subredditSubscriptionListener::onSubredditSubscriptionSuccess);
}); });