use view holder pattern in KeyListFragment

Closes #551
This commit is contained in:
Vincent Breitmoser 2014-04-10 19:25:44 +02:00
parent 31e8926d6f
commit 0687f7f40e

View File

@ -464,6 +464,31 @@ public class KeyListFragment extends Fragment
return super.swapCursor(newCursor); return super.swapCursor(newCursor);
} }
private class ItemViewHolder {
TextView mMainUserId;
TextView mMainUserIdRest;
View mStatusDivider;
FrameLayout mStatusLayout;
Button mButton;
TextView mRevoked;
ImageView mVerified;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mInflater.inflate(R.layout.key_list_item, parent, false);
ItemViewHolder holder = new ItemViewHolder();
holder.mMainUserId = (TextView) view.findViewById(R.id.mainUserId);
holder.mMainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest);
holder.mStatusDivider = (View) view.findViewById(R.id.status_divider);
holder.mStatusLayout = (FrameLayout) view.findViewById(R.id.status_layout);
holder.mButton = (Button) view.findViewById(R.id.edit);
holder.mRevoked = (TextView) view.findViewById(R.id.revoked);
holder.mVerified = (ImageView) view.findViewById(R.id.verified);
view.setTag(holder);
return view;
}
/** /**
* Bind cursor data to the item list view * Bind cursor data to the item list view
* <p/> * <p/>
@ -472,43 +497,36 @@ public class KeyListFragment extends Fragment
*/ */
@Override @Override
public void bindView(View view, Context context, Cursor cursor) { public void bindView(View view, Context context, Cursor cursor) {
ItemViewHolder h = (ItemViewHolder) view.getTag();
{ // set name and stuff, common to both key types { // set name and stuff, common to both key types
TextView mainUserId = (TextView) view.findViewById(R.id.mainUserId);
TextView mainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest);
String userId = cursor.getString(INDEX_USER_ID); String userId = cursor.getString(INDEX_USER_ID);
String[] userIdSplit = PgpKeyHelper.splitUserId(userId); String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
if (userIdSplit[0] != null) { if (userIdSplit[0] != null) {
mainUserId.setText(highlightSearchQuery(userIdSplit[0])); h.mMainUserId.setText(highlightSearchQuery(userIdSplit[0]));
} else { } else {
mainUserId.setText(R.string.user_id_no_name); h.mMainUserId.setText(R.string.user_id_no_name);
} }
if (userIdSplit[1] != null) { if (userIdSplit[1] != null) {
mainUserIdRest.setText(highlightSearchQuery(userIdSplit[1])); h.mMainUserIdRest.setText(highlightSearchQuery(userIdSplit[1]));
mainUserIdRest.setVisibility(View.VISIBLE); h.mMainUserIdRest.setVisibility(View.VISIBLE);
} else { } else {
mainUserIdRest.setVisibility(View.GONE); h.mMainUserIdRest.setVisibility(View.GONE);
} }
} }
{ // set edit button and revoked info, specific by key type { // set edit button and revoked info, specific by key type
View statusDivider = (View) view.findViewById(R.id.status_divider);
FrameLayout statusLayout = (FrameLayout) view.findViewById(R.id.status_layout);
Button button = (Button) view.findViewById(R.id.edit);
TextView revoked = (TextView) view.findViewById(R.id.revoked);
ImageView verified = (ImageView) view.findViewById(R.id.verified);
if (cursor.getInt(KeyListFragment.INDEX_HAS_SECRET) != 0) { if (cursor.getInt(KeyListFragment.INDEX_HAS_SECRET) != 0) {
// this is a secret key - show the edit button // this is a secret key - show the edit mButton
statusDivider.setVisibility(View.VISIBLE); h.mStatusDivider.setVisibility(View.VISIBLE);
statusLayout.setVisibility(View.VISIBLE); h.mStatusLayout.setVisibility(View.VISIBLE);
revoked.setVisibility(View.GONE); h.mRevoked.setVisibility(View.GONE);
verified.setVisibility(View.GONE); h.mVerified.setVisibility(View.GONE);
button.setVisibility(View.VISIBLE); h.mButton.setVisibility(View.VISIBLE);
final long id = cursor.getLong(INDEX_MASTER_KEY_ID); final long id = cursor.getLong(INDEX_MASTER_KEY_ID);
button.setOnClickListener(new OnClickListener() { h.mButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) { public void onClick(View view) {
Intent editIntent = new Intent(getActivity(), EditKeyActivity.class); Intent editIntent = new Intent(getActivity(), EditKeyActivity.class);
editIntent.setData(KeyRingData.buildSecretKeyRingUri(Long.toString(id))); editIntent.setData(KeyRingData.buildSecretKeyRingUri(Long.toString(id)));
@ -517,23 +535,23 @@ public class KeyListFragment extends Fragment
} }
}); });
} else { } else {
// this is a public key - hide the edit button, show if it's revoked // this is a public key - hide the edit mButton, show if it's revoked
statusDivider.setVisibility(View.GONE); h.mStatusDivider.setVisibility(View.GONE);
button.setVisibility(View.GONE); h.mButton.setVisibility(View.GONE);
boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0; boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0;
boolean isExpired = !cursor.isNull(INDEX_EXPIRY) boolean isExpired = !cursor.isNull(INDEX_EXPIRY)
&& new Date(cursor.getLong(INDEX_EXPIRY)*1000).before(new Date()); && new Date(cursor.getLong(INDEX_EXPIRY)*1000).before(new Date());
if(isRevoked || isExpired) { if(isRevoked || isExpired) {
statusLayout.setVisibility(View.VISIBLE); h.mStatusLayout.setVisibility(View.VISIBLE);
revoked.setVisibility(View.VISIBLE); h.mRevoked.setVisibility(View.VISIBLE);
verified.setVisibility(View.GONE); h.mVerified.setVisibility(View.GONE);
revoked.setText(isRevoked ? R.string.revoked : R.string.expired); h.mRevoked.setText(isRevoked ? R.string.revoked : R.string.expired);
} else { } else {
boolean isVerified = cursor.getInt(INDEX_VERIFIED) > 0; boolean isVerified = cursor.getInt(INDEX_VERIFIED) > 0;
statusLayout.setVisibility(isVerified ? View.VISIBLE : View.GONE); h.mStatusLayout.setVisibility(isVerified ? View.VISIBLE : View.GONE);
revoked.setVisibility(View.GONE); h.mRevoked.setVisibility(View.GONE);
verified.setVisibility(isVerified ? View.VISIBLE : View.GONE); h.mVerified.setVisibility(isVerified ? View.VISIBLE : View.GONE);
} }
} }
} }
@ -555,11 +573,6 @@ public class KeyListFragment extends Fragment
return mCursor.getLong(INDEX_MASTER_KEY_ID); return mCursor.getLong(INDEX_MASTER_KEY_ID);
} }
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.key_list_item, parent, false);
}
/** /**
* Creates a new header view and binds the section headers to it. It uses the ViewHolder * Creates a new header view and binds the section headers to it. It uses the ViewHolder
* pattern. Most functionality is similar to getView() from Android's CursorAdapter. * pattern. Most functionality is similar to getView() from Android's CursorAdapter.
@ -641,7 +654,7 @@ public class KeyListFragment extends Fragment
} }
} }
class HeaderViewHolder { private class HeaderViewHolder {
TextView mText; TextView mText;
TextView mCount; TextView mCount;
} }