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

Grouping messages from the same author #271

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ public interface IMessage {
* @return the message creation date
*/
Date getCreatedAt();

}
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ protected ViewHolder getHolder(ViewGroup parent, int viewType, MessagesListStyle
}

@SuppressWarnings("unchecked")
protected void bind(final ViewHolder holder, final Object item, boolean isSelected,
protected void bind(final ViewHolder holder, final Object item, boolean isSelected, boolean isContinuous,
final ImageLoader imageLoader,
final View.OnClickListener onMessageClickListener,
final View.OnLongClickListener onMessageLongClickListener,
Expand All @@ -572,6 +572,7 @@ protected void bind(final ViewHolder holder, final Object item, boolean isSelect

if (item instanceof IMessage) {
((MessageHolders.BaseMessageViewHolder) holder).isSelected = isSelected;
((BaseMessageViewHolder) holder).isContinuous = isContinuous;
((MessageHolders.BaseMessageViewHolder) holder).imageLoader = imageLoader;
holder.itemView.setOnLongClickListener(onMessageLongClickListener);
holder.itemView.setOnClickListener(onMessageClickListener);
Expand Down Expand Up @@ -676,6 +677,7 @@ public static abstract class BaseMessageViewHolder<MESSAGE extends IMessage> ext

boolean isSelected;

boolean isContinuous;
/**
* For setting custom data to ViewHolder
*/
Expand Down Expand Up @@ -705,6 +707,15 @@ public boolean isSelected() {
return isSelected;
}

/**
* Returns whether item belongs to the same user
*
* @return weather item belongs to the same user.
*/
public boolean isContinuous() {
return isContinuous;
}

/**
* Returns weather is selection mode enabled
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
Expand All @@ -34,6 +35,7 @@
import com.stfalcon.chatkit.commons.ImageLoader;
import com.stfalcon.chatkit.commons.ViewHolder;
import com.stfalcon.chatkit.commons.models.IMessage;
import com.stfalcon.chatkit.commons.models.IUser;
import com.stfalcon.chatkit.utils.DateFormatter;

import java.util.ArrayList;
Expand Down Expand Up @@ -103,13 +105,45 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Wrapper wrapper = items.get(position);
holders.bind(holder, wrapper.item, wrapper.isSelected, imageLoader,
boolean isContinuous = false;
if (position > 0 && position != items.size() - 1) {
isContinuous = isContinuous(items.get(position), items.get(position - 1));
}
holders.bind(holder, wrapper.item, wrapper.isSelected, isContinuous, imageLoader,
getMessageClickListener(wrapper),
getMessageLongClickListener(wrapper),
dateHeadersFormatter,
viewClickListenersArray);
}

private boolean isContinuous(Wrapper currentMsg, Wrapper precedingMsg) {
// null check
if (currentMsg == null || precedingMsg == null) {
return false;
}

IUser currentUser = null, precedingUser = null;
if (currentMsg.item instanceof IMessage) {
currentUser = ((IMessage) currentMsg.item).getUser();
} else {
return false;
}
if (precedingMsg.item instanceof IMessage) {
precedingUser = ((IMessage) precedingMsg.item).getUser();
} else {
return false;
}


System.out.println(" ------------------ " + (!(currentUser == null || precedingUser == null)
&& currentUser.getId().equals(precedingUser.getId())));
// If admin message or
return !(currentUser == null || precedingUser == null)
&& currentUser.getId().equals(precedingUser.getId());


}

@Override
public int getItemCount() {
return items.size();
Expand Down Expand Up @@ -155,7 +189,11 @@ public void addToStart(MESSAGE message, boolean scroll) {
}
Wrapper<MESSAGE> element = new Wrapper<>(message);
items.add(0, element);
boolean isContinuous = isPreviousSameAuthor(message.getUser().getId(), 0);
notifyItemRangeInserted(0, isNewMessageToday ? 2 : 1);
if (isContinuous) {
notifyItemChanged(1);
}
if (layoutManager != null && scroll) {
layoutManager.scrollToPosition(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Message implements IMessage,
private User user;
private Image image;
private Voice voice;
private boolean isContinous;

public Message(String id, User user, String text) {
this(id, user, text, new Date());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stfalcon.chatkit.sample.features.demo.custom.holder.holders.messages;

import android.animation.Animator;
import android.view.View;

import com.stfalcon.chatkit.messages.MessageHolders;
Expand Down Expand Up @@ -29,5 +30,13 @@ public void onBind(Message message) {
} else {
onlineIndicator.setBackgroundResource(R.drawable.shape_bubble_offline);
}

if (isContinuous()) {
userAvatar.setVisibility(View.INVISIBLE);
onlineIndicator.setVisibility(View.GONE);
} else {
userAvatar.setVisibility(View.VISIBLE);
onlineIndicator.setVisibility(View.VISIBLE);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stfalcon.chatkit.sample.features.demo.custom.holder.holders.messages;

import android.animation.Animator;
import android.view.View;

import com.stfalcon.chatkit.messages.MessageHolders;
Expand Down Expand Up @@ -37,6 +38,13 @@ public void onClick(View view) {
}
}
});
if (isContinuous()) {
userAvatar.setVisibility(View.INVISIBLE);
onlineIndicator.setVisibility(View.GONE);
} else {
userAvatar.setVisibility(View.VISIBLE);
onlineIndicator.setVisibility(View.VISIBLE);
}
}

public static class Payload {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<de.hdodenhof.circleimageview.CircleImageView
android:id="@id/messageUserAvatar"
android:layout_width="40dp"
android:layout_width="20dp"
android:layout_height="40dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"/>
Expand Down