Try fixing SQLiteConstraintException when inserting anonymous account.

This commit is contained in:
Alex Ning 2021-05-30 00:19:31 +08:00
parent 8f49352d78
commit 85474aebf0
4 changed files with 14 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit.account;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.room.ColumnInfo; import androidx.room.ColumnInfo;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
@Entity(tableName = "accounts") @Entity(tableName = "accounts")
@ -26,6 +27,11 @@ public class Account {
@ColumnInfo(name = "is_current_user") @ColumnInfo(name = "is_current_user")
private boolean isCurrentUser; 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, public Account(@NonNull String accountName, String accessToken, String refreshToken, String code,
String profileImageUrl, String bannerImageUrl, int karma, boolean isCurrentUser) { String profileImageUrl, String bannerImageUrl, int karma, boolean isCurrentUser) {
this.accountName = accountName; this.accountName = accountName;

View File

@ -16,9 +16,6 @@ public interface AccountDao {
@Query("SELECT EXISTS (SELECT 1 FROM accounts WHERE username = '-')") @Query("SELECT EXISTS (SELECT 1 FROM accounts WHERE username = '-')")
boolean isAnonymousAccountInserted(); boolean isAnonymousAccountInserted();
@Query("INSERT INTO accounts(username, karma, is_current_user) VALUES('-', 0, 0)")
void insertAnonymousAccount();
@Query("SELECT * FROM accounts WHERE username != '-'") @Query("SELECT * FROM accounts WHERE username != '-'")
List<Account> getAllAccounts(); List<Account> getAllAccounts();

View File

@ -5,6 +5,7 @@ import android.os.Handler;
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.subscribedsubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData;
public class CheckIsSubscribedToSubreddit { public class CheckIsSubscribedToSubreddit {
@ -13,8 +14,10 @@ public class CheckIsSubscribedToSubreddit {
String subredditName, String accountName, String subredditName, String accountName,
CheckIsSubscribedToSubredditListener checkIsSubscribedToSubredditListener) { CheckIsSubscribedToSubredditListener checkIsSubscribedToSubredditListener) {
executor.execute(() -> { executor.execute(() -> {
if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { if (accountName == null) {
redditDataRoomDatabase.accountDao().insertAnonymousAccount(); if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) {
redditDataRoomDatabase.accountDao().insert(Account.getAnonymousAccount());
}
} }
SubscribedSubredditData subscribedSubredditData = redditDataRoomDatabase.subscribedSubredditDao().getSubscribedSubreddit(subredditName, accountName == null ? "-" : accountName); SubscribedSubredditData subscribedSubredditData = redditDataRoomDatabase.subscribedSubredditDao().getSubscribedSubreddit(subredditName, accountName == null ? "-" : accountName);
handler.post(() -> { handler.post(() -> {

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;
@ -129,7 +130,7 @@ public class SubredditSubscription {
subredditData.getIconUrl(), accountName, false); subredditData.getIconUrl(), accountName, false);
if (accountName.equals("-")) { if (accountName.equals("-")) {
if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) {
redditDataRoomDatabase.accountDao().insertAnonymousAccount(); redditDataRoomDatabase.accountDao().insert(Account.getAnonymousAccount());
} }
} }
redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData); redditDataRoomDatabase.subscribedSubredditDao().insert(subscribedSubredditData);
@ -144,7 +145,7 @@ public class SubredditSubscription {
executor.execute(() -> { executor.execute(() -> {
if (accountName.equals("-")) { if (accountName.equals("-")) {
if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { if (!redditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) {
redditDataRoomDatabase.accountDao().insertAnonymousAccount(); redditDataRoomDatabase.accountDao().insert(Account.getAnonymousAccount());
} }
} }
redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName); redditDataRoomDatabase.subscribedSubredditDao().deleteSubscribedSubreddit(subredditName, accountName);