From 1ecdb93139ccf9dca793ebdc9380db2ac7db5aad Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 9 Mar 2021 23:20:06 +0800 Subject: Fix crash when subscribing in anonymous mode. --- .../docilealligator/infinityforreddit/account/AccountDao.java | 3 +++ .../infinityforreddit/subreddit/SubredditSubscription.java | 11 +++++++++++ 2 files changed, 14 insertions(+) (limited to 'app/src/main/java/ml') 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 d2895479..fcd824a1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java @@ -13,6 +13,9 @@ public interface AccountDao { @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(Account account); + @Query("SELECT EXISTS (SELECT 1 FROM accounts WHERE username = '-')") + boolean isAnonymousAccountInserted(); + @Query("SELECT * FROM accounts WHERE username != '-'") List getAllAccounts(); 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 eab9957d..296e319b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java @@ -9,6 +9,7 @@ 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; @@ -127,6 +128,11 @@ public class SubredditSubscription { executor.execute(() -> { SubscribedSubredditData subscribedSubredditData = new SubscribedSubredditData(subredditData.getId(), subredditData.getName(), 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); handler.post(subredditSubscriptionListener::onSubredditSubscriptionSuccess); }); @@ -137,6 +143,11 @@ public class SubredditSubscription { String subredditName, String accountName, SubredditSubscriptionListener subredditSubscriptionListener) { 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); handler.post(subredditSubscriptionListener::onSubredditSubscriptionSuccess); }); -- cgit v1.2.3