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..80f0a304 100644 --- a/chatkit/src/main/java/com/stfalcon/chatkit/messages/MessagesListAdapter.java +++ b/chatkit/src/main/java/com/stfalcon/chatkit/messages/MessagesListAdapter.java @@ -51,7 +51,7 @@ public class MessagesListAdapter<MESSAGE extends IMessage> protected static boolean isSelectionModeEnabled; - protected List<Wrapper> items; + public List<Wrapper> items; private MessageHolders holders; private String senderId; @@ -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<MESSAGE> 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<MESSAGE> messages) { + public void generateDateHeaders(List<MESSAGE> 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<DATA> { - protected DATA item; - protected boolean isSelected; + public class Wrapper<DATA> { + public DATA item; + public boolean isSelected; Wrapper(DATA item) { this.item = item;