Implemented good suggestion by @delvh regarding performance increase

This commit is contained in:
DieGurke 2020-07-12 14:34:07 +02:00
parent d8eb2d6f6e
commit d2ec04b568
3 changed files with 8 additions and 19 deletions

View File

@ -29,6 +29,8 @@ public class Chat implements Serializable {
protected final Contact recipient;
protected final List<Message> messages = new ArrayList<>();
protected int unreadAmount;
private static final long serialVersionUID = 1L;
/**
@ -87,6 +89,7 @@ public class Chat implements Serializable {
writeProxy.writeMessageStatusChange(new MessageStatusChange(m));
}
}
unreadAmount = 0;
}
/**
@ -111,14 +114,9 @@ public class Chat implements Serializable {
messages.add(0, message);
}
public int getUnreadAmount() {
int unreadMessagesAmount = 0;
for (int i = messages.size() - 1; i >= 0; i--) {
if (messages.get(i).getSenderID() == recipient.getID() && messages.get(i).getStatus() == MessageStatus.RECEIVED) unreadMessagesAmount++;
else break;
}
return unreadMessagesAmount;
}
public void incrementUnreadAmount() { unreadAmount++; }
public int getUnreadAmount() { return unreadAmount; }
/**
* @return all messages in the current chat

View File

@ -37,16 +37,6 @@ public class GroupChat extends Chat {
this.sender = sender;
}
@Override
public int getUnreadAmount() {
int unreadMessagesAmount = 0;
for (int i = messages.size() - 1; i >= 0; i--) {
if (messages.get(i).getSenderID() != sender.getID() && messages.get(i).getStatus() == MessageStatus.RECEIVED) unreadMessagesAmount++;
else break;
}
return unreadMessagesAmount;
}
@Override
public void read(WriteProxy writeProxy) throws IOException {
for (int i = messages.size() - 1; i >= 0; --i) {
@ -60,5 +50,6 @@ public class GroupChat extends Chat {
}
}
}
super.unreadAmount = 0;
}
}

View File

@ -134,7 +134,7 @@ public final class ChatScene implements Restorable {
logger.log(Level.WARNING, "Could not read current chat: ", e1);
}
Platform.runLater(() -> { messageList.refresh(); scrollToMessageListEnd(); });
}
} else chat.incrementUnreadAmount();
// Moving chat with most recent unreadMessages to the top
Platform.runLater(() -> {
userList.getItems().remove(chat);