Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify notification service #3087

Merged
merged 3 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/main/java/com/faforever/client/FafClientApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,7 @@
notificationService.addNotification(new ImmediateNotification(i18n.get("exitWarning.title"),
i18n.get("exitWarning.message"),
Severity.WARN,
List.of(
new Action(i18n.get("yes"), ev -> {
Platform.exit();
}),
new Action(i18n.get("no"), ev -> {
})
List.of(new Action(i18n.get("yes"), Platform::exit), new Action(i18n.get("no"), () -> {})

Check warning on line 98 in src/main/java/com/faforever/client/FafClientApplication.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/FafClientApplication.java#L98

Added line #L98 was not covered by tests
)));
event.consume();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
null, ACHIEVEMENT_IMAGE_SIZE, ACHIEVEMENT_IMAGE_SIZE);
} catch (MalformedURLException e) {
log.warn("Could not load achievement image bad url for achievement: {}", achievementDefinition.getName(), e);
notificationService.addPersistentErrorNotification(e, "achievements.load.badUrl", achievementDefinition.getName());
notificationService.addPersistentErrorNotification("achievements.load.badUrl", achievementDefinition.getName());

Check warning on line 73 in src/main/java/com/faforever/client/achievements/AchievementService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/achievements/AchievementService.java#L73

Added line #L73 was not covered by tests
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ public abstract class AbstractChatTabController extends TabController {
private static final String JOIN_PREFIX = "/join ";
private static final String WHOIS_PREFIX = "/whois ";
/**
* Added if a message is what IRC calls an "action".
* Added if a message is what IRC calls an "onAction".
*/
private static final String ACTION_CSS_CLASS = "action";
private static final String ACTION_CSS_CLASS = "onAction";
private static final String MESSAGE_CSS_CLASS = "message";

protected final ChatService chatService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,7 @@
if (!chatChannel.isOpen() && notificationPrefs.isPrivateMessageToastEnabled()) {
notificationService.addNotification(
new TransientNotification(sender.getUsername(), text, IdenticonUtil.createIdenticon(identIconSource),
evt -> {
navigationHandler.navigateTo(new NavigateEvent(NavigationItem.CHAT));
}));
() -> navigationHandler.navigateTo(new NavigateEvent(NavigationItem.CHAT))));

Check warning on line 314 in src/main/java/com/faforever/client/chat/KittehChatService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/chat/KittehChatService.java#L314

Added line #L314 was not covered by tests
}
}
}
Expand All @@ -328,9 +326,8 @@
.map(String::valueOf)
.orElse(sender);
notificationService.addNotification(
new TransientNotification(sender, text, IdenticonUtil.createIdenticon(identIconSource), evt -> {
navigationHandler.navigateTo(new NavigateEvent(NavigationItem.CHAT));
}));
new TransientNotification(sender, text, IdenticonUtil.createIdenticon(identIconSource),
() -> navigationHandler.navigateTo(new NavigateEvent(NavigationItem.CHAT))));

Check warning on line 330 in src/main/java/com/faforever/client/chat/KittehChatService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/chat/KittehChatService.java#L330

Added line #L330 was not covered by tests
}
}
}
Expand Down
31 changes: 4 additions & 27 deletions src/main/java/com/faforever/client/config/AsyncConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@EnableAsync
@EnableScheduling
Expand All @@ -27,10 +23,12 @@
public class AsyncConfig implements AsyncConfigurer, SchedulingConfigurer {

private final GlobalExceptionHandler globalExceptionHandler;
private final ExecutorService taskExecutor;
private final TaskScheduler taskScheduler;

@Override
public Executor getAsyncExecutor() {
return taskExecutor();
return taskExecutor;

Check warning on line 31 in src/main/java/com/faforever/client/config/AsyncConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/config/AsyncConfig.java#L31

Added line #L31 was not covered by tests
}

@Override
Expand All @@ -40,27 +38,6 @@

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setTaskScheduler(taskScheduler());
}

@Bean
public ExecutorService taskExecutor() {
return Executors.newCachedThreadPool();
}

@Bean
public TaskScheduler taskScheduler() {
return new ThreadPoolTaskScheduler();
}

@Bean
public DestructionAwareBeanPostProcessor threadPoolShutdownProcessor() {
return (Object bean, String beanName) -> {
if ("taskExecutor".equals(beanName)) {
log.info("Shutting down ExecutorService '" + beanName + "'");
ExecutorService executor = (ExecutorService) bean;
executor.shutdownNow();
}
};
taskRegistrar.setTaskScheduler(taskScheduler);

Check warning on line 41 in src/main/java/com/faforever/client/config/AsyncConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/config/AsyncConfig.java#L41

Added line #L41 was not covered by tests
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/faforever/client/config/BaseConfig.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.faforever.client.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* This configuration has to be imported by other configurations and should only contain beans that are necessary to run
Expand All @@ -25,4 +31,25 @@
messageSource.setFallbackToSystemLocale(false);
return messageSource;
}

@Bean
public ExecutorService taskExecutor() {
return Executors.newCachedThreadPool();

Check warning on line 37 in src/main/java/com/faforever/client/config/BaseConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/config/BaseConfig.java#L37

Added line #L37 was not covered by tests
}

@Bean
public TaskScheduler taskScheduler() {
return new ThreadPoolTaskScheduler();

Check warning on line 42 in src/main/java/com/faforever/client/config/BaseConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/config/BaseConfig.java#L42

Added line #L42 was not covered by tests
}

@Bean
public DestructionAwareBeanPostProcessor threadPoolShutdownProcessor() {
return (Object bean, String beanName) -> {

Check warning on line 47 in src/main/java/com/faforever/client/config/BaseConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/config/BaseConfig.java#L47

Added line #L47 was not covered by tests
if ("taskExecutor".equals(beanName)) {
log.info("Shutting down ExecutorService '" + beanName + "'");
ExecutorService executor = (ExecutorService) bean;
executor.shutdownNow();

Check warning on line 51 in src/main/java/com/faforever/client/config/BaseConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/config/BaseConfig.java#L49-L51

Added lines #L49 - L51 were not covered by tests
}
};

Check warning on line 53 in src/main/java/com/faforever/client/config/BaseConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/config/BaseConfig.java#L53

Added line #L53 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ protected void onInitialize() {
selectionModel.selectFirst();
}
}, fxApplicationThreadExecutor).exceptionally(throwable -> {
notificationService.addPersistentErrorNotification(throwable, "coop.couldNotLoad",
notificationService.addPersistentErrorNotification("coop.couldNotLoad",
throwable.getLocalizedMessage());
return null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@

@Override
public void handleUncaughtException(@NotNull Throwable ex, @NotNull Method method, Object @NotNull ... params) {
if (ex instanceof NotifiableException) {
if (ex instanceof NotifiableException notifiableException) {
log.error("Exception on Method {} with parameters {}: ", method.getName(), params, ex);
notificationService.addErrorNotification((NotifiableException) ex);
notificationService.addErrorNotification(notifiableException);

Check warning on line 43 in src/main/java/com/faforever/client/exception/GlobalExceptionHandler.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/exception/GlobalExceptionHandler.java#L43

Added line #L43 was not covered by tests
} else {
log.error("Uncaught Exception on Method {} with parameters {}: ", method.getName(), params, ex);
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/faforever/client/fa/GameFullNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public void run() {

notificationService.addNotification(new TransientNotification(i18n.get("game.full"), i18n.get("game.full.action"),
mapService.loadPreview(runningGame.getMapFolderName(),
PreviewSize.SMALL),
ignored -> {
PreviewSize.SMALL), () -> {
if (platformService.isWindowFocused(faWindowTitle)) {
// Switching to the game lobby window from replay window may not work correctly (no interaction) for reasons:
// 1) The game has full screen mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

import javafx.scene.control.MenuItem;
import javafx.scene.layout.Region;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public abstract class AbstractMenuItem<T> extends MenuItem {

protected T object;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/faforever/client/game/GamePathHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public void afterPropertiesSet() {

public void notifyMissingGamePath(boolean immediateUserActionRequired) {
List<Action> actions = Collections.singletonList(
new Action(i18n.get("missingGamePath.locate"),
chooseEvent -> chooseAndValidateGameDirectory())
new Action(i18n.get("missingGamePath.locate"), this::chooseAndValidateGameDirectory)
);
String notificationText = i18n.get("missingGamePath.notification");

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/faforever/client/game/GameRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.faforever.client.notification.ImmediateNotification;
import com.faforever.client.notification.NotificationService;
import com.faforever.client.notification.PersistentNotification;
import com.faforever.client.notification.ServerNotification;
import com.faforever.client.notification.Severity;
import com.faforever.client.os.OperatingSystem;
import com.faforever.client.player.PlayerService;
Expand Down Expand Up @@ -309,7 +310,7 @@
i18n.get("game.joinGameRatingConfirmation.text",
game.getRatingMin(), game.getRatingMax(),
playerRating), Severity.INFO, List.of(
new Action(i18n.get("game.join"), event -> join(game, password, true)), new Action(i18n.get("game.cancel")))));
new Action(i18n.get("game.join"), () -> join(game, password, true)), new Action(i18n.get("game.cancel")))));
}

public void startSearchMatchmaker() {
Expand Down Expand Up @@ -340,9 +341,9 @@
if (throwable instanceof CancellationException) {
log.info("Matchmaking search has been cancelled");
if (isRunning()) {
notificationService.addServerNotification(
new ImmediateNotification(i18n.get("matchmaker.cancelled.title"), i18n.get("matchmaker.cancelled"),
Severity.INFO));
notificationService.addNotification(
new ServerNotification(i18n.get("matchmaker.cancelled.title"), i18n.get("matchmaker.cancelled"),
Severity.INFO));
killGame();
}
} else {
Expand Down Expand Up @@ -422,8 +423,7 @@
GameBean game = getRunningGame();
notificationService.addNotification(
new PersistentNotification(i18n.get("game.ended", game.getTitle()), Severity.INFO, List.of(
new Action(i18n.get("game.rate"),
actionEvent -> navigationHandler.navigateTo(new ShowReplayEvent(game.getId()))))));
new Action(i18n.get("game.rate"), () -> navigationHandler.navigateTo(new ShowReplayEvent(game.getId()))))));
}

private void alertOnBadExit(int exitCode, Optional<Path> logFile) {
Expand All @@ -434,7 +434,7 @@
i18n.get("game.crash", exitCode,
logFile.map(Path::toString).orElse("")),
WARN, List.of(new Action(i18n.get("game.open.log"),
event -> platformService.reveal(
() -> platformService.reveal(

Check warning on line 437 in src/main/java/com/faforever/client/game/GameRunner.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/game/GameRunner.java#L437

Added line #L437 was not covered by tests
logFile.orElse(
operatingSystem.getLoggingDirectory()))),
new DismissAction(i18n))));
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/faforever/client/game/VaultPathHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void showWarning() {

notificationService.addImmediateWarnNotification(i18n.get("vaultBasePath.nonAscii.warning.title"),
i18n.get("vaultBasePath.nonAscii.warning.text"), List.of(
new Action(i18n.get("vaultBasePath.nonAscii.warning.changePath"), event -> askForPathAndUpdate()),
new Action(i18n.get("vaultBasePath.nonAscii.warning.changePath"), this::askForPathAndUpdate),
new OkAction(i18n)), ignoreCheckbox);
}

Expand All @@ -79,10 +79,12 @@ private void onVaultPathUpdated(Path newPath) {
moveDirectoryTask.setOldDirectory(forgedAlliancePrefs.getVaultBaseDirectory());
moveDirectoryTask.setNewDirectory(newPath);
moveDirectoryTask.setAfterCopyAction(() -> forgedAlliancePrefs.setVaultBaseDirectory(newPath));
notificationService.addNotification(new ImmediateNotification(i18n.get("settings.vault.change"), i18n.get("settings.vault.change.message"), Severity.INFO, List.of(new Action(i18n.get("no"), event -> {
notificationService.addNotification(
new ImmediateNotification(i18n.get("settings.vault.change"), i18n.get("settings.vault.change.message"),
Severity.INFO, List.of(new Action(i18n.get("no"), () -> {
moveDirectoryTask.setPreserveOldDirectory(false);
taskService.submitTask(moveDirectoryTask);
}), new Action(i18n.get("yes"), event -> {
}), new Action(i18n.get("yes"), () -> {
moveDirectoryTask.setPreserveOldDirectory(true);
taskService.submitTask(moveDirectoryTask);
}), new CancelAction(i18n))));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.faforever.client.headerbar;

import com.faforever.client.fx.FxApplicationThreadExecutor;
import com.faforever.client.fx.JavaFxUtil;
import com.faforever.client.fx.NodeController;
import com.faforever.client.notification.NotificationService;
import com.faforever.client.notification.PersistentNotification;
Expand Down Expand Up @@ -38,21 +39,20 @@

@Override
protected void onInitialize() {
JavaFxUtil.bindManagedToVisible(notificationButton);

Check warning on line 42 in src/main/java/com/faforever/client/headerbar/NotificationButtonController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/headerbar/NotificationButtonController.java#L42

Added line #L42 was not covered by tests
updateNotificationsButton(notificationService.getPersistentNotifications());
notificationService.addPersistentNotificationListener(change -> fxApplicationThreadExecutor.execute(() -> updateNotificationsButton(change.getSet())));

notificationButton.managedProperty().bind(notificationButton.visibleProperty());
notificationService.getPersistentNotifications()
.subscribe(() -> updateNotificationsButton(notificationService.getPersistentNotifications()));

Check warning on line 45 in src/main/java/com/faforever/client/headerbar/NotificationButtonController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/headerbar/NotificationButtonController.java#L44-L45

Added lines #L44 - L45 were not covered by tests
}

/**
* Updates the number displayed in the notifications button and sets its CSS pseudo class based on the highest
* notification {@code Severity} of all current notifications.
*/
private void updateNotificationsButton(Collection<? extends PersistentNotification> notifications) {
private void updateNotificationsButton(Collection<PersistentNotification> notifications) {
int size = notifications.size();

Severity highestSeverity = notifications.stream()
.map(PersistentNotification::getSeverity)
Severity highestSeverity = notifications.stream().map(PersistentNotification::severity)

Check warning on line 55 in src/main/java/com/faforever/client/headerbar/NotificationButtonController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/faforever/client/headerbar/NotificationButtonController.java#L55

Added line #L55 was not covered by tests
.max(Enum::compareTo)
.orElse(null);

Expand Down
Loading