Skip to content

Commit

Permalink
Move chat message show details logic up
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheikah45 committed May 11, 2024
1 parent 35ab746 commit c7ee7a8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 56 deletions.
14 changes: 7 additions & 7 deletions src/main/java/com/faforever/client/chat/ChatMessage.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ChatMessage {
private final Type type; private final Type type;
private final ChatMessage targetMessage; private final ChatMessage targetMessage;


private final BooleanProperty open = new SimpleBooleanProperty(); private final BooleanProperty seen = new SimpleBooleanProperty();
private final ObservableMap<Emoticon, ObservableMap<String, String>> reactions = FXCollections.synchronizedObservableMap( private final ObservableMap<Emoticon, ObservableMap<String, String>> reactions = FXCollections.synchronizedObservableMap(
FXCollections.observableHashMap()); FXCollections.observableHashMap());
private final ObservableMap<Emoticon, ObservableMap<String, String>> unmodifiableReactions = FXCollections.unmodifiableObservableMap( private final ObservableMap<Emoticon, ObservableMap<String, String>> unmodifiableReactions = FXCollections.unmodifiableObservableMap(
Expand Down Expand Up @@ -59,16 +59,16 @@ public void removeReaction(Reaction reaction) {
} }
} }


public boolean isOpen() { public boolean isSeen() {
return open.get(); return seen.get();
} }


public BooleanProperty openProperty() { public BooleanProperty seenProperty() {
return open; return seen;
} }


public void setOpen(boolean open) { public void setSeen(boolean seen) {
this.open.set(open); this.seen.set(seen);
} }


