Skip to content

Commit

Permalink
♻️ Extract language flags into files
Browse files Browse the repository at this point in the history
to make use of browser caching

Signed-off-by: Marcus Fihlon <[email protected]>
  • Loading branch information
McPringle committed Jun 15, 2024
1 parent de6a73a commit dc09e85
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 28 deletions.
5 changes: 1 addition & 4 deletions frontend/themes/apus/views/room-view.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,8 @@
width: 376px;
}

.room-view .title .language {
.room-view .title img.language {
float: right;
}

.room-view .title .language svg {
height: 16px;
}

Expand Down
28 changes: 9 additions & 19 deletions src/main/java/swiss/fihlon/apus/event/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,13 @@
public enum Language {

// Important: When adding a new language, modify the test accordingly!
DE("de", """
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 480">
<path fill="#fc0" d="M0 320h640v160H0z"/>
<path fill="#000001" d="M0 0h640v160H0z"/>
<path fill="red" d="M0 160h640v160H0z"/>
</svg>"""),
EN("en", """
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 480">
<path fill="#012169" d="M0 0h640v480H0z"/>
<path fill="#FFF" d="m75 0 244 181L562 0h78v62L400 241l240 178v61h-80L320 301 81 480H0v-60l239-178L0 64V0z"/>
<path fill="#C8102E" d="m424 281 216 159v40L369 281zm-184 20 6 35L54 480H0zM640 0v3L391 191l2-44L590 0zM0 0l239 176h-60L0 42z"/>
<path fill="#FFF" d="M241 0v480h160V0zM0 160v160h640V160z"/>
<path fill="#C8102E" d="M0 193v96h640v-96zM273 0v480h96V0z"/>
</svg>""");
DE("de"),
EN("en");

private static final String FILE_NAME_TEMPLATE = "icons/flags/%s.svg";

private final String languageCode;
private final String svgCode;
private final String flagFileName;

public static Language languageWithCode(@NotNull final String languageCode) {
for (final Language language : values()) {
Expand All @@ -49,9 +39,9 @@ public static Language languageWithCode(@NotNull final String languageCode) {
throw new IllegalArgumentException(String.format("No language constant with language code '%s'!", languageCode));
}

Language(@NotNull final String languageCode, @NotNull final String svgCode) {
Language(@NotNull final String languageCode) {
this.languageCode = languageCode;
this.svgCode = svgCode;
this.flagFileName = FILE_NAME_TEMPLATE.formatted(languageCode);
}

@NotNull
Expand All @@ -60,7 +50,7 @@ public String getLanguageCode() {
}

@NotNull
public String getSvgCode() {
return svgCode;
public String getFlagFileName() {
return flagFileName;
}
}
3 changes: 1 addition & 2 deletions src/main/java/swiss/fihlon/apus/ui/view/RoomView.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Html;
import com.vaadin.flow.component.Svg;
import com.vaadin.flow.component.Text;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.html.Div;
Expand Down Expand Up @@ -100,7 +99,7 @@ private Component createTitleComponent() {
titleComponent.addClassName("title");
titleComponent.add(new H3(new Text(title == null ? getTranslation("event.room.empty") : title)));
if (language != null) {
final var flagComponent = new Svg(language.getSvgCode());
final var flagComponent = new Image(language.getFlagFileName(), language.getLanguageCode());
flagComponent.addClassName("language");
titleComponent.add(flagComponent);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/resources/icons/flags/de.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src/main/resources/META-INF/resources/icons/flags/en.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions src/test/java/swiss/fihlon/apus/event/LanguageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class LanguageTest {

Expand All @@ -41,8 +41,7 @@ void getLanguageCode() {
@Test
void getFlagIcon() {
for (final Language language : Language.values()) {
assertTrue(language.getSvgCode().trim().startsWith("<svg "));
assertTrue(language.getSvgCode().trim().endsWith("</svg>"));
assertFalse(language.getFlagFileName().isBlank());
}
}

Expand Down

0 comments on commit dc09e85

Please sign in to comment.