diff --git a/chatkit/src/main/java/com/stfalcon/chatkit/messages/MessagesListAdapter.java b/chatkit/src/main/java/com/stfalcon/chatkit/messages/MessagesListAdapter.java index acc105f9..e0fcb8a2 100644 --- a/chatkit/src/main/java/com/stfalcon/chatkit/messages/MessagesListAdapter.java +++ b/chatkit/src/main/java/com/stfalcon/chatkit/messages/MessagesListAdapter.java @@ -213,6 +213,22 @@ public boolean update(String oldId, MESSAGE newMessage) { } } + /** + * Moves the elements position from current to start + * + * @param newMessage new message object. + */ + public void updateAndMoveToStart(MESSAGE newMessage) { + int position = getMessagePositionById(newMessage.getId()); + if (position >= 0) { + Wrapper element = new Wrapper<>(newMessage); + items.remove(position); + items.add(0, element); + notifyItemMoved(position, 0); + notifyItemChanged(0); + } + } + /** * Updates message by its id if it exists, add to start if not * @@ -224,6 +240,24 @@ public void upsert(MESSAGE message) { } } + /** + * Updates and moves to start if message by its id exists and if specified move to start, if not + * specified the item stays at current position and updated + * + * @param message message object to insert or update. + */ + public void upsert(MESSAGE message, boolean moveToStartIfUpdate) { + if (moveToStartIfUpdate) { + if (getMessagePositionById(message.getId()) > 0) { + updateAndMoveToStart(message); + } else { + upsert(message); + } + } else { + upsert(message); + } + } + /** * Deletes message. * @@ -494,7 +528,7 @@ private void recountDateHeaders() { } } - private void generateDateHeaders(List messages) { + protected void generateDateHeaders(List messages) { for (int i = 0; i < messages.size(); i++) { MESSAGE message = messages.get(i); this.items.add(new Wrapper<>(message)); @@ -653,9 +687,9 @@ void setStyle(MessagesListStyle style) { /* * WRAPPER * */ - private class Wrapper { - protected DATA item; - protected boolean isSelected; + public class Wrapper { + public DATA item; + public boolean isSelected; Wrapper(DATA item) { this.item = item;