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

Add upsert method to documentation #207

Closed
wants to merge 10 commits into from
6 changes: 3 additions & 3 deletions chatkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
defaultConfig {
minSdkVersion 14
versionCode 1
versionName "0.3.1"
versionName "0.3.3"

consumerProguardFiles 'proguard.txt'
}
Expand All @@ -20,9 +20,9 @@ android {
}

publish {
groupId = 'com.github.stfalcon'
groupId = 'com.github.mathroule'
artifactId = 'chatkit'
publishVersion = '0.3.1'
publishVersion = '0.3.3'
desc = 'ChatKit - is a library designed to simplify the development of UI for such a trivial task as chat. It have flexible possibilities for styling, customizing and data management'
licences = ['Apache-2.0']
uploadName = 'ChatKit'
Expand Down
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 @@ -29,7 +29,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 @@ -55,7 +55,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 @@ -68,7 +68,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 @@ -78,7 +78,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 @@ -90,7 +90,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 @@ -353,7 +353,7 @@ public void sort(Comparator<DIALOG> comparator) {
/**
* @return registered image loader
*/
public ImageLoader getImageLoader() {
public ContextImageLoader getImageLoader() {
return imageLoader;
}

Expand All @@ -362,7 +362,7 @@ public ImageLoader getImageLoader() {
*
* @param imageLoader image loading method
*/
public void setImageLoader(ImageLoader imageLoader) {
public void setImageLoader(ContextImageLoader imageLoader) {
this.imageLoader = imageLoader;
}

Expand Down Expand Up @@ -474,7 +474,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 @@ -485,7 +485,7 @@ public BaseDialogViewHolder(View itemView) {
super(itemView);
}

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

Expand Down Expand Up @@ -651,23 +651,28 @@ public void onBind(final DIALOG dialog) {
tvDate.setText(formattedDate == null
? getDateString(lastMessageDate)
: formattedDate);
} else {
tvDate.setText(null);
}

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

//Set Last message user avatar with check if there is last message
if (imageLoader != null && dialog.getLastMessage() != null) {
imageLoader.loadImage(ivLastMessageUser, dialog.getLastMessage().getUser().getAvatar());
imageLoader.loadImage(ivLastMessageUser, dialog.getLastMessage().getUser());
}
ivLastMessageUser.setVisibility(dialogStyle.isDialogMessageAvatarEnabled()
&& dialog.getUsers().size() > 1 ? VISIBLE : GONE);
&& dialog.getUsers().size() > 1
&& dialog.getLastMessage() != null ? VISIBLE : GONE);

//Set Last message text
if (dialog.getLastMessage() != null) {
tvLastMessage.setText(dialog.getLastMessage().getText());
} else {
tvLastMessage.setText(null);
}

//Set Unread message count bubble
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ public IncomingImageMessageViewHolder(View itemView, Object payload) {
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 @@ -912,7 +912,7 @@ public OutcomingImageMessageViewHolder(View itemView, Object payload) {
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 @@ -1020,7 +1020,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
1 change: 1 addition & 0 deletions docs/COMPONENT_DIALOGS_LIST.MD
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ When your models are ready to be used by adapter, you can simply add them to the
* adapter.addItems(List<DIALOG> items) - adds a new dialog list to the end of the list;
* adapter.addItem(DIALOG dialog) - adds one dialog to the end of the list
* adapter.addItem(int position, DIALOG dialog) - adds a new dialog to the specified position.
* adapter.upsertItem(DIALOG dialog) - adds one dialog to the end of the list if not exists, otherwise updates the existing dialog.

#### Updating dialogs
If dialog has changed, you can update it by position in list by calling `adapter.updateItem(int position, DIALOG item)` or update it by dialog id by calling `adapter.updateItemById(DIALOG item)`
Expand Down
2 changes: 2 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ext {
circleImageViewVersion = '2.2.0'
shapeImageViewVersion = '0.9.3'
circleindicatorVersion = '1.2.2@aar'
textDrawableVersion = '1.0.1'
}

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

//Utils
implementation "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