Skip to content

Commit

Permalink
Caching of avatar images #34
Browse files Browse the repository at this point in the history
  • Loading branch information
basalt79 authored and Brutus5000 committed Oct 22, 2018
1 parent 40451f1 commit 0eb0550
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/main/java/com/faforever/moderatorclient/ui/AvatarCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.faforever.moderatorclient.ui;

import javafx.scene.image.Image;

import java.util.HashMap;
import java.util.Map;

public class AvatarCache {

private final Map<String, Image> cache;

private static AvatarCache singleton;

private AvatarCache() {
this.cache = new HashMap<>();
}

public static synchronized AvatarCache getInstance() {
if (singleton == null) {
singleton = new AvatarCache();
}
return singleton;
}

public Image get(String key) {
return this.cache.get(key);
}

public boolean containsKey(String key) {
return this.cache.containsKey(key);
}

public void put(String cacheKey, Image img) {
this.cache.put(cacheKey, img);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.faforever.moderatorclient.ui;

import com.faforever.moderatorclient.ui.domain.AvatarAssignmentFX;
import com.faforever.moderatorclient.ui.domain.AvatarFX;
import javafx.scene.control.TableCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.time.OffsetDateTime;
import java.util.Objects;

public class UrlImageViewTableCell<T> extends TableCell<T, String> {
Expand All @@ -17,11 +20,33 @@ protected void updateItem(String item, boolean empty) {
if (item != null) {
if (!Objects.equals(currentUrl, item)) {
currentUrl = item;
imageView.setImage(new Image(item));
Image img;
if (getTableRow() != null && getTableRow().getItem() != null) {
String cacheKey = cacheKeyFrom(item, getTableRow().getItem());
if (AvatarCache.getInstance().containsKey(cacheKey)) {
img = AvatarCache.getInstance().get(cacheKey);
} else {
img = new Image(item);
AvatarCache.getInstance().put(cacheKey, img);
}
} else {
img = new Image(item);
}
imageView.setImage(img);
}
setGraphic(imageView);
} else {
setGraphic(null);
}
}

private String cacheKeyFrom(String item, Object rawData) {
OffsetDateTime updateTime = null;
if (rawData instanceof AvatarFX) {
updateTime = ((AvatarFX)rawData).getUpdateTime();
} else if (rawData instanceof AvatarAssignmentFX) {
updateTime = ((AvatarAssignmentFX)rawData).getUpdateTime();
}
return updateTime != null ? item+updateTime.toString() : item;
}
}

0 comments on commit 0eb0550

Please sign in to comment.