Add better use of AccountType enum instead of using a class of the same name holding same fields

This commit is contained in:
Shinokuni 2019-06-14 15:28:33 +02:00
parent 65c7141c29
commit 61f79b8546
6 changed files with 69 additions and 120 deletions

View File

@ -2,6 +2,7 @@ package com.readrops.app.activities;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Toast; import android.widget.Toast;
@ -15,7 +16,6 @@ import com.readrops.app.R;
import com.readrops.app.database.entities.Account; import com.readrops.app.database.entities.Account;
import com.readrops.app.databinding.ActivityAccountTypeListBinding; import com.readrops.app.databinding.ActivityAccountTypeListBinding;
import com.readrops.app.viewmodels.AccountViewModel; import com.readrops.app.viewmodels.AccountViewModel;
import com.readrops.app.views.AccountType;
import com.readrops.app.views.AccountTypeListAdapter; import com.readrops.app.views.AccountTypeListAdapter;
import java.util.ArrayList; import java.util.ArrayList;
@ -46,13 +46,13 @@ public class AccountTypeListActivity extends AppCompatActivity {
fromMainActivity = getIntent().getBooleanExtra("fromMainActivity", false); fromMainActivity = getIntent().getBooleanExtra("fromMainActivity", false);
adapter = new AccountTypeListAdapter(accountType -> { adapter = new AccountTypeListAdapter(accountType -> {
if (!(accountType.getAccountType() == Account.AccountType.LOCAL)) { if (!(accountType == Account.AccountType.LOCAL)) {
Intent intent = new Intent(getApplicationContext(), AddAccountActivity.class); Intent intent = new Intent(getApplicationContext(), AddAccountActivity.class);
if (fromMainActivity) if (fromMainActivity)
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
intent.putExtra("accountType", accountType); intent.putExtra("accountType", (Parcelable) accountType);
startActivity(intent); startActivity(intent);
finish(); finish();
@ -65,22 +65,17 @@ public class AccountTypeListActivity extends AppCompatActivity {
adapter.setAccountTypes(getData()); adapter.setAccountTypes(getData());
} }
private List<AccountType> getData() { private List<Account.AccountType> getData() {
List<AccountType> accountTypes = new ArrayList<>(); List<Account.AccountType> accountTypes = new ArrayList<>();
AccountType localAccount = new AccountType(getString(R.string.local_account), accountTypes.add(Account.AccountType.LOCAL);
R.drawable.ic_readrops, Account.AccountType.LOCAL); accountTypes.add(Account.AccountType.NEXTCLOUD_NEWS);
AccountType nextNewsAccount = new AccountType(getString(R.string.nextcloud_news),
R.drawable.ic_nextcloud_news, Account.AccountType.NEXTCLOUD_NEWS);
accountTypes.add(localAccount);
accountTypes.add(nextNewsAccount);
return accountTypes; return accountTypes;
} }
private void createNewLocalAccount(AccountType accountType) { private void createNewLocalAccount(Account.AccountType accountType) {
Account account = new Account(null, accountType.getName(), Account.AccountType.LOCAL); Account account = new Account(null, getString(accountType.getName()), accountType);
account.setCurrentAccount(true); account.setCurrentAccount(true);
viewModel.insert(account) viewModel.insert(account)

View File

@ -15,7 +15,6 @@ import com.readrops.app.database.entities.Account;
import com.readrops.app.databinding.ActivityAddAccountBinding; import com.readrops.app.databinding.ActivityAddAccountBinding;
import com.readrops.app.utils.SharedPreferencesManager; import com.readrops.app.utils.SharedPreferencesManager;
import com.readrops.app.viewmodels.AccountViewModel; import com.readrops.app.viewmodels.AccountViewModel;
import com.readrops.app.views.AccountType;
import io.reactivex.SingleObserver; import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
@ -27,7 +26,7 @@ public class AddAccountActivity extends AppCompatActivity {
private ActivityAddAccountBinding binding; private ActivityAddAccountBinding binding;
private AccountViewModel viewModel; private AccountViewModel viewModel;
private AccountType accountType; private Account.AccountType accountType;
private boolean forwardResult; private boolean forwardResult;
@Override @Override
@ -42,7 +41,7 @@ public class AddAccountActivity extends AppCompatActivity {
int flag = getIntent().getFlags(); int flag = getIntent().getFlags();
forwardResult = flag == Intent.FLAG_ACTIVITY_FORWARD_RESULT; forwardResult = flag == Intent.FLAG_ACTIVITY_FORWARD_RESULT;
binding.providerImage.setImageResource(accountType.getLogoId()); binding.providerImage.setImageResource(accountType.getIconRes());
binding.providerName.setText(accountType.getName()); binding.providerName.setText(accountType.getName());
binding.addAccountSkip.setOnClickListener(v -> { binding.addAccountSkip.setOnClickListener(v -> {
@ -62,7 +61,7 @@ public class AddAccountActivity extends AppCompatActivity {
String login = binding.addAccountLogin.getText().toString().trim(); String login = binding.addAccountLogin.getText().toString().trim();
String password = binding.addAccountPassword.getText().toString().trim(); String password = binding.addAccountPassword.getText().toString().trim();
Account account = new Account(url, name, accountType.getAccountType()); Account account = new Account(url, name, accountType);
account.setLogin(login); account.setLogin(login);
account.setPassword(password); account.setPassword(password);

View File

@ -4,6 +4,7 @@ import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import androidx.annotation.DrawableRes; import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;
import androidx.room.ColumnInfo; import androidx.room.ColumnInfo;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.Ignore; import androidx.room.Ignore;
@ -172,20 +173,62 @@ public class Account implements Parcelable {
dest.writeString(password); dest.writeString(password);
} }
public enum AccountType { public enum AccountType implements Parcelable {
LOCAL(0), LOCAL(0, R.drawable.ic_readrops, R.string.local_account),
NEXTCLOUD_NEWS(1), NEXTCLOUD_NEWS(1, R.drawable.ic_nextcloud_news, R.string.nextcloud_news),
FEEDLY(2), FEEDLY(2, 0, 0),
FRESHRSS(3); FRESHRSS(3, 0, 0);
private int code; 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);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<AccountType> CREATOR = new Creator<AccountType>() {
@Override
public AccountType createFromParcel(Parcel in) {
return AccountType.values()[in.readInt()];
}
@Override
public AccountType[] newArray(int size) {
return new AccountType[size];
}
};
public int getCode() { public int getCode() {
return code; return code;
} }
AccountType(int code) { public int getIconRes() {
return iconRes;
}
public int getName() {
return name;
}
AccountType(int code, @DrawableRes int iconRes, @StringRes int name) {
this.code = code; this.code = code;
this.iconRes = iconRes;
this.name = name;
} }
} }
@ -201,15 +244,4 @@ public class Account implements Parcelable {
return null; return null;
} }
public static @DrawableRes int getLogoFromAccountType(AccountType accountType) {
switch (accountType) {
case LOCAL:
return R.drawable.ic_readrops;
case NEXTCLOUD_NEWS:
return R.drawable.ic_nextcloud_news;
default:
return R.drawable.ic_readrops;
}
}
} }

View File

@ -186,7 +186,7 @@ public class DrawerManager {
private ProfileDrawerItem createProfileItem(Account account) { private ProfileDrawerItem createProfileItem(Account account) {
return new ProfileDrawerItem() return new ProfileDrawerItem()
.withIcon(Account.getLogoFromAccountType(account.getAccountType())) .withIcon(account.getAccountType().getIconRes())
.withName(account.getDisplayedName()) .withName(account.getDisplayedName())
.withEmail(account.getAccountName()) .withEmail(account.getAccountName())
.withIdentifier(account.getId()); .withIdentifier(account.getId());

View File

@ -1,78 +0,0 @@
package com.readrops.app.views;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.DrawableRes;
import com.readrops.app.database.entities.Account;
public class AccountType implements Parcelable {
private String name;
private @DrawableRes int logoId;
private Account.AccountType accountType;
public AccountType(String name, int logoId, Account.AccountType accountType) {
this.name = name;
this.logoId = logoId;
this.accountType = accountType;
}
protected AccountType(Parcel in) {
name = in.readString();
logoId = in.readInt();
accountType = Account.AccountType.values()[in.readInt()];
}
public static final Creator<AccountType> CREATOR = new Creator<AccountType>() {
@Override
public AccountType createFromParcel(Parcel in) {
return new AccountType(in);
}
@Override
public AccountType[] newArray(int size) {
return new AccountType[size];
}
};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public @DrawableRes
int getLogoId() {
return logoId;
}
public void setLogoId(@DrawableRes int logoId) {
this.logoId = logoId;
}
public Account.AccountType getAccountType() {
return accountType;
}
public void setAccountType(Account.AccountType accountType) {
this.accountType = accountType;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(logoId);
dest.writeInt(accountType.getCode());
}
}

View File

@ -8,13 +8,14 @@ import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.readrops.app.R; import com.readrops.app.R;
import com.readrops.app.database.entities.Account;
import com.readrops.app.databinding.AccountTypeItemBinding; import com.readrops.app.databinding.AccountTypeItemBinding;
import java.util.List; import java.util.List;
public class AccountTypeListAdapter extends RecyclerView.Adapter<AccountTypeListAdapter.AccountTypeViewHolder> { public class AccountTypeListAdapter extends RecyclerView.Adapter<AccountTypeListAdapter.AccountTypeViewHolder> {
private List<AccountType> accountTypes; private List<Account.AccountType> accountTypes;
private OnItemClickListener listener; private OnItemClickListener listener;
public AccountTypeListAdapter(OnItemClickListener listener) { public AccountTypeListAdapter(OnItemClickListener listener) {
@ -32,10 +33,10 @@ public class AccountTypeListAdapter extends RecyclerView.Adapter<AccountTypeList
@Override @Override
public void onBindViewHolder(@NonNull AccountTypeViewHolder holder, int position) { public void onBindViewHolder(@NonNull AccountTypeViewHolder holder, int position) {
AccountType accountType = accountTypes.get(position); Account.AccountType accountType = accountTypes.get(position);
holder.binding.accountTypeName.setText(accountType.getName()); holder.binding.accountTypeName.setText(accountType.getName());
holder.binding.accountTypeLogo.setImageResource(accountType.getLogoId()); holder.binding.accountTypeLogo.setImageResource(accountType.getIconRes());
holder.binding.getRoot().setOnClickListener(v -> listener.onItemClick(accountType)); holder.binding.getRoot().setOnClickListener(v -> listener.onItemClick(accountType));
} }
@ -45,13 +46,13 @@ public class AccountTypeListAdapter extends RecyclerView.Adapter<AccountTypeList
return accountTypes.size(); return accountTypes.size();
} }
public void setAccountTypes(List<AccountType> accountTypes) { public void setAccountTypes(List<Account.AccountType> accountTypes) {
this.accountTypes = accountTypes; this.accountTypes = accountTypes;
notifyDataSetChanged(); notifyDataSetChanged();
} }
public interface OnItemClickListener { public interface OnItemClickListener {
void onItemClick(AccountType accountType); void onItemClick(Account.AccountType accountType);
} }
public class AccountTypeViewHolder extends RecyclerView.ViewHolder { public class AccountTypeViewHolder extends RecyclerView.ViewHolder {