public enum Type { public enum Type {
Expand Down
70 changes: 35 additions & 35 deletions src/main/java/com/faforever/client/chat/ChatMessageCell.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@


import com.faforever.client.fx.FxApplicationThreadExecutor; import com.faforever.client.fx.FxApplicationThreadExecutor;
import com.faforever.client.theme.UiService; import com.faforever.client.theme.UiService;
import javafx.beans.binding.Bindings; import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty; import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.ReadOnlyIntegerProperty;
import javafx.beans.property.ReadOnlyIntegerWrapper;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import org.fxmisc.flowless.Cell; import org.fxmisc.flowless.Cell;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;


import java.util.Objects;
import java.util.function.Consumer; import java.util.function.Consumer;


@Component @Component
Expand All @@ -28,19 +28,15 @@ public class ChatMessageCell implements Cell<ChatMessage, Node> {


private final ObjectProperty<Consumer<ChatMessage>> onReplyButtonClicked = new SimpleObjectProperty<>(); private final ObjectProperty<Consumer<ChatMessage>> onReplyButtonClicked = new SimpleObjectProperty<>();
private final ObjectProperty<Consumer<ChatMessage>> onReplyClicked = new SimpleObjectProperty<>(); private final ObjectProperty<Consumer<ChatMessage>> onReplyClicked = new SimpleObjectProperty<>();
private final ObjectProperty<ObservableList<ChatMessage>> items = new SimpleObjectProperty<>(); private final ReadOnlyIntegerWrapper index = new ReadOnlyIntegerWrapper();
private final IntegerProperty index = new SimpleIntegerProperty(); private final BooleanProperty showDetails = new SimpleBooleanProperty();
private final ReadOnlyObjectWrapper<ChatMessage> item = new ReadOnlyObjectWrapper<>();


public ChatMessageCell(UiService uiService, FxApplicationThreadExecutor fxApplicationThreadExecutor) { public ChatMessageCell(UiService uiService, FxApplicationThreadExecutor fxApplicationThreadExecutor) {
this.fxApplicationThreadExecutor = fxApplicationThreadExecutor; this.fxApplicationThreadExecutor = fxApplicationThreadExecutor;
chatMessageController = uiService.loadFxml("theme/chat/chat_message.fxml"); chatMessageController = uiService.loadFxml("theme/chat/chat_message.fxml");
ObservableValue<ChatMessage> previousMessageProperty = items.flatMap( chatMessageController.chatMessageProperty().bind(item);
items -> Bindings.valueAt(items, index.subtract(1))); chatMessageController.showDetailsProperty().bind(showDetails);
chatMessageController.showDetailsProperty()
.bind(Bindings.createBooleanBinding(() -> showDetails(previousMessageProperty.getValue(),
chatMessageController.getChatMessage()),
previousMessageProperty,
chatMessageController.chatMessageProperty()));
chatMessageController.onReplyButtonClickedProperty().bind(onReplyButtonClicked); chatMessageController.onReplyButtonClickedProperty().bind(onReplyButtonClicked);
chatMessageController.onReplyClickedProperty().bind(onReplyClicked); chatMessageController.onReplyClickedProperty().bind(onReplyClicked);
} }
Expand All @@ -57,7 +53,7 @@ public boolean isReusable() {


@Override @Override
public void updateItem(ChatMessage item) { public void updateItem(ChatMessage item) {
fxApplicationThreadExecutor.execute(() -> chatMessageController.setChatMessage(item)); fxApplicationThreadExecutor.execute(() -> this.item.set(item));
} }


@Override @Override
Expand All @@ -67,19 +63,7 @@ public void updateIndex(int index) {


@Override @Override
public void reset() { public void reset() {
fxApplicationThreadExecutor.execute(() -> chatMessageController.setChatMessage(null)); fxApplicationThreadExecutor.execute(() -> this.item.set(null));
}

private boolean showDetails(ChatMessage previousMessage, ChatMessage currentMessage) {
if (currentMessage == null) {
return false;
}

if (previousMessage == null) {
return true;
}

return !Objects.equals(previousMessage.getSender(), currentMessage.getSender());
} }


public Consumer<ChatMessage> getOnReplyButtonClicked() { public Consumer<ChatMessage> getOnReplyButtonClicked() {
Expand All @@ -106,15 +90,31 @@ public void setOnReplyClicked(Consumer<ChatMessage> onReplyClicked) {
this.onReplyClicked.set(onReplyClicked); this.onReplyClicked.set(onReplyClicked);
} }


public ObservableList<ChatMessage> getItems() { public boolean isShowDetails() {
return items.get(); return showDetails.get();
}

public BooleanProperty showDetailsProperty() {
return showDetails;
}

public void setShowDetails(boolean showDetails) {
this.showDetails.set(showDetails);
}

public int getIndex() {
return index.get();
}

public ReadOnlyIntegerProperty indexProperty() {
return index.getReadOnlyProperty();
} }


public ObjectProperty<ObservableList<ChatMessage>> itemsProperty() { public ReadOnlyObjectProperty<ChatMessage> itemProperty() {
return items; return item.getReadOnlyProperty();
} }


public void setItems(ObservableList<ChatMessage> items) { public ChatMessage getItem() {
this.items.set(items); return item.get();
} }
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ protected void onInitialize() {
chatMessage.when(attached).subscribe((oldValue, newValue) -> { chatMessage.when(attached).subscribe((oldValue, newValue) -> {
if (oldValue != null) { if (oldValue != null) {
oldValue.getReactions().removeListener(reactionChangeListener); oldValue.getReactions().removeListener(reactionChangeListener);
oldValue.openProperty().unbind();
oldValue.setOpen(false);
} }


reactionNodeMap.clear(); reactionNodeMap.clear();
Expand All @@ -172,7 +170,7 @@ protected void onInitialize() {
if (newValue != null) { if (newValue != null) {
newValue.getReactions().forEach(this::addReaction); newValue.getReactions().forEach(this::addReaction);
newValue.getReactions().addListener(reactionChangeListener); newValue.getReactions().addListener(reactionChangeListener);
newValue.openProperty().bind(showing); newValue.setSeen(true);
} }
}); });


Expand All @@ -195,15 +193,6 @@ protected void onInitialize() {
.when(showing)); .when(showing));
} }


@Override
protected void onDetached() {
ChatMessage chatMessage = getChatMessage();
if (chatMessage != null) {
chatMessage.openProperty().unbind();
chatMessage.setOpen(false);
}
}

private void onReactionChange( private void onReactionChange(
MapChangeListener.Change<? extends Emoticon, ? extends ObservableMap<String, String>> change) { MapChangeListener.Change<? extends Emoticon, ? extends ObservableMap<String, String>> change) {
Emoticon reaction = change.getKey(); Emoticon reaction = change.getKey();
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.faforever.client.util.ConcurrentUtil; import com.faforever.client.util.ConcurrentUtil;
import com.faforever.client.util.PopupUtil; import com.faforever.client.util.PopupUtil;
import javafx.beans.Observable; import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty; import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
Expand Down Expand Up @@ -50,6 +51,7 @@
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors; import java.util.stream.Collectors;


Expand Down Expand Up @@ -132,7 +134,7 @@ protected void onInitialize() {


if (newValue != null) { if (newValue != null) {
newValue.getMessages().addListener(chatMessageListener); newValue.getMessages().addListener(chatMessageListener);
newValue.getMessages().forEach(message -> fxApplicationThreadExecutor.execute(() -> rawMessages.add(message))); fxApplicationThreadExecutor.execute(() -> rawMessages.addAll(newValue.getMessages()));
ObservableList<ChatChannelUser> typingUsers = newValue.getTypingUsers(); ObservableList<ChatChannelUser> typingUsers = newValue.getTypingUsers();
setTypingLabel(typingUsers); setTypingLabel(typingUsers);
typingUsers.addListener(typingUsersChangeListener); typingUsers.addListener(typingUsersChangeListener);
Expand Down Expand Up @@ -161,7 +163,13 @@ protected void onInitialize() {


messageListView = VirtualFlow.createVertical(filteredMessages, item -> { messageListView = VirtualFlow.createVertical(filteredMessages, item -> {
ChatMessageCell cell = chatMessageCellFactory.getObject(); ChatMessageCell cell = chatMessageCellFactory.getObject();
cell.setItems(filteredMessages); cell.showDetailsProperty()
.bind(Bindings.valueAt(filteredMessages, cell.indexProperty().subtract(1))
.flatMap(previousMessage -> cell.itemProperty()
.map(currentMessage -> showDetails(previousMessage,
currentMessage)))
.orElse(true)
.when(showing));
cell.updateItem(item); cell.updateItem(item);
cell.setOnReplyButtonClicked(message -> { cell.setOnReplyButtonClicked(message -> {
targetMessage.set(message); targetMessage.set(message);
Expand All @@ -185,6 +193,18 @@ protected void onInitialize() {
}); });
} }


private boolean showDetails(ChatMessage previousMessage, ChatMessage currentMessage) {
if (currentMessage == null) {
return false;
}

if (previousMessage == null) {
return true;
}

return !Objects.equals(previousMessage.getSender(), currentMessage.getSender());
}

private void scrollToEnd() { private void scrollToEnd() {
fxApplicationThreadExecutor.execute(() -> messageListView.showAsLast(filteredMessages.size() - 1)); fxApplicationThreadExecutor.execute(() -> messageListView.showAsLast(filteredMessages.size() - 1));
} }
Expand Down Expand Up @@ -325,6 +345,7 @@ private void sendMessage() {
messageTextField.setDisable(false); messageTextField.setDisable(false);
messageTextField.requestFocus(); messageTextField.requestFocus();
removeReply(); removeReply();
scrollToEnd();
}, fxApplicationThreadExecutor); }, fxApplicationThreadExecutor);
} }


Expand Down

0 comments on commit c7ee7a8

Please sign in to comment.