Add Image Caching (#95)

Add image caching

Co-authored-by: kske <kai@kske.dev>
Reviewed-on: https://git.kske.dev/zdm/envoy/pulls/95
Reviewed-by: kske <kai@kske.dev>
Reviewed-by: DieGurke <maxi@kske.dev>
This commit is contained in:
Leon Hofmeister 2020-10-13 11:30:19 +02:00 committed by kske
parent 75f0a65517
commit ab2e9aa114
Signed by: Käfer & Engelbart Git
GPG Key ID: 70F2F9206EDC1FCE
3 changed files with 21 additions and 17 deletions

View File

@ -132,7 +132,6 @@ public final class ChatScene implements EventListener, Restorable {
private Chat currentChat; private Chat currentChat;
private FilteredList<Chat> chats; private FilteredList<Chat> chats;
private boolean recording;
private Attachment pendingAttachment; private Attachment pendingAttachment;
private boolean postingPermanentlyDisabled; private boolean postingPermanentlyDisabled;
private boolean isCustomAttachmentImage; private boolean isCustomAttachmentImage;
@ -354,7 +353,8 @@ public final class ChatScene implements EventListener, Restorable {
// Discard the pending attachment // Discard the pending attachment
if (recorder.isRecording()) { if (recorder.isRecording()) {
recorder.cancel(); recorder.cancel();
recording = false; voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", DEFAULT_ICON_SIZE)));
voiceButton.setText(null);
} }
pendingAttachment = null; pendingAttachment = null;
updateAttachmentView(false); updateAttachmentView(false);
@ -414,18 +414,16 @@ public final class ChatScene implements EventListener, Restorable {
private void voiceButtonClicked() { private void voiceButtonClicked() {
new Thread(() -> { new Thread(() -> {
try { try {
if (!recording) { if (!recorder.isRecording()) {
recording = true;
Platform.runLater(() -> { Platform.runLater(() -> {
voiceButton.setText("Recording"); voiceButton.setText("Recording");
voiceButton.setGraphic(new ImageView(IconUtil.loadIcon("microphone_recording", DEFAULT_ICON_SIZE))); voiceButton.setGraphic(new ImageView(IconUtil.loadIcon("microphone_recording", DEFAULT_ICON_SIZE)));
}); });
recorder.start(); recorder.start();
} else { } else {
pendingAttachment = new Attachment(recorder.finish(), "Voice_recording_" pendingAttachment = new Attachment(recorder.finish(), "Voice_recording_"
+ DateTimeFormatter.ofPattern("yyyy_MM_dd-HH_mm_ss").format(LocalDateTime.now()) + "." + AudioRecorder.FILE_FORMAT, + DateTimeFormatter.ofPattern("yyyy_MM_dd-HH_mm_ss").format(LocalDateTime.now()) + "." + AudioRecorder.FILE_FORMAT,
AttachmentType.VOICE); AttachmentType.VOICE);
recording = false;
Platform.runLater(() -> { Platform.runLater(() -> {
voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", DEFAULT_ICON_SIZE))); voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", DEFAULT_ICON_SIZE)));
voiceButton.setText(null); voiceButton.setText(null);

View File

@ -21,6 +21,10 @@ import envoy.util.EnvoyLog;
*/ */
public final class IconUtil { public final class IconUtil {
private static final HashMap<String, Image> cache = new HashMap<>();
private static final HashMap<String, Image> scaledCache = new HashMap<>();
private static final HashMap<String, BufferedImage> awtCache = new HashMap<>();
private IconUtil() {} private IconUtil() {}
/** /**
@ -30,7 +34,7 @@ public final class IconUtil {
* @return the loaded image * @return the loaded image
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public static Image load(String path) { return new Image(IconUtil.class.getResource(path).toExternalForm()); } public static Image load(String path) { return cache.computeIfAbsent(path, p -> new Image(IconUtil.class.getResource(p).toExternalForm())); }
/** /**
* Loads an image from the resource folder and scales it to the given size. * Loads an image from the resource folder and scales it to the given size.
@ -41,12 +45,13 @@ public final class IconUtil {
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public static Image load(String path, int size) { public static Image load(String path, int size) {
return new Image(IconUtil.class.getResource(path).toExternalForm(), size, size, true, true); return scaledCache.computeIfAbsent(path + size, p -> new Image(IconUtil.class.getResource(path).toExternalForm(), size, size, true, true));
} }
/** /**
* Loads a {@code .png} image from the sub-folder {@code /icons/} of the * Loads a {@code .png} image from the sub-folder {@code /icons/} of the
* resource folder.<br> * resource folder.
* <p>
* The suffix {@code .png} is automatically appended. * The suffix {@code .png} is automatically appended.
* *
* @param name the image name without the .png suffix * @param name the image name without the .png suffix
@ -138,13 +143,14 @@ public final class IconUtil {
* @since Envoy Client v0.2-beta * @since Envoy Client v0.2-beta
*/ */
public static BufferedImage loadAWTCompatible(String path) { public static BufferedImage loadAWTCompatible(String path) {
BufferedImage image = null; return awtCache.computeIfAbsent(path, p -> {
try { try {
image = ImageIO.read(IconUtil.class.getResource(path)); return ImageIO.read(IconUtil.class.getResource(path));
} catch (final IOException e) { } catch (IOException e) {
EnvoyLog.getLogger(IconUtil.class).log(Level.WARNING, String.format("Could not load image at path %s: ", path), e); EnvoyLog.getLogger(IconUtil.class).log(Level.WARNING, String.format("Could not load image at path %s: ", path), e);
} return null;
return image; }
});
} }
/** /**

View File

@ -175,7 +175,7 @@
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding> </padding>
<HBox.margin> <HBox.margin>
<Insets bottom="35.0" left="5.0" top="35.0"/> <Insets bottom="35.0" left="5.0" top="35.0" />
</HBox.margin> </HBox.margin>
</Button> </Button>
</children> </children>