From 85474aebf01dcf77f82de1cb1e3e7734d3ecfc36 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 30 May 2021 00:19:31 +0800 Subject: [PATCH] Try fixing SQLiteConstraintException when inserting anonymous account. --- .../docilealligator/infinityforreddit/account/Account.java | 6 ++++++ .../infinityforreddit/account/AccountDao.java | 3 --- .../asynctasks/CheckIsSubscribedToSubreddit.java | 7 +++++-- .../infinityforreddit/subreddit/SubredditSubscription.java | 5 +++-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/account/Account.java b/app/src/main/java/ml/docilealligator/infinityforreddit/account/Account.java index 6232c94d..246c3c05 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/account/Account.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/account/Account.java @@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit.account; import androidx.annotation.NonNull; import androidx.room.ColumnInfo; import androidx.room.Entity; +import androidx.room.Ignore; import androidx.room.PrimaryKey; @Entity(tableName = "accounts") @@ -26,6 +27,11 @@ public class Account { @ColumnInfo(name = "is_current_user") private boolean isCurrentUser; + @Ignore + public static Account getAnonymousAccount() { + return new Account("-", null, null, null, null, null, 0, false); + } + public Account(@NonNull String accountName, String accessToken, String refreshToken, String code, String profileImageUrl, String bannerImageUrl, int karma, boolean isCurrentUser) { this.accountName = accountName; 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 721dea6f..fcd824a1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java @@ -16,9 +16,6 @@ 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/asynctasks/CheckIsSubscribedToSubreddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/CheckIsSubscribedToSubreddit.java index f6768d11..572baf17 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/CheckIsSubscribedToSubreddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/CheckIsSubscribedToSubreddit.java @@ -5,6 +5,7 @@ import android.os.Handler; import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData; public class CheckIsSubscribedToSubreddit { @@ -13,8 +14,10 @@ public class CheckIsSubscribedToSubreddit { String subredditName, String accountName, CheckIsSubscribedToSubredditListener checkIsSubscribedToSubredditListener) { executor.execute(() -> { - if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { - redditDataRoomDatabase.accountDao().insertAnonymousAccount(); + if (accountName == null) { + if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { + redditDataRoomDatabase.accountDao().insert(Account.getAnonymousAccount()); + } } SubscribedSubredditData subscribedSubredditData = redditDataRoomDatabase.subscribedSubredditDao().getSubscribedSubreddit(subredditName, accountName == null ? "-" : accountName); handler.post(() -> { 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 7302dd6a..242edf18 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; @@ -129,7 +130,7 @@ public class SubredditSubscription { subredditData.getIconUrl(), accountName, false); if (accountName.equals("-")) { if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { - redditDataRoomDatabase.accountDao().insertAnonymousAccount(); + redditDataRoomDatabase.accountDao().insert(Account.getAnonymousAccount()); } } redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData); @@ -144,7 +145,7 @@ public class SubredditSubscription { executor.execute(() -> { if (accountName.equals("-")) { if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { - redditDataRoomDatabase.accountDao().insertAnonymousAccount(); + redditDataRoomDatabase.accountDao().insert(Account.getAnonymousAccount()); } } redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName);