From d93137b0a5a0a24f979d3ba8e6c5cced01decb44 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Fri, 14 Jun 2019 15:49:25 +0200 Subject: [PATCH] Use enum ordinal value instead of a custom field to store the enum in db --- .../com/readrops/app/database/Converters.java | 21 ++--------- .../app/database/entities/Account.java | 35 ++++--------------- 2 files changed, 10 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/com/readrops/app/database/Converters.java b/app/src/main/java/com/readrops/app/database/Converters.java index fde370bd..cafc862f 100644 --- a/app/src/main/java/com/readrops/app/database/Converters.java +++ b/app/src/main/java/com/readrops/app/database/Converters.java @@ -6,11 +6,6 @@ import com.readrops.app.database.entities.Account; import org.joda.time.LocalDateTime; -import static com.readrops.app.database.entities.Account.AccountType.FEEDLY; -import static com.readrops.app.database.entities.Account.AccountType.FRESHRSS; -import static com.readrops.app.database.entities.Account.AccountType.LOCAL; -import static com.readrops.app.database.entities.Account.AccountType.NEXTCLOUD_NEWS; - public class Converters { @TypeConverter @@ -24,23 +19,13 @@ public class Converters { } @TypeConverter - public Account.AccountType fromAccountTypeCode(int code) { - if (code == LOCAL.getCode()) - return LOCAL; - else if (code == NEXTCLOUD_NEWS.getCode()) - return NEXTCLOUD_NEWS; - else if (code == FEEDLY.getCode()) - return FEEDLY; - else if (code == FRESHRSS.getCode()) - return FRESHRSS; - - return null; + public Account.AccountType fromAccountTypeCode(int ordinal) { + return Account.AccountType.values()[ordinal]; } @TypeConverter public int getAccountTypeCode(Account.AccountType accountType) { - return accountType.getCode(); + return accountType.ordinal(); } - } diff --git a/app/src/main/java/com/readrops/app/database/entities/Account.java b/app/src/main/java/com/readrops/app/database/entities/Account.java index 04e638f4..71045584 100644 --- a/app/src/main/java/com/readrops/app/database/entities/Account.java +++ b/app/src/main/java/com/readrops/app/database/entities/Account.java @@ -55,7 +55,7 @@ public class Account implements Parcelable { id = in.readInt(); url = in.readString(); accountName = in.readString(); - accountType = getAccountTypeFromCode(in.readInt()); + accountType = AccountType.values()[in.readInt()]; displayedName = in.readString(); lastModified = in.readLong(); currentAccount = in.readByte() != 0; @@ -165,7 +165,7 @@ public class Account implements Parcelable { dest.writeInt(id); dest.writeString(url); dest.writeString(accountName); - dest.writeInt(accountType.code); + dest.writeInt(accountType.ordinal()); dest.writeString(displayedName); dest.writeLong(lastModified); dest.writeByte((byte) (currentAccount ? 1 : 0)); @@ -174,24 +174,21 @@ public class Account implements Parcelable { } public enum AccountType implements Parcelable { - LOCAL(0, R.drawable.ic_readrops, R.string.local_account), - NEXTCLOUD_NEWS(1, R.drawable.ic_nextcloud_news, R.string.nextcloud_news), - FEEDLY(2, 0, 0), - FRESHRSS(3, 0, 0); + LOCAL(R.drawable.ic_readrops, R.string.local_account), + NEXTCLOUD_NEWS(R.drawable.ic_nextcloud_news, R.string.nextcloud_news), + FEEDLY(0, 0), + FRESHRSS(0, 0); - private int code; // TODO see for using ordinal() private @DrawableRes int iconRes; private @StringRes int name; AccountType(Parcel in) { - code = in.readInt(); iconRes = in.readInt(); name = in.readInt(); } @Override public void writeToParcel(Parcel dest, int flags) { - dest.writeInt(code); dest.writeInt(iconRes); dest.writeInt(name); } @@ -213,10 +210,6 @@ public class Account implements Parcelable { } }; - public int getCode() { - return code; - } - public int getIconRes() { return iconRes; } @@ -225,23 +218,9 @@ public class Account implements Parcelable { return name; } - AccountType(int code, @DrawableRes int iconRes, @StringRes int name) { - this.code = code; + AccountType(@DrawableRes int iconRes, @StringRes int name) { this.iconRes = iconRes; this.name = name; } } - - public static AccountType getAccountTypeFromCode(int code) { - if (code == AccountType.LOCAL.getCode()) - return AccountType.LOCAL; - else if (code == AccountType.NEXTCLOUD_NEWS.getCode()) - return AccountType.NEXTCLOUD_NEWS; - else if (code == AccountType.FEEDLY.getCode()) - return AccountType.FEEDLY; - else if (code == AccountType.FRESHRSS.getCode()) - return AccountType.FRESHRSS; - - return null; - } }