Skip to content

Commit

Permalink
Update controllers for no cache and fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheikah45 committed Nov 25, 2023
1 parent fca2b29 commit a49a2c8
Show file tree
Hide file tree
Showing 116 changed files with 2,044 additions and 1,838 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ dependencies {
testImplementation("org.testfx:testfx-junit5:4.0.17")
testImplementation("com.natpryce.hamcrest:hamcrest-reflection:0.1-2")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.testfx:openjfx-monocle:jdk-12.0.1+2")

annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
annotationProcessor("org.projectlombok:lombok")
Expand Down
246 changes: 97 additions & 149 deletions src/main/java/com/faforever/client/chat/AbstractChatTabController.java

Large diffs are not rendered by default.

56 changes: 24 additions & 32 deletions src/main/java/com/faforever/client/chat/ChannelTabController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.faforever.client.chat;

import com.faforever.client.audio.AudioService;
import com.faforever.client.chat.emoticons.EmoticonService;
import com.faforever.client.domain.PlayerBean;
import com.faforever.client.fx.FxApplicationThreadExecutor;
import com.faforever.client.fx.JavaFxUtil;
import com.faforever.client.fx.PlatformService;
import com.faforever.client.fx.SimpleChangeListener;
import com.faforever.client.fx.SimpleInvalidationListener;
import com.faforever.client.fx.WebViewConfigurer;
import com.faforever.client.i18n.I18n;
import com.faforever.client.navigation.NavigationHandler;
Expand All @@ -25,7 +22,6 @@
import javafx.collections.ListChangeListener;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;
import javafx.collections.WeakListChangeListener;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
Expand Down Expand Up @@ -67,7 +63,6 @@ public class ChannelTabController extends AbstractChatTabController {
private static final int TOPIC_CHARACTERS_LIMIT = 350;

private final PlatformService platformService;
private final AudioService audioService;

public Tab root;
public SplitPane splitPane;
Expand All @@ -91,13 +86,11 @@ public class ChannelTabController extends AbstractChatTabController {
.orElse(
FXCollections.emptyObservableList());
private final ListChangeListener<ChatChannelUser> channelUserListChangeListener = this::updateChangedUsersStyles;
private final WeakListChangeListener<ChatChannelUser> weakChannelUserListChangeListener = new WeakListChangeListener<>(
channelUserListChangeListener);


public ChannelTabController(WebViewConfigurer webViewConfigurer, LoginService loginService, ChatService chatService,
PlayerService playerService,
AudioService audioService, TimeService timeService, I18n i18n,
TimeService timeService, I18n i18n,
NotificationService notificationService, UiService uiService,
NavigationHandler navigationHandler,
CountryFlagService countryFlagService, EmoticonService emoticonService,
Expand All @@ -108,7 +101,6 @@ public ChannelTabController(WebViewConfigurer webViewConfigurer, LoginService lo
webViewConfigurer, emoticonService, countryFlagService, chatPrefs, notificationPrefs,
fxApplicationThreadExecutor, navigationHandler);
this.platformService = platformService;
this.audioService = audioService;
}

@Override
Expand All @@ -117,17 +109,12 @@ protected void onInitialize() {
JavaFxUtil.bindManagedToVisible(topicPane, chatUserList, changeTopicTextButton, topicTextField,
cancelChangesTopicTextButton, topicText, topicCharactersLimitLabel,
chatMessageSearchContainer);
JavaFxUtil.bind(topicCharactersLimitLabel.visibleProperty(), topicTextField.visibleProperty());
JavaFxUtil.bind(cancelChangesTopicTextButton.visibleProperty(), topicTextField.visibleProperty());
JavaFxUtil.bind(chatUserList.visibleProperty(), userListVisibilityToggleButton.selectedProperty());

BooleanExpression tabPaneShowing = BooleanExpression.booleanExpression(getRoot().tabPaneProperty()
.flatMap(
uiService::createShowingProperty));
ObservableValue<Boolean> showing = getRoot().selectedProperty()
.and(tabPaneShowing);

topicCharactersLimitLabel.visibleProperty().bind(topicTextField.visibleProperty());
cancelChangesTopicTextButton.visibleProperty().bind(topicTextField.visibleProperty());
chatUserList.visibleProperty().bind(userListVisibilityToggleButton.selectedProperty());
userListVisibilityToggleButton.selectedProperty().bindBidirectional(chatPrefs.playerListShownProperty());

topicTextField.setTextFormatter(new TextFormatter<>(change -> change.getControlNewText()
.length() <= TOPIC_CHARACTERS_LIMIT ? change : null));

Expand All @@ -144,10 +131,9 @@ protected void onInitialize() {
.or(topicTextField.visibleProperty())
.when(showing));

root.idProperty().bind(channelName.when(tabPaneShowing));
root.textProperty().bind(channelName.map(name -> name.replaceFirst("^#", "")).when(tabPaneShowing));
root.textProperty().bind(channelName.map(name -> name.replaceFirst("^#", "")).when(attached));

chatUserListController.chatChannelProperty().bind(chatChannel);
chatUserListController.chatChannelProperty().bind(chatChannel.when(showing));

ObservableValue<Boolean> isModerator = chatChannel.map(channel -> channel.getUser(loginService.getUsername())
.orElse(null))
Expand All @@ -159,30 +145,38 @@ protected void onInitialize() {
.and(topicTextField.visibleProperty().not())
.when(showing));

chatMessageSearchTextField.textProperty().addListener((SimpleChangeListener<String>) this::highlightText);

chatMessageSearchTextField.textProperty().when(showing).subscribe(this::highlightText);
chatPrefs.hideFoeMessagesProperty()
.when(showing)
.addListener((SimpleChangeListener<Boolean>) this::hideFoeMessages);
.subscribe(this::hideFoeMessages);
chatPrefs.chatColorModeProperty()
.when(showing)
.addListener((SimpleInvalidationListener) () -> users.getValue().forEach(this::updateUserMessageColor));
.subscribe(() -> users.getValue().forEach(this::updateUserMessageColor));
channelTopic.when(showing).subscribe(this::updateChannelTopic);
users.when(showing).addListener((observable, oldValue, newValue) -> {
userListVisibilityToggleButton.selectedProperty().when(showing).subscribe(this::updateDividerPosition);

users.when(attached).subscribe((oldValue, newValue) -> {
if (oldValue != null) {
oldValue.removeListener(weakChannelUserListChangeListener);
oldValue.removeListener(channelUserListChangeListener);
}
if (newValue != null) {
newValue.addListener(weakChannelUserListChangeListener);
newValue.addListener(channelUserListChangeListener);
}
});

userListVisibilityToggleButton.selectedProperty().when(showing).subscribe(this::updateDividerPosition);

AutoCompletionHelper autoCompletionHelper = getAutoCompletionHelper();
autoCompletionHelper.bindTo(messageTextField());
}

@Override
public void onDetached() {
super.onDetached();
ObservableList<ChatChannelUser> users = this.users.getValue();
if (users != null) {
users.removeListener(channelUserListChangeListener);
}
}

public AutoCompletionHelper getAutoCompletionHelper() {
return new AutoCompletionHelper(currentWord -> users.getValue()
.stream()
Expand Down Expand Up @@ -345,8 +339,6 @@ protected void onMention(ChatMessage chatMessage) {
.orElse(false)) {
log.debug("Ignored ping from {}", chatMessage.username());
} else if (!hasFocus()) {
audioService.playChatMentionSound();
showNotificationIfNecessary(chatMessage);
incrementUnreadMessagesCount();
setUnread(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableSet;
import javafx.scene.Node;
Expand Down Expand Up @@ -54,8 +53,6 @@ protected void onInitialize() {
}

private void bindProperties() {
ObservableValue<Boolean> showing = uiService.createShowingProperty(getRoot());

categoryLabel.styleProperty()
.bind(chatPrefs.groupToColorProperty()
.flatMap(groupToColor -> chatUserCategory.map(groupToColor::get))
Expand All @@ -64,17 +61,17 @@ private void bindProperties() {
.orElse("")
.when(showing));

categoryLabel.textProperty().bind(chatUserCategory.map(ChatUserCategory::getI18nKey).map(i18n::get));
categoryLabel.textProperty().bind(chatUserCategory.map(ChatUserCategory::getI18nKey).map(i18n::get).when(showing));

channelHiddenCategories.bind(Bindings.valueAt(chatPrefs.getChannelNameToHiddenCategories(), channelName)
.orElse(FXCollections.observableSet())
.when(showing));

arrowLabel.textProperty()
.bind(channelHiddenCategories.flatMap(hiddenCategories -> Bindings.createBooleanBinding(() -> hiddenCategories.contains(chatUserCategory.get()), hiddenCategories, chatUserCategory))
.map(hidden -> hidden ? "˃" : "˅"));
.map(hidden -> hidden ? "˃" : "˅").when(showing));

userCounterLabel.textProperty().bind(numCategoryItems.map(String::valueOf));
userCounterLabel.textProperty().bind(numCategoryItems.map(String::valueOf).when(showing));
}

public void onCategoryClicked(MouseEvent mouseEvent) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/faforever/client/chat/ChatChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.faforever.client.fx.JavaFxUtil;
import javafx.beans.Observable;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
Expand Down Expand Up @@ -33,6 +35,7 @@ public class ChatChannel {
private final ObjectProperty<ChannelTopic> topic = new SimpleObjectProperty<>(new ChannelTopic("", ""));
private final Set<Consumer<ChatMessage>> messageListeners = new HashSet<>();
private final List<ChatMessage> messages = new ArrayList<>();
private final BooleanProperty open = new SimpleBooleanProperty();

private int maxNumMessages = Integer.MAX_VALUE;

Expand Down Expand Up @@ -111,4 +114,16 @@ public boolean isPrivateChannel() {
public boolean isPartyChannel() {
return name.endsWith(ChatService.PARTY_CHANNEL_SUFFIX);
}

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

public BooleanProperty openProperty() {
return open;
}

public void setOpen(boolean open) {
this.open.set(open);
}
}
10 changes: 4 additions & 6 deletions src/main/java/com/faforever/client/chat/ChatController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.faforever.client.main.event.NavigateEvent;
import com.faforever.client.net.ConnectionState;
import com.faforever.client.theme.UiService;
import javafx.beans.binding.BooleanExpression;
import javafx.collections.ListChangeListener;
import javafx.collections.MapChangeListener;
import javafx.collections.WeakListChangeListener;
Expand Down Expand Up @@ -63,8 +62,6 @@ public class ChatController extends NodeController<AnchorPane> {
protected void onInitialize() {
super.onInitialize();

BooleanExpression showing = uiService.createShowingProperty(getRoot());

chatService.addChannelsListener(new WeakMapChangeListener<>(channelChangeListener));
chatService.getChannels().forEach(this::onChannelJoined);

Expand Down Expand Up @@ -130,9 +127,10 @@ private void addAndSelectTab(ChatChannel chatChannel) {
}
tabController.setChatChannel(chatChannel);
channelToChatTabController.put(chatChannel, tabController);

Tab tab = tabController.getRoot();
tab.setUserData(chatChannel);


if (chatService.isDefaultChannel(chatChannel)) {
tabPane.getTabs().add(0, tab);
tabPane.getSelectionModel().select(tab);
Expand All @@ -141,7 +139,7 @@ private void addAndSelectTab(ChatChannel chatChannel) {
tabPane.getTabs().add(tabPane.getTabs().size() - 1, tab);

if (chatChannel.isPrivateChannel() || tabPane.getSelectionModel().getSelectedIndex() == tabPane.getTabs()
.size() - 1) {
.size() - 1) {
tabPane.getSelectionModel().select(tab);
tabController.onDisplay();
}
Expand Down Expand Up @@ -179,7 +177,7 @@ protected void onNavigate(NavigateEvent navigateEvent) {
if (tabPane.getTabs().size() > 1) {
Tab tab = tabPane.getSelectionModel().getSelectedItem();
Optional.ofNullable(channelToChatTabController.get((ChatChannel) tab.getUserData()))
.ifPresent(AbstractChatTabController::onDisplay);
.ifPresent(AbstractChatTabController::onDisplay);
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/faforever/client/chat/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public interface ChatService {

void removeChannelsListener(MapChangeListener<String, ChatChannel> listener);

default void leaveChannel(String channelName) {
leaveChannel(getOrCreateChannel(channelName));
}

void leaveChannel(ChatChannel channel);

CompletableFuture<Void> sendActionInBackground(ChatChannel chatChannel, String action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private void initializePlayerNoteTooltip() {
noteTooltip = new Tooltip();
noteTooltip.setShowDelay(Duration.ZERO);
noteTooltip.setShowDuration(Duration.seconds(30));
noteTooltip.textProperty().isEmpty().addListener((observable, oldValue, newValue) -> {
noteTooltip.textProperty().isEmpty().when(showing).subscribe((oldValue, newValue) -> {
if (newValue) {
Tooltip.uninstall(userContainer, noteTooltip);
} else {
Expand Down Expand Up @@ -140,8 +140,6 @@ private void initializeStatusTooltip() {
}

public void installGameTooltip(GameTooltipController gameInfoController, Tooltip tooltip) {
ObservableValue<Boolean> showing = uiService.createShowingProperty(getRoot());

mapImageView.setOnMouseEntered(event -> gameInfoController.gameProperty()
.bind(chatUser.flatMap(ChatChannelUser::playerProperty).flatMap(PlayerBean::gameProperty).when(showing)));
Tooltip.install(mapImageView, tooltip);
Expand Down Expand Up @@ -208,8 +206,6 @@ public ObjectProperty<ChatChannelUser> chatUserProperty() {
}

private void bindProperties() {
ObservableValue<Boolean> showing = uiService.createShowingProperty(getRoot());

ObservableValue<PlayerBean> playerProperty = chatUser.flatMap(ChatChannelUser::playerProperty);
ObservableValue<GameBean> gameProperty = playerProperty.flatMap(PlayerBean::gameProperty);
BooleanExpression gameNotClosedObservable = BooleanExpression.booleanExpression(gameProperty.flatMap(GameBean::statusProperty)
Expand Down
Loading

0 comments on commit a49a2c8

Please sign in to comment.