Skip to content

Commit

Permalink
Added config converter for EmojiImageCache and fixed initialization o…
Browse files Browse the repository at this point in the history
…rder. (TweetWallFX#1962)
  • Loading branch information
mklaehn authored Oct 6, 2024
1 parent 4857a97 commit fb9baeb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 22 deletions.
22 changes: 1 addition & 21 deletions emoji/src/main/java/org/tweetwallfx/emoji/EmojiImageCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
*/
package org.tweetwallfx.emoji;

import java.util.Objects;
import org.tweetwallfx.cache.URLContent;
import org.tweetwallfx.cache.URLContentCacheBase;
import org.tweetwallfx.config.Configuration;

/**
* Cache used to provide the avatar image of a User.
Expand All @@ -37,30 +35,12 @@ public final class EmojiImageCache extends URLContentCacheBase {
* Cache instance.
*/
public static final EmojiImageCache INSTANCE = new EmojiImageCache();
private final Config config;

private EmojiImageCache() {
super("emojiImage");
this.config = Configuration.getInstance().getConfigTyped(EmojiImageCache.class.getSimpleName(), Config.class);
}

public URLContent get(final String hex) {
return EmojiImageCache.INSTANCE.getCachedOrLoad(config.emojiImageBaseUrl() + hex + ".png");
}

public static record Config(
String emojiImageBaseUrl) {

public Config(
final String emojiImageBaseUrl) {
String bu = Objects.requireNonNullElse(emojiImageBaseUrl, "https://cdnjs.cloudflare.com/ajax/libs/twemoji/15.1.0/72x72");

// ensure baseUrl ends with a '/'
if (!bu.endsWith("/")) {
bu += "/";
}

this.emojiImageBaseUrl = bu;
}
return EmojiImageCache.INSTANCE.getCachedOrLoad(EmojiImageCacheConfig.getInstance().emojiImageBaseUrl() + hex + ".png");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 TweetWallFX
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.tweetwallfx.emoji;

import java.util.Objects;
import org.tweetwallfx.config.Configuration;
import org.tweetwallfx.config.ConfigurationConverter;

public record EmojiImageCacheConfig(
String emojiImageBaseUrl) {

/**
* Configuration key under which the data for this Settings object is stored
* in the configuration data map.
*/
public static final String CONFIG_KEY = EmojiImageCache.class.getSimpleName();

public EmojiImageCacheConfig(
final String emojiImageBaseUrl) {
String bu = Objects.requireNonNullElse(emojiImageBaseUrl, "https://cdnjs.cloudflare.com/ajax/libs/twemoji/15.1.0/72x72");

// ensure baseUrl ends with a '/'
if (!bu.endsWith("/")) {
bu += "/";
}

this.emojiImageBaseUrl = bu;
}

public static EmojiImageCacheConfig getInstance() {
return Configuration.getInstance().getConfigTyped(CONFIG_KEY, EmojiImageCacheConfig.class);
}

/**
* Service implementation converting the configuration data of the root key
* {@link #CONFIG_KEY} into {@link EmojiImageCacheConfig}.
*/
public static final class Converter implements ConfigurationConverter {

@Override
public String getResponsibleKey() {
return CONFIG_KEY;
}

@Override
public Class<?> getDataClass() {
return EmojiImageCacheConfig.class;
}
}
}
2 changes: 1 addition & 1 deletion emoji/src/main/java/org/tweetwallfx/emoji/Emojify.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static List<Object> tokenizeStringToTextAndEmoji(final String message) {
var cp = codePoints[idx];

if (Character.isEmojiPresentation(cp)) {
// we have an emoji
// we have an emoji we can display
if (idx != idxStart) {
// prior to the emoji is a non-empty text
// add non-emoji text to tokens
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.tweetwallfx.emoji.EmojiImageCacheConfig$Converter

0 comments on commit fb9baeb

Please sign in to comment.