Skip to content

Instantly share code, notes, and snippets.

@ZherebtsovAlexandr
Last active May 3, 2017 06:55
Show Gist options
  • Save ZherebtsovAlexandr/1267a67b3d1daa454dbba7b81865ea67 to your computer and use it in GitHub Desktop.
Save ZherebtsovAlexandr/1267a67b3d1daa454dbba7b81865ea67 to your computer and use it in GitHub Desktop.
public class MediumCardDelegate extends AdapterDelegate<List<EntityViewModel>> {
private final Context context;
private final ImageManager imageManager;
private final DeviceManager deviceManager;
private final LayoutInflater inflater;
private final CardListener listener;
private final BookmarksStorage bookmarksStorage;
private final float coefficient;
public MediumCardDelegate(Context context,
CardListener listener,
ImageManager imageManager,
DeviceManager deviceManager,
BookmarksStorage bookmarksStorage) {
this.context = context;
this.listener = listener;
this.imageManager = imageManager;
this.deviceManager = deviceManager;
this.bookmarksStorage = bookmarksStorage;
this.inflater = LayoutInflater.from(context);
TypedValue typedValue = new TypedValue();
context.getResources().getValue(R.dimen.medium_card_coefficient, typedValue, true);
this.coefficient = typedValue.getFloat();
}
@Override
protected boolean isForViewType(@NonNull List<EntityViewModel> items, int position) {
return items.get(position).getType() == EntityViewModel.MEDIUM;
}
@NonNull
@Override
protected RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
return new ItemViewHolder(inflater.inflate(R.layout.medium_card, parent, false));
}
@Override
protected void onBindViewHolder(@NonNull List<EntityViewModel> items, int position, @NonNull RecyclerView.ViewHolder holder, @NonNull List<Object> payloads) {
ItemViewHolder vh = (ItemViewHolder) holder;
final EntityViewModel entityViewModel = items.get(position);
ViewGroup.LayoutParams layoutParams = vh.container.getLayoutParams();
layoutParams.height = (int) (deviceManager.getScreenWidthInPx() * coefficient);
vh.container.setLayoutParams(layoutParams);
vh.container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onClick(entityViewModel);
}
});
vh.tvTitle.setText(entityViewModel.getTitle());
vh.tvCategoryName.setText(entityViewModel.getCategoryName());
if (entityViewModel.isBookmarked()) {
vh.ivBookmarked.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_bookmark_small_active));
} else {
vh.ivBookmarked.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_bookmark_small_white));
}
vh.ivBookmarked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (entityViewModel.isBookmarked()) {
bookmarksStorage.remove(entityViewModel.getEntityId(), entityViewModel.getEntityType());
} else {
bookmarksStorage.add(entityViewModel.getEntityId(), entityViewModel.getEntityType());
}
listener.onClickBookmark(entityViewModel);
}
});
Uri uri = Uri.parse(imageManager.getUrl(
entityViewModel.getImageName(),
(int) deviceManager.getScreenWidthInDp(),
(int) (deviceManager.getScreenWidthInDp() * coefficient))
);
vh.ivCover.setBackgroundColor(Color.parseColor(String.format("#%s", entityViewModel.getImageAverageColor())));
vh.ivCover.setImageURI(uri);
}
protected static class ItemViewHolder extends RecyclerView.ViewHolder {
public ViewGroup container;
public TextView tvTitle;
public TextView tvCategoryName;
public SimpleDraweeView ivCover;
public ImageView ivBookmarked;
public ItemViewHolder(View itemView) {
super(itemView);
container = (ViewGroup) itemView.findViewById(R.id.container);
tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
tvCategoryName = (TextView) itemView.findViewById(R.id.tv_categoryName);
ivCover = (SimpleDraweeView) itemView.findViewById(R.id.iv_cover);
ivBookmarked = (ImageView) itemView.findViewById(R.id.iv_bookmarked);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment