Skip to content

Commit

Permalink
Merge pull request stfalcon-studio#209 from PrimozPernat/move_to_star…
Browse files Browse the repository at this point in the history
…t_if_update

Added upsert method to move a specific item to the start.
  • Loading branch information
bevzaanton authored Oct 12, 2018
2 parents 5edd804 + e1b9332 commit c143b4a
Showing 1 changed file with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -494,7 +528,7 @@ private void recountDateHeaders() {
}
}

private void generateDateHeaders(List<MESSAGE> messages) {
protected void generateDateHeaders(List<MESSAGE> messages) {
for (int i = 0; i < messages.size(); i++) {
MESSAGE message = messages.get(i);
this.items.add(new Wrapper<>(message));
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c143b4a

Please sign in to comment.