Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve image loader #72

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.stfalcon.chatkit.commons;

import android.widget.ImageView;

import com.stfalcon.chatkit.commons.models.IDialog;
import com.stfalcon.chatkit.commons.models.IUser;
import com.stfalcon.chatkit.commons.models.MessageContentType;

public interface ContextImageLoader {

void loadImage(ImageView imageView, IDialog dialog);

void loadImage(ImageView imageView, IUser user);

void loadImage(ImageView imageView, MessageContentType.Image messageContent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,30 @@

import android.widget.ImageView;

import com.stfalcon.chatkit.commons.models.IDialog;
import com.stfalcon.chatkit.commons.models.IUser;
import com.stfalcon.chatkit.commons.models.MessageContentType;

/**
* Callback for implementing images loading in message list
*/
public interface ImageLoader {
public abstract class ImageLoader implements ContextImageLoader {

public abstract void loadImage(ImageView imageView, String url);

void loadImage(ImageView imageView, String url);
@Override
public void loadImage(ImageView imageView, IDialog dialog) {
loadImage(imageView, dialog.getDialogPhoto());
}

@Override
public void loadImage(ImageView imageView, IUser user) {
loadImage(imageView, user.getAvatar());
}

@Override
public void loadImage(ImageView imageView, MessageContentType.Image messageContent) {
loadImage(imageView, messageContent.getImageUrl());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import android.widget.TextView;

import com.stfalcon.chatkit.R;
import com.stfalcon.chatkit.commons.ImageLoader;
import com.stfalcon.chatkit.commons.ContextImageLoader;
import com.stfalcon.chatkit.commons.ViewHolder;
import com.stfalcon.chatkit.commons.models.IDialog;
import com.stfalcon.chatkit.commons.models.IMessage;
Expand All @@ -54,7 +54,7 @@ public class DialogsListAdapter<DIALOG extends IDialog>
private List<DIALOG> items = new ArrayList<>();
private int itemLayoutId;
private Class<? extends BaseDialogViewHolder> holderClass;
private ImageLoader imageLoader;
private ContextImageLoader imageLoader;
private OnDialogClickListener<DIALOG> onDialogClickListener;
private OnDialogViewClickListener<DIALOG> onDialogViewClickListener;
private OnDialogLongClickListener<DIALOG> onLongItemClickListener;
Expand All @@ -67,7 +67,7 @@ public class DialogsListAdapter<DIALOG extends IDialog>
*
* @param imageLoader image loading method
*/
public DialogsListAdapter(ImageLoader imageLoader) {
public DialogsListAdapter(ContextImageLoader imageLoader) {
this(R.layout.item_dialog, DialogViewHolder.class, imageLoader);
}

Expand All @@ -77,7 +77,7 @@ public DialogsListAdapter(ImageLoader imageLoader) {
* @param itemLayoutId custom list item resource id
* @param imageLoader image loading method
*/
public DialogsListAdapter(@LayoutRes int itemLayoutId, ImageLoader imageLoader) {
public DialogsListAdapter(@LayoutRes int itemLayoutId, ContextImageLoader imageLoader) {
this(itemLayoutId, DialogViewHolder.class, imageLoader);
}

Expand All @@ -89,7 +89,7 @@ public DialogsListAdapter(@LayoutRes int itemLayoutId, ImageLoader imageLoader)
* @param imageLoader image loading method
*/
public DialogsListAdapter(@LayoutRes int itemLayoutId, Class<? extends BaseDialogViewHolder> holderClass,
ImageLoader imageLoader) {
ContextImageLoader imageLoader) {
this.itemLayoutId = itemLayoutId;
this.holderClass = holderClass;
this.imageLoader = imageLoader;
Expand Down Expand Up @@ -298,19 +298,19 @@ public void sort(Comparator<DIALOG> comparator) {
}

/**
* Register a callback to be invoked when image need to load.
*
* @param imageLoader image loading method
* @return registered image loader
*/
public void setImageLoader(ImageLoader imageLoader) {
this.imageLoader = imageLoader;
public ContextImageLoader getImageLoader() {
return imageLoader;
}

/**
* @return registered image loader
* Register a callback to be invoked when image need to load.
*
* @param imageLoader image loading method
*/
public ImageLoader getImageLoader() {
return imageLoader;
public void setImageLoader(ContextImageLoader imageLoader) {
this.imageLoader = imageLoader;
}

/**
Expand Down Expand Up @@ -414,7 +414,7 @@ public interface OnDialogViewLongClickListener<DIALOG extends IDialog> {
public abstract static class BaseDialogViewHolder<DIALOG extends IDialog>
extends ViewHolder<DIALOG> {

protected ImageLoader imageLoader;
protected ContextImageLoader imageLoader;
protected OnDialogClickListener<DIALOG> onDialogClickListener;
protected OnDialogLongClickListener<DIALOG> onLongItemClickListener;
protected OnDialogViewClickListener<DIALOG> onDialogViewClickListener;
Expand All @@ -425,7 +425,7 @@ public BaseDialogViewHolder(View itemView) {
super(itemView);
}

void setImageLoader(ImageLoader imageLoader) {
void setImageLoader(ContextImageLoader imageLoader) {
this.imageLoader = imageLoader;
}

Expand Down Expand Up @@ -592,12 +592,12 @@ public void onBind(final DIALOG dialog) {

//Set Dialog avatar
if (imageLoader != null) {
imageLoader.loadImage(ivAvatar, dialog.getDialogPhoto());
imageLoader.loadImage(ivAvatar, dialog);
}

//Set Last message user avatar
if (imageLoader != null) {
imageLoader.loadImage(ivLastMessageUser, dialog.getLastMessage().getUser().getAvatar());
imageLoader.loadImage(ivLastMessageUser, dialog.getLastMessage().getUser());
}
ivLastMessageUser.setVisibility(dialogStyle.isDialogMessageAvatarEnabled()
&& dialog.getUsers().size() > 1 ? VISIBLE : GONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ public IncomingImageMessageViewHolder(View itemView) {
public void onBind(MESSAGE message) {
super.onBind(message);
if (image != null && imageLoader != null) {
imageLoader.loadImage(image, message.getImageUrl());
imageLoader.loadImage(image, message);
}

if (imageOverlay != null) {
Expand Down Expand Up @@ -681,7 +681,7 @@ public OutcomingImageMessageViewHolder(View itemView) {
public void onBind(MESSAGE message) {
super.onBind(message);
if (image != null && imageLoader != null) {
imageLoader.loadImage(image, message.getImageUrl());
imageLoader.loadImage(image, message);
}

if (imageOverlay != null) {
Expand Down Expand Up @@ -770,7 +770,7 @@ public void onBind(MESSAGE message) {

userAvatar.setVisibility(isAvatarExists ? View.VISIBLE : View.GONE);
if (isAvatarExists) {
imageLoader.loadImage(userAvatar, message.getUser().getAvatar());
imageLoader.loadImage(userAvatar, message.getUser());
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ext {
circleImageViewVersion = '2.1.0'
shapeImageViewVersion = '0.9.3'
circleindicatorVersion = '1.2.2@aar'
textDrawableVersion = '1.0.1'
}

dependencies {
Expand All @@ -51,6 +52,7 @@ dependencies {
//ImageViews
compile "de.hdodenhof:circleimageview:$circleImageViewVersion"
compile "com.github.siyamed:android-shape-imageview:$shapeImageViewVersion"
compile "com.amulyakhare:com.amulyakhare.textdrawable:$textDrawableVersion"

//Utils
compile "me.relex:circleindicator:$circleindicatorVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.amulyakhare.textdrawable.TextDrawable;
import com.squareup.picasso.Picasso;
import com.stfalcon.chatkit.commons.ImageLoader;
import com.stfalcon.chatkit.commons.models.IUser;
import com.stfalcon.chatkit.dialogs.DialogsListAdapter;
import com.stfalcon.chatkit.sample.common.data.model.Dialog;
import com.stfalcon.chatkit.sample.utils.AppUtils;

import java.util.Random;

/*
* Created by troy379 on 05.04.17.
*/
Expand All @@ -26,9 +30,26 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

imageLoader = new ImageLoader() {

@Override
public void loadImage(ImageView imageView, String url) {
Picasso.with(DemoDialogsActivity.this).load(url).into(imageView);
Picasso.with(DemoDialogsActivity.this)
.load(url)
.into(imageView);
}

@Override
public void loadImage(ImageView imageView, IUser user) {
if(new Random().nextInt(2) == 1) {
Picasso.with(DemoDialogsActivity.this)
.load(user.getAvatar())
.into(imageView);
} else {
TextDrawable drawable = TextDrawable.builder().buildRound(
user.getName().substring(0, 2),
DemoDialogsActivity.this.getResources().getColor(android.R.color.holo_red_dark));
imageView.setImageDrawable(drawable);
}
}
};
}
Expand Down