diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-05-29 16:19:31 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-05-29 16:19:31 +0000 |
commit | 85474aebf01dcf77f82de1cb1e3e7734d3ecfc36 (patch) | |
tree | 5a8167f281aae31a4b95f521ef1530bee682b944 /app/src/main | |
parent | 8f49352d788570dba2b1bc97c7938866f78ac805 (diff) | |
download | infinity-for-reddit-85474aebf01dcf77f82de1cb1e3e7734d3ecfc36.tar infinity-for-reddit-85474aebf01dcf77f82de1cb1e3e7734d3ecfc36.tar.gz infinity-for-reddit-85474aebf01dcf77f82de1cb1e3e7734d3ecfc36.tar.bz2 infinity-for-reddit-85474aebf01dcf77f82de1cb1e3e7734d3ecfc36.tar.lz infinity-for-reddit-85474aebf01dcf77f82de1cb1e3e7734d3ecfc36.tar.xz infinity-for-reddit-85474aebf01dcf77f82de1cb1e3e7734d3ecfc36.tar.zst infinity-for-reddit-85474aebf01dcf77f82de1cb1e3e7734d3ecfc36.zip |
Try fixing SQLiteConstraintException when inserting anonymous account.
Diffstat (limited to '')
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<Account> 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); |