Use enum ordinal value instead of a custom field to store the enum in db

This commit is contained in:
Shinokuni 2019-06-14 15:49:25 +02:00
parent 61f79b8546
commit d93137b0a5
2 changed files with 10 additions and 46 deletions

View File

@ -6,11 +6,6 @@ import com.readrops.app.database.entities.Account;
import org.joda.time.LocalDateTime; 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 { public class Converters {
@TypeConverter @TypeConverter
@ -24,23 +19,13 @@ public class Converters {
} }
@TypeConverter @TypeConverter
public Account.AccountType fromAccountTypeCode(int code) { public Account.AccountType fromAccountTypeCode(int ordinal) {
if (code == LOCAL.getCode()) return Account.AccountType.values()[ordinal];
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;
} }
@TypeConverter @TypeConverter
public int getAccountTypeCode(Account.AccountType accountType) { public int getAccountTypeCode(Account.AccountType accountType) {
return accountType.getCode(); return accountType.ordinal();
} }
} }

View File

@ -55,7 +55,7 @@ public class Account implements Parcelable {
id = in.readInt(); id = in.readInt();
url = in.readString(); url = in.readString();
accountName = in.readString(); accountName = in.readString();
accountType = getAccountTypeFromCode(in.readInt()); accountType = AccountType.values()[in.readInt()];
displayedName = in.readString(); displayedName = in.readString();
lastModified = in.readLong(); lastModified = in.readLong();
currentAccount = in.readByte() != 0; currentAccount = in.readByte() != 0;
@ -165,7 +165,7 @@ public class Account implements Parcelable {
dest.writeInt(id); dest.writeInt(id);
dest.writeString(url); dest.writeString(url);
dest.writeString(accountName); dest.writeString(accountName);
dest.writeInt(accountType.code); dest.writeInt(accountType.ordinal());
dest.writeString(displayedName); dest.writeString(displayedName);
dest.writeLong(lastModified); dest.writeLong(lastModified);
dest.writeByte((byte) (currentAccount ? 1 : 0)); dest.writeByte((byte) (currentAccount ? 1 : 0));
@ -174,24 +174,21 @@ public class Account implements Parcelable {
} }
public enum AccountType implements Parcelable { public enum AccountType implements Parcelable {
LOCAL(0, R.drawable.ic_readrops, R.string.local_account), LOCAL(R.drawable.ic_readrops, R.string.local_account),
NEXTCLOUD_NEWS(1, R.drawable.ic_nextcloud_news, R.string.nextcloud_news), NEXTCLOUD_NEWS(R.drawable.ic_nextcloud_news, R.string.nextcloud_news),
FEEDLY(2, 0, 0), FEEDLY(0, 0),
FRESHRSS(3, 0, 0); FRESHRSS(0, 0);
private int code; // TODO see for using ordinal()
private @DrawableRes int iconRes; private @DrawableRes int iconRes;
private @StringRes int name; private @StringRes int name;
AccountType(Parcel in) { AccountType(Parcel in) {
code = in.readInt();
iconRes = in.readInt(); iconRes = in.readInt();
name = in.readInt(); name = in.readInt();
} }
@Override @Override
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(code);
dest.writeInt(iconRes); dest.writeInt(iconRes);
dest.writeInt(name); dest.writeInt(name);
} }
@ -213,10 +210,6 @@ public class Account implements Parcelable {
} }
}; };
public int getCode() {
return code;
}
public int getIconRes() { public int getIconRes() {
return iconRes; return iconRes;
} }
@ -225,23 +218,9 @@ public class Account implements Parcelable {
return name; return name;
} }
AccountType(int code, @DrawableRes int iconRes, @StringRes int name) { AccountType(@DrawableRes int iconRes, @StringRes int name) {
this.code = code;
this.iconRes = iconRes; this.iconRes = iconRes;
this.name = name; 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;
}
} }