refactor and optimize binder code

This commit is contained in:
AndyScherzinger 2018-06-23 22:50:52 +02:00
parent 620ecbea33
commit 9b1520c0f2
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B

View file

@ -613,65 +613,27 @@ class ContactListAdapter extends RecyclerView.Adapter<ContactListFragment.Contac
}
@Override
public ContactListFragment.ContactItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
@NonNull
public ContactListFragment.ContactItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.contactlist_list_item, parent, false);
return new ContactListFragment.ContactItemViewHolder(view);
}
@Override
public void onBindViewHolder(final ContactListFragment.ContactItemViewHolder holder, final int position) {
public void onBindViewHolder(@NonNull final ContactListFragment.ContactItemViewHolder holder, final int position) {
final int verifiedPosition = holder.getAdapterPosition();
final VCard vcard = vCards.get(verifiedPosition);
if (vcard != null) {
if (checkedVCards.contains(position)) {
holder.getName().setChecked(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
holder.getName().getCheckMarkDrawable()
.setColorFilter(ThemeUtils.primaryAccentColor(context), PorterDuff.Mode.SRC_ATOP);
}
} else {
holder.getName().setChecked(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
holder.getName().getCheckMarkDrawable().clearColorFilter();
}
}
setChecked(checkedVCards.contains(position), holder.getName());
holder.getName().setText(getDisplayName(vcard));
// photo
if (vcard.getPhotos().size() > 0) {
Photo firstPhoto = vcard.getPhotos().get(0);
String url = firstPhoto.getUrl();
byte[] data = firstPhoto.getData();
if (data != null && data.length > 0) {
Bitmap thumbnail = BitmapFactory.decodeByteArray(data, 0, data.length);
RoundedBitmapDrawable drawable = BitmapUtils.bitmapToCircularBitmapDrawable(context.getResources(),
thumbnail);
holder.getBadge().setImageDrawable(drawable);
} else if (url != null) {
ImageView badge = holder.getBadge();
SimpleTarget target = new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
holder.getBadge().setImageDrawable(resource);
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
holder.getBadge().setImageDrawable(errorDrawable);
}
};
DisplayUtils.downloadIcon(context, url, target, R.drawable.ic_user,
badge.getWidth(), badge.getHeight());
}
setPhoto(holder.getBadge(), vcard.getPhotos().get(0));
} else {
try {
holder.getBadge().setImageDrawable(
@ -689,6 +651,47 @@ class ContactListAdapter extends RecyclerView.Adapter<ContactListFragment.Contac
}
}
private void setPhoto(ImageView imageView, Photo firstPhoto) {
String url = firstPhoto.getUrl();
byte[] data = firstPhoto.getData();
if (data != null && data.length > 0) {
Bitmap thumbnail = BitmapFactory.decodeByteArray(data, 0, data.length);
RoundedBitmapDrawable drawable = BitmapUtils.bitmapToCircularBitmapDrawable(context.getResources(),
thumbnail);
imageView.setImageDrawable(drawable);
} else if (url != null) {
SimpleTarget target = new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
imageView.setImageDrawable(resource);
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
imageView.setImageDrawable(errorDrawable);
}
};
DisplayUtils.downloadIcon(context, url, target, R.drawable.ic_user, imageView.getWidth(),
imageView.getHeight());
}
}
private void setChecked(boolean checked, CheckedTextView checkedTextView) {
checkedTextView.setChecked(checked);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
if (checked) {
checkedTextView.getCheckMarkDrawable()
.setColorFilter(ThemeUtils.primaryAccentColor(context), PorterDuff.Mode.SRC_ATOP);
} else {
checkedTextView.getCheckMarkDrawable().clearColorFilter();
}
}
}
private void toggleVCard(ContactListFragment.ContactItemViewHolder holder, int verifiedPosition) {
holder.getName().setChecked(!holder.getName().isChecked());