mirror of
https://github.com/nextcloud/android.git
synced 2024-11-27 09:39:25 +03:00
proper view type switch
Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
parent
7bf354928a
commit
670dfa75eb
1 changed files with 69 additions and 60 deletions
|
@ -40,7 +40,6 @@ import com.owncloud.android.lib.common.utils.Log_OC;
|
||||||
import com.owncloud.android.ui.activity.BaseActivity;
|
import com.owncloud.android.ui.activity.BaseActivity;
|
||||||
import com.owncloud.android.ui.activity.UserInfoActivity;
|
import com.owncloud.android.ui.activity.UserInfoActivity;
|
||||||
import com.owncloud.android.utils.DisplayUtils;
|
import com.owncloud.android.utils.DisplayUtils;
|
||||||
import com.owncloud.android.utils.ThemeUtils;
|
|
||||||
|
|
||||||
import org.parceler.Parcels;
|
import org.parceler.Parcels;
|
||||||
|
|
||||||
|
@ -53,14 +52,14 @@ import androidx.recyclerview.widget.RecyclerView;
|
||||||
/**
|
/**
|
||||||
* This Adapter populates a RecyclerView with all accounts within the app.
|
* This Adapter populates a RecyclerView with all accounts within the app.
|
||||||
*/
|
*/
|
||||||
public class AccountListAdapter extends RecyclerView.Adapter<AccountListAdapter.AccountViewHolderItem>
|
public class AccountListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
|
||||||
implements DisplayUtils.AvatarGenerationListener {
|
implements DisplayUtils.AvatarGenerationListener {
|
||||||
private static final String TAG = AccountListAdapter.class.getSimpleName();
|
private static final String TAG = AccountListAdapter.class.getSimpleName();
|
||||||
private float mAccountAvatarRadiusDimension;
|
private float accountAvatarRadiusDimension;
|
||||||
private final BaseActivity mContext;
|
private final BaseActivity context;
|
||||||
private List<AccountListItem> mValues;
|
private List<AccountListItem> values;
|
||||||
private AccountListAdapterListener mListener;
|
private AccountListAdapterListener accountListAdapterListener;
|
||||||
private Drawable mTintedCheck;
|
private Drawable tintedCheck;
|
||||||
private RecyclerView mRecyclerView;
|
private RecyclerView mRecyclerView;
|
||||||
private UserAccountManager accountManager;
|
private UserAccountManager accountManager;
|
||||||
|
|
||||||
|
@ -68,14 +67,14 @@ public class AccountListAdapter extends RecyclerView.Adapter<AccountListAdapter.
|
||||||
private static final int KEY_USER_INFO_REQUEST_CODE = 13;
|
private static final int KEY_USER_INFO_REQUEST_CODE = 13;
|
||||||
|
|
||||||
public AccountListAdapter(BaseActivity context, UserAccountManager accountManager, List<AccountListItem> values, Drawable tintedCheck) {
|
public AccountListAdapter(BaseActivity context, UserAccountManager accountManager, List<AccountListItem> values, Drawable tintedCheck) {
|
||||||
this.mContext = context;
|
this.context = context;
|
||||||
this.accountManager = accountManager;
|
this.accountManager = accountManager;
|
||||||
this.mValues = values;
|
this.values = values;
|
||||||
if (context instanceof AccountListAdapterListener) {
|
if (context instanceof AccountListAdapterListener) {
|
||||||
this.mListener = (AccountListAdapterListener) context;
|
this.accountListAdapterListener = (AccountListAdapterListener) context;
|
||||||
}
|
}
|
||||||
this.mAccountAvatarRadiusDimension = context.getResources().getDimension(R.dimen.list_item_avatar_icon_radius);
|
this.accountAvatarRadiusDimension = context.getResources().getDimension(R.dimen.list_item_avatar_icon_radius);
|
||||||
this.mTintedCheck = tintedCheck;
|
this.tintedCheck = tintedCheck;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -84,35 +83,49 @@ public class AccountListAdapter extends RecyclerView.Adapter<AccountListAdapter.
|
||||||
mRecyclerView = recyclerView;
|
mRecyclerView = recyclerView;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
|
||||||
@Override
|
@Override
|
||||||
public AccountViewHolderItem onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
public int getItemViewType(int position) {
|
||||||
AccountViewHolderItem viewHolder;
|
if (position == values.size()-1) {
|
||||||
View view = LayoutInflater.from(mContext).inflate(R.layout.account_item, parent, false);
|
return AccountListItem.TYPE_ACTION_ADD;
|
||||||
viewHolder = new AccountViewHolderItem(view);
|
}
|
||||||
viewHolder.checkViewItem.setImageDrawable(mTintedCheck);
|
return AccountListItem.TYPE_ACCOUNT;
|
||||||
return viewHolder;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(@NonNull AccountViewHolderItem holder, int position) {
|
public @NonNull
|
||||||
AccountListItem accountListItem = mValues.get(position);
|
RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||||
|
View view;
|
||||||
|
if (AccountListItem.TYPE_ACCOUNT == viewType) {
|
||||||
|
view = LayoutInflater.from(context).inflate(R.layout.account_item, parent, false);
|
||||||
|
AccountViewHolderItem viewHolder = new AccountViewHolderItem(view);
|
||||||
|
viewHolder.checkViewItem.setImageDrawable(tintedCheck);
|
||||||
|
return viewHolder;
|
||||||
|
} else {
|
||||||
|
view = LayoutInflater.from(context).inflate(R.layout.account_action, parent, false);
|
||||||
|
return new AddAccountViewHolderItem(view);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
||||||
|
AccountListItem accountListItem = values.get(position);
|
||||||
|
|
||||||
if (accountListItem != null) {
|
if (accountListItem != null) {
|
||||||
// create account item
|
// create account item
|
||||||
if (AccountListItem.TYPE_ACCOUNT == accountListItem.getType()) {
|
if (AccountListItem.TYPE_ACCOUNT == accountListItem.getType()) {
|
||||||
Account account = accountListItem.getAccount();
|
Account account = accountListItem.getAccount();
|
||||||
setAccount(holder, account);
|
AccountViewHolderItem item = (AccountViewHolderItem)holder;
|
||||||
setUsername(holder, account);
|
setAccount(item, account);
|
||||||
setAvatar(holder, account);
|
setUsername(item, account);
|
||||||
setCurrentlyActiveState(holder, account);
|
setAvatar(item, account);
|
||||||
|
setCurrentlyActiveState(item, account);
|
||||||
|
|
||||||
TextView usernameView = holder.usernameViewItem;
|
TextView usernameView = item.usernameViewItem;
|
||||||
TextView accountView = holder.accountViewItem;
|
TextView accountView = item.accountViewItem;
|
||||||
|
|
||||||
// OnClickListener for when the user selects an account
|
// OnClickListener for when the user selects an account
|
||||||
holder.itemView.setOnClickListener(view -> {
|
holder.itemView.setOnClickListener(view -> {
|
||||||
final Intent intent = new Intent(mContext, UserInfoActivity.class);
|
final Intent intent = new Intent(context, UserInfoActivity.class);
|
||||||
if (accountListItem.isEnabled()) {
|
if (accountListItem.isEnabled()) {
|
||||||
intent.putExtra(UserInfoActivity.KEY_ACCOUNT, Parcels.wrap(account));
|
intent.putExtra(UserInfoActivity.KEY_ACCOUNT, Parcels.wrap(account));
|
||||||
try {
|
try {
|
||||||
|
@ -121,7 +134,7 @@ public class AccountListAdapter extends RecyclerView.Adapter<AccountListAdapter.
|
||||||
} catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
|
} catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
|
||||||
Log_OC.d(TAG, "Failed to find NC account");
|
Log_OC.d(TAG, "Failed to find NC account");
|
||||||
}
|
}
|
||||||
mContext.startActivityForResult(intent, KEY_USER_INFO_REQUEST_CODE);
|
context.startActivityForResult(intent, KEY_USER_INFO_REQUEST_CODE);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -134,37 +147,26 @@ public class AccountListAdapter extends RecyclerView.Adapter<AccountListAdapter.
|
||||||
}
|
}
|
||||||
|
|
||||||
} // create add account action item
|
} // create add account action item
|
||||||
else if (AccountListItem.TYPE_ACTION_ADD == accountListItem.getType() && mListener != null) {
|
else if (AccountListItem.TYPE_ACTION_ADD == accountListItem.getType() && accountListAdapterListener != null) {
|
||||||
setupAddAccountListItem(holder);
|
setupAddAccountListItem(holder.itemView);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets up a View to be used for adding a new account
|
* Sets up a View to be used for adding a new account
|
||||||
*
|
*
|
||||||
* @param holder the holder which contains the View to be used for the Add Account action
|
* @param actionView the action view
|
||||||
*/
|
*/
|
||||||
private void setupAddAccountListItem(AccountViewHolderItem holder) {
|
private void setupAddAccountListItem(View actionView) {
|
||||||
View actionView = holder.itemView;
|
|
||||||
|
|
||||||
holder.accountViewItem.setVisibility(View.INVISIBLE);
|
|
||||||
holder.checkViewItem.setVisibility(View.INVISIBLE);
|
|
||||||
TextView userName = actionView.findViewById(R.id.user_name);
|
|
||||||
userName.setText(R.string.prefs_add_account);
|
|
||||||
userName.setTextColor(ThemeUtils.primaryColor(mContext, true));
|
|
||||||
|
|
||||||
((ImageView) actionView.findViewById(R.id.user_icon)).setImageResource(R.drawable.ic_account_plus);
|
|
||||||
|
|
||||||
// bind action listener
|
// bind action listener
|
||||||
boolean isProviderOrOwnInstallationVisible = mContext.getResources()
|
boolean isProviderOrOwnInstallationVisible = context.getResources()
|
||||||
.getBoolean(R.bool.show_provider_or_own_installation);
|
.getBoolean(R.bool.show_provider_or_own_installation);
|
||||||
|
|
||||||
if (isProviderOrOwnInstallationVisible) {
|
if (isProviderOrOwnInstallationVisible) {
|
||||||
actionView.setOnClickListener(v -> mListener.showFirstRunActivity());
|
actionView.setOnClickListener(v -> accountListAdapterListener.showFirstRunActivity());
|
||||||
} else {
|
} else {
|
||||||
actionView.setOnClickListener(v -> mListener.createAccount());
|
actionView.setOnClickListener(v -> accountListAdapterListener.createAccount());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,8 +207,8 @@ public class AccountListAdapter extends RecyclerView.Adapter<AccountListAdapter.
|
||||||
try {
|
try {
|
||||||
View viewItem = viewHolder.imageViewItem;
|
View viewItem = viewHolder.imageViewItem;
|
||||||
viewItem.setTag(account.name);
|
viewItem.setTag(account.name);
|
||||||
DisplayUtils.setAvatar(account, this, mAccountAvatarRadiusDimension, mContext.getResources(), viewItem,
|
DisplayUtils.setAvatar(account, this, accountAvatarRadiusDimension, context.getResources(), viewItem,
|
||||||
mContext);
|
context);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log_OC.e(TAG, "Error calculating RGB value for account list item.", e);
|
Log_OC.e(TAG, "Error calculating RGB value for account list item.", e);
|
||||||
// use user icon as a fallback
|
// use user icon as a fallback
|
||||||
|
@ -222,7 +224,7 @@ public class AccountListAdapter extends RecyclerView.Adapter<AccountListAdapter.
|
||||||
*/
|
*/
|
||||||
private void setUsername(AccountViewHolderItem viewHolder, Account account) {
|
private void setUsername(AccountViewHolderItem viewHolder, Account account) {
|
||||||
try {
|
try {
|
||||||
OwnCloudAccount oca = new OwnCloudAccount(account, mContext);
|
OwnCloudAccount oca = new OwnCloudAccount(account, context);
|
||||||
viewHolder.usernameViewItem.setText(oca.getDisplayName());
|
viewHolder.usernameViewItem.setText(oca.getDisplayName());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log_OC.w(TAG, "Account not found right after being read; using account name instead");
|
Log_OC.w(TAG, "Account not found right after being read; using account name instead");
|
||||||
|
@ -248,25 +250,25 @@ public class AccountListAdapter extends RecyclerView.Adapter<AccountListAdapter.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int getItemCount() {
|
public int getItemCount() {
|
||||||
return this.mValues.size();
|
return this.values.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an AccountListItem from the specified position in the mValues list
|
* Returns an AccountListItem from the specified position in the values list
|
||||||
*
|
*
|
||||||
* @param position of the object to be returned
|
* @param position of the object to be returned
|
||||||
* @return An AccountListItem of the specified position
|
* @return An AccountListItem of the specified position
|
||||||
*/
|
*/
|
||||||
public AccountListItem getItem(int position) {
|
public AccountListItem getItem(int position) {
|
||||||
return mValues.get(position);
|
return values.get(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes the elements in the mValues list and notifies the Adapter
|
* Deletes the elements in the values list and notifies the Adapter
|
||||||
*/
|
*/
|
||||||
public void clear() {
|
public void clear() {
|
||||||
final int size = mValues.size();
|
final int size = values.size();
|
||||||
mValues.clear();
|
values.clear();
|
||||||
notifyItemRangeRemoved(0, size);
|
notifyItemRangeRemoved(0, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -276,10 +278,10 @@ public class AccountListAdapter extends RecyclerView.Adapter<AccountListAdapter.
|
||||||
* @param items The item list to be added
|
* @param items The item list to be added
|
||||||
*/
|
*/
|
||||||
public void addAll(List<AccountListItem> items){
|
public void addAll(List<AccountListItem> items){
|
||||||
if(mValues == null){
|
if(values == null){
|
||||||
mValues = new ArrayList<>();
|
values = new ArrayList<>();
|
||||||
}
|
}
|
||||||
mValues.addAll(items);
|
values.addAll(items);
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -310,7 +312,14 @@ public class AccountListAdapter extends RecyclerView.Adapter<AccountListAdapter.
|
||||||
this.usernameViewItem = view.findViewById(R.id.user_name);
|
this.usernameViewItem = view.findViewById(R.id.user_name);
|
||||||
this.accountViewItem = view.findViewById(R.id.account);
|
this.accountViewItem = view.findViewById(R.id.account);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Account ViewHolderItem to get smooth scrolling.
|
||||||
|
*/
|
||||||
|
static class AddAccountViewHolderItem extends RecyclerView.ViewHolder {
|
||||||
|
AddAccountViewHolderItem(@NonNull View view) {
|
||||||
|
super(view);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue