Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:FAForever/downlords-faf-client i…
Browse files Browse the repository at this point in the history
…nto feature/FAForever#3023-replay-card-enhancement
  • Loading branch information
obydog002 committed Jan 6, 2024
2 parents ba48149 + d927569 commit f9bbd8a
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 156 deletions.
26 changes: 21 additions & 5 deletions src/main/java/com/faforever/client/chat/ChatChannel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.faforever.client.chat;

import com.faforever.client.chat.ChatMessage.Type;
import com.faforever.client.fx.JavaFxUtil;
import com.google.common.annotations.VisibleForTesting;
import javafx.beans.Observable;
Expand All @@ -13,10 +14,12 @@
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
Expand All @@ -25,6 +28,10 @@
@ToString(onlyExplicitlyIncluded = true)
public class ChatChannel {

private static final Comparator<ChatMessage> CHAT_MESSAGE_COMPARATOR = Comparator.comparing(ChatMessage::getType)
.thenComparing(ChatMessage::getTime,
Comparator.nullsLast(
Comparator.naturalOrder()));
@Getter
@EqualsAndHashCode.Include
@ToString.Include
Expand All @@ -39,9 +46,12 @@ public class ChatChannel {
private final ObservableList<ChatChannelUser> unmodifiableUsers = FXCollections.unmodifiableObservableList(users);
private final ObservableList<ChatChannelUser> typingUsers = new FilteredList<>(users, ChatChannelUser::isTyping);
private final ObjectProperty<ChannelTopic> topic = new SimpleObjectProperty<>(new ChannelTopic(null, ""));
private final ObservableMap<String, ChatMessage> messagesById = FXCollections.synchronizedObservableMap(
FXCollections.observableHashMap());
private final ObservableList<ChatMessage> rawMessages = JavaFxUtil.attachListToMap(
FXCollections.synchronizedObservableList(FXCollections.observableArrayList()), messagesById);
private final ObservableList<ChatMessage> messages = FXCollections.synchronizedObservableList(
FXCollections.observableArrayList());
private final ObservableList<ChatMessage> unmodifiableMessages = FXCollections.unmodifiableObservableList(messages);
new SortedList<>(rawMessages, CHAT_MESSAGE_COMPARATOR));
private final BooleanProperty open = new SimpleBooleanProperty();
private final IntegerProperty maxNumMessages = new SimpleIntegerProperty(Integer.MAX_VALUE);
private final IntegerProperty numUnreadMessages = new SimpleIntegerProperty();
Expand All @@ -60,7 +70,8 @@ private void pruneMessages() {
int maxNumMessages = getMaxNumMessages();
int numMessages = messages.size();
if (numMessages > maxNumMessages) {
messages.subList(0, numMessages - maxNumMessages).clear();
List.copyOf(messages.subList(0, numMessages - maxNumMessages))
.forEach(message -> messagesById.remove(message.getId()));
}
}

Expand Down Expand Up @@ -134,13 +145,18 @@ public Optional<ChatChannelUser> getUser(String username) {
return Optional.ofNullable(usernameToChatUser.get(username));
}

public void removePendingMessage(String messageId) {
messagesById.computeIfPresent(messageId,
(ignored, chatMessage) -> chatMessage.getType() == Type.PENDING ? null : chatMessage);
}

public void addMessage(ChatMessage message) {
messages.add(message);
messagesById.put(message.getId(), message);
pruneMessages();
}

public ObservableList<ChatMessage> getMessages() {
return unmodifiableMessages;
return messages;
}

public boolean isPrivateChannel() {
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/com/faforever/client/chat/ChatMessage.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
package com.faforever.client.chat;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.time.Instant;

public record ChatMessage(Instant time, ChatChannelUser sender, String message, String id, boolean action) {
@RequiredArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Getter
public class ChatMessage {

@EqualsAndHashCode.Include
private final String id;
private final Instant time;
private final ChatChannelUser sender;
private final String content;
private final Type type;

public ChatMessage(Instant time, ChatChannelUser sender, String message, String id) {
this(time, sender, message, id, false);
public enum Type {
MESSAGE, ACTION, PENDING
}
}
13 changes: 10 additions & 3 deletions src/main/java/com/faforever/client/chat/ChatMessageController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.faforever.client.chat;

import com.faforever.client.avatar.AvatarService;
import com.faforever.client.chat.ChatMessage.Type;
import com.faforever.client.chat.emoticons.EmoticonService;
import com.faforever.client.domain.AvatarBean;
import com.faforever.client.domain.PlayerBean;
import com.faforever.client.fx.ImageViewHelper;
import com.faforever.client.fx.JavaFxUtil;
import com.faforever.client.fx.NodeController;
import com.faforever.client.fx.PlatformService;
import com.faforever.client.i18n.I18n;
import com.faforever.client.player.CountryFlagService;
import com.faforever.client.util.TimeService;
import javafx.beans.property.BooleanProperty;
Expand Down Expand Up @@ -53,6 +55,7 @@ public class ChatMessageController extends NodeController<VBox> {
private final ChatService chatService;
private final EmoticonService emoticonService;
private final ImageViewHelper imageViewHelper;
private final I18n i18n;

public VBox root;
public HBox detailsContainer;
Expand All @@ -76,7 +79,7 @@ protected void onInitialize() {

mentionPattern = chatService.getMentionPattern();

ObservableValue<ChatChannelUser> sender = chatMessage.map(ChatMessage::sender);
ObservableValue<ChatChannelUser> sender = chatMessage.map(ChatMessage::getSender);
ObservableValue<PlayerBean> player = sender.flatMap(ChatChannelUser::playerProperty);
avatarImageView.imageProperty()
.bind(player.flatMap(PlayerBean::avatarProperty)
Expand Down Expand Up @@ -104,8 +107,12 @@ protected void onInitialize() {
chatService.onInitiatePrivateChat(username);
}
});
timeLabel.textProperty().bind(chatMessage.map(ChatMessage::time).map(timeService::asShortTime).when(showing));
chatMessage.map(ChatMessage::message).map(this::convertMessageToNodes).when(showing).subscribe(messageNodes -> {
timeLabel.textProperty()
.bind(chatMessage.map(message -> message.getType() != Type.PENDING ? message.getTime() : null)
.map(timeService::asShortTime)
.orElse(i18n.get("pending"))
.when(showing));
chatMessage.map(ChatMessage::getContent).map(this::convertMessageToNodes).when(showing).subscribe(messageNodes -> {
Collection<? extends Node> children = messageNodes == null ? List.of() : messageNodes;
message.getChildren().setAll(children);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ private boolean showDetails(ChatMessage previousMessage, ChatMessage currentMess
return true;
}

return !Objects.equals(previousMessage.sender(), currentMessage.sender());
return !Objects.equals(previousMessage.getSender(), currentMessage.getSender());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -81,7 +79,7 @@ public class ChatMessageViewController extends NodeController<VBox> {
private final ListChangeListener<ChatChannelUser> typingUserListChangeListener = this::updateTypingUsersLabel;

private final ObservableList<ChatMessage> rawMessages = FXCollections.synchronizedObservableList(
FXCollections.observableArrayList(chatMessage -> new Observable[]{chatMessage.sender().categoryProperty()}));
FXCollections.observableArrayList(chatMessage -> new Observable[]{chatMessage.getSender().categoryProperty()}));
private final FilteredList<ChatMessage> filteredMessages = new FilteredList<>(rawMessages);

private Popup emoticonsPopup;
Expand All @@ -95,7 +93,7 @@ protected void onInitialize() {
if (!hideFoes) {
return message -> true;
} else {
return message -> message.sender().getCategory() != ChatUserCategory.FOE;
return message -> message.getSender().getCategory() != ChatUserCategory.FOE;
}
}));

Expand Down Expand Up @@ -249,15 +247,8 @@ private void sendMessage() {
messageTextField.setDisable(true);

final String text = messageTextField.getText();
CompletableFuture<Void> sendFuture;
if (text.startsWith(ACTION_PREFIX)) {
sendFuture = chatService.sendActionInBackground(chatChannel.get(),
text.replaceFirst(Pattern.quote(ACTION_PREFIX), ""));
} else {
sendFuture = chatService.sendMessageInBackground(chatChannel.get(), text);
}

sendFuture.whenComplete((result, throwable) -> {
chatService.sendMessageInBackground(chatChannel.get(), text).whenComplete((result, throwable) -> {
if (throwable != null) {
throwable = ConcurrentUtil.unwrapIfCompletionException(throwable);
log.warn("Message could not be sent: {}", text, throwable);
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/faforever/client/chat/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ default void leaveChannel(String channelName) {

void leaveChannel(ChatChannel channel);

CompletableFuture<Void> sendActionInBackground(ChatChannel chatChannel, String action);

void joinChannel(String channelName);

boolean isDefaultChannel(ChatChannel chatChannel);
Expand Down

0 comments on commit f9bbd8a

Please sign in to comment.