Skip to content

Commit

Permalink
Cache random faction image
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheikah45 committed Jun 8, 2023
1 parent bec19fc commit bd7da67
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/faforever/client/config/CacheConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static com.faforever.client.config.CacheNames.FEATURED_MODS;
import static com.faforever.client.config.CacheNames.FEATURED_MOD_FILES;
import static com.faforever.client.config.CacheNames.GLOBAL_LEADERBOARD;
import static com.faforever.client.config.CacheNames.IMAGES;
import static com.faforever.client.config.CacheNames.LADDER_1V1_LEADERBOARD;
import static com.faforever.client.config.CacheNames.LEADERBOARD;
import static com.faforever.client.config.CacheNames.LEAGUE;
Expand Down Expand Up @@ -109,6 +110,7 @@ public CacheManager cacheManager() {
new CaffeineCache(COUNTRY_FLAGS, newBuilder().weakValues().build()),
new CaffeineCache(COUNTRY_NAMES, newBuilder().weakValues().build()),
new CaffeineCache(THEME_IMAGES, newBuilder().weakValues().build()),
new CaffeineCache(IMAGES, newBuilder().weakValues().build()),
new CaffeineCache(MOD_THUMBNAIL, newBuilder().weakValues().build()
)));
return simpleCacheManager;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/faforever/client/config/CacheNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class CacheNames {
public static final String MAP_GENERATOR = "mapGenerator";
public static final String MAP_GENERATOR_STYLES = "mapGeneratorStyles";
public static final String THEME_IMAGES = "themeImages";
public static final String IMAGES = "images";
public static final String MOD_THUMBNAIL = "modThumbnail";
public static final String COOP_MAPS = "coopMaps";
public static final String AVAILABLE_AVATARS = "availableAvatars";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
Expand Down Expand Up @@ -78,7 +77,7 @@ public void initialize() {

ObservableValue<Boolean> showing = uiService.createShowingProperty(getRoot());

factionImage.setImage(new Image(UiService.RANDOM_FACTION_IMAGE));
factionImage.setImage(uiService.getImage(UiService.RANDOM_FACTION_IMAGE));
factionImage.visibleProperty().bind(faction.map(value -> value == Faction.RANDOM));
factionIcon.visibleProperty().bind(faction.map(value -> value != Faction.RANDOM && value != Faction.CIVILIAN));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private void onInternalLoginFailed(Throwable throwable) {
}

public <T extends ServerMessage> Flux<T> getEvents(Class<T> type) {
return lobbyClient.getEvents().filter(message -> type.isAssignableFrom(message.getClass())).cast(type);
return lobbyClient.getEvents().ofType(type);
}

/**
Expand All @@ -112,8 +112,7 @@ public <T extends ServerMessage> Flux<T> getEvents(Class<T> type) {
@Deprecated
public <T extends ServerMessage> void addEventListener(Class<T> type, Consumer<T> listener) {
lobbyClient.getEvents()
.filter(serverMessage -> type.isAssignableFrom(serverMessage.getClass()))
.cast(type)
.ofType(type)
.flatMap(message -> Mono.fromRunnable(() -> listener.accept(message)).onErrorResume(throwable -> {
log.error("Could not process listener for `{}`", message, throwable);
return Mono.empty();
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/faforever/client/theme/UiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ public Image getThemeImage(String relativeImage) {
}
}

/**
* Loads an image with caching.
*/
@Cacheable(value = CacheNames.IMAGES, sync = true)
public Image getImage(String relativeImage) {
return new Image(relativeImage, true);
}


public URL getThemeFileUrl(String relativeFile) throws IOException {
String themeFile = getThemeFile(relativeFile);
Expand Down

0 comments on commit bd7da67

Please sign in to comment.