Skip to content

Commit

Permalink
♻️ refactor to separate images from text #15
Browse files Browse the repository at this point in the history
  • Loading branch information
McPringle committed Mar 31, 2024
1 parent 124dec7 commit dc90621
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/main/java/swiss/fihlon/apus/social/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
import org.jetbrains.annotations.NotNull;

import java.time.LocalDateTime;
import java.util.List;

public record Message(String id, LocalDateTime date, String author, String avatar, String html)
public record Message(@NotNull String id, @NotNull LocalDateTime date,
@NotNull String author, @NotNull String avatar,
@NotNull String html, @NotNull List<String> images)
implements Comparable<Message> {
@Override
public int compareTo(@NotNull final Message other) {
return date.compareTo(other.date);
final int dateCompareResult = date.compareTo(other.date);
return dateCompareResult == 0 ? id.compareTo(other.id) : dateCompareResult;
}
}
13 changes: 6 additions & 7 deletions src/main/java/swiss/fihlon/apus/social/mastodon/MastodonAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,20 @@ private Message convertToMessage(@NotNull final Status status) {
final LocalDateTime date = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
final String author = account == null ? "" : account.getDisplayName();
final String avatar = account == null ? "" : account.getAvatar();
final String html = status.getContent() + convertToImages(status.getMediaAttachments());
final String html = status.getContent();
final List<String> images = getImages(status.getMediaAttachments());

return new Message(id, date, author, avatar, html);
return new Message(id, date, author, avatar, html, images);
}

private String convertToImages(@NotNull final List<MediaAttachment> mediaAttachments) {
private List<String> getImages(@NotNull final List<MediaAttachment> mediaAttachments) {
final List<String> images = new ArrayList<>();

for (final MediaAttachment mediaAttachment : mediaAttachments) {
if (MediaAttachment.MediaType.IMAGE.equals(mediaAttachment.getType())) {
images.add("<img src=\"" + mediaAttachment.getUrl() + "\" />");
images.add(mediaAttachment.getUrl());
}
}

return String.join("", images);
return images;
}

}
4 changes: 4 additions & 0 deletions src/main/java/swiss/fihlon/apus/ui/view/MessageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.vaadin.flow.component.Html;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Image;
import org.jetbrains.annotations.NotNull;
import swiss.fihlon.apus.social.Message;

Expand All @@ -37,5 +38,8 @@ public MessageView(@NotNull final Message message) {
protected void onAttach(@NotNull final AttachEvent attachEvent) {
addClassName("message-view");
add(new Html("<div>" + message.html() + "</div>"));
for (final String image : message.images()) {
add(new Image(image, image));
}
}
}

0 comments on commit dc90621

Please sign in to comment